swarm.go 5.1 KB

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