csi_drivers_store.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package csi
  14. import (
  15. "sync"
  16. utilversion "k8s.io/apimachinery/pkg/util/version"
  17. )
  18. // Driver is a description of a CSI Driver, defined by an enpoint and the
  19. // highest CSI version supported
  20. type Driver struct {
  21. endpoint string
  22. highestSupportedVersion *utilversion.Version
  23. }
  24. // DriversStore holds a list of CSI Drivers
  25. type DriversStore struct {
  26. store
  27. sync.RWMutex
  28. }
  29. type store map[string]Driver
  30. // Get lets you retrieve a CSI Driver by name.
  31. // This method is protected by a mutex.
  32. func (s *DriversStore) Get(driverName string) (Driver, bool) {
  33. s.RLock()
  34. defer s.RUnlock()
  35. driver, ok := s.store[driverName]
  36. return driver, ok
  37. }
  38. // Set lets you save a CSI Driver to the list and give it a specific name.
  39. // This method is protected by a mutex.
  40. func (s *DriversStore) Set(driverName string, driver Driver) {
  41. s.Lock()
  42. defer s.Unlock()
  43. if s.store == nil {
  44. s.store = store{}
  45. }
  46. s.store[driverName] = driver
  47. }
  48. // Delete lets you delete a CSI Driver by name.
  49. // This method is protected by a mutex.
  50. func (s *DriversStore) Delete(driverName string) {
  51. s.Lock()
  52. defer s.Unlock()
  53. delete(s.store, driverName)
  54. }
  55. // Clear deletes all entries in the store.
  56. // This methiod is protected by a mutex.
  57. func (s *DriversStore) Clear() {
  58. s.Lock()
  59. defer s.Unlock()
  60. s.store = store{}
  61. }