swarm.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package convert // import "github.com/docker/docker/daemon/cluster/convert"
  2. import (
  3. "fmt"
  4. "strings"
  5. types "github.com/docker/docker/api/types/swarm"
  6. gogotypes "github.com/gogo/protobuf/types"
  7. swarmapi "github.com/moby/swarmkit/v2/api"
  8. "github.com/moby/swarmkit/v2/ca"
  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. CAConfig: types.CAConfig{
  30. // do not include the signing CA cert or key (it should already be redacted via the swarm APIs) -
  31. // the key because it's secret, and the cert because otherwise doing a get + update on the spec
  32. // can cause issues because the key would be missing and the cert wouldn't
  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. DefaultAddrPool: c.DefaultAddressPool,
  41. SubnetSize: c.SubnetSize,
  42. DataPathPort: c.VXLANUDPPort,
  43. },
  44. JoinTokens: types.JoinTokens{
  45. Worker: c.RootCA.JoinTokens.Worker,
  46. Manager: c.RootCA.JoinTokens.Manager,
  47. },
  48. }
  49. issuerInfo, err := ca.IssuerFromAPIRootCA(&c.RootCA)
  50. if err == nil && issuerInfo != nil {
  51. swarm.TLSInfo.CertIssuerSubject = issuerInfo.Subject
  52. swarm.TLSInfo.CertIssuerPublicKey = issuerInfo.PublicKey
  53. }
  54. heartbeatPeriod, _ := gogotypes.DurationFromProto(c.Spec.Dispatcher.HeartbeatPeriod)
  55. swarm.Spec.Dispatcher.HeartbeatPeriod = heartbeatPeriod
  56. swarm.Spec.CAConfig.NodeCertExpiry, _ = gogotypes.DurationFromProto(c.Spec.CAConfig.NodeCertExpiry)
  57. for _, ca := range c.Spec.CAConfig.ExternalCAs {
  58. swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{
  59. Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())),
  60. URL: ca.URL,
  61. Options: ca.Options,
  62. CACert: string(ca.CACert),
  63. })
  64. }
  65. // Meta
  66. swarm.Version.Index = c.Meta.Version.Index
  67. swarm.CreatedAt, _ = gogotypes.TimestampFromProto(c.Meta.CreatedAt)
  68. swarm.UpdatedAt, _ = gogotypes.TimestampFromProto(c.Meta.UpdatedAt)
  69. // Annotations
  70. swarm.Spec.Annotations = annotationsFromGRPC(c.Spec.Annotations)
  71. return swarm
  72. }
  73. // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec.
  74. func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) {
  75. return MergeSwarmSpecToGRPC(s, swarmapi.ClusterSpec{})
  76. }
  77. // MergeSwarmSpecToGRPC merges a Spec with an initial grpc ClusterSpec
  78. func MergeSwarmSpecToGRPC(s types.Spec, spec swarmapi.ClusterSpec) (swarmapi.ClusterSpec, error) {
  79. // We take the initSpec (either created from scratch, or returned by swarmkit),
  80. // and will only change the value if the one taken from types.Spec is not nil or 0.
  81. // In other words, if the value taken from types.Spec is nil or 0, we will maintain the status quo.
  82. if s.Annotations.Name != "" {
  83. spec.Annotations.Name = s.Annotations.Name
  84. }
  85. if len(s.Annotations.Labels) != 0 {
  86. spec.Annotations.Labels = s.Annotations.Labels
  87. }
  88. if s.Orchestration.TaskHistoryRetentionLimit != nil {
  89. spec.Orchestration.TaskHistoryRetentionLimit = *s.Orchestration.TaskHistoryRetentionLimit
  90. }
  91. if s.Raft.SnapshotInterval != 0 {
  92. spec.Raft.SnapshotInterval = s.Raft.SnapshotInterval
  93. }
  94. if s.Raft.KeepOldSnapshots != nil {
  95. spec.Raft.KeepOldSnapshots = *s.Raft.KeepOldSnapshots
  96. }
  97. if s.Raft.LogEntriesForSlowFollowers != 0 {
  98. spec.Raft.LogEntriesForSlowFollowers = s.Raft.LogEntriesForSlowFollowers
  99. }
  100. if s.Raft.HeartbeatTick != 0 {
  101. spec.Raft.HeartbeatTick = uint32(s.Raft.HeartbeatTick)
  102. }
  103. if s.Raft.ElectionTick != 0 {
  104. spec.Raft.ElectionTick = uint32(s.Raft.ElectionTick)
  105. }
  106. if s.Dispatcher.HeartbeatPeriod != 0 {
  107. spec.Dispatcher.HeartbeatPeriod = gogotypes.DurationProto(s.Dispatcher.HeartbeatPeriod)
  108. }
  109. if s.CAConfig.NodeCertExpiry != 0 {
  110. spec.CAConfig.NodeCertExpiry = gogotypes.DurationProto(s.CAConfig.NodeCertExpiry)
  111. }
  112. if s.CAConfig.SigningCACert != "" {
  113. spec.CAConfig.SigningCACert = []byte(s.CAConfig.SigningCACert)
  114. }
  115. if s.CAConfig.SigningCAKey != "" {
  116. // do propagate the signing CA key here because we want to provide it TO the swarm APIs
  117. spec.CAConfig.SigningCAKey = []byte(s.CAConfig.SigningCAKey)
  118. }
  119. spec.CAConfig.ForceRotate = s.CAConfig.ForceRotate
  120. for _, ca := range s.CAConfig.ExternalCAs {
  121. protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))]
  122. if !ok {
  123. return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol)
  124. }
  125. spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{
  126. Protocol: swarmapi.ExternalCA_CAProtocol(protocol),
  127. URL: ca.URL,
  128. Options: ca.Options,
  129. CACert: []byte(ca.CACert),
  130. })
  131. }
  132. spec.EncryptionConfig.AutoLockManagers = s.EncryptionConfig.AutoLockManagers
  133. return spec, nil
  134. }