github.com/jmrodri/operator-sdk@v0.5.0/pkg/ansible/proxy/controllermap/controllermap.go (about) 1 // Copyright 2018 The Operator-SDK Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package controllermap 16 17 import ( 18 "sync" 19 20 "k8s.io/apimachinery/pkg/runtime/schema" 21 "k8s.io/apimachinery/pkg/types" 22 "sigs.k8s.io/controller-runtime/pkg/controller" 23 ) 24 25 // ControllerMap - map of GVK to ControllerMapContents 26 type ControllerMap struct { 27 mutex sync.RWMutex 28 internal map[schema.GroupVersionKind]*ControllerMapContents 29 } 30 31 // UIDMap - map of UID to namespaced name of owner 32 type UIDMap struct { 33 mutex sync.RWMutex 34 internal map[types.UID]types.NamespacedName 35 } 36 37 // WatchMap - map of GVK to interface. Determines if resource is being watched already 38 type WatchMap struct { 39 mutex sync.RWMutex 40 internal map[schema.GroupVersionKind]interface{} 41 } 42 43 // ControllerMapContents- Contains internal data associated with each controller 44 type ControllerMapContents struct { 45 Controller controller.Controller 46 WatchDependentResources bool 47 WatchClusterScopedResources bool 48 WatchMap *WatchMap 49 UIDMap *UIDMap 50 } 51 52 // NewControllerMap returns a new object that contains a mapping between GVK 53 // and ControllerMapContents object 54 func NewControllerMap() *ControllerMap { 55 return &ControllerMap{ 56 internal: make(map[schema.GroupVersionKind]*ControllerMapContents), 57 } 58 } 59 60 // NewWatchMap - returns a new object that maps GVK to interface to determine 61 // if resource is being watched 62 func NewWatchMap() *WatchMap { 63 return &WatchMap{ 64 internal: make(map[schema.GroupVersionKind]interface{}), 65 } 66 } 67 68 // NewUIDMap - returns a new object that maps UID to namespaced name of owner 69 func NewUIDMap() *UIDMap { 70 return &UIDMap{ 71 internal: make(map[types.UID]types.NamespacedName), 72 } 73 } 74 75 // Get - Returns a ControllerMapContents given a GVK as the key. `ok` 76 // determines if the key exists 77 func (cm *ControllerMap) Get(key schema.GroupVersionKind) (value *ControllerMapContents, ok bool) { 78 cm.mutex.RLock() 79 defer cm.mutex.RUnlock() 80 value, ok = cm.internal[key] 81 return value, ok 82 } 83 84 // Delete - Deletes associated GVK to controller mapping from the ControllerMap 85 func (cm *ControllerMap) Delete(key schema.GroupVersionKind) { 86 cm.mutex.Lock() 87 defer cm.mutex.Unlock() 88 delete(cm.internal, key) 89 } 90 91 // Store - Adds a new GVK to controller mapping 92 func (cm *ControllerMap) Store(key schema.GroupVersionKind, value *ControllerMapContents) { 93 cm.mutex.Lock() 94 defer cm.mutex.Unlock() 95 cm.internal[key] = value 96 } 97 98 // Get - Checks if GVK is already watched 99 func (wm *WatchMap) Get(key schema.GroupVersionKind) (value interface{}, ok bool) { 100 wm.mutex.RLock() 101 defer wm.mutex.RUnlock() 102 value, ok = wm.internal[key] 103 return value, ok 104 } 105 106 // Delete - Deletes associated watches for a specific GVK 107 func (wm *WatchMap) Delete(key schema.GroupVersionKind) { 108 wm.mutex.Lock() 109 defer wm.mutex.Unlock() 110 delete(wm.internal, key) 111 } 112 113 // Store - Adds a new GVK to be watched 114 func (wm *WatchMap) Store(key schema.GroupVersionKind) { 115 wm.mutex.Lock() 116 defer wm.mutex.Unlock() 117 wm.internal[key] = nil 118 } 119 120 // Get - Returns a NamespacedName of the owner given a UID 121 func (um *UIDMap) Get(key types.UID) (value types.NamespacedName, ok bool) { 122 um.mutex.RLock() 123 defer um.mutex.RUnlock() 124 value, ok = um.internal[key] 125 return value, ok 126 } 127 128 // Delete - Deletes associated UID to NamespacedName mapping 129 func (um *UIDMap) Delete(key types.UID) { 130 um.mutex.Lock() 131 defer um.mutex.Unlock() 132 delete(um.internal, key) 133 } 134 135 // Store - Adds a new UID to NamespacedName mapping 136 func (um *UIDMap) Store(key types.UID, value types.NamespacedName) { 137 um.mutex.Lock() 138 defer um.mutex.Unlock() 139 um.internal[key] = value 140 }