github.com/ssdev-go/moby@v17.12.1-ce-rc2+incompatible/daemon/cluster/convert/swarm.go (about) 1 package convert 2 3 import ( 4 "fmt" 5 "strings" 6 7 types "github.com/docker/docker/api/types/swarm" 8 swarmapi "github.com/docker/swarmkit/api" 9 "github.com/docker/swarmkit/ca" 10 gogotypes "github.com/gogo/protobuf/types" 11 ) 12 13 // SwarmFromGRPC converts a grpc Cluster to a Swarm. 14 func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm { 15 swarm := types.Swarm{ 16 ClusterInfo: types.ClusterInfo{ 17 ID: c.ID, 18 Spec: types.Spec{ 19 Orchestration: types.OrchestrationConfig{ 20 TaskHistoryRetentionLimit: &c.Spec.Orchestration.TaskHistoryRetentionLimit, 21 }, 22 Raft: types.RaftConfig{ 23 SnapshotInterval: c.Spec.Raft.SnapshotInterval, 24 KeepOldSnapshots: &c.Spec.Raft.KeepOldSnapshots, 25 LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers, 26 HeartbeatTick: int(c.Spec.Raft.HeartbeatTick), 27 ElectionTick: int(c.Spec.Raft.ElectionTick), 28 }, 29 EncryptionConfig: types.EncryptionConfig{ 30 AutoLockManagers: c.Spec.EncryptionConfig.AutoLockManagers, 31 }, 32 CAConfig: types.CAConfig{ 33 // do not include the signing CA cert or key (it should already be redacted via the swarm APIs) - 34 // the key because it's secret, and the cert because otherwise doing a get + update on the spec 35 // can cause issues because the key would be missing and the cert wouldn't 36 ForceRotate: c.Spec.CAConfig.ForceRotate, 37 }, 38 }, 39 TLSInfo: types.TLSInfo{ 40 TrustRoot: string(c.RootCA.CACert), 41 }, 42 RootRotationInProgress: c.RootCA.RootRotation != nil, 43 }, 44 JoinTokens: types.JoinTokens{ 45 Worker: c.RootCA.JoinTokens.Worker, 46 Manager: c.RootCA.JoinTokens.Manager, 47 }, 48 } 49 50 issuerInfo, err := ca.IssuerFromAPIRootCA(&c.RootCA) 51 if err == nil && issuerInfo != nil { 52 swarm.TLSInfo.CertIssuerSubject = issuerInfo.Subject 53 swarm.TLSInfo.CertIssuerPublicKey = issuerInfo.PublicKey 54 } 55 56 heartbeatPeriod, _ := gogotypes.DurationFromProto(c.Spec.Dispatcher.HeartbeatPeriod) 57 swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod 58 59 swarm.Spec.CAConfig.NodeCertExpiry, _ = gogotypes.DurationFromProto(c.Spec.CAConfig.NodeCertExpiry) 60 61 for _, ca := range c.Spec.CAConfig.ExternalCAs { 62 swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{ 63 Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())), 64 URL: ca.URL, 65 Options: ca.Options, 66 CACert: string(ca.CACert), 67 }) 68 } 69 70 // Meta 71 swarm.Version.Index = c.Meta.Version.Index 72 swarm.CreatedAt, _ = gogotypes.TimestampFromProto(c.Meta.CreatedAt) 73 swarm.UpdatedAt, _ = gogotypes.TimestampFromProto(c.Meta.UpdatedAt) 74 75 // Annotations 76 swarm.Spec.Annotations = annotationsFromGRPC(c.Spec.Annotations) 77 78 return swarm 79 } 80 81 // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec. 82 func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) { 83 return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{}) 84 } 85 86 // MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec 87 func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) { 88 // We take the initSpec (either created from scratch, or returned by swarmkit), 89 // and will only change the value if the one taken from types.Spec is not nil or 0. 90 // In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo. 91 if s.Annotations.Name != "" { 92 spec.Annotations.Name = s.Annotations.Name 93 } 94 if len(s.Annotations.Labels) != 0 { 95 spec.Annotations.Labels = s.Annotations.Labels 96 } 97 98 if s.Orchestration.TaskHistoryRetentionLimit != nil { 99 spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit 100 } 101 if s.Raft.SnapshotInterval != 0 { 102 spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval 103 } 104 if s.Raft.KeepOldSnapshots != nil { 105 spec.Raft.KeepOldSnapshots = *s.Raft.KeepOldSnapshots 106 } 107 if s.Raft.LogEntriesForSlowFollowers != 0 { 108 spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers 109 } 110 if s.Raft.HeartbeatTick != 0 { 111 spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick) 112 } 113 if s.Raft.ElectionTick != 0 { 114 spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick) 115 } 116 if s.Dispatcher.HeartbeatPeriod != 0 { 117 spec.Dispatcher.HeartbeatPeriod = gogotypes.DurationProto(s.Dispatcher.HeartbeatPeriod) 118 } 119 if s.CAConfig.NodeCertExpiry != 0 { 120 spec.CAConfig.NodeCertExpiry = gogotypes.DurationProto(s.CAConfig.NodeCertExpiry) 121 } 122 if s.CAConfig.SigningCACert != "" { 123 spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert) 124 } 125 if s.CAConfig.SigningCAKey != "" { 126 // do propagate the signing CA key here because we want to provide it TO the swarm APIs 127 spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey) 128 } 129 spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate 130 131 for _, ca := range s.CAConfig.ExternalCAs { 132 protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))] 133 if !ok { 134 return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol) 135 } 136 spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{ 137 Protocol: swarmapi.ExternalCA_CAProtocol(protocol), 138 URL: ca.URL, 139 Options: ca.Options, 140 CACert: []byte(ca.CACert), 141 }) 142 } 143 144 spec.EncryptionConfig.AutoLockManagers = s.EncryptionConfig.AutoLockManagers 145 146 return spec, nil 147 }