github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/runsc/specutils/cri.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package specutils
    16  
    17  import (
    18  	specs "github.com/opencontainers/runtime-spec/specs-go"
    19  )
    20  
    21  const (
    22  	// ContainerdContainerTypeAnnotation is the OCI annotation set by
    23  	// containerd to indicate whether the container to create should have
    24  	// its own sandbox or a container within an existing sandbox.
    25  	ContainerdContainerTypeAnnotation = "io.kubernetes.cri.container-type"
    26  	// ContainerdContainerTypeContainer is the container type value
    27  	// indicating the container should be created in an existing sandbox.
    28  	ContainerdContainerTypeContainer = "container"
    29  	// ContainerdContainerTypeSandbox is the container type value
    30  	// indicating the container should be created in a new sandbox.
    31  	ContainerdContainerTypeSandbox = "sandbox"
    32  
    33  	// ContainerdSandboxIDAnnotation is the OCI annotation set to indicate
    34  	// which sandbox the container should be created in when the container
    35  	// is not the first container in the sandbox.
    36  	ContainerdSandboxIDAnnotation = "io.kubernetes.cri.sandbox-id"
    37  
    38  	// CRIOContainerTypeAnnotation is the OCI annotation set by
    39  	// CRI-O to indicate whether the container to create should have
    40  	// its own sandbox or a container within an existing sandbox.
    41  	CRIOContainerTypeAnnotation = "io.kubernetes.cri-o.ContainerType"
    42  
    43  	// CRIOContainerTypeContainer is the container type value
    44  	// indicating the container should be created in an existing sandbox.
    45  	CRIOContainerTypeContainer = "container"
    46  	// CRIOContainerTypeSandbox is the container type value
    47  	// indicating the container should be created in a new sandbox.
    48  	CRIOContainerTypeSandbox = "sandbox"
    49  
    50  	// CRIOSandboxIDAnnotation is the OCI annotation set to indicate
    51  	// which sandbox the container should be created in when the container
    52  	// is not the first container in the sandbox.
    53  	CRIOSandboxIDAnnotation = "io.kubernetes.cri-o.SandboxID"
    54  )
    55  
    56  // ContainerType represents the type of container requested by the calling container manager.
    57  type ContainerType int
    58  
    59  const (
    60  	// ContainerTypeUnspecified indicates that no known container type
    61  	// annotation was found in the spec.
    62  	ContainerTypeUnspecified ContainerType = iota
    63  	// ContainerTypeUnknown indicates that a container type was specified
    64  	// but is unknown to us.
    65  	ContainerTypeUnknown
    66  	// ContainerTypeSandbox indicates that the container should be run in a
    67  	// new sandbox.
    68  	ContainerTypeSandbox
    69  	// ContainerTypeContainer indicates that the container should be run in
    70  	// an existing sandbox.
    71  	ContainerTypeContainer
    72  )
    73  
    74  // SpecContainerType tries to determine the type of container specified by the
    75  // container manager using well-known container annotations.
    76  func SpecContainerType(spec *specs.Spec) ContainerType {
    77  	if t, ok := spec.Annotations[ContainerdContainerTypeAnnotation]; ok {
    78  		switch t {
    79  		case ContainerdContainerTypeSandbox:
    80  			return ContainerTypeSandbox
    81  		case ContainerdContainerTypeContainer:
    82  			return ContainerTypeContainer
    83  		default:
    84  			return ContainerTypeUnknown
    85  		}
    86  	}
    87  	if t, ok := spec.Annotations[CRIOContainerTypeAnnotation]; ok {
    88  		switch t {
    89  		case CRIOContainerTypeSandbox:
    90  			return ContainerTypeSandbox
    91  		case CRIOContainerTypeContainer:
    92  			return ContainerTypeContainer
    93  		default:
    94  			return ContainerTypeUnknown
    95  		}
    96  	}
    97  	return ContainerTypeUnspecified
    98  }
    99  
   100  // SandboxID returns the ID of the sandbox to join and whether an ID was found
   101  // in the spec.
   102  func SandboxID(spec *specs.Spec) (string, bool) {
   103  	if id, ok := spec.Annotations[ContainerdSandboxIDAnnotation]; ok {
   104  		return id, true
   105  	}
   106  	if id, ok := spec.Annotations[CRIOSandboxIDAnnotation]; ok {
   107  		return id, true
   108  	}
   109  	return "", false
   110  }