provider_schema_maintenance.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright © The ESO 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. https://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 v1
  14. import (
  15. "fmt"
  16. "sync"
  17. )
  18. // MaintenanceStatus defines a type for different maintenance states of a provider schema.
  19. type MaintenanceStatus string
  20. // These are the defined maintenance states for a provider schema.
  21. const (
  22. MaintenanceStatusMaintained MaintenanceStatus = "Maintained"
  23. MaintenanceStatusNotMaintained MaintenanceStatus = "NotMaintained"
  24. MaintenanceStatusDeprecated MaintenanceStatus = "Deprecated"
  25. )
  26. var maintenance map[string]MaintenanceStatus
  27. var mlock sync.RWMutex
  28. func init() {
  29. maintenance = make(map[string]MaintenanceStatus)
  30. }
  31. // RegisterMaintenanceStatus registers the maintenance status of the provider from the generic store.
  32. // It panics if the provider is already registered or if there is an error getting the provider name.
  33. func RegisterMaintenanceStatus(status MaintenanceStatus, storeSpec *SecretStoreProvider) {
  34. storeName, err := getProviderName(storeSpec)
  35. if err != nil {
  36. panic(fmt.Sprintf("store error registering schema: %s", err.Error()))
  37. }
  38. mlock.Lock()
  39. defer mlock.Unlock()
  40. _, exists := maintenance[storeName]
  41. if exists {
  42. panic(fmt.Sprintf("store %q already registered", storeName))
  43. }
  44. maintenance[storeName] = status
  45. }
  46. // ForceRegisterMaintenanceStatus registers the maintenance status of the provider from the generic store.
  47. // It panics if there is an error getting the provider name, it overwrites existing provider status or
  48. // stores new status for a provider if it exists.
  49. func ForceRegisterMaintenanceStatus(status MaintenanceStatus, storeSpec *SecretStoreProvider) {
  50. storeName, err := getProviderName(storeSpec)
  51. if err != nil {
  52. panic(fmt.Sprintf("store error registering schema: %s", err.Error()))
  53. }
  54. mlock.Lock()
  55. defer mlock.Unlock()
  56. maintenance[storeName] = status
  57. }
  58. // GetMaintenanceStatus returns the maintenance status of the provider from the generic store.
  59. func GetMaintenanceStatus(s GenericStore) (MaintenanceStatus, error) {
  60. if s == nil {
  61. return MaintenanceStatusNotMaintained, nil
  62. }
  63. spec := s.GetSpec()
  64. if spec == nil {
  65. // Note, this condition can never be reached, because
  66. // the Spec is not a pointer in Kubernetes. It will
  67. // always exist.
  68. return MaintenanceStatusNotMaintained, fmt.Errorf("no spec found in %#v", s)
  69. }
  70. storeName, err := getProviderName(spec.Provider)
  71. if err != nil {
  72. return MaintenanceStatusNotMaintained, fmt.Errorf("store error for %s: %w", s.GetName(), err)
  73. }
  74. mlock.RLock()
  75. status, ok := maintenance[storeName]
  76. mlock.RUnlock()
  77. if !ok {
  78. return MaintenanceStatusNotMaintained, fmt.Errorf("failed to find registered store backend for type: %s, name: %s", storeName, s.GetName())
  79. }
  80. return status, nil
  81. }