k8s.io/kubernetes@v1.29.3/pkg/kubelet/cm/fake_pod_container_manager.go (about) 1 /* 2 Copyright 2021 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 "reflect" 21 "sync" 22 23 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/types" 25 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 26 ) 27 28 type FakePodContainerManager struct { 29 sync.Mutex 30 CalledFunctions []string 31 Cgroups map[types.UID]CgroupName 32 } 33 34 var _ PodContainerManager = &FakePodContainerManager{} 35 36 func NewFakePodContainerManager() *FakePodContainerManager { 37 return &FakePodContainerManager{ 38 Cgroups: make(map[types.UID]CgroupName), 39 } 40 } 41 42 func (m *FakePodContainerManager) AddPodFromCgroups(pod *kubecontainer.Pod) { 43 m.Lock() 44 defer m.Unlock() 45 m.Cgroups[pod.ID] = []string{pod.Name} 46 } 47 48 func (m *FakePodContainerManager) Exists(_ *v1.Pod) bool { 49 m.Lock() 50 defer m.Unlock() 51 m.CalledFunctions = append(m.CalledFunctions, "Exists") 52 return true 53 } 54 55 func (m *FakePodContainerManager) EnsureExists(_ *v1.Pod) error { 56 m.Lock() 57 defer m.Unlock() 58 m.CalledFunctions = append(m.CalledFunctions, "EnsureExists") 59 return nil 60 } 61 62 func (m *FakePodContainerManager) GetPodContainerName(_ *v1.Pod) (CgroupName, string) { 63 m.Lock() 64 defer m.Unlock() 65 m.CalledFunctions = append(m.CalledFunctions, "GetPodContainerName") 66 return nil, "" 67 } 68 69 func (m *FakePodContainerManager) Destroy(name CgroupName) error { 70 m.Lock() 71 defer m.Unlock() 72 m.CalledFunctions = append(m.CalledFunctions, "Destroy") 73 for key, cgname := range m.Cgroups { 74 if reflect.DeepEqual(cgname, name) { 75 delete(m.Cgroups, key) 76 return nil 77 } 78 } 79 return nil 80 } 81 82 func (m *FakePodContainerManager) ReduceCPULimits(_ CgroupName) error { 83 m.Lock() 84 defer m.Unlock() 85 m.CalledFunctions = append(m.CalledFunctions, "ReduceCPULimits") 86 return nil 87 } 88 89 func (m *FakePodContainerManager) GetAllPodsFromCgroups() (map[types.UID]CgroupName, error) { 90 m.Lock() 91 defer m.Unlock() 92 m.CalledFunctions = append(m.CalledFunctions, "GetAllPodsFromCgroups") 93 // return a copy for the race detector 94 grp := make(map[types.UID]CgroupName) 95 for key, value := range m.Cgroups { 96 grp[key] = value 97 } 98 return grp, nil 99 } 100 101 func (m *FakePodContainerManager) IsPodCgroup(cgroupfs string) (bool, types.UID) { 102 m.Lock() 103 defer m.Unlock() 104 m.CalledFunctions = append(m.CalledFunctions, "IsPodCgroup") 105 return false, types.UID("") 106 } 107 108 func (cm *FakePodContainerManager) GetPodCgroupMemoryUsage(_ *v1.Pod) (uint64, error) { 109 cm.Lock() 110 defer cm.Unlock() 111 cm.CalledFunctions = append(cm.CalledFunctions, "GetPodCgroupMemoryUsage") 112 return 0, nil 113 } 114 115 func (cm *FakePodContainerManager) GetPodCgroupConfig(_ *v1.Pod, _ v1.ResourceName) (*ResourceConfig, error) { 116 cm.Lock() 117 defer cm.Unlock() 118 cm.CalledFunctions = append(cm.CalledFunctions, "GetPodCgroupConfig") 119 return nil, nil 120 } 121 122 func (cm *FakePodContainerManager) SetPodCgroupConfig(_ *v1.Pod, _ v1.ResourceName, _ *ResourceConfig) error { 123 cm.Lock() 124 defer cm.Unlock() 125 cm.CalledFunctions = append(cm.CalledFunctions, "SetPodCgroupConfig") 126 return nil 127 }