github.com/docker/compose-on-kubernetes@v0.5.0/api/labels/labels.go (about)

     1  package labels
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  const (
     9  	// ForServiceName is the label for the service name.
    10  	ForServiceName = "com.docker.service.name"
    11  	// ForStackName is the label for the stack name.
    12  	ForStackName = "com.docker.stack.namespace"
    13  	// ForServiceID is the label for the service id.
    14  	ForServiceID = "com.docker.service.id"
    15  )
    16  
    17  // ForService gives the labels to select a given service in a stack.
    18  func ForService(stackName, serviceName string) map[string]string {
    19  	labels := map[string]string{}
    20  
    21  	if serviceName != "" {
    22  		labels[ForServiceName] = serviceName
    23  	}
    24  	if stackName != "" {
    25  		labels[ForStackName] = stackName
    26  	}
    27  	if serviceName != "" && stackName != "" {
    28  		labels[ForServiceID] = stackName + "-" + serviceName
    29  	}
    30  
    31  	return labels
    32  }
    33  
    34  // SelectorForStack gives the labelSelector to use for a given stack.
    35  // Specific service names can be passed to narrow down the selection.
    36  func SelectorForStack(stackName string, serviceNames ...string) string {
    37  	switch len(serviceNames) {
    38  	case 0:
    39  		return fmt.Sprintf("%s=%s", ForStackName, stackName)
    40  	case 1:
    41  		return fmt.Sprintf("%s=%s,%s=%s", ForStackName, stackName, ForServiceName, serviceNames[0])
    42  	default:
    43  		return fmt.Sprintf("%s=%s,%s in (%s)", ForStackName, stackName, ForServiceName, strings.Join(serviceNames, ","))
    44  	}
    45  }