swarm.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package convert
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. types "github.com/docker/docker/api/types/swarm"
  7. swarmapi "github.com/docker/swarmkit/api"
  8. gogotypes "github.com/gogo/protobuf/types"
  9. )
  10. // SwarmFromGRPC converts a grpc Cluster to a Swarm.
  11. func SwarmFromGRPC(c swarmapi.Cluster) types.Swarm {
  12. swarm := types.Swarm{
  13. ClusterInfo: types.ClusterInfo{
  14. ID: c.ID,
  15. Spec: types.Spec{
  16. Orchestration: types.OrchestrationConfig{
  17. TaskHistoryRetentionLimit: &c.Spec.Orchestration.TaskHistoryRetentionLimit,
  18. },
  19. Raft: types.RaftConfig{
  20. SnapshotInterval: c.Spec.Raft.SnapshotInterval,
  21. KeepOldSnapshots: &c.Spec.Raft.KeepOldSnapshots,
  22. LogEntriesForSlowFollowers: c.Spec.Raft.LogEntriesForSlowFollowers,
  23. HeartbeatTick: int(c.Spec.Raft.HeartbeatTick),
  24. ElectionTick: int(c.Spec.Raft.ElectionTick),
  25. },
  26. EncryptionConfig: types.EncryptionConfig{
  27. AutoLockManagers: c.Spec.EncryptionConfig.AutoLockManagers,
  28. },
  29. },
  30. },
  31. JoinTokens: types.JoinTokens{
  32. Worker: c.RootCA.JoinTokens.Worker,
  33. Manager: c.RootCA.JoinTokens.Manager,
  34. },
  35. }
  36. heartbeatPeriod, _ := gogotypes.DurationFromProto(c.Spec.Dispatcher.HeartbeatPeriod)
  37. swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod
  38. swarm.Spec.CAConfig.NodeCertExpiry, _ = gogotypes.DurationFromProto(c.Spec.CAConfig.NodeCertExpiry)
  39. for _, ca := range c.Spec.CAConfig.ExternalCAs {
  40. swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{
  41. Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())),
  42. URL: ca.URL,
  43. Options: ca.Options,
  44. })
  45. }
  46. // Meta
  47. swarm.Version.Index = c.Meta.Version.Index
  48. swarm.CreatedAt, _ = gogotypes.TimestampFromProto(c.Meta.CreatedAt)
  49. swarm.UpdatedAt, _ = gogotypes.TimestampFromProto(c.Meta.UpdatedAt)
  50. // Annotations
  51. swarm.Spec.Name = c.Spec.Annotations.Name
  52. swarm.Spec.Labels = c.Spec.Annotations.Labels
  53. return swarm
  54. }
  55. // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec.
  56. func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) {
  57. return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{})
  58. }
  59. // MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec
  60. func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) {
  61. // We take the initSpec (either created from scratch, or returned by swarmkit),
  62. // and will only change the value if the one taken from types.Spec is not nil or 0.
  63. // In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo.
  64. if s.Annotations.Name != "" {
  65. spec.Annotations.Name = s.Annotations.Name
  66. }
  67. if len(s.Annotations.Labels) != 0 {
  68. spec.Annotations.Labels = s.Annotations.Labels
  69. }
  70. if s.Orchestration.TaskHistoryRetentionLimit != nil {
  71. spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit
  72. }
  73. if s.Raft.SnapshotInterval != 0 {
  74. spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval
  75. }
  76. if s.Raft.KeepOldSnapshots != nil {
  77. spec.Raft.KeepOldSnapshots = *s.Raft.KeepOldSnapshots
  78. }
  79. if s.Raft.LogEntriesForSlowFollowers != 0 {
  80. spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers
  81. }
  82. if s.Raft.HeartbeatTick != 0 {
  83. spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick)
  84. }
  85. if s.Raft.ElectionTick != 0 {
  86. spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick)
  87. }
  88. if s.Dispatcher.HeartbeatPeriod != 0 {
  89. spec.Dispatcher.HeartbeatPeriod = gogotypes.DurationProto(time.Duration(s.Dispatcher.HeartbeatPeriod))
  90. }
  91. if s.CAConfig.NodeCertExpiry != 0 {
  92. spec.CAConfig.NodeCertExpiry = gogotypes.DurationProto(s.CAConfig.NodeCertExpiry)
  93. }
  94. for _, ca := range s.CAConfig.ExternalCAs {
  95. protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))]
  96. if !ok {
  97. return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol)
  98. }
  99. spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{
  100. Protocol: swarmapi.ExternalCA_CAProtocol(protocol),
  101. URL: ca.URL,
  102. Options: ca.Options,
  103. })
  104. }
  105. spec.EncryptionConfig.AutoLockManagers = s.EncryptionConfig.AutoLockManagers
  106. return spec, nil
  107. }