1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Add a lockedManagerAction method to Cluster…

… in order to remove duplication.
Each time we update a cluster object, we do some common
operations (lock, verify it's on a manager, get the request context,
and the update). This introduce a method and refactor few
update/remove method that allows to duplicate less code.

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2017-02-28 11:12:11 +01:00
parent f7819fcb25
commit 250e05e427
No known key found for this signature in database
GPG key ID: 083CC6FD6EB699A3
7 changed files with 332 additions and 423 deletions

View file

@ -386,3 +386,18 @@ func detectLockedError(err error) error {
} }
return err return err
} }
func (c *Cluster) lockedManagerAction(fn func(ctx context.Context, state nodeState) error) error {
c.mu.RLock()
defer c.mu.RUnlock()
state := c.currentNodeState()
if !state.IsActiveManager() {
return c.errNoManager(state)
}
ctx, cancel := c.getRequestContext()
defer cancel()
return fn(ctx, state)
}

View file

@ -48,19 +48,16 @@ func (c *Cluster) getNetworks(filters *swarmapi.ListNetworksRequest_Filters) ([]
// GetNetwork returns a cluster network by an ID. // GetNetwork returns a cluster network by an ID.
func (c *Cluster) GetNetwork(input string) (apitypes.NetworkResource, error) { func (c *Cluster) GetNetwork(input string) (apitypes.NetworkResource, error) {
c.mu.RLock() var network *swarmapi.Network
defer c.mu.RUnlock()
state := c.currentNodeState() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
if !state.IsActiveManager() { n, err := getNetwork(ctx, state.controlClient, input)
return apitypes.NetworkResource{}, c.errNoManager(state) if err != nil {
} return err
}
ctx, cancel := c.getRequestContext() network = n
defer cancel() return nil
}); err != nil {
network, err := getNetwork(ctx, state.controlClient, input)
if err != nil {
return apitypes.NetworkResource{}, err return apitypes.NetworkResource{}, err
} }
return convert.BasicNetworkFromGRPC(*network), nil return convert.BasicNetworkFromGRPC(*network), nil
@ -224,51 +221,38 @@ func (c *Cluster) DetachNetwork(target string, containerID string) error {
// CreateNetwork creates a new cluster managed network. // CreateNetwork creates a new cluster managed network.
func (c *Cluster) CreateNetwork(s apitypes.NetworkCreateRequest) (string, error) { func (c *Cluster) CreateNetwork(s apitypes.NetworkCreateRequest) (string, error) {
c.mu.RLock()
defer c.mu.RUnlock()
state := c.currentNodeState()
if !state.IsActiveManager() {
return "", c.errNoManager(state)
}
if runconfig.IsPreDefinedNetwork(s.Name) { if runconfig.IsPreDefinedNetwork(s.Name) {
err := fmt.Errorf("%s is a pre-defined network and cannot be created", s.Name) err := fmt.Errorf("%s is a pre-defined network and cannot be created", s.Name)
return "", apierrors.NewRequestForbiddenError(err) return "", apierrors.NewRequestForbiddenError(err)
} }
ctx, cancel := c.getRequestContext() var resp *swarmapi.CreateNetworkResponse
defer cancel() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
networkSpec := convert.BasicNetworkCreateToGRPC(s)
networkSpec := convert.BasicNetworkCreateToGRPC(s) r, err := state.controlClient.CreateNetwork(ctx, &swarmapi.CreateNetworkRequest{Spec: &networkSpec})
r, err := state.controlClient.CreateNetwork(ctx, &swarmapi.CreateNetworkRequest{Spec: &networkSpec}) if err != nil {
if err != nil { return err
}
resp = r
return nil
}); err != nil {
return "", err return "", err
} }
return r.Network.ID, nil return resp.Network.ID, nil
} }
// RemoveNetwork removes a cluster network. // RemoveNetwork removes a cluster network.
func (c *Cluster) RemoveNetwork(input string) error { func (c *Cluster) RemoveNetwork(input string) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() network, err := getNetwork(ctx, state.controlClient, input)
if err != nil {
return err
}
state := c.currentNodeState() _, err = state.controlClient.RemoveNetwork(ctx, &swarmapi.RemoveNetworkRequest{NetworkID: network.ID})
if !state.IsActiveManager() {
return c.errNoManager(state)
}
ctx, cancel := c.getRequestContext()
defer cancel()
network, err := getNetwork(ctx, state.controlClient, input)
if err != nil {
return err return err
} })
_, err = state.controlClient.RemoveNetwork(ctx, &swarmapi.RemoveNetworkRequest{NetworkID: network.ID})
return err
} }
func (c *Cluster) populateNetworkID(ctx context.Context, client swarmapi.ControlClient, s *types.ServiceSpec) error { func (c *Cluster) populateNetworkID(ctx context.Context, client swarmapi.ControlClient, s *types.ServiceSpec) error {

View file

@ -6,6 +6,7 @@ import (
types "github.com/docker/docker/api/types/swarm" types "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/cluster/convert" "github.com/docker/docker/daemon/cluster/convert"
swarmapi "github.com/docker/swarmkit/api" swarmapi "github.com/docker/swarmkit/api"
"golang.org/x/net/context"
) )
// GetNodes returns a list of all nodes known to a cluster. // GetNodes returns a list of all nodes known to a cluster.
@ -43,78 +44,61 @@ func (c *Cluster) GetNodes(options apitypes.NodeListOptions) ([]types.Node, erro
// GetNode returns a node based on an ID. // GetNode returns a node based on an ID.
func (c *Cluster) GetNode(input string) (types.Node, error) { func (c *Cluster) GetNode(input string) (types.Node, error) {
c.mu.RLock() var node *swarmapi.Node
defer c.mu.RUnlock()
state := c.currentNodeState() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
if !state.IsActiveManager() { n, err := getNode(ctx, state.controlClient, input)
return types.Node{}, c.errNoManager(state) if err != nil {
} return err
}
ctx, cancel := c.getRequestContext() node = n
defer cancel() return nil
}); err != nil {
node, err := getNode(ctx, state.controlClient, input)
if err != nil {
return types.Node{}, err return types.Node{}, err
} }
return convert.NodeFromGRPC(*node), nil return convert.NodeFromGRPC(*node), nil
} }
// UpdateNode updates existing nodes properties. // UpdateNode updates existing nodes properties.
func (c *Cluster) UpdateNode(input string, version uint64, spec types.NodeSpec) error { func (c *Cluster) UpdateNode(input string, version uint64, spec types.NodeSpec) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() nodeSpec, err := convert.NodeSpecToGRPC(spec)
if err != nil {
return apierrors.NewBadRequestError(err)
}
state := c.currentNodeState() ctx, cancel := c.getRequestContext()
if !state.IsActiveManager() { defer cancel()
return c.errNoManager(state)
}
nodeSpec, err := convert.NodeSpecToGRPC(spec) currentNode, err := getNode(ctx, state.controlClient, input)
if err != nil { if err != nil {
return apierrors.NewBadRequestError(err) return err
} }
ctx, cancel := c.getRequestContext() _, err = state.controlClient.UpdateNode(
defer cancel() ctx,
&swarmapi.UpdateNodeRequest{
currentNode, err := getNode(ctx, state.controlClient, input) NodeID: currentNode.ID,
if err != nil { Spec: &nodeSpec,
return err NodeVersion: &swarmapi.Version{
} Index: version,
},
_, err = state.controlClient.UpdateNode(
ctx,
&swarmapi.UpdateNodeRequest{
NodeID: currentNode.ID,
Spec: &nodeSpec,
NodeVersion: &swarmapi.Version{
Index: version,
}, },
}, )
) return err
return err })
} }
// RemoveNode removes a node from a cluster // RemoveNode removes a node from a cluster
func (c *Cluster) RemoveNode(input string, force bool) error { func (c *Cluster) RemoveNode(input string, force bool) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() node, err := getNode(ctx, state.controlClient, input)
if err != nil {
return err
}
state := c.currentNodeState() _, err = state.controlClient.RemoveNode(ctx, &swarmapi.RemoveNodeRequest{NodeID: node.ID, Force: force})
if !state.IsActiveManager() {
return c.errNoManager(state)
}
ctx, cancel := c.getRequestContext()
defer cancel()
node, err := getNode(ctx, state.controlClient, input)
if err != nil {
return err return err
} })
_, err = state.controlClient.RemoveNode(ctx, &swarmapi.RemoveNodeRequest{NodeID: node.ID, Force: force})
return err
} }

