Browse code

daemon/cluster: use types/registry.AuthConfig

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/03/03 18:28:13
Showing 5 changed files
... ...
@@ -6,7 +6,8 @@ import (
6 6
 	"net/http"
7 7
 
8 8
 	"github.com/docker/distribution/reference"
9
-	enginetypes "github.com/docker/docker/api/types"
9
+	"github.com/docker/docker/api/types"
10
+	"github.com/docker/docker/api/types/registry"
10 11
 	"github.com/docker/docker/api/types/swarm/runtime"
11 12
 	"github.com/docker/docker/errdefs"
12 13
 	"github.com/docker/docker/plugin"
... ...
@@ -41,11 +42,11 @@ type Controller struct {
41 41
 // Backend is the interface for interacting with the plugin manager
42 42
 // Controller actions are passed to the configured backend to do the real work.
43 43
 type Backend interface {
44
-	Disable(name string, config *enginetypes.PluginDisableConfig) error
45
-	Enable(name string, config *enginetypes.PluginEnableConfig) error
46
-	Remove(name string, config *enginetypes.PluginRmConfig) error
47
-	Pull(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *enginetypes.AuthConfig, privileges enginetypes.PluginPrivileges, outStream io.Writer, opts ...plugin.CreateOpt) error
48
-	Upgrade(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *enginetypes.AuthConfig, privileges enginetypes.PluginPrivileges, outStream io.Writer) error
44
+	Disable(name string, config *types.PluginDisableConfig) error
45
+	Enable(name string, config *types.PluginEnableConfig) error
46
+	Remove(name string, config *types.PluginRmConfig) error
47
+	Pull(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *registry.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer, opts ...plugin.CreateOpt) error
48
+	Upgrade(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *registry.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer) error
49 49
 	Get(name string) (*v2.Plugin, error)
50 50
 	SubscribeEvents(buffer int, events ...plugin.Event) (eventCh <-chan interface{}, cancel func())
51 51
 }
... ...
@@ -96,7 +97,7 @@ func (p *Controller) Prepare(ctx context.Context) (err error) {
96 96
 		p.spec.Name = remote.String()
97 97
 	}
98 98
 
99
-	var authConfig enginetypes.AuthConfig
99
+	var authConfig registry.AuthConfig
100 100
 	privs := convertPrivileges(p.spec.Privileges)
101 101
 
102 102
 	pl, err := p.backend.Get(p.spec.Name)
... ...
@@ -112,7 +113,7 @@ func (p *Controller) Prepare(ctx context.Context) (err error) {
112 112
 			return errors.Errorf("plugin already exists: %s", p.spec.Name)
113 113
 		}
114 114
 		if pl.IsEnabled() {
115
-			if err := p.backend.Disable(pl.GetID(), &enginetypes.PluginDisableConfig{ForceDisable: true}); err != nil {
115
+			if err := p.backend.Disable(pl.GetID(), &types.PluginDisableConfig{ForceDisable: true}); err != nil {
116 116
 				p.logger.WithError(err).Debug("could not disable plugin before running upgrade")
117 117
 			}
118 118
 		}
... ...
@@ -143,12 +144,12 @@ func (p *Controller) Start(ctx context.Context) error {
143 143
 
144 144
 	if p.spec.Disabled {
145 145
 		if pl.IsEnabled() {
146
-			return p.backend.Disable(p.pluginID, &enginetypes.PluginDisableConfig{ForceDisable: false})
146
+			return p.backend.Disable(p.pluginID, &types.PluginDisableConfig{ForceDisable: false})
147 147
 		}
148 148
 		return nil
149 149
 	}
150 150
 	if !pl.IsEnabled() {
151
-		return p.backend.Enable(p.pluginID, &enginetypes.PluginEnableConfig{Timeout: 30})
151
+		return p.backend.Enable(p.pluginID, &types.PluginEnableConfig{Timeout: 30})
152 152
 	}
153 153
 	return nil
154 154
 }
... ...
@@ -232,7 +233,7 @@ func (p *Controller) Remove(ctx context.Context) error {
232 232
 
233 233
 	// This may error because we have exactly 1 plugin, but potentially multiple
234 234
 	// tasks which are calling remove.
235
-	err = p.backend.Remove(p.pluginID, &enginetypes.PluginRmConfig{ForceRemove: true})
235
+	err = p.backend.Remove(p.pluginID, &types.PluginRmConfig{ForceRemove: true})
236 236
 	if isNotFound(err) {
237 237
 		return nil
238 238
 	}
... ...
@@ -245,10 +246,10 @@ func (p *Controller) Close() error {
245 245
 	return nil
246 246
 }
247 247
 
248
-func convertPrivileges(ls []*runtime.PluginPrivilege) enginetypes.PluginPrivileges {
249
-	var out enginetypes.PluginPrivileges
248
+func convertPrivileges(ls []*runtime.PluginPrivilege) types.PluginPrivileges {
249
+	var out types.PluginPrivileges
250 250
 	for _, p := range ls {
251
-		pp := enginetypes.PluginPrivilege{
251
+		pp := types.PluginPrivilege{
252 252
 			Name:        p.Name,
253 253
 			Description: p.Description,
254 254
 			Value:       p.Value,
... ...
@@ -10,7 +10,8 @@ import (
10 10
 	"time"
11 11
 
12 12
 	"github.com/docker/distribution/reference"
13
-	enginetypes "github.com/docker/docker/api/types"
13
+	"github.com/docker/docker/api/types"
14
+	"github.com/docker/docker/api/types/registry"
14 15
 	"github.com/docker/docker/api/types/swarm/runtime"
15 16
 	"github.com/docker/docker/pkg/pubsub"
16 17
 	"github.com/docker/docker/plugin"
... ...
@@ -341,27 +342,27 @@ type mockBackend struct {
341 341
 	pub *pubsub.Publisher
342 342
 }
343 343
 
344
-func (m *mockBackend) Disable(name string, config *enginetypes.PluginDisableConfig) error {
344
+func (m *mockBackend) Disable(name string, config *types.PluginDisableConfig) error {
345 345
 	m.p.PluginObj.Enabled = false
346 346
 	m.pub.Publish(plugin.EventDisable{})
347 347
 	return nil
348 348
 }
349 349
 
350
-func (m *mockBackend) Enable(name string, config *enginetypes.PluginEnableConfig) error {
350
+func (m *mockBackend) Enable(name string, config *types.PluginEnableConfig) error {
351 351
 	m.p.PluginObj.Enabled = true
352 352
 	m.pub.Publish(plugin.EventEnable{})
353 353
 	return nil
354 354
 }
355 355
 
356
-func (m *mockBackend) Remove(name string, config *enginetypes.PluginRmConfig) error {
356
+func (m *mockBackend) Remove(name string, config *types.PluginRmConfig) error {
357 357
 	m.p = nil
358 358
 	m.pub.Publish(plugin.EventRemove{})
359 359
 	return nil
360 360
 }
361 361
 
362
-func (m *mockBackend) Pull(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *enginetypes.AuthConfig, privileges enginetypes.PluginPrivileges, outStream io.Writer, opts ...plugin.CreateOpt) error {
362
+func (m *mockBackend) Pull(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *registry.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer, opts ...plugin.CreateOpt) error {
363 363
 	m.p = &v2.Plugin{
364
-		PluginObj: enginetypes.Plugin{
364
+		PluginObj: types.Plugin{
365 365
 			ID:              "1234",
366 366
 			Name:            name,
367 367
 			PluginReference: ref.String(),
... ...
@@ -370,7 +371,7 @@ func (m *mockBackend) Pull(ctx context.Context, ref reference.Named, name string
370 370
 	return nil
371 371
 }
372 372
 
373
-func (m *mockBackend) Upgrade(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *enginetypes.AuthConfig, privileges enginetypes.PluginPrivileges, outStream io.Writer) error {
373
+func (m *mockBackend) Upgrade(ctx context.Context, ref reference.Named, name string, metaHeaders http.Header, authConfig *registry.AuthConfig, privileges types.PluginPrivileges, outStream io.Writer) error {
374 374
 	m.p.PluginObj.PluginReference = pluginTestRemoteUpgrade
375 375
 	return nil
376 376
 }
... ...
@@ -13,7 +13,8 @@ import (
13 13
 	"github.com/docker/docker/api/types/events"
14 14
 	"github.com/docker/docker/api/types/filters"
15 15
 	"github.com/docker/docker/api/types/network"
16
-	swarmtypes "github.com/docker/docker/api/types/swarm"
16
+	"github.com/docker/docker/api/types/registry"
17
+	"github.com/docker/docker/api/types/swarm"
17 18
 	"github.com/docker/docker/api/types/volume"
18 19
 	containerpkg "github.com/docker/docker/container"
19 20
 	clustertypes "github.com/docker/docker/daemon/cluster/provider"
... ...
@@ -48,8 +49,8 @@ type Backend interface {
48 48
 	ContainerRm(name string, config *types.ContainerRmConfig) error
49 49
 	ContainerKill(name string, sig string) error
50 50
 	SetContainerDependencyStore(name string, store exec.DependencyGetter) error
51
-	SetContainerSecretReferences(name string, refs []*swarmtypes.SecretReference) error
52
-	SetContainerConfigReferences(name string, refs []*swarmtypes.ConfigReference) error
51
+	SetContainerSecretReferences(name string, refs []*swarm.SecretReference) error
52
+	SetContainerConfigReferences(name string, refs []*swarm.ConfigReference) error
53 53
 	SystemInfo() *types.Info
54 54
 	Containers(config *types.ContainerListOptions) ([]*types.Container, error)
55 55
 	SetNetworkBootstrapKeys([]*networktypes.EncryptionKey) error
... ...
@@ -73,7 +74,7 @@ type VolumeBackend interface {
73 73
 
74 74
 // ImageBackend is used by an executor to perform image operations
75 75
 type ImageBackend interface {
76
-	PullImage(ctx context.Context, image, tag string, platform *specs.Platform, metaHeaders map[string][]string, authConfig *types.AuthConfig, outStream io.Writer) error
77
-	GetRepository(context.Context, reference.Named, *types.AuthConfig) (distribution.Repository, error)
76
+	PullImage(ctx context.Context, image, tag string, platform *specs.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error
77
+	GetRepository(context.Context, reference.Named, *registry.AuthConfig) (distribution.Repository, error)
78 78
 	GetImage(refOrID string, platform *specs.Platform) (retImg *image.Image, retErr error)
79 79
 }
... ...
@@ -16,6 +16,7 @@ import (
16 16
 	"github.com/docker/docker/api/types/backend"
17 17
 	containertypes "github.com/docker/docker/api/types/container"
18 18
 	"github.com/docker/docker/api/types/events"
19
+	"github.com/docker/docker/api/types/registry"
19 20
 	containerpkg "github.com/docker/docker/container"
20 21
 	"github.com/docker/docker/daemon"
21 22
 	"github.com/docker/docker/daemon/cluster/convert"
... ...
@@ -87,7 +88,7 @@ func (c *containerAdapter) pullImage(ctx context.Context) error {
87 87
 		encodedAuthConfig = spec.PullOptions.RegistryAuth
88 88
 	}
89 89
 
90
-	authConfig := &types.AuthConfig{}
90
+	authConfig := &registry.AuthConfig{}
91 91
 	if encodedAuthConfig != "" {
92 92
 		if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuthConfig))).Decode(authConfig); err != nil {
93 93
 			logrus.Warnf("invalid authconfig: %v", err)
... ...
@@ -12,9 +12,10 @@ import (
12 12
 	"time"
13 13
 
14 14
 	"github.com/docker/distribution/reference"
15
-	apitypes "github.com/docker/docker/api/types"
15
+	"github.com/docker/docker/api/types"
16 16
 	"github.com/docker/docker/api/types/backend"
17
-	types "github.com/docker/docker/api/types/swarm"
17
+	"github.com/docker/docker/api/types/registry"
18
+	"github.com/docker/docker/api/types/swarm"
18 19
 	timetypes "github.com/docker/docker/api/types/time"
19 20
 	"github.com/docker/docker/daemon/cluster/convert"
20 21
 	"github.com/docker/docker/errdefs"
... ...
@@ -27,7 +28,7 @@ import (
27 27
 )
28 28
 
29 29
 // GetServices returns all services of a managed swarm cluster.
30
-func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Service, error) {
30
+func (c *Cluster) GetServices(options types.ServiceListOptions) ([]swarm.Service, error) {
31 31
 	c.mu.RLock()
32 32
 	defer c.mu.RUnlock()
33 33
 
... ...
@@ -53,7 +54,7 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
53 53
 
54 54
 	if len(options.Filters.Get("runtime")) == 0 {
55 55
 		// Default to using the container runtime filter
56
-		options.Filters.Add("runtime", string(types.RuntimeContainer))
56
+		options.Filters.Add("runtime", string(swarm.RuntimeContainer))
57 57
 	}
58 58
 
59 59
 	filters := &swarmapi.ListServicesRequest_Filters{
... ...
@@ -75,7 +76,7 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
75 75
 		return nil, err
76 76
 	}
77 77
 
78
-	services := make([]types.Service, 0, len(r.Services))
78
+	services := make([]swarm.Service, 0, len(r.Services))
79 79
 
80 80
 	// if the  user requests the service statuses, we'll store the IDs needed
81 81
 	// in this slice
... ...
@@ -132,9 +133,9 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
132 132
 		// result would be quadratic. instead, make a mapping of service IDs to
133 133
 		// service statuses so that this is roughly linear. additionally,
134 134
 		// convert the status response to an engine api service status here.
135
-		serviceMap := map[string]*types.ServiceStatus{}
135
+		serviceMap := map[string]*swarm.ServiceStatus{}
136 136
 		for _, status := range resp.Statuses {
137
-			serviceMap[status.ServiceID] = &types.ServiceStatus{
137
+			serviceMap[status.ServiceID] = &swarm.ServiceStatus{
138 138
 				RunningTasks:   status.RunningTasks,
139 139
 				DesiredTasks:   status.DesiredTasks,
140 140
 				CompletedTasks: status.CompletedTasks,
... ...
@@ -159,7 +160,7 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
159 159
 }
160 160
 
161 161
 // GetService returns a service based on an ID or name.
162
-func (c *Cluster) GetService(input string, insertDefaults bool) (types.Service, error) {
162
+func (c *Cluster) GetService(input string, insertDefaults bool) (swarm.Service, error) {
163 163
 	var service *swarmapi.Service
164 164
 	if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
165 165
 		s, err := getService(ctx, state.controlClient, input, insertDefaults)
... ...
@@ -169,18 +170,18 @@ func (c *Cluster) GetService(input string, insertDefaults bool) (types.Service,
169 169
 		service = s
170 170
 		return nil
171 171
 	}); err != nil {
172
-		return types.Service{}, err
172
+		return swarm.Service{}, err
173 173
 	}
174 174
 	svc, err := convert.ServiceFromGRPC(*service)
175 175
 	if err != nil {
176
-		return types.Service{}, err
176
+		return swarm.Service{}, err
177 177
 	}
178 178
 	return svc, nil
179 179
 }
180 180
 
181 181
 // CreateService creates a new service in a managed swarm cluster.
182
-func (c *Cluster) CreateService(s types.ServiceSpec, encodedAuth string, queryRegistry bool) (*apitypes.ServiceCreateResponse, error) {
183
-	var resp *apitypes.ServiceCreateResponse
182
+func (c *Cluster) CreateService(s swarm.ServiceSpec, encodedAuth string, queryRegistry bool) (*types.ServiceCreateResponse, error) {
183
+	var resp *types.ServiceCreateResponse
184 184
 	err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
185 185
 		err := c.populateNetworkID(ctx, state.controlClient, &s)
186 186
 		if err != nil {
... ...
@@ -192,17 +193,17 @@ func (c *Cluster) CreateService(s types.ServiceSpec, encodedAuth string, queryRe
192 192
 			return errdefs.InvalidParameter(err)
193 193
 		}
194 194
 
195
-		resp = &apitypes.ServiceCreateResponse{}
195
+		resp = &types.ServiceCreateResponse{}
196 196
 
197 197
 		switch serviceSpec.Task.Runtime.(type) {
198 198
 		case *swarmapi.TaskSpec_Attachment:
199
-			return fmt.Errorf("invalid task spec: spec type %q not supported", types.RuntimeNetworkAttachment)
199
+			return fmt.Errorf("invalid task spec: spec type %q not supported", swarm.RuntimeNetworkAttachment)
200 200
 		// handle other runtimes here
201 201
 		case *swarmapi.TaskSpec_Generic:
202 202
 			switch serviceSpec.Task.GetGeneric().Kind {
203
-			case string(types.RuntimePlugin):
203
+			case string(swarm.RuntimePlugin):
204 204
 				if !c.config.Backend.HasExperimental() {
205
-					return fmt.Errorf("runtime type %q only supported in experimental", types.RuntimePlugin)
205
+					return fmt.Errorf("runtime type %q only supported in experimental", swarm.RuntimePlugin)
206 206
 				}
207 207
 				if s.TaskTemplate.PluginSpec == nil {
208 208
 					return errors.New("plugin spec must be set")
... ...
@@ -228,7 +229,7 @@ func (c *Cluster) CreateService(s types.ServiceSpec, encodedAuth string, queryRe
228 228
 			}
229 229
 
230 230
 			// retrieve auth config from encoded auth
231
-			authConfig := &apitypes.AuthConfig{}
231
+			authConfig := &registry.AuthConfig{}
232 232
 			if encodedAuth != "" {
233 233
 				authReader := strings.NewReader(encodedAuth)
234 234
 				dec := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, authReader))
... ...
@@ -282,8 +283,8 @@ func (c *Cluster) CreateService(s types.ServiceSpec, encodedAuth string, queryRe
282 282
 }
283 283
 
284 284
 // UpdateService updates existing service to match new properties.
285
-func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec types.ServiceSpec, flags apitypes.ServiceUpdateOptions, queryRegistry bool) (*apitypes.ServiceUpdateResponse, error) {
286
-	var resp *apitypes.ServiceUpdateResponse
285
+func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec swarm.ServiceSpec, flags types.ServiceUpdateOptions, queryRegistry bool) (*types.ServiceUpdateResponse, error) {
286
+	var resp *types.ServiceUpdateResponse
287 287
 
288 288
 	err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
289 289
 
... ...
@@ -302,14 +303,14 @@ func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec typ
302 302
 			return err
303 303
 		}
304 304
 
305
-		resp = &apitypes.ServiceUpdateResponse{}
305
+		resp = &types.ServiceUpdateResponse{}
306 306
 
307 307
 		switch serviceSpec.Task.Runtime.(type) {
308 308
 		case *swarmapi.TaskSpec_Attachment:
309
-			return fmt.Errorf("invalid task spec: spec type %q not supported", types.RuntimeNetworkAttachment)
309
+			return fmt.Errorf("invalid task spec: spec type %q not supported", swarm.RuntimeNetworkAttachment)
310 310
 		case *swarmapi.TaskSpec_Generic:
311 311
 			switch serviceSpec.Task.GetGeneric().Kind {
312
-			case string(types.RuntimePlugin):
312
+			case string(swarm.RuntimePlugin):
313 313
 				if spec.TaskTemplate.PluginSpec == nil {
314 314
 					return errors.New("plugin spec must be set")
315 315
 				}
... ...
@@ -328,9 +329,9 @@ func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec typ
328 328
 				// shouldn't lose it, and continue to use the one that was already present
329 329
 				var ctnr *swarmapi.ContainerSpec
330 330
 				switch flags.RegistryAuthFrom {
331
-				case apitypes.RegistryAuthFromSpec, "":
331
+				case types.RegistryAuthFromSpec, "":
332 332
 					ctnr = currentService.Spec.Task.GetContainer()
333
-				case apitypes.RegistryAuthFromPreviousSpec:
333
+				case types.RegistryAuthFromPreviousSpec:
334 334
 					if currentService.PreviousSpec == nil {
335 335
 						return errors.New("service does not have a previous spec")
336 336
 					}
... ...
@@ -349,7 +350,7 @@ func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec typ
349 349
 			}
350 350
 
351 351
 			// retrieve auth config from encoded auth
352
-			authConfig := &apitypes.AuthConfig{}
352
+			authConfig := &registry.AuthConfig{}
353 353
 			if encodedAuth != "" {
354 354
 				if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuth))).Decode(authConfig); err != nil {
355 355
 					logrus.Warnf("invalid authconfig: %v", err)
... ...
@@ -425,7 +426,7 @@ func (c *Cluster) RemoveService(input string) error {
425 425
 }
426 426
 
427 427
 // ServiceLogs collects service logs and writes them back to `config.OutStream`
428
-func (c *Cluster) ServiceLogs(ctx context.Context, selector *backend.LogSelector, config *apitypes.ContainerLogsOptions) (<-chan *backend.LogMessage, error) {
428
+func (c *Cluster) ServiceLogs(ctx context.Context, selector *backend.LogSelector, config *types.ContainerLogsOptions) (<-chan *backend.LogMessage, error) {
429 429
 	c.mu.RLock()
430 430
 	defer c.mu.RUnlock()
431 431
 
... ...
@@ -612,7 +613,7 @@ func convertSelector(ctx context.Context, cc swarmapi.ControlClient, selector *b
612 612
 
613 613
 // imageWithDigestString takes an image such as name or name:tag
614 614
 // and returns the image pinned to a digest, such as name@sha256:34234
615
-func (c *Cluster) imageWithDigestString(ctx context.Context, image string, authConfig *apitypes.AuthConfig) (string, error) {
615
+func (c *Cluster) imageWithDigestString(ctx context.Context, image string, authConfig *registry.AuthConfig) (string, error) {
616 616
 	ref, err := reference.ParseAnyReference(image)
617 617
 	if err != nil {
618 618
 		return "", err