k8s.io/kubernetes@v1.29.3/pkg/kubelet/kuberuntime/convert.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package kuberuntime 18 19 import ( 20 "sort" 21 22 utilfeature "k8s.io/apiserver/pkg/util/feature" 23 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" 24 "k8s.io/kubernetes/pkg/features" 25 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 26 ) 27 28 // This file contains help function to kuberuntime types to CRI runtime API types, or vice versa. 29 30 func toKubeContainerImageSpec(image *runtimeapi.Image) kubecontainer.ImageSpec { 31 var annotations []kubecontainer.Annotation 32 33 if image.Spec != nil && len(image.Spec.Annotations) > 0 { 34 annotationKeys := make([]string, 0, len(image.Spec.Annotations)) 35 for k := range image.Spec.Annotations { 36 annotationKeys = append(annotationKeys, k) 37 } 38 sort.Strings(annotationKeys) 39 for _, k := range annotationKeys { 40 annotations = append(annotations, kubecontainer.Annotation{ 41 Name: k, 42 Value: image.Spec.Annotations[k], 43 }) 44 } 45 } 46 47 spec := kubecontainer.ImageSpec{ 48 Image: image.Id, 49 Annotations: annotations, 50 } 51 // if RuntimeClassInImageCriAPI feature gate is enabled, set runtimeHandler CRI field 52 if utilfeature.DefaultFeatureGate.Enabled(features.RuntimeClassInImageCriAPI) { 53 runtimeHandler := "" 54 if image.Spec != nil { 55 runtimeHandler = image.Spec.RuntimeHandler 56 } 57 spec.RuntimeHandler = runtimeHandler 58 } 59 60 return spec 61 } 62 63 func toRuntimeAPIImageSpec(imageSpec kubecontainer.ImageSpec) *runtimeapi.ImageSpec { 64 var annotations = make(map[string]string) 65 if imageSpec.Annotations != nil { 66 for _, a := range imageSpec.Annotations { 67 annotations[a.Name] = a.Value 68 } 69 } 70 71 spec := runtimeapi.ImageSpec{ 72 Image: imageSpec.Image, 73 Annotations: annotations, 74 } 75 // if RuntimeClassInImageCriAPI feature gate is enabled, set runtimeHandler CRI field 76 if utilfeature.DefaultFeatureGate.Enabled(features.RuntimeClassInImageCriAPI) { 77 spec.RuntimeHandler = imageSpec.RuntimeHandler 78 } 79 80 return &spec 81 }