File: labels.go

package info (click to toggle)
docker.io 20.10.24%2Bdfsg1-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 60,824 kB
  • sloc: sh: 5,621; makefile: 593; ansic: 179; python: 162; asm: 7
file content (45 lines) | stat: -rw-r--r-- 1,292 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package labels

import (
	"fmt"
	"strings"
)

const (
	// ForServiceName is the label for the service name.
	ForServiceName = "com.docker.service.name"
	// ForStackName is the label for the stack name.
	ForStackName = "com.docker.stack.namespace"
	// ForServiceID is the label for the service id.
	ForServiceID = "com.docker.service.id"
)

// ForService gives the labels to select a given service in a stack.
func ForService(stackName, serviceName string) map[string]string {
	labels := map[string]string{}

	if serviceName != "" {
		labels[ForServiceName] = serviceName
	}
	if stackName != "" {
		labels[ForStackName] = stackName
	}
	if serviceName != "" && stackName != "" {
		labels[ForServiceID] = stackName + "-" + serviceName
	}

	return labels
}

// SelectorForStack gives the labelSelector to use for a given stack.
// Specific service names can be passed to narrow down the selection.
func SelectorForStack(stackName string, serviceNames ...string) string {
	switch len(serviceNames) {
	case 0:
		return fmt.Sprintf("%s=%s", ForStackName, stackName)
	case 1:
		return fmt.Sprintf("%s=%s,%s=%s", ForStackName, stackName, ForServiceName, serviceNames[0])
	default:
		return fmt.Sprintf("%s=%s,%s in (%s)", ForStackName, stackName, ForServiceName, strings.Join(serviceNames, ","))
	}
}