1 /* 2 Copyright 2019 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 csi 18 19 import ( 20 "sync" 21 22 utilversion "k8s.io/apimachinery/pkg/util/version" 23 ) 24 25 // Driver is a description of a CSI Driver, defined by an endpoint and the 26 // highest CSI version supported 27 type Driver struct { 28 endpoint string 29 highestSupportedVersion *utilversion.Version 30 } 31 32 // DriversStore holds a list of CSI Drivers 33 type DriversStore struct { 34 store 35 sync.RWMutex 36 } 37 38 type store map[string]Driver 39 40 // Get lets you retrieve a CSI Driver by name. 41 // This method is protected by a mutex. 42 func (s *DriversStore) Get(driverName string) (Driver, bool) { 43 s.RLock() 44 defer s.RUnlock() 45 46 driver, ok := s.store[driverName] 47 return driver, ok 48 } 49 50 // Set lets you save a CSI Driver to the list and give it a specific name. 51 // This method is protected by a mutex. 52 func (s *DriversStore) Set(driverName string, driver Driver) { 53 s.Lock() 54 defer s.Unlock() 55 56 if s.store == nil { 57 s.store = store{} 58 } 59 60 s.store[driverName] = driver 61 } 62 63 // Delete lets you delete a CSI Driver by name. 64 // This method is protected by a mutex. 65 func (s *DriversStore) Delete(driverName string) { 66 s.Lock() 67 defer s.Unlock() 68 69 delete(s.store, driverName) 70 } 71 72 // Clear deletes all entries in the store. 73 // This methiod is protected by a mutex. 74 func (s *DriversStore) Clear() { 75 s.Lock() 76 defer s.Unlock() 77 78 s.store = store{} 79 } 80