naming.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Package naming centralizes the naming of SwarmKit objects.
  2. package naming
  3. import (
  4. "errors"
  5. "fmt"
  6. "strings"
  7. "github.com/docker/swarmkit/api"
  8. )
  9. var (
  10. errUnknownRuntime = errors.New("unrecognized runtime")
  11. )
  12. // Task returns the task name from Annotations.Name,
  13. // and, in case Annotations.Name is missing, fallback
  14. // to construct the name from other information.
  15. func Task(t *api.Task) string {
  16. if t.Annotations.Name != "" {
  17. // if set, use the container Annotations.Name field, set in the orchestrator.
  18. return t.Annotations.Name
  19. }
  20. slot := fmt.Sprint(t.Slot)
  21. if slot == "" || t.Slot == 0 {
  22. // when no slot id is assigned, we assume that this is node-bound task.
  23. slot = t.NodeID
  24. }
  25. // fallback to service.instance.id.
  26. return fmt.Sprintf("%s.%s.%s", t.ServiceAnnotations.Name, slot, t.ID)
  27. }
  28. // TODO(stevvooe): Consolidate "Hostname" style validation here.
  29. // Runtime returns the runtime name from a given spec.
  30. func Runtime(t api.TaskSpec) (string, error) {
  31. switch r := t.GetRuntime().(type) {
  32. case *api.TaskSpec_Attachment:
  33. return "attachment", nil
  34. case *api.TaskSpec_Container:
  35. return "container", nil
  36. case *api.TaskSpec_Generic:
  37. return strings.ToLower(r.Generic.Kind), nil
  38. default:
  39. return "", errUnknownRuntime
  40. }
  41. }