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