k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/state/namespaces_state.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 state 18 19 import ( 20 "fmt" 21 "sync" 22 23 "k8s.io/perf-tests/clusterloader2/api" 24 ) 25 26 // InstancesState represents state of object replicas. 27 type InstancesState struct { 28 DesiredReplicaCount int32 29 CurrentReplicaCount int32 30 Object *api.Object 31 } 32 33 // InstancesIdentifier is a unique identifier for object replicas group 34 type InstancesIdentifier struct { 35 Basename string 36 ObjectKind string 37 APIGroup string 38 } 39 40 // namespaceState represents state of a single namespace. 41 type namespaceState map[InstancesIdentifier]*InstancesState 42 43 // NamespacesState represents state of all used namespaces. 44 type NamespacesState struct { 45 lock sync.RWMutex 46 namespaceStates map[string]namespaceState 47 } 48 49 // newNamespacesState creates new namespaces state. 50 func newNamespacesState() *NamespacesState { 51 return &NamespacesState{ 52 namespaceStates: make(map[string]namespaceState), 53 } 54 } 55 56 // Get returns state of object instances - 57 // number of existing replicas and its configuration. 58 func (ns *NamespacesState) Get(namespace string, identifier InstancesIdentifier) (*InstancesState, bool) { 59 ns.lock.RLock() 60 defer ns.lock.RUnlock() 61 namespaceState, exists := ns.namespaceStates[namespace] 62 if !exists { 63 return nil, false 64 } 65 instances, exists := namespaceState[identifier] 66 return instances, exists 67 } 68 69 // Set stores information about object instances state 70 // to test state. 71 func (ns *NamespacesState) Set(namespace string, identifier InstancesIdentifier, instances *InstancesState) { 72 ns.lock.Lock() 73 defer ns.lock.Unlock() 74 _, exists := ns.namespaceStates[namespace] 75 if !exists { 76 ns.namespaceStates[namespace] = make(namespaceState) 77 } 78 ns.namespaceStates[namespace][identifier] = instances 79 } 80 81 // Delete removes information about given instances. 82 // It there is no information for given object it is assumed that 83 // there are no object replicas. 84 func (ns *NamespacesState) Delete(namespace string, identifier InstancesIdentifier) error { 85 ns.lock.Lock() 86 defer ns.lock.Unlock() 87 namespaceState, exists := ns.namespaceStates[namespace] 88 if !exists { 89 return fmt.Errorf("namespace %v not found", namespace) 90 } 91 _, exists = namespaceState[identifier] 92 if !exists { 93 return fmt.Errorf("no instances of %+v found in %v namespace", identifier, namespace) 94 } 95 delete(namespaceState, identifier) 96 return nil 97 }