github.com/olljanat/moby@v1.13.1/daemon/cluster/convert/swarm.go (about) 1 package convert 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 types "github.com/docker/docker/api/types/swarm" 9 swarmapi "github.com/docker/swarmkit/api" 10 "github.com/docker/swarmkit/protobuf/ptypes" 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 }, 33 }, 34 JoinTokens: types.JoinTokens{ 35 Worker: c.RootCA.JoinTokens.Worker, 36 Manager: c.RootCA.JoinTokens.Manager, 37 }, 38 } 39 40 heartbeatPeriod, _ := ptypes.Duration(c.Spec.Dispatcher.HeartbeatPeriod) 41 swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod 42 43 swarm.Spec.CAConfig.NodeCertExpiry, _ = ptypes.Duration(c.Spec.CAConfig.NodeCertExpiry) 44 45 for _, ca := range c.Spec.CAConfig.ExternalCAs { 46 swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{ 47 Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())), 48 URL: ca.URL, 49 Options: ca.Options, 50 }) 51 } 52 53 // Meta 54 swarm.Version.Index = c.Meta.Version.Index 55 swarm.CreatedAt, _ = ptypes.Timestamp(c.Meta.CreatedAt) 56 swarm.UpdatedAt, _ = ptypes.Timestamp(c.Meta.UpdatedAt) 57 58 // Annotations 59 swarm.Spec.Name = c.Spec.Annotations.Name 60 swarm.Spec.Labels = c.Spec.Annotations.Labels 61 62 return swarm 63 } 64 65 // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec. 66 func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) { 67 return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{}) 68 } 69 70 // MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec 71 func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) { 72 // We take the initSpec (either created from scratch, or returned by swarmkit), 73 // and will only change the value if the one taken from types.Spec is not nil or 0. 74 // In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo. 75 if s.Annotations.Name != "" { 76 spec.Annotations.Name = s.Annotations.Name 77 } 78 if len(s.Annotations.Labels) != 0 { 79 spec.Annotations.Labels = s.Annotations.Labels 80 } 81 82 if s.Orchestration.TaskHistoryRetentionLimit != nil { 83 spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit 84 } 85 if s.Raft.SnapshotInterval != 0 { 86 spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval 87 } 88 if s.Raft.KeepOldSnapshots != nil { 89 spec.Raft.KeepOldSnapshots = *s.Raft.KeepOldSnapshots 90 } 91 if s.Raft.LogEntriesForSlowFollowers != 0 { 92 spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers 93 } 94 if s.Raft.HeartbeatTick != 0 { 95 spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick) 96 } 97 if s.Raft.ElectionTick != 0 { 98 spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick) 99 } 100 if s.Dispatcher.HeartbeatPeriod != 0 { 101 spec.Dispatcher.HeartbeatPeriod = ptypes.DurationProto(time.Duration(s.Dispatcher.HeartbeatPeriod)) 102 } 103 if s.CAConfig.NodeCertExpiry != 0 { 104 spec.CAConfig.NodeCertExpiry = ptypes.DurationProto(s.CAConfig.NodeCertExpiry) 105 } 106 107 for _, ca := range s.CAConfig.ExternalCAs { 108 protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))] 109 if !ok { 110 return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol) 111 } 112 spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{ 113 Protocol: swarmapi.ExternalCA_CAProtocol(protocol), 114 URL: ca.URL, 115 Options: ca.Options, 116 }) 117 } 118 119 spec.EncryptionConfig.AutoLockManagers = s.EncryptionConfig.AutoLockManagers 120 121 return spec, nil 122 }