k8s.io/kubernetes@v1.29.3/pkg/kubelet/pod/testing/fake_mirror_client.go (about) 1 /* 2 Copyright 2015 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 testing 18 19 import ( 20 "sync" 21 22 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/types" 24 "k8s.io/apimachinery/pkg/util/sets" 25 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 26 ) 27 28 type FakeMirrorClient struct { 29 mirrorPodLock sync.RWMutex 30 // Note that a real mirror manager does not store the mirror pods in 31 // itself. This fake manager does this to track calls. 32 mirrorPods sets.String 33 createCounts map[string]int 34 deleteCounts map[string]int 35 } 36 37 func NewFakeMirrorClient() *FakeMirrorClient { 38 m := FakeMirrorClient{} 39 m.mirrorPods = sets.NewString() 40 m.createCounts = make(map[string]int) 41 m.deleteCounts = make(map[string]int) 42 return &m 43 } 44 45 func (fmc *FakeMirrorClient) CreateMirrorPod(pod *v1.Pod) error { 46 fmc.mirrorPodLock.Lock() 47 defer fmc.mirrorPodLock.Unlock() 48 podFullName := kubecontainer.GetPodFullName(pod) 49 fmc.mirrorPods.Insert(podFullName) 50 fmc.createCounts[podFullName]++ 51 return nil 52 } 53 54 // TODO (Robert Krawitz): Implement UID checking 55 func (fmc *FakeMirrorClient) DeleteMirrorPod(podFullName string, _ *types.UID) (bool, error) { 56 fmc.mirrorPodLock.Lock() 57 defer fmc.mirrorPodLock.Unlock() 58 fmc.mirrorPods.Delete(podFullName) 59 fmc.deleteCounts[podFullName]++ 60 return true, nil 61 } 62 63 func (fmc *FakeMirrorClient) HasPod(podFullName string) bool { 64 fmc.mirrorPodLock.RLock() 65 defer fmc.mirrorPodLock.RUnlock() 66 return fmc.mirrorPods.Has(podFullName) 67 } 68 69 func (fmc *FakeMirrorClient) NumOfPods() int { 70 fmc.mirrorPodLock.RLock() 71 defer fmc.mirrorPodLock.RUnlock() 72 return fmc.mirrorPods.Len() 73 } 74 75 func (fmc *FakeMirrorClient) GetPods() []string { 76 fmc.mirrorPodLock.RLock() 77 defer fmc.mirrorPodLock.RUnlock() 78 return fmc.mirrorPods.List() 79 } 80 81 func (fmc *FakeMirrorClient) GetCounts(podFullName string) (int, int) { 82 fmc.mirrorPodLock.RLock() 83 defer fmc.mirrorPodLock.RUnlock() 84 return fmc.createCounts[podFullName], fmc.deleteCounts[podFullName] 85 }