View file

@ -5,23 +5,21 @@ import (
types "github.com/docker/docker/api/types/swarm" types "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/cluster/convert" "github.com/docker/docker/daemon/cluster/convert"
swarmapi "github.com/docker/swarmkit/api" swarmapi "github.com/docker/swarmkit/api"
"golang.org/x/net/context"
) )
// GetSecret returns a secret from a managed swarm cluster // GetSecret returns a secret from a managed swarm cluster
func (c *Cluster) GetSecret(input string) (types.Secret, error) { func (c *Cluster) GetSecret(input string) (types.Secret, error) {
c.mu.RLock() var secret *swarmapi.Secret
defer c.mu.RUnlock()
state := c.currentNodeState() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
if !state.IsActiveManager() { s, err := getSecret(ctx, state.controlClient, input)
return types.Secret{}, c.errNoManager(state) if err != nil {
} return err
}
ctx, cancel := c.getRequestContext() secret = s
defer cancel() return nil
}); err != nil {
secret, err := getSecret(ctx, state.controlClient, input)
if err != nil {
return types.Secret{}, err return types.Secret{}, err
} }
return convert.SecretFromGRPC(secret), nil return convert.SecretFromGRPC(secret), nil
@ -61,77 +59,54 @@ func (c *Cluster) GetSecrets(options apitypes.SecretListOptions) ([]types.Secret
// CreateSecret creates a new secret in a managed swarm cluster. // CreateSecret creates a new secret in a managed swarm cluster.
func (c *Cluster) CreateSecret(s types.SecretSpec) (string, error) { func (c *Cluster) CreateSecret(s types.SecretSpec) (string, error) {
c.mu.RLock() var resp *swarmapi.CreateSecretResponse
defer c.mu.RUnlock() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
secretSpec := convert.SecretSpecToGRPC(s)
state := c.currentNodeState() r, err := state.controlClient.CreateSecret(ctx,
if !state.IsActiveManager() { &swarmapi.CreateSecretRequest{Spec: &secretSpec})
return "", c.errNoManager(state) if err != nil {
} return err
}
ctx, cancel := c.getRequestContext() resp = r
defer cancel() return nil
}); err != nil {
secretSpec := convert.SecretSpecToGRPC(s)
r, err := state.controlClient.CreateSecret(ctx,
&swarmapi.CreateSecretRequest{Spec: &secretSpec})
if err != nil {
return "", err return "", err
} }
return resp.Secret.ID, nil
return r.Secret.ID, nil
} }
// RemoveSecret removes a secret from a managed swarm cluster. // RemoveSecret removes a secret from a managed swarm cluster.
func (c *Cluster) RemoveSecret(input string) error { func (c *Cluster) RemoveSecret(input string) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() secret, err := getSecret(ctx, state.controlClient, input)
if err != nil {
return err
}
state := c.currentNodeState() req := &swarmapi.RemoveSecretRequest{
if !state.IsActiveManager() { SecretID: secret.ID,
return c.errNoManager(state) }
}
ctx, cancel := c.getRequestContext() _, err = state.controlClient.RemoveSecret(ctx, req)
defer cancel()
secret, err := getSecret(ctx, state.controlClient, input)
if err != nil {
return err return err
} })
req := &swarmapi.RemoveSecretRequest{
SecretID: secret.ID,
}
_, err = state.controlClient.RemoveSecret(ctx, req)
return err
} }
// UpdateSecret updates a secret in a managed swarm cluster. // UpdateSecret updates a secret in a managed swarm cluster.
// Note: this is not exposed to the CLI but is available from the API only // Note: this is not exposed to the CLI but is available from the API only
func (c *Cluster) UpdateSecret(id string, version uint64, spec types.SecretSpec) error { func (c *Cluster) UpdateSecret(id string, version uint64, spec types.SecretSpec) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() secretSpec := convert.SecretSpecToGRPC(spec)
state := c.currentNodeState() _, err := state.controlClient.UpdateSecret(ctx,
if !state.IsActiveManager() { &swarmapi.UpdateSecretRequest{
return c.errNoManager(state) SecretID: id,
} SecretVersion: &swarmapi.Version{
Index: version,
ctx, cancel := c.getRequestContext() },
defer cancel() Spec: &secretSpec,
})
secretSpec := convert.SecretSpecToGRPC(spec) return err
})
_, err := state.controlClient.UpdateSecret(ctx,
&swarmapi.UpdateSecretRequest{
SecretID: id,
SecretVersion: &swarmapi.Version{
Index: version,
},
Spec: &secretSpec,
})
return err
} }

