github.com/lablabs/operator-sdk@v0.8.2/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  	"sigs.k8s.io/controller-runtime/pkg/controller"
    22  )
    23  
    24  // ControllerMap - map of GVK to ControllerMapContents
    25  type ControllerMap struct {
    26  	mutex    sync.RWMutex
    27  	internal map[schema.GroupVersionKind]*Contents
    28  }
    29  
    30  // WatchMap - map of GVK to interface. Determines if resource is being watched already
    31  type WatchMap struct {
    32  	mutex    sync.RWMutex
    33  	internal map[schema.GroupVersionKind]interface{}
    34  }
    35  
    36  // Contents - Contains internal data associated with each controller
    37  type Contents struct {
    38  	Controller                  controller.Controller
    39  	WatchDependentResources     bool
    40  	WatchClusterScopedResources bool
    41  	OwnerWatchMap               *WatchMap
    42  	AnnotationWatchMap          *WatchMap
    43  }
    44  
    45  // NewControllerMap returns a new object that contains a mapping between GVK
    46  // and ControllerMapContents object
    47  func NewControllerMap() *ControllerMap {
    48  	return &ControllerMap{
    49  		internal: make(map[schema.GroupVersionKind]*Contents),
    50  	}
    51  }
    52  
    53  // NewWatchMap - returns a new object that maps GVK to interface to determine
    54  // if resource is being watched
    55  func NewWatchMap() *WatchMap {
    56  	return &WatchMap{
    57  		internal: make(map[schema.GroupVersionKind]interface{}),
    58  	}
    59  }
    60  
    61  // Get - Returns a ControllerMapContents given a GVK as the key. `ok`
    62  // determines if the key exists
    63  func (cm *ControllerMap) Get(key schema.GroupVersionKind) (value *Contents, ok bool) {
    64  	cm.mutex.RLock()
    65  	defer cm.mutex.RUnlock()
    66  	value, ok = cm.internal[key]
    67  	return value, ok
    68  }
    69  
    70  // Delete - Deletes associated GVK to controller mapping from the ControllerMap
    71  func (cm *ControllerMap) Delete(key schema.GroupVersionKind) {
    72  	cm.mutex.Lock()
    73  	defer cm.mutex.Unlock()
    74  	delete(cm.internal, key)
    75  }
    76  
    77  // Store - Adds a new GVK to controller mapping
    78  func (cm *ControllerMap) Store(key schema.GroupVersionKind, value *Contents) {
    79  	cm.mutex.Lock()
    80  	defer cm.mutex.Unlock()
    81  	cm.internal[key] = value
    82  }
    83  
    84  // Get - Checks if GVK is already watched
    85  func (wm *WatchMap) Get(key schema.GroupVersionKind) (value interface{}, ok bool) {
    86  	wm.mutex.RLock()
    87  	defer wm.mutex.RUnlock()
    88  	value, ok = wm.internal[key]
    89  	return value, ok
    90  }
    91  
    92  // Delete - Deletes associated watches for a specific GVK
    93  func (wm *WatchMap) Delete(key schema.GroupVersionKind) {
    94  	wm.mutex.Lock()
    95  	defer wm.mutex.Unlock()
    96  	delete(wm.internal, key)
    97  }
    98  
    99  // Store - Adds a new GVK to be watched
   100  func (wm *WatchMap) Store(key schema.GroupVersionKind) {
   101  	wm.mutex.Lock()
   102  	defer wm.mutex.Unlock()
   103  	wm.internal[key] = nil
   104  }