opts.go 5.5 KB

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