View file

@ -59,19 +59,15 @@ func (c *Cluster) GetServices(options apitypes.ServiceListOptions) ([]types.Serv
// GetService returns a service based on an ID or name. // GetService returns a service based on an ID or name.
func (c *Cluster) GetService(input string) (types.Service, error) { func (c *Cluster) GetService(input string) (types.Service, error) {
c.mu.RLock() var service *swarmapi.Service
defer c.mu.RUnlock() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
s, err := getService(ctx, state.controlClient, input)
state := c.currentNodeState() if err != nil {
if !state.IsActiveManager() { return err
return types.Service{}, c.errNoManager(state) }
} service = s
return nil
ctx, cancel := c.getRequestContext() }); err != nil {
defer cancel()
service, err := getService(ctx, state.controlClient, input)
if err != nil {
return types.Service{}, err return types.Service{}, err
} }
return convert.ServiceFromGRPC(*service), nil return convert.ServiceFromGRPC(*service), nil
@ -79,187 +75,165 @@ func (c *Cluster) GetService(input string) (types.Service, error) {
// CreateService creates a new service in a managed swarm cluster. // CreateService creates a new service in a managed swarm cluster.
func (c *Cluster) CreateService(s types.ServiceSpec, encodedAuth string) (*apitypes.ServiceCreateResponse, error) { func (c *Cluster) CreateService(s types.ServiceSpec, encodedAuth string) (*apitypes.ServiceCreateResponse, error) {
c.mu.RLock() var resp *apitypes.ServiceCreateResponse
defer c.mu.RUnlock() err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
err := c.populateNetworkID(ctx, state.controlClient, &s)
state := c.currentNodeState()
if !state.IsActiveManager() {
return nil, c.errNoManager(state)
}
ctx, cancel := c.getRequestContext()
defer cancel()
err := c.populateNetworkID(ctx, state.controlClient, &s)
if err != nil {
return nil, err
}
serviceSpec, err := convert.ServiceSpecToGRPC(s)
if err != nil {
return nil, apierrors.NewBadRequestError(err)
}
ctnr := serviceSpec.Task.GetContainer()
if ctnr == nil {
return nil, errors.New("service does not use container tasks")
}
if encodedAuth != "" {
ctnr.PullOptions = &swarmapi.ContainerSpec_PullOptions{RegistryAuth: encodedAuth}
}
// retrieve auth config from encoded auth
authConfig := &apitypes.AuthConfig{}
if encodedAuth != "" {
if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuth))).Decode(authConfig); err != nil {
logrus.Warnf("invalid authconfig: %v", err)
}
}
resp := &apitypes.ServiceCreateResponse{}
// pin image by digest
if os.Getenv("DOCKER_SERVICE_PREFER_OFFLINE_IMAGE") != "1" {
digestImage, err := c.imageWithDigestString(ctx, ctnr.Image, authConfig)
if err != nil { if err != nil {
logrus.Warnf("unable to pin image %s to digest: %s", ctnr.Image, err.Error()) return err
resp.Warnings = append(resp.Warnings, fmt.Sprintf("unable to pin image %s to digest: %s", ctnr.Image, err.Error()))
} else if ctnr.Image != digestImage {
logrus.Debugf("pinning image %s by digest: %s", ctnr.Image, digestImage)
ctnr.Image = digestImage
} else {
logrus.Debugf("creating service using supplied digest reference %s", ctnr.Image)
} }
}
r, err := state.controlClient.CreateService(ctx, &swarmapi.CreateServiceRequest{Spec: &serviceSpec}) serviceSpec, err := convert.ServiceSpecToGRPC(s)
if err != nil { if err != nil {
return nil, err return apierrors.NewBadRequestError(err)
} }
resp.ID = r.Service.ID ctnr := serviceSpec.Task.GetContainer()
return resp, nil if ctnr == nil {
return errors.New("service does not use container tasks")
}
if encodedAuth != "" {
ctnr.PullOptions = &swarmapi.ContainerSpec_PullOptions{RegistryAuth: encodedAuth}
}
// retrieve auth config from encoded auth
authConfig := &apitypes.AuthConfig{}
if encodedAuth != "" {
if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuth))).Decode(authConfig); err != nil {
logrus.Warnf("invalid authconfig: %v", err)
}
}
resp = &apitypes.ServiceCreateResponse{}
// pin image by digest
if os.Getenv("DOCKER_SERVICE_PREFER_OFFLINE_IMAGE") != "1" {
digestImage, err := c.imageWithDigestString(ctx, ctnr.Image, authConfig)
if err != nil {
logrus.Warnf("unable to pin image %s to digest: %s", ctnr.Image, err.Error())
resp.Warnings = append(resp.Warnings, fmt.Sprintf("unable to pin image %s to digest: %s", ctnr.Image, err.Error()))
} else if ctnr.Image != digestImage {
logrus.Debugf("pinning image %s by digest: %s", ctnr.Image, digestImage)
ctnr.Image = digestImage
} else {
logrus.Debugf("creating service using supplied digest reference %s", ctnr.Image)
}
}
r, err := state.controlClient.CreateService(ctx, &swarmapi.CreateServiceRequest{Spec: &serviceSpec})
if err != nil {
return err
}
resp.ID = r.Service.ID
return nil
})
return resp, err
} }
// UpdateService updates existing service to match new properties. // UpdateService updates existing service to match new properties.
func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec types.ServiceSpec, encodedAuth string, registryAuthFrom string) (*apitypes.ServiceUpdateResponse, error) { func (c *Cluster) UpdateService(serviceIDOrName string, version uint64, spec types.ServiceSpec, encodedAuth string, registryAuthFrom string) (*apitypes.ServiceUpdateResponse, error) {
c.mu.RLock() var resp *apitypes.ServiceUpdateResponse
defer c.mu.RUnlock()
state := c.currentNodeState() err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
if !state.IsActiveManager() {
return nil, c.errNoManager(state)
}
ctx, cancel := c.getRequestContext() err := c.populateNetworkID(ctx, state.controlClient, &spec)
defer cancel()
err := c.populateNetworkID(ctx, state.controlClient, &spec)
if err != nil {
return nil, err
}
serviceSpec, err := convert.ServiceSpecToGRPC(spec)
if err != nil {
return nil, apierrors.NewBadRequestError(err)
}
currentService, err := getService(ctx, state.controlClient, serviceIDOrName)
if err != nil {
return nil, err
}
newCtnr := serviceSpec.Task.GetContainer()
if newCtnr == nil {
return nil, errors.New("service does not use container tasks")
}
if encodedAuth != "" {
newCtnr.PullOptions = &swarmapi.ContainerSpec_PullOptions{RegistryAuth: encodedAuth}
} else {
// this is needed because if the encodedAuth isn't being updated then we
// shouldn't lose it, and continue to use the one that was already present
var ctnr *swarmapi.ContainerSpec
switch registryAuthFrom {
case apitypes.RegistryAuthFromSpec, "":
ctnr = currentService.Spec.Task.GetContainer()
case apitypes.RegistryAuthFromPreviousSpec:
if currentService.PreviousSpec == nil {
return nil, errors.New("service does not have a previous spec")
}
ctnr = currentService.PreviousSpec.Task.GetContainer()
default:
return nil, errors.New("unsupported registryAuthFrom value")
}
if ctnr == nil {
return nil, errors.New("service does not use container tasks")
}
newCtnr.PullOptions = ctnr.PullOptions
// update encodedAuth so it can be used to pin image by digest
if ctnr.PullOptions != nil {
encodedAuth = ctnr.PullOptions.RegistryAuth
}
}
// retrieve auth config from encoded auth
authConfig := &apitypes.AuthConfig{}
if encodedAuth != "" {
if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuth))).Decode(authConfig); err != nil {
logrus.Warnf("invalid authconfig: %v", err)
}
}
resp := &apitypes.ServiceUpdateResponse{}
// pin image by digest
if os.Getenv("DOCKER_SERVICE_PREFER_OFFLINE_IMAGE") != "1" {
digestImage, err := c.imageWithDigestString(ctx, newCtnr.Image, authConfig)
if err != nil { if err != nil {
logrus.Warnf("unable to pin image %s to digest: %s", newCtnr.Image, err.Error()) return err
resp.Warnings = append(resp.Warnings, fmt.Sprintf("unable to pin image %s to digest: %s", newCtnr.Image, err.Error()))
} else if newCtnr.Image != digestImage {
logrus.Debugf("pinning image %s by digest: %s", newCtnr.Image, digestImage)
newCtnr.Image = digestImage
} else {
logrus.Debugf("updating service using supplied digest reference %s", newCtnr.Image)
} }
}
_, err = state.controlClient.UpdateService( serviceSpec, err := convert.ServiceSpecToGRPC(spec)
ctx, if err != nil {
&swarmapi.UpdateServiceRequest{ return apierrors.NewBadRequestError(err)
ServiceID: currentService.ID, }
Spec: &serviceSpec,
ServiceVersion: &swarmapi.Version{ currentService, err := getService(ctx, state.controlClient, serviceIDOrName)
Index: version, if err != nil {
return err
}
newCtnr := serviceSpec.Task.GetContainer()
if newCtnr == nil {
return errors.New("service does not use container tasks")
}
if encodedAuth != "" {
newCtnr.PullOptions = &swarmapi.ContainerSpec_PullOptions{RegistryAuth: encodedAuth}
} else {
// this is needed because if the encodedAuth isn't being updated then we
// shouldn't lose it, and continue to use the one that was already present
var ctnr *swarmapi.ContainerSpec
switch registryAuthFrom {
case apitypes.RegistryAuthFromSpec, "":
ctnr = currentService.Spec.Task.GetContainer()
case apitypes.RegistryAuthFromPreviousSpec:
if currentService.PreviousSpec == nil {
return errors.New("service does not have a previous spec")
}
ctnr = currentService.PreviousSpec.Task.GetContainer()
default:
return errors.New("unsupported registryAuthFrom value")
}
if ctnr == nil {
return errors.New("service does not use container tasks")
}
newCtnr.PullOptions = ctnr.PullOptions
// update encodedAuth so it can be used to pin image by digest
if ctnr.PullOptions != nil {
encodedAuth = ctnr.PullOptions.RegistryAuth
}
}
// retrieve auth config from encoded auth
authConfig := &apitypes.AuthConfig{}
if encodedAuth != "" {
if err := json.NewDecoder(base64.NewDecoder(base64.URLEncoding, strings.NewReader(encodedAuth))).Decode(authConfig); err != nil {
logrus.Warnf("invalid authconfig: %v", err)
}
}
resp := &apitypes.ServiceUpdateResponse{}
// pin image by digest
if os.Getenv("DOCKER_SERVICE_PREFER_OFFLINE_IMAGE") != "1" {
digestImage, err := c.imageWithDigestString(ctx, newCtnr.Image, authConfig)
if err != nil {
logrus.Warnf("unable to pin image %s to digest: %s", newCtnr.Image, err.Error())
resp.Warnings = append(resp.Warnings, fmt.Sprintf("unable to pin image %s to digest: %s", newCtnr.Image, err.Error()))
} else if newCtnr.Image != digestImage {
logrus.Debugf("pinning image %s by digest: %s", newCtnr.Image, digestImage)
newCtnr.Image = digestImage
} else {
logrus.Debugf("updating service using supplied digest reference %s", newCtnr.Image)
}
}
_, err = state.controlClient.UpdateService(
ctx,
&swarmapi.UpdateServiceRequest{
ServiceID: currentService.ID,
Spec: &serviceSpec,
ServiceVersion: &swarmapi.Version{
Index: version,
},
}, },
}, )
) return err
})
return resp, err return resp, err
} }
// RemoveService removes a service from a managed swarm cluster. // RemoveService removes a service from a managed swarm cluster.
func (c *Cluster) RemoveService(input string) error { func (c *Cluster) RemoveService(input string) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() service, err := getService(ctx, state.controlClient, input)
if err != nil {
return err
}
state := c.currentNodeState() _, err = state.controlClient.RemoveService(ctx, &swarmapi.RemoveServiceRequest{ServiceID: service.ID})
if !state.IsActiveManager() {
return c.errNoManager(state)
}
ctx, cancel := c.getRequestContext()
defer cancel()
service, err := getService(ctx, state.controlClient, input)
if err != nil {
return err return err
} })
_, err = state.controlClient.RemoveService(ctx, &swarmapi.RemoveServiceRequest{ServiceID: service.ID})
return err
} }
// ServiceLogs collects service logs and writes them back to `config.OutStream` // ServiceLogs collects service logs and writes them back to `config.OutStream`

