github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/daemon/cluster/convert/swarm.go (about) 1 package convert // import "github.com/docker/docker/daemon/cluster/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 DefaultAddrPool: c.DefaultAddressPool, 44 SubnetSize: c.SubnetSize, 45 }, 46 JoinTokens: types.JoinTokens{ 47 Worker: c.RootCA.JoinTokens.Worker, 48 Manager: c.RootCA.JoinTokens.Manager, 49 }, 50 } 51 52 issuerInfo, err := ca.IssuerFromAPIRootCA(&c.RootCA) 53 if err == nil && issuerInfo != nil { 54 swarm.TLSInfo.CertIssuerSubject = issuerInfo.Subject 55 swarm.TLSInfo.CertIssuerPublicKey = issuerInfo.PublicKey 56 } 57 58 heartbeatPeriod, _ := gogotypes.DurationFromProto(c.Spec.Dispatcher.HeartbeatPeriod) 59 swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod 60 61 swarm.Spec.CAConfig.NodeCertExpiry, _ = gogotypes.DurationFromProto(c.Spec.CAConfig.NodeCertExpiry) 62 63 for _, ca := range c.Spec.CAConfig.ExternalCAs { 64 swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{ 65 Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())), 66 URL: ca.URL, 67 Options: ca.Options, 68 CACert: string(ca.CACert), 69 }) 70 } 71 72 // Meta 73 swarm.Version.Index = c.Meta.Version.Index 74 swarm.CreatedAt, _ = gogotypes.TimestampFromProto(c.Meta.CreatedAt) 75 swarm.UpdatedAt, _ = gogotypes.TimestampFromProto(c.Meta.UpdatedAt) 76 77 // Annotations 78 swarm.Spec.Annotations = annotationsFromGRPC(c.Spec.Annotations) 79 80 return swarm 81 } 82 83 // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec. 84 func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) { 85 return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{}) 86 } 87 88 // MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec 89 func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) { 90 // We take the initSpec (either created from scratch, or returned by swarmkit), 91 // and will only change the value if the one taken from types.Spec is not nil or 0. 92 // In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo. 93 if s.Annotations.Name != "" { 94 spec.Annotations.Name = s.Annotations.Name 95 } 96 if len(s.Annotations.Labels) != 0 { 97 spec.Annotations.Labels = s.Annotations.Labels 98 } 99 100 if s.Orchestration.TaskHistoryRetentionLimit != nil { 101 spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit 102 } 103 if s.Raft.SnapshotInterval != 0 { 104 spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval 105 } 106 if s.Raft.KeepOldSnapshots != nil { 107 spec.Raft.KeepOldSnapshots = *s.Raft.KeepOldSnapshots 108 } 109 if s.Raft.LogEntriesForSlowFollowers != 0 { 110 spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers 111 } 112 if s.Raft.HeartbeatTick != 0 { 113 spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick) 114 } 115 if s.Raft.ElectionTick != 0 { 116 spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick) 117 } 118 if s.Dispatcher.HeartbeatPeriod != 0 { 119 spec.Dispatcher.HeartbeatPeriod = gogotypes.DurationProto(s.Dispatcher.HeartbeatPeriod) 120 } 121 if s.CAConfig.NodeCertExpiry != 0 { 122 spec.CAConfig.NodeCertExpiry = gogotypes.DurationProto(s.CAConfig.NodeCertExpiry) 123 } 124 if s.CAConfig.SigningCACert != "" { 125 spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert) 126 } 127 if s.CAConfig.SigningCAKey != "" { 128 // do propagate the signing CA key here because we want to provide it TO the swarm APIs 129 spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey) 130 } 131 spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate 132 133 for _, ca := range s.CAConfig.ExternalCAs { 134 protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))] 135 if !ok { 136 return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol) 137 } 138 spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{ 139 Protocol: swarmapi.ExternalCA_CAProtocol(protocol), 140 URL: ca.URL, 141 Options: ca.Options, 142 CACert: []byte(ca.CACert), 143 }) 144 } 145 146 spec.EncryptionConfig.AutoLockManagers = s.EncryptionConfig.AutoLockManagers 147 148 return spec, nil 149 }