opts.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package swarm
  2. import (
  3. "encoding/csv"
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/docker/docker/api/types/swarm"
  9. "github.com/docker/docker/opts"
  10. "github.com/spf13/pflag"
  11. )
  12. const (
  13. defaultListenAddr = "0.0.0.0:2377"
  14. flagCertExpiry = "cert-expiry"
  15. flagDispatcherHeartbeat = "dispatcher-heartbeat"
  16. flagListenAddr = "listen-addr"
  17. flagAdvertiseAddr = "advertise-addr"
  18. flagQuiet = "quiet"
  19. flagRotate = "rotate"
  20. flagToken = "token"
  21. flagTaskHistoryLimit = "task-history-limit"
  22. flagExternalCA = "external-ca"
  23. flagMaxSnapshots = "max-snapshots"
  24. flagSnapshotInterval = "snapshot-interval"
  25. flagLockKey = "lock-key"
  26. flagAutolock = "autolock"
  27. )
  28. type swarmOptions struct {
  29. taskHistoryLimit int64
  30. dispatcherHeartbeat time.Duration
  31. nodeCertExpiry time.Duration
  32. externalCA ExternalCAOption
  33. maxSnapshots uint64
  34. snapshotInterval uint64
  35. autolock bool
  36. }
  37. // NodeAddrOption is a pflag.Value for listening addresses
  38. type NodeAddrOption struct {
  39. addr string
  40. }
  41. // String prints the representation of this flag
  42. func (a *NodeAddrOption) String() string {
  43. return a.Value()
  44. }
  45. // Set the value for this flag
  46. func (a *NodeAddrOption) Set(value string) error {
  47. addr, err := opts.ParseTCPAddr(value, a.addr)
  48. if err != nil {
  49. return err
  50. }
  51. a.addr = addr
  52. return nil
  53. }
  54. // Type returns the type of this flag
  55. func (a *NodeAddrOption) Type() string {
  56. return "node-addr"
  57. }
  58. // Value returns the value of this option as addr:port
  59. func (a *NodeAddrOption) Value() string {
  60. return strings.TrimPrefix(a.addr, "tcp://")
  61. }
  62. // NewNodeAddrOption returns a new node address option
  63. func NewNodeAddrOption(addr string) NodeAddrOption {
  64. return NodeAddrOption{addr}
  65. }
  66. // NewListenAddrOption returns a NodeAddrOption with default values
  67. func NewListenAddrOption() NodeAddrOption {
  68. return NewNodeAddrOption(defaultListenAddr)
  69. }
  70. // ExternalCAOption is a Value type for parsing external CA specifications.
  71. type ExternalCAOption struct {
  72. values []*swarm.ExternalCA
  73. }
  74. // Set parses an external CA option.
  75. func (m *ExternalCAOption) Set(value string) error {
  76. parsed, err := parseExternalCA(value)
  77. if err != nil {
  78. return err
  79. }
  80. m.values = append(m.values, parsed)
  81. return nil
  82. }
  83. // Type returns the type of this option.
  84. func (m *ExternalCAOption) Type() string {
  85. return "external-ca"
  86. }
  87. // String returns a string repr of this option.
  88. func (m *ExternalCAOption) String() string {
  89. externalCAs := []string{}
  90. for _, externalCA := range m.values {
  91. repr := fmt.Sprintf("%s: %s", externalCA.Protocol, externalCA.URL)
  92. externalCAs = append(externalCAs, repr)
  93. }
  94. return strings.Join(externalCAs, ", ")
  95. }
  96. // Value returns the external CAs
  97. func (m *ExternalCAOption) Value() []*swarm.ExternalCA {
  98. return m.values
  99. }
  100. // parseExternalCA parses an external CA specification from the command line,
  101. // such as protocol=cfssl,url=https://example.com.
  102. func parseExternalCA(caSpec string) (*swarm.ExternalCA, error) {
  103. csvReader := csv.NewReader(strings.NewReader(caSpec))
  104. fields, err := csvReader.Read()
  105. if err != nil {
  106. return nil, err
  107. }
  108. externalCA := swarm.ExternalCA{
  109. Options: make(map[string]string),
  110. }
  111. var (
  112. hasProtocol bool
  113. hasURL bool
  114. )
  115. for _, field := range fields {
  116. parts := strings.SplitN(field, "=", 2)
  117. if len(parts) != 2 {
  118. return nil, fmt.Errorf("invalid field '%s' must be a key=value pair", field)
  119. }
  120. key, value := parts[0], parts[1]
  121. switch strings.ToLower(key) {
  122. case "protocol":
  123. hasProtocol = true
  124. if strings.ToLower(value) == string(swarm.ExternalCAProtocolCFSSL) {
  125. externalCA.Protocol = swarm.ExternalCAProtocolCFSSL
  126. } else {
  127. return nil, fmt.Errorf("unrecognized external CA protocol %s", value)
  128. }
  129. case "url":
  130. hasURL = true
  131. externalCA.URL = value
  132. default:
  133. externalCA.Options[key] = value
  134. }
  135. }
  136. if !hasProtocol {
  137. return nil, errors.New("the external-ca option needs a protocol= parameter")
  138. }
  139. if !hasURL {
  140. return nil, errors.New("the external-ca option needs a url= parameter")
  141. }
  142. return &externalCA, nil
  143. }
  144. func addSwarmFlags(flags *pflag.FlagSet, opts *swarmOptions) {
  145. flags.Int64Var(&opts.taskHistoryLimit, flagTaskHistoryLimit, 5, "Task history retention limit")
  146. flags.DurationVar(&opts.dispatcherHeartbeat, flagDispatcherHeartbeat, time.Duration(5*time.Second), "Dispatcher heartbeat period (ns|us|ms|s|m|h)")
  147. flags.DurationVar(&opts.nodeCertExpiry, flagCertExpiry, time.Duration(90*24*time.Hour), "Validity period for node certificates (ns|us|ms|s|m|h)")
  148. flags.Var(&opts.externalCA, flagExternalCA, "Specifications of one or more certificate signing endpoints")
  149. flags.Uint64Var(&opts.maxSnapshots, flagMaxSnapshots, 0, "Number of additional Raft snapshots to retain")
  150. flags.Uint64Var(&opts.snapshotInterval, flagSnapshotInterval, 10000, "Number of log entries between Raft snapshots")
  151. }
  152. func (opts *swarmOptions) mergeSwarmSpec(spec *swarm.Spec, flags *pflag.FlagSet) {
  153. if flags.Changed(flagTaskHistoryLimit) {
  154. spec.Orchestration.TaskHistoryRetentionLimit = &opts.taskHistoryLimit
  155. }
  156. if flags.Changed(flagDispatcherHeartbeat) {
  157. spec.Dispatcher.HeartbeatPeriod = opts.dispatcherHeartbeat
  158. }
  159. if flags.Changed(flagCertExpiry) {
  160. spec.CAConfig.NodeCertExpiry = opts.nodeCertExpiry
  161. }
  162. if flags.Changed(flagExternalCA) {
  163. spec.CAConfig.ExternalCAs = opts.externalCA.Value()
  164. }
  165. if flags.Changed(flagMaxSnapshots) {
  166. spec.Raft.KeepOldSnapshots = &opts.maxSnapshots
  167. }
  168. if flags.Changed(flagSnapshotInterval) {
  169. spec.Raft.SnapshotInterval = opts.snapshotInterval
  170. }
  171. if flags.Changed(flagAutolock) {
  172. spec.EncryptionConfig.AutoLockManagers = opts.autolock
  173. }
  174. }
  175. func (opts *swarmOptions) ToSpec(flags *pflag.FlagSet) swarm.Spec {
  176. var spec swarm.Spec
  177. opts.mergeSwarmSpec(&spec, flags)
  178. return spec
  179. }