Skip to content

Commit b84e7be

Browse files
committed
manager: restore NewServer to v2 signature
While NewServer is probably not called by any code outside the swarmkit module, we should still be good maintainers by taking reasonable measures to avoid introducing breaking changes to the module. Instead provide a view-response mutator to controlapi by setting an exported struct field on the constructed Server. Since for similar compatibility reasons we can't ever add new methods to the view-response interface, rename it to better reflect its narrow scope. Signed-off-by: Cory Snider <[email protected]>
1 parent 53ae288 commit b84e7be

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

manager/controlapi/apihooks.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"github.com/moby/swarmkit/v2/api"
77
)
88

9-
// ViewResponseMutator provides callbacks which may modify the response objects
10-
// for Get or List Control API requests before they are sent to the client.
11-
type ViewResponseMutator interface {
9+
// NetworkViewResponseMutator provides callbacks which may modify the response
10+
// objects for GetNetwork and ListNetworks Control API requests before they are
11+
// sent to the client.
12+
type NetworkViewResponseMutator interface {
1213
OnGetNetwork(context.Context, *api.Network) error
1314
OnListNetworks(context.Context, []*api.Network) error
1415
}
@@ -22,3 +23,10 @@ func (NoopViewResponseMutator) OnGetNetwork(ctx context.Context, n *api.Network)
2223
func (NoopViewResponseMutator) OnListNetworks(ctx context.Context, networks []*api.Network) error {
2324
return nil
2425
}
26+
27+
func (s *Server) networkhooks() NetworkViewResponseMutator {
28+
if s.NetworkHooks != nil {
29+
return s.NetworkHooks
30+
}
31+
return NoopViewResponseMutator{}
32+
}

manager/controlapi/network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ 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-
if err := s.viewhooks.OnGetNetwork(ctx, n); err != nil {
151+
if err := s.networkhooks().OnGetNetwork(ctx, n); err != nil {
152152
return nil, err
153153
}
154154
return &api.GetNetworkResponse{
@@ -295,7 +295,7 @@ func (s *Server) ListNetworks(ctx context.Context, request *api.ListNetworksRequ
295295
)
296296
}
297297

298-
if err := s.viewhooks.OnListNetworks(ctx, networks); err != nil {
298+
if err := s.networkhooks().OnListNetworks(ctx, networks); err != nil {
299299
return nil, err
300300
}
301301

manager/controlapi/server.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,23 @@ type Server struct {
2020
raft *raft.Node
2121
securityConfig *ca.SecurityConfig
2222
netvalidator networkallocator.DriverValidator
23-
viewhooks ViewResponseMutator
2423
dr *drivers.DriverProvider
24+
25+
// NetworkHooks intercept and mutate API server responses for GetNetwork
26+
// and ListNetworks API requests when set.
27+
NetworkHooks NetworkViewResponseMutator
2528
}
2629

2730
// NewServer creates a Cluster API server.
28-
func NewServer(store *store.MemoryStore, raft *raft.Node, securityConfig *ca.SecurityConfig, nv networkallocator.DriverValidator, vrm ViewResponseMutator, dr *drivers.DriverProvider) *Server {
31+
func NewServer(store *store.MemoryStore, raft *raft.Node, securityConfig *ca.SecurityConfig, nv networkallocator.DriverValidator, dr *drivers.DriverProvider) *Server {
2932
if nv == nil {
3033
nv = networkallocator.InertProvider{}
3134
}
32-
if vrm == nil {
33-
vrm = NoopViewResponseMutator{}
34-
}
3535
return &Server{
3636
store: store,
3737
dr: dr,
3838
raft: raft,
3939
securityConfig: securityConfig,
4040
netvalidator: nv,
41-
viewhooks: vrm,
4241
}
4342
}

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, nil)
50+
ts.Server = NewServer(ts.Store, nil, securityConfig, nil, nil)
5151
assert.NotNil(t, ts.Server)
5252

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

manager/manager.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,8 @@ func (m *Manager) Run(parent context.Context) error {
472472
return err
473473
}
474474

475-
baseControlAPI := controlapi.NewServer(m.raftNode.MemoryStore(), m.raftNode, m.config.SecurityConfig, m.config.networkProvider(), m, drivers.New(m.config.PluginGetter))
475+
baseControlAPI := controlapi.NewServer(m.raftNode.MemoryStore(), m.raftNode, m.config.SecurityConfig, m.config.networkProvider(), drivers.New(m.config.PluginGetter))
476+
baseControlAPI.NetworkHooks = m
476477
baseResourceAPI := resourceapi.New(m.raftNode.MemoryStore())
477478
healthServer := health.NewHealthServer()
478479
localHealthServer := health.NewHealthServer()

0 commit comments

Comments
 (0)