opts.go 5.6 KB

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