Skip to content

Commit 7d06b7c

Browse files
committed
Pass NetworkStateUpdater to Cluster API Server to retrieve current IPAM state for network introspection
Add UpdateNetworkState() method to NetworkAllocator interface Signed-off-by: Andrey Epifanov <[email protected]>
1 parent 95d6aee commit 7d06b7c

File tree

9 files changed

+60
-14
lines changed

9 files changed

+60
-14
lines changed

manager/allocator/allocator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,7 @@ nextVoter:
229229

230230
return true
231231
}
232+
233+
func (a *Allocator) UpdateNetworkState(net *api.Network) error {
234+
return a.nwkAllocator.UpdateNetworkState(net)
235+
}

manager/allocator/networkallocator/inert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ func (Inert) IsServiceAllocated(s *api.Service, flags ...func(*ServiceAllocation
127127
func (Inert) IsTaskAllocated(t *api.Task) bool {
128128
return (Inert{}).AllocateTask(t) == nil
129129
}
130+
131+
func (Inert) UpdateNetworkState(*api.Network) error {
132+
return nil
133+
}

manager/allocator/networkallocator/networkallocator.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ type NetworkAllocator interface {
8585

8686
// IsAttachmentAllocated If lb endpoint is allocated on the node
8787
IsAttachmentAllocated(node *api.Node, networkAttachment *api.NetworkAttachment) bool
88+
89+
// UpdateNetworkState updates the network state
90+
UpdateNetworkState(network *api.Network) error
91+
}
92+
93+
// NetworkStateUpdater is an interface for updating the network state in the store.
94+
type NetworkStateUpdater interface {
95+
UpdateNetworkState(*api.Network) error
8896
}
8997

9098
// Config is used to store network related cluster config in the Manager.

manager/controlapi/network.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ func (s *Server) GetNetwork(ctx context.Context, request *api.GetNetworkRequest)
148148
if n == nil {
149149
return nil, status.Errorf(codes.NotFound, "network %s not found", request.NetworkID)
150150
}
151+
152+
if err := s.netStateUpdater.UpdateNetworkState(n); err != nil {
153+
return nil, status.Errorf(codes.NotFound, "failed to get IPAM data for network %s: %v", request.NetworkID, err)
154+
}
155+
151156
return &api.GetNetworkResponse{
152157
Network: n,
153158
}, nil
@@ -292,6 +297,12 @@ func (s *Server) ListNetworks(ctx context.Context, request *api.ListNetworksRequ
292297
)
293298
}
294299

300+
for _, n := range networks {
301+
if err := s.netStateUpdater.UpdateNetworkState(n); err != nil {
302+
return nil, err
303+
}
304+
}
305+
295306
return &api.ListNetworksResponse{
296307
Networks: networks,
297308
}, nil

manager/controlapi/server.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,28 @@ var (
1616

1717
// Server is the Cluster API gRPC server.
1818
type Server struct {
19-
store *store.MemoryStore
20-
raft *raft.Node
21-
securityConfig *ca.SecurityConfig
22-
netvalidator networkallocator.DriverValidator
23-
dr *drivers.DriverProvider
19+
store *store.MemoryStore
20+
raft *raft.Node
21+
securityConfig *ca.SecurityConfig
22+
netvalidator networkallocator.DriverValidator
23+
dr *drivers.DriverProvider
24+
netStateUpdater networkallocator.NetworkStateUpdater
2425
}
2526

2627
// NewServer creates a Cluster API server.
27-
func NewServer(store *store.MemoryStore, raft *raft.Node, securityConfig *ca.SecurityConfig, nv networkallocator.DriverValidator, dr *drivers.DriverProvider) *Server {
28+
func NewServer(store *store.MemoryStore, raft *raft.Node, securityConfig *ca.SecurityConfig, nv networkallocator.DriverValidator, dr *drivers.DriverProvider, nsu networkallocator.NetworkStateUpdater) *Server {
2829
if nv == nil {
2930
nv = networkallocator.InertProvider{}
3031
}
32+
if nsu == nil {
33+
nsu = networkallocator.Inert{}
34+
}
3135
return &Server{
32-
store: store,
33-
dr: dr,
34-
raft: raft,
35-
securityConfig: securityConfig,
36-
netvalidator: nv,
36+
store: store,
37+
dr: dr,
38+
raft: raft,
39+
securityConfig: securityConfig,
40+
netvalidator: nv,
41+
netStateUpdater: nsu,
3742
}
3843
}

manager/controlapi/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func newTestServer(t *testing.T) *testServer {
4747
ts.Store = store.NewMemoryStore(&stateutils.MockProposer{})
4848
assert.NotNil(t, ts.Store)
4949

50-
ts.Server = NewServer(ts.Store, nil, securityConfig, nil, nil)
50+
ts.Server = NewServer(ts.Store, nil, securityConfig, nil, nil, nil)
5151
assert.NotNil(t, ts.Server)
5252

5353
temp, err := os.CreateTemp("", "test-socket")

manager/manager.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ func (m *Manager) Addr() string {
435435
return m.config.RemoteAPI.ListenAddr
436436
}
437437

438+
func (m *Manager) UpdateNetworkState(net *api.Network) error {
439+
return m.allocator.UpdateNetworkState(net)
440+
}
441+
438442
// Run starts all manager sub-systems and the gRPC server at the configured
439443
// address.
440444
// The call never returns unless an error occurs or `Stop()` is called.
@@ -472,7 +476,14 @@ func (m *Manager) Run(parent context.Context) error {
472476
return err
473477
}
474478

475-
baseControlAPI := controlapi.NewServer(m.raftNode.MemoryStore(), m.raftNode, m.config.SecurityConfig, m.config.networkProvider(), drivers.New(m.config.PluginGetter))
479+
networkProvider := m.config.networkProvider()
480+
baseControlAPI := controlapi.NewServer(
481+
m.raftNode.MemoryStore(),
482+
m.raftNode,
483+
m.config.SecurityConfig,
484+
networkProvider,
485+
drivers.New(m.config.PluginGetter),
486+
m)
476487
baseResourceAPI := resourceapi.New(m.raftNode.MemoryStore())
477488
healthServer := health.NewHealthServer()
478489
localHealthServer := health.NewHealthServer()

manager/orchestrator/jobs/orchestrator_controlapi_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var _ = Describe("Integration between the controlapi and jobs orchestrator", fun
5454
// but it's unexported. in any case, re-writing it in ginkgo isn't
5555
// unwarranted.
5656
s = store.NewMemoryStore(&stateutils.MockProposer{})
57-
server = controlapi.NewServer(s, nil, nil, nil, nil)
57+
server = controlapi.NewServer(s, nil, nil, nil, nil, nil)
5858

5959
// we need a temporary unix socket to server on
6060
temp, err := os.CreateTemp("", "test-socket")

swarmd/go.work.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802 h1:1BDTz0u9nC3//pOC
257257
github.com/Microsoft/hcsshim v0.9.8 h1:lf7xxK2+Ikbj9sVf2QZsouGjRjEp2STj1yDHgoVtU5k=
258258
github.com/Microsoft/hcsshim v0.9.8/go.mod h1:7pLA8lDk46WKDWlVsENo92gC0XFa8rbKfyFRBqxEbCc=
259259
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
260+
github.com/akutz/gosync v0.1.0/go.mod h1:I8I4aiqJI1nqaeYOOB1WS+CgRJVVPqhct9Y4njywM84=
261+
github.com/akutz/memconn v0.1.0/go.mod h1:Jo8rI7m0NieZyLI5e2CDlRdRqRRB4S7Xp77ukDjH+Fw=
260262
github.com/alecthomas/kingpin/v2 v2.3.1 h1:ANLJcKmQm4nIaog7xdr/id6FM6zm5hHnfZrvtKPxqGg=
261263
github.com/alecthomas/kingpin/v2 v2.3.1/go.mod h1:oYL5vtsvEHZGHxU7DMp32Dvx+qL+ptGn6lWaot2vCNE=
262264
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=
@@ -329,6 +331,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHH
329331
github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw=
330332
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
331333
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954 h1:RMLoZVzv4GliuWafOuPuQDKSm1SJph7uCRnnS61JAn4=
334+
github.com/dperny/gocsi v1.2.3-pre/go.mod h1:qQw5mIunz1RqMUfZcGJ9/Lt9EDaL0N3wPNYxFTuyLQo=
332335
github.com/emicklei/go-restful v2.9.5+incompatible h1:spTtZBk5DYEvbxMVutUuTyh1Ao2r4iyvLdACqsl/Ljk=
333336
github.com/emicklei/go-restful v2.9.5+incompatible/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
334337
github.com/emicklei/go-restful/v3 v3.10.1 h1:rc42Y5YTp7Am7CS630D7JmhRjq4UlEUuEKfrDac4bSQ=

0 commit comments

Comments
 (0)