k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/helpers.go (about) 1 /* 2 Copyright 2018 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 cm 18 19 import ( 20 "context" 21 22 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/util/sets" 24 internalapi "k8s.io/cri-api/pkg/apis" 25 runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" 26 "k8s.io/klog/v2" 27 "k8s.io/kubernetes/pkg/kubelet/cm/containermap" 28 evictionapi "k8s.io/kubernetes/pkg/kubelet/eviction/api" 29 ) 30 31 // hardEvictionReservation returns a resourcelist that includes reservation of resources based on hard eviction thresholds. 32 func hardEvictionReservation(thresholds []evictionapi.Threshold, capacity v1.ResourceList) v1.ResourceList { 33 if len(thresholds) == 0 { 34 return nil 35 } 36 ret := v1.ResourceList{} 37 for _, threshold := range thresholds { 38 if threshold.Operator != evictionapi.OpLessThan { 39 continue 40 } 41 switch threshold.Signal { 42 case evictionapi.SignalMemoryAvailable: 43 memoryCapacity := capacity[v1.ResourceMemory] 44 value := evictionapi.GetThresholdQuantity(threshold.Value, &memoryCapacity) 45 ret[v1.ResourceMemory] = *value 46 case evictionapi.SignalNodeFsAvailable: 47 storageCapacity := capacity[v1.ResourceEphemeralStorage] 48 value := evictionapi.GetThresholdQuantity(threshold.Value, &storageCapacity) 49 ret[v1.ResourceEphemeralStorage] = *value 50 } 51 } 52 return ret 53 } 54 55 func buildContainerMapAndRunningSetFromRuntime(ctx context.Context, runtimeService internalapi.RuntimeService) (containermap.ContainerMap, sets.Set[string]) { 56 podSandboxMap := make(map[string]string) 57 podSandboxList, _ := runtimeService.ListPodSandbox(ctx, nil) 58 for _, p := range podSandboxList { 59 podSandboxMap[p.Id] = p.Metadata.Uid 60 } 61 62 runningSet := sets.New[string]() 63 containerMap := containermap.NewContainerMap() 64 containerList, _ := runtimeService.ListContainers(ctx, nil) 65 for _, c := range containerList { 66 if _, exists := podSandboxMap[c.PodSandboxId]; !exists { 67 klog.InfoS("No PodSandBox found for the container", "podSandboxId", c.PodSandboxId, "containerName", c.Metadata.Name, "containerId", c.Id) 68 continue 69 } 70 podUID := podSandboxMap[c.PodSandboxId] 71 containerMap.Add(podUID, c.Metadata.Name, c.Id) 72 if c.State == runtimeapi.ContainerState_CONTAINER_RUNNING { 73 klog.V(4).InfoS("Container reported running", "podSandboxId", c.PodSandboxId, "podUID", podUID, "containerName", c.Metadata.Name, "containerId", c.Id) 74 runningSet.Insert(c.Id) 75 } 76 } 77 return containerMap, runningSet 78 }