...

Source file src/github.com/Microsoft/hcsshim/internal/oci/sandbox.go

Documentation: github.com/Microsoft/hcsshim/internal/oci

     1  package oci
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Microsoft/hcsshim/pkg/annotations"
     7  )
     8  
     9  // KubernetesContainerType defines the valid types of the
    10  // `annotations.KubernetesContainerType` annotation.
    11  type KubernetesContainerType string
    12  
    13  const (
    14  	// KubernetesContainerTypeNone is only valid when
    15  	// `annotations.KubernetesContainerType` is not set.
    16  	KubernetesContainerTypeNone KubernetesContainerType = ""
    17  	// KubernetesContainerTypeContainer is valid when
    18  	// `annotations.KubernetesContainerType == "container"`.
    19  	KubernetesContainerTypeContainer KubernetesContainerType = "container"
    20  	// KubernetesContainerTypeSandbox is valid when
    21  	// `annotations.KubernetesContainerType == "sandbox"`.
    22  	KubernetesContainerTypeSandbox KubernetesContainerType = "sandbox"
    23  )
    24  
    25  // GetSandboxTypeAndID parses `specAnnotations` searching for the
    26  // `KubernetesContainerTypeAnnotation` and `KubernetesSandboxIDAnnotation`
    27  // annotations and if found validates the set before returning.
    28  func GetSandboxTypeAndID(specAnnotations map[string]string) (KubernetesContainerType, string, error) {
    29  	var ct KubernetesContainerType
    30  	if t, ok := specAnnotations[annotations.KubernetesContainerType]; ok {
    31  		switch t {
    32  		case string(KubernetesContainerTypeContainer):
    33  			ct = KubernetesContainerTypeContainer
    34  		case string(KubernetesContainerTypeSandbox):
    35  			ct = KubernetesContainerTypeSandbox
    36  		default:
    37  			return KubernetesContainerTypeNone, "", fmt.Errorf("invalid '%s': '%s'", annotations.KubernetesContainerType, t)
    38  		}
    39  	}
    40  
    41  	id := specAnnotations[annotations.KubernetesSandboxID]
    42  
    43  	switch ct {
    44  	case KubernetesContainerTypeContainer, KubernetesContainerTypeSandbox:
    45  		if id == "" {
    46  			return KubernetesContainerTypeNone, "", fmt.Errorf("cannot specify '%s' without '%s'", annotations.KubernetesContainerType, annotations.KubernetesSandboxID)
    47  		}
    48  	default:
    49  		if id != "" {
    50  			return KubernetesContainerTypeNone, "", fmt.Errorf("cannot specify '%s' without '%s'", annotations.KubernetesSandboxID, annotations.KubernetesContainerType)
    51  		}
    52  	}
    53  	return ct, id, nil
    54  }
    55  
    56  // SandboxAnnotationsPassThrough passes through the annotations specified by 'vals' from the sandboxes set of
    57  // annotations through to every container in the pod. Kubernetes only passes metadata annotations to the
    58  // RunPodSandbox request, so annotations that are meant to be available for use/checking for individual
    59  // containers need some way to know they were passed for the pod.
    60  func SandboxAnnotationsPassThrough(podAnnots, containerAnnots map[string]string, vals ...string) {
    61  	if podAnnots == nil || containerAnnots == nil {
    62  		return
    63  	}
    64  	for _, val := range vals {
    65  		if v, ok := podAnnots[val]; ok {
    66  			containerAnnots[val] = v
    67  		}
    68  	}
    69  }
    70  

View as plain text