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