opts.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. availability string
  13. }
  14. type annotations struct {
  15. name string
  16. labels opts.ListOpts
  17. }
  18. func newNodeOptions() *nodeOptions {
  19. return &nodeOptions{
  20. annotations: annotations{
  21. labels: opts.NewListOpts(nil),
  22. },
  23. }
  24. }
  25. func (opts *nodeOptions) ToNodeSpec() (swarm.NodeSpec, error) {
  26. var spec swarm.NodeSpec
  27. spec.Annotations.Name = opts.annotations.name
  28. spec.Annotations.Labels = runconfigopts.ConvertKVStringsToMap(opts.annotations.labels.GetAll())
  29. switch swarm.NodeRole(strings.ToLower(opts.role)) {
  30. case swarm.NodeRoleWorker:
  31. spec.Role = swarm.NodeRoleWorker
  32. case swarm.NodeRoleManager:
  33. spec.Role = swarm.NodeRoleManager
  34. case "":
  35. default:
  36. return swarm.NodeSpec{}, fmt.Errorf("invalid role %q, only worker and manager are supported", opts.role)
  37. }
  38. switch swarm.NodeAvailability(strings.ToLower(opts.availability)) {
  39. case swarm.NodeAvailabilityActive:
  40. spec.Availability = swarm.NodeAvailabilityActive
  41. case swarm.NodeAvailabilityPause:
  42. spec.Availability = swarm.NodeAvailabilityPause
  43. case swarm.NodeAvailabilityDrain:
  44. spec.Availability = swarm.NodeAvailabilityDrain
  45. case "":
  46. default:
  47. return swarm.NodeSpec{}, fmt.Errorf("invalid availability %q, only active, pause and drain are supported", opts.availability)
  48. }
  49. return spec, nil
  50. }