provider_schema_maintenance.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package v1
  13. import (
  14. "fmt"
  15. "sync"
  16. )
  17. type MaintenanceStatus bool
  18. const (
  19. MaintenanceStatusMaintained MaintenanceStatus = true
  20. MaintenanceStatusNotMaintained MaintenanceStatus = false
  21. )
  22. var maintenance map[string]MaintenanceStatus
  23. var mlock sync.RWMutex
  24. func init() {
  25. maintenance = make(map[string]MaintenanceStatus)
  26. }
  27. func RegisterMaintenanceStatus(status MaintenanceStatus, storeSpec *SecretStoreProvider) {
  28. storeName, err := getProviderName(storeSpec)
  29. if err != nil {
  30. panic(fmt.Sprintf("store error registering schema: %s", err.Error()))
  31. }
  32. mlock.Lock()
  33. defer mlock.Unlock()
  34. _, exists := maintenance[storeName]
  35. if exists {
  36. panic(fmt.Sprintf("store %q already registered", storeName))
  37. }
  38. maintenance[storeName] = status
  39. }
  40. func ForceRegisterMaintenanceStatus(status MaintenanceStatus, storeSpec *SecretStoreProvider) {
  41. storeName, err := getProviderName(storeSpec)
  42. if err != nil {
  43. panic(fmt.Sprintf("store error registering schema: %s", err.Error()))
  44. }
  45. mlock.Lock()
  46. defer mlock.Unlock()
  47. maintenance[storeName] = status
  48. }
  49. // GetMaintenanceStatus returns the maintenance status of the provider from the generic store.
  50. func GetMaintenanceStatus(s GenericStore) (MaintenanceStatus, error) {
  51. if s == nil {
  52. return MaintenanceStatusNotMaintained, nil
  53. }
  54. spec := s.GetSpec()
  55. if spec == nil {
  56. // Note, this condition can never be reached, because
  57. // the Spec is not a pointer in Kubernetes. It will
  58. // always exist.
  59. return MaintenanceStatusNotMaintained, fmt.Errorf("no spec found in %#v", s)
  60. }
  61. storeName, err := getProviderName(spec.Provider)
  62. if err != nil {
  63. return MaintenanceStatusNotMaintained, fmt.Errorf("store error for %s: %w", s.GetName(), err)
  64. }
  65. mlock.RLock()
  66. status, ok := maintenance[storeName]
  67. mlock.RUnlock()
  68. if !ok {
  69. return MaintenanceStatusNotMaintained, fmt.Errorf("failed to find registered store backend for type: %s, name: %s", storeName, s.GetName())
  70. }
  71. return status, nil
  72. }