k8s.io/kubernetes@v1.29.3/pkg/kubelet/status/state/state_mem.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 state 18 19 import ( 20 "sync" 21 22 "k8s.io/api/core/v1" 23 "k8s.io/klog/v2" 24 ) 25 26 type stateMemory struct { 27 sync.RWMutex 28 podAllocation PodResourceAllocation 29 podResizeStatus PodResizeStatus 30 } 31 32 var _ State = &stateMemory{} 33 34 // NewStateMemory creates new State to track resources allocated to pods 35 func NewStateMemory() State { 36 klog.V(2).InfoS("Initialized new in-memory state store for pod resource allocation tracking") 37 return &stateMemory{ 38 podAllocation: PodResourceAllocation{}, 39 podResizeStatus: PodResizeStatus{}, 40 } 41 } 42 43 func (s *stateMemory) GetContainerResourceAllocation(podUID string, containerName string) (v1.ResourceList, bool) { 44 s.RLock() 45 defer s.RUnlock() 46 47 alloc, ok := s.podAllocation[podUID][containerName] 48 return alloc.DeepCopy(), ok 49 } 50 51 func (s *stateMemory) GetPodResourceAllocation() PodResourceAllocation { 52 s.RLock() 53 defer s.RUnlock() 54 return s.podAllocation.Clone() 55 } 56 57 func (s *stateMemory) GetPodResizeStatus(podUID string) (v1.PodResizeStatus, bool) { 58 s.RLock() 59 defer s.RUnlock() 60 61 resizeStatus, ok := s.podResizeStatus[podUID] 62 return resizeStatus, ok 63 } 64 65 func (s *stateMemory) GetResizeStatus() PodResizeStatus { 66 s.RLock() 67 defer s.RUnlock() 68 prs := make(map[string]v1.PodResizeStatus) 69 for k, v := range s.podResizeStatus { 70 prs[k] = v 71 } 72 return prs 73 } 74 75 func (s *stateMemory) SetContainerResourceAllocation(podUID string, containerName string, alloc v1.ResourceList) error { 76 s.Lock() 77 defer s.Unlock() 78 79 if _, ok := s.podAllocation[podUID]; !ok { 80 s.podAllocation[podUID] = make(map[string]v1.ResourceList) 81 } 82 83 s.podAllocation[podUID][containerName] = alloc 84 klog.V(3).InfoS("Updated container resource allocation", "podUID", podUID, "containerName", containerName, "alloc", alloc) 85 return nil 86 } 87 88 func (s *stateMemory) SetPodResourceAllocation(a PodResourceAllocation) error { 89 s.Lock() 90 defer s.Unlock() 91 92 s.podAllocation = a.Clone() 93 klog.V(3).InfoS("Updated pod resource allocation", "allocation", a) 94 return nil 95 } 96 97 func (s *stateMemory) SetPodResizeStatus(podUID string, resizeStatus v1.PodResizeStatus) error { 98 s.Lock() 99 defer s.Unlock() 100 101 if resizeStatus != "" { 102 s.podResizeStatus[podUID] = resizeStatus 103 } else { 104 delete(s.podResizeStatus, podUID) 105 } 106 klog.V(3).InfoS("Updated pod resize state", "podUID", podUID, "resizeStatus", resizeStatus) 107 return nil 108 } 109 110 func (s *stateMemory) SetResizeStatus(rs PodResizeStatus) error { 111 s.Lock() 112 defer s.Unlock() 113 prs := make(map[string]v1.PodResizeStatus) 114 for k, v := range rs { 115 prs[k] = v 116 } 117 s.podResizeStatus = prs 118 klog.V(3).InfoS("Updated pod resize state", "resizes", rs) 119 return nil 120 } 121 122 func (s *stateMemory) deleteContainer(podUID string, containerName string) { 123 delete(s.podAllocation[podUID], containerName) 124 if len(s.podAllocation[podUID]) == 0 { 125 delete(s.podAllocation, podUID) 126 delete(s.podResizeStatus, podUID) 127 } 128 klog.V(3).InfoS("Deleted pod resource allocation", "podUID", podUID, "containerName", containerName) 129 } 130 131 func (s *stateMemory) Delete(podUID string, containerName string) error { 132 s.Lock() 133 defer s.Unlock() 134 if len(containerName) == 0 { 135 delete(s.podAllocation, podUID) 136 delete(s.podResizeStatus, podUID) 137 klog.V(3).InfoS("Deleted pod resource allocation and resize state", "podUID", podUID) 138 return nil 139 } 140 s.deleteContainer(podUID, containerName) 141 return nil 142 } 143 144 func (s *stateMemory) ClearState() error { 145 s.Lock() 146 defer s.Unlock() 147 148 s.podAllocation = make(PodResourceAllocation) 149 s.podResizeStatus = make(PodResizeStatus) 150 klog.V(3).InfoS("Cleared state") 151 return nil 152 }