View file

@ -187,95 +187,75 @@ func (c *Cluster) Join(req types.JoinRequest) error {
// Inspect retrieves the configuration properties of a managed swarm cluster. // Inspect retrieves the configuration properties of a managed swarm cluster.
func (c *Cluster) Inspect() (types.Swarm, error) { func (c *Cluster) Inspect() (types.Swarm, error) {
c.mu.RLock() var swarm *swarmapi.Cluster
defer c.mu.RUnlock() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
s, err := getSwarm(ctx, state.controlClient)
state := c.currentNodeState() if err != nil {
if !state.IsActiveManager() { return err
return types.Swarm{}, c.errNoManager(state) }
} swarm = s
return nil
ctx, cancel := c.getRequestContext() }); err != nil {
defer cancel()
swarm, err := getSwarm(ctx, state.controlClient)
if err != nil {
return types.Swarm{}, err return types.Swarm{}, err
} }
return convert.SwarmFromGRPC(*swarm), nil return convert.SwarmFromGRPC(*swarm), nil
} }
// Update updates configuration of a managed swarm cluster. // Update updates configuration of a managed swarm cluster.
func (c *Cluster) Update(version uint64, spec types.Spec, flags types.UpdateFlags) error { func (c *Cluster) Update(version uint64, spec types.Spec, flags types.UpdateFlags) error {
c.mu.RLock() return c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
defer c.mu.RUnlock() swarm, err := getSwarm(ctx, state.controlClient)
if err != nil {
return err
}
state := c.currentNodeState() // In update, client should provide the complete spec of the swarm, including
if !state.IsActiveManager() { // Name and Labels. If a field is specified with 0 or nil, then the default value
return c.errNoManager(state) // will be used to swarmkit.
} clusterSpec, err := convert.SwarmSpecToGRPC(spec)
if err != nil {
return apierrors.NewBadRequestError(err)
}
ctx, cancel := c.getRequestContext() _, err = state.controlClient.UpdateCluster(
defer cancel() ctx,
&swarmapi.UpdateClusterRequest{
swarm, err := getSwarm(ctx, state.controlClient) ClusterID: swarm.ID,
if err != nil { Spec: &clusterSpec,
ClusterVersion: &swarmapi.Version{
Index: version,
},
Rotation: swarmapi.KeyRotation{
WorkerJoinToken: flags.RotateWorkerToken,
ManagerJoinToken: flags.RotateManagerToken,
ManagerUnlockKey: flags.RotateManagerUnlockKey,
},
},
)
return err return err
} })
// In update, client should provide the complete spec of the swarm, including
// Name and Labels. If a field is specified with 0 or nil, then the default value
// will be used to swarmkit.
clusterSpec, err := convert.SwarmSpecToGRPC(spec)
if err != nil {
return apierrors.NewBadRequestError(err)
}
_, err = state.controlClient.UpdateCluster(
ctx,
&swarmapi.UpdateClusterRequest{
ClusterID: swarm.ID,
Spec: &clusterSpec,
ClusterVersion: &swarmapi.Version{
Index: version,
},
Rotation: swarmapi.KeyRotation{
WorkerJoinToken: flags.RotateWorkerToken,
ManagerJoinToken: flags.RotateManagerToken,
ManagerUnlockKey: flags.RotateManagerUnlockKey,
},
},
)
return err
} }
// GetUnlockKey returns the unlock key for the swarm. // GetUnlockKey returns the unlock key for the swarm.
func (c *Cluster) GetUnlockKey() (string, error) { func (c *Cluster) GetUnlockKey() (string, error) {
c.mu.RLock() var resp *swarmapi.GetUnlockKeyResponse
defer c.mu.RUnlock() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
client := swarmapi.NewCAClient(state.grpcConn)
state := c.currentNodeState() r, err := client.GetUnlockKey(ctx, &swarmapi.GetUnlockKeyRequest{})
if !state.IsActiveManager() { if err != nil {
return "", c.errNoManager(state) return err
} }
resp = r
ctx, cancel := c.getRequestContext() return nil
defer cancel() }); err != nil {
client := swarmapi.NewCAClient(state.grpcConn)
r, err := client.GetUnlockKey(ctx, &swarmapi.GetUnlockKeyRequest{})
if err != nil {
return "", err return "", err
} }
if len(resp.UnlockKey) == 0 {
if len(r.UnlockKey) == 0 {
// no key // no key
return "", nil return "", nil
} }
return encryption.HumanReadableKey(resp.UnlockKey), nil
return encryption.HumanReadableKey(r.UnlockKey), nil
} }
// UnlockSwarm provides a key to decrypt data that is encrypted at rest. // UnlockSwarm provides a key to decrypt data that is encrypted at rest.

