2018-02-05 16:05:59 -05:00
|
|
|
package convert // import "github.com/docker/docker/daemon/cluster/convert"
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
types "github.com/docker/docker/api/types/swarm"
|
2017-01-23 18:50:10 -05:00
|
|
|
gogotypes "github.com/gogo/protobuf/types"
|
2022-04-21 17:33:07 -04:00
|
|
|
swarmapi "github.com/moby/swarmkit/v2/api"
|
|
|
|
"github.com/moby/swarmkit/v2/ca"
|
2016-06-13 22:52:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SwarmFromGRPC converts a grpc Cluster to a Swarm.
|
|
|
|
func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm {
|
|
|
|
swarm := types.Swarm{
|
2016-07-27 15:06:00 -04:00
|
|
|
ClusterInfo: types.ClusterInfo{
|
|
|
|
ID: c.ID,
|
|
|
|
Spec: types.Spec{
|
|
|
|
Orchestration: types.OrchestrationConfig{
|
2016-08-26 00:08:53 -04:00
|
|
|
TaskHistoryRetentionLimit: &c.Spec.Orchestration.TaskHistoryRetentionLimit,
|
2016-07-27 15:06:00 -04:00
|
|
|
},
|
|
|
|
Raft: types.RaftConfig{
|
|
|
|
SnapshotInterval: c.Spec.Raft.SnapshotInterval,
|
2016-11-02 15:29:51 -04:00
|
|
|
KeepOldSnapshots: &c.Spec.Raft.KeepOldSnapshots,
|
2016-07-27 15:06:00 -04:00
|
|
|
LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers,
|
2016-08-31 11:25:14 -04:00
|
|
|
HeartbeatTick: int(c.Spec.Raft.HeartbeatTick),
|
|
|
|
ElectionTick: int(c.Spec.Raft.ElectionTick),
|
2016-07-27 15:06:00 -04:00
|
|
|
},
|
2016-10-27 21:50:49 -04:00
|
|
|
EncryptionConfig: types.EncryptionConfig{
|
|
|
|
AutoLockManagers: c.Spec.EncryptionConfig.AutoLockManagers,
|
|
|
|
},
|
2017-05-02 17:58:57 -04:00
|
|
|
CAConfig: types.CAConfig{
|
2017-06-15 21:11:14 -04:00
|
|
|
// do not include the signing CA cert or key (it should already be redacted via the swarm APIs) -
|
|
|
|
// the key because it's secret, and the cert because otherwise doing a get + update on the spec
|
|
|
|
// can cause issues because the key would be missing and the cert wouldn't
|
|
|
|
ForceRotate: c.Spec.CAConfig.ForceRotate,
|
2017-05-02 17:58:57 -04:00
|
|
|
},
|
2016-06-13 22:52:49 -04:00
|
|
|
},
|
2017-04-26 22:46:20 -04:00
|
|
|
TLSInfo: types.TLSInfo{
|
|
|
|
TrustRoot: string(c.RootCA.CACert),
|
|
|
|
},
|
|
|
|
RootRotationInProgress: c.RootCA.RootRotation != nil,
|
2018-07-30 11:25:02 -04:00
|
|
|
DefaultAddrPool: c.DefaultAddressPool,
|
|
|
|
SubnetSize: c.SubnetSize,
|
2018-11-20 16:44:40 -05:00
|
|
|
DataPathPort: c.VXLANUDPPort,
|
2016-06-13 22:52:49 -04:00
|
|
|
},
|
2016-07-20 14:15:08 -04:00
|
|
|
JoinTokens: types.JoinTokens{
|
|
|
|
Worker: c.RootCA.JoinTokens.Worker,
|
|
|
|
Manager: c.RootCA.JoinTokens.Manager,
|
|
|
|
},
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
|
|
|
|
2017-04-26 22:46:20 -04:00
|
|
|
issuerInfo, err := ca.IssuerFromAPIRootCA(&c.RootCA)
|
|
|
|
if err == nil && issuerInfo != nil {
|
|
|
|
swarm.TLSInfo.CertIssuerSubject = issuerInfo.Subject
|
|
|
|
swarm.TLSInfo.CertIssuerPublicKey = issuerInfo.PublicKey
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:50:10 -05:00
|
|
|
heartbeatPeriod, _ := gogotypes.DurationFromProto(c.Spec.Dispatcher.HeartbeatPeriod)
|
2016-08-31 11:25:14 -04:00
|
|
|
swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod
|
2016-06-17 22:01:18 -04:00
|
|
|
|
2017-01-23 18:50:10 -05:00
|
|
|
swarm.Spec.CAConfig.NodeCertExpiry, _ = gogotypes.DurationFromProto(c.Spec.CAConfig.NodeCertExpiry)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2016-06-30 17:39:39 -04:00
|
|
|
for _, ca := range c.Spec.CAConfig.ExternalCAs {
|
|
|
|
swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{
|
|
|
|
Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())),
|
|
|
|
URL: ca.URL,
|
|
|
|
Options: ca.Options,
|
2017-04-25 18:40:46 -04:00
|
|
|
CACert: string(ca.CACert),
|
2016-06-30 17:39:39 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
// Meta
|
|
|
|
swarm.Version.Index = c.Meta.Version.Index
|
2017-01-23 18:50:10 -05:00
|
|
|
swarm.CreatedAt, _ = gogotypes.TimestampFromProto(c.Meta.CreatedAt)
|
|
|
|
swarm.UpdatedAt, _ = gogotypes.TimestampFromProto(c.Meta.UpdatedAt)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
// Annotations
|
2017-02-13 03:07:03 -05:00
|
|
|
swarm.Spec.Annotations = annotationsFromGRPC(c.Spec.Annotations)
|
2016-06-13 22:52:49 -04:00
|
|
|
|
|
|
|
return swarm
|
|
|
|
}
|
|
|
|
|
2016-07-20 14:15:08 -04:00
|
|
|
// SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec.
|
|
|
|
func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) {
|
2016-08-26 00:08:53 -04:00
|
|
|
return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec
|
|
|
|
func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) {
|
|
|
|
// We take the initSpec (either created from scratch, or returned by swarmkit),
|
|
|
|
// and will only change the value if the one taken from types.Spec is not nil or 0.
|
|
|
|
// In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo.
|
|
|
|
if s.Annotations.Name != "" {
|
|
|
|
spec.Annotations.Name = s.Annotations.Name
|
|
|
|
}
|
|
|
|
if len(s.Annotations.Labels) != 0 {
|
|
|
|
spec.Annotations.Labels = s.Annotations.Labels
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Orchestration.TaskHistoryRetentionLimit != nil {
|
|
|
|
spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit
|
|
|
|
}
|
|
|
|
if s.Raft.SnapshotInterval != 0 {
|
|
|
|
spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval
|
|
|
|
}
|
2016-11-02 15:29:51 -04:00
|
|
|
if s.Raft.KeepOldSnapshots != nil {
|
|
|
|
spec.Raft.KeepOldSnapshots = *s.Raft.KeepOldSnapshots
|
2016-08-26 00:08:53 -04:00
|
|
|
}
|
|
|
|
if s.Raft.LogEntriesForSlowFollowers != 0 {
|
|
|
|
spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers
|
|
|
|
}
|
|
|
|
if s.Raft.HeartbeatTick != 0 {
|
|
|
|
spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick)
|
|
|
|
}
|
|
|
|
if s.Raft.ElectionTick != 0 {
|
|
|
|
spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick)
|
|
|
|
}
|
|
|
|
if s.Dispatcher.HeartbeatPeriod != 0 {
|
2017-08-24 13:11:44 -04:00
|
|
|
spec.Dispatcher.HeartbeatPeriod = gogotypes.DurationProto(s.Dispatcher.HeartbeatPeriod)
|
2016-08-26 00:08:53 -04:00
|
|
|
}
|
|
|
|
if s.CAConfig.NodeCertExpiry != 0 {
|
2017-01-23 18:50:10 -05:00
|
|
|
spec.CAConfig.NodeCertExpiry = gogotypes.DurationProto(s.CAConfig.NodeCertExpiry)
|
2016-06-13 22:52:49 -04:00
|
|
|
}
|
2017-05-02 17:58:57 -04:00
|
|
|
if s.CAConfig.SigningCACert != "" {
|
|
|
|
spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert)
|
|
|
|
}
|
|
|
|
if s.CAConfig.SigningCAKey != "" {
|
2017-05-03 00:22:56 -04:00
|
|
|
// do propagate the signing CA key here because we want to provide it TO the swarm APIs
|
2017-05-02 17:58:57 -04:00
|
|
|
spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey)
|
|
|
|
}
|
|
|
|
spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate
|
2016-06-13 22:52:49 -04:00
|
|
|
|
2016-06-30 17:39:39 -04:00
|
|
|
for _, ca := range s.CAConfig.ExternalCAs {
|
|
|
|
protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))]
|
|
|
|
if !ok {
|
|
|
|
return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol)
|
|
|
|
}
|
|
|
|
spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{
|
|
|
|
Protocol: swarmapi.ExternalCA_CAProtocol(protocol),
|
|
|
|
URL: ca.URL,
|
|
|
|
Options: ca.Options,
|
2017-04-25 18:40:46 -04:00
|
|
|
CACert: []byte(ca.CACert),
|
2016-06-30 17:39:39 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-10-27 21:50:49 -04:00
|
|
|
spec.EncryptionConfig.AutoLockManagers = s.EncryptionConfig.AutoLockManagers
|
|
|
|
|
2016-06-13 22:52:49 -04:00
|
|
|
return spec, nil
|
|
|
|
}
|