...
1
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
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 }
79
View as plain text