swarm.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package convert
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. types "github.com/docker/engine-api/types/swarm"
  7. swarmapi "github.com/docker/swarmkit/api"
  8. "github.com/docker/swarmkit/protobuf/ptypes"
  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: c.Spec.Raft.HeartbeatTick,
  24. ElectionTick: c.Spec.Raft.ElectionTick,
  25. },
  26. },
  27. },
  28. JoinTokens: types.JoinTokens{
  29. Worker: c.RootCA.JoinTokens.Worker,
  30. Manager: c.RootCA.JoinTokens.Manager,
  31. },
  32. }
  33. heartbeatPeriod, _ := ptypes.Duration(c.Spec.Dispatcher.HeartbeatPeriod)
  34. swarm.Spec.Dispatcher.HeartbeatPeriod = uint64(heartbeatPeriod)
  35. swarm.Spec.CAConfig.NodeCertExpiry, _ = ptypes.Duration(c.Spec.CAConfig.NodeCertExpiry)
  36. for _, ca := range c.Spec.CAConfig.ExternalCAs {
  37. swarm.Spec.CAConfig.ExternalCAs = append(swarm.Spec.CAConfig.ExternalCAs, &types.ExternalCA{
  38. Protocol: types.ExternalCAProtocol(strings.ToLower(ca.Protocol.String())),
  39. URL: ca.URL,
  40. Options: ca.Options,
  41. })
  42. }
  43. // Meta
  44. swarm.Version.Index = c.Meta.Version.Index
  45. swarm.CreatedAt, _ = ptypes.Timestamp(c.Meta.CreatedAt)
  46. swarm.UpdatedAt, _ = ptypes.Timestamp(c.Meta.UpdatedAt)
  47. // Annotations
  48. swarm.Spec.Name = c.Spec.Annotations.Name
  49. swarm.Spec.Labels = c.Spec.Annotations.Labels
  50. return swarm
  51. }
  52. // SwarmSpecToGRPC converts a Spec to a grpc ClusterSpec.
  53. func SwarmSpecToGRPC(s types.Spec) (swarmapi.ClusterSpec, error) {
  54. spec := swarmapi.ClusterSpec{
  55. Annotations: swarmapi.Annotations{
  56. Name: s.Name,
  57. Labels: s.Labels,
  58. },
  59. Orchestration: swarmapi.OrchestrationConfig{
  60. TaskHistoryRetentionLimit: s.Orchestration.TaskHistoryRetentionLimit,
  61. },
  62. Raft: swarmapi.RaftConfig{
  63. SnapshotInterval: s.Raft.SnapshotInterval,
  64. KeepOldSnapshots: s.Raft.KeepOldSnapshots,
  65. LogEntriesForSlowFollowers: s.Raft.LogEntriesForSlowFollowers,
  66. HeartbeatTick: s.Raft.HeartbeatTick,
  67. ElectionTick: s.Raft.ElectionTick,
  68. },
  69. Dispatcher: swarmapi.DispatcherConfig{
  70. HeartbeatPeriod: ptypes.DurationProto(time.Duration(s.Dispatcher.HeartbeatPeriod)),
  71. },
  72. CAConfig: swarmapi.CAConfig{
  73. NodeCertExpiry: ptypes.DurationProto(s.CAConfig.NodeCertExpiry),
  74. },
  75. }
  76. for _, ca := range s.CAConfig.ExternalCAs {
  77. protocol, ok := swarmapi.ExternalCA_CAProtocol_value[strings.ToUpper(string(ca.Protocol))]
  78. if !ok {
  79. return swarmapi.ClusterSpec{}, fmt.Errorf("invalid protocol: %q", ca.Protocol)
  80. }
  81. spec.CAConfig.ExternalCAs = append(spec.CAConfig.ExternalCAs, &swarmapi.ExternalCA{
  82. Protocol: swarmapi.ExternalCA_CAProtocol(protocol),
  83. URL: ca.URL,
  84. Options: ca.Options,
  85. })
  86. }
  87. return spec, nil
  88. }