opts.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package node
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/docker/opts"
  6. runconfigopts "github.com/docker/docker/runconfig/opts"
  7. "github.com/docker/engine-api/types/swarm"
  8. )
  9. type nodeOptions struct {
  10. annotations
  11. role string
  12. membership string
  13. availability string
  14. }
  15. type annotations struct {
  16. name string
  17. labels opts.ListOpts
  18. }
  19. func newNodeOptions() *nodeOptions {
  20. return &nodeOptions{
  21. annotations: annotations{
  22. labels: opts.NewListOpts(nil),
  23. },
  24. }
  25. }
  26. func (opts *nodeOptions) ToNodeSpec() (swarm.NodeSpec, error) {
  27. var spec swarm.NodeSpec
  28. spec.Annotations.Name = opts.annotations.name
  29. spec.Annotations.Labels = runconfigopts.ConvertKVStringsToMap(opts.annotations.labels.GetAll())
  30. switch swarm.NodeRole(strings.ToLower(opts.role)) {
  31. case swarm.NodeRoleWorker:
  32. spec.Role = swarm.NodeRoleWorker
  33. case swarm.NodeRoleManager:
  34. spec.Role = swarm.NodeRoleManager
  35. case "":
  36. default:
  37. return swarm.NodeSpec{}, fmt.Errorf("invalid role %q, only worker and manager are supported", opts.role)
  38. }
  39. switch swarm.NodeMembership(strings.ToLower(opts.membership)) {
  40. case swarm.NodeMembershipAccepted:
  41. spec.Membership = swarm.NodeMembershipAccepted
  42. case "":
  43. default:
  44. return swarm.NodeSpec{}, fmt.Errorf("invalid membership %q, only accepted is supported", opts.membership)
  45. }
  46. switch swarm.NodeAvailability(strings.ToLower(opts.availability)) {
  47. case swarm.NodeAvailabilityActive:
  48. spec.Availability = swarm.NodeAvailabilityActive
  49. case swarm.NodeAvailabilityPause:
  50. spec.Availability = swarm.NodeAvailabilityPause
  51. case swarm.NodeAvailabilityDrain:
  52. spec.Availability = swarm.NodeAvailabilityDrain
  53. case "":
  54. default:
  55. return swarm.NodeSpec{}, fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
  56. }
  57. return spec, nil
  58. }