View file

@ -6,6 +6,7 @@ import (
types "github.com/docker/docker/api/types/swarm" types "github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/cluster/convert" "github.com/docker/docker/daemon/cluster/convert"
swarmapi "github.com/docker/swarmkit/api" swarmapi "github.com/docker/swarmkit/api"
"golang.org/x/net/context"
) )
// GetTasks returns a list of tasks matching the filter options. // GetTasks returns a list of tasks matching the filter options.
@ -71,19 +72,15 @@ func (c *Cluster) GetTasks(options apitypes.TaskListOptions) ([]types.Task, erro
// GetTask returns a task by an ID. // GetTask returns a task by an ID.
func (c *Cluster) GetTask(input string) (types.Task, error) { func (c *Cluster) GetTask(input string) (types.Task, error) {
c.mu.RLock() var task *swarmapi.Task
defer c.mu.RUnlock() if err := c.lockedManagerAction(func(ctx context.Context, state nodeState) error {
t, err := getTask(ctx, state.controlClient, input)
state := c.currentNodeState() if err != nil {
if !state.IsActiveManager() { return err
return types.Task{}, c.errNoManager(state) }
} task = t
return nil
ctx, cancel := c.getRequestContext() }); err != nil {
defer cancel()
task, err := getTask(ctx, state.controlClient, input)
if err != nil {
return types.Task{}, err return types.Task{}, err
} }
return convert.TaskFromGRPC(*task), nil return convert.TaskFromGRPC(*task), nil