provider_schema_maintenance.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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. type MaintenanceStatus bool
  19. const (
  20. MaintenanceStatusMaintained MaintenanceStatus = true
  21. MaintenanceStatusNotMaintained MaintenanceStatus = false
  22. )
  23. var maintenance map[string]MaintenanceStatus
  24. var mlock sync.RWMutex
  25. func init() {
  26. maintenance = make(map[string]MaintenanceStatus)
  27. }
  28. func RegisterMaintenanceStatus(status MaintenanceStatus, storeSpec *SecretStoreProvider) {
  29. storeName, err := getProviderName(storeSpec)
  30. if err != nil {
  31. panic(fmt.Sprintf("store error registering schema: %s", err.Error()))
  32. }
  33. mlock.Lock()
  34. defer mlock.Unlock()
  35. _, exists := maintenance[storeName]
  36. if exists {
  37. panic(fmt.Sprintf("store %q already registered", storeName))
  38. }
  39. maintenance[storeName] = status
  40. }
  41. func ForceRegisterMaintenanceStatus(status MaintenanceStatus, storeSpec *SecretStoreProvider) {
  42. storeName, err := getProviderName(storeSpec)
  43. if err != nil {
  44. panic(fmt.Sprintf("store error registering schema: %s", err.Error()))
  45. }
  46. mlock.Lock()
  47. defer mlock.Unlock()
  48. maintenance[storeName] = status
  49. }
  50. // GetMaintenanceStatus returns the maintenance status of the provider from the generic store.
  51. func GetMaintenanceStatus(s GenericStore) (MaintenanceStatus, error) {
  52. if s == nil {
  53. return MaintenanceStatusNotMaintained, nil
  54. }
  55. spec := s.GetSpec()
  56. if spec == nil {
  57. // Note, this condition can never be reached, because
  58. // the Spec is not a pointer in Kubernetes. It will
  59. // always exist.
  60. return MaintenanceStatusNotMaintained, fmt.Errorf("no spec found in %#v", s)
  61. }
  62. storeName, err := getProviderName(spec.Provider)
  63. if err != nil {
  64. return MaintenanceStatusNotMaintained, fmt.Errorf("store error for %s: %w", s.GetName(), err)
  65. }
  66. mlock.RLock()
  67. status, ok := maintenance[storeName]
  68. mlock.RUnlock()
  69. if !ok {
  70. return MaintenanceStatusNotMaintained, fmt.Errorf("failed to find registered store backend for type: %s, name: %s", storeName, s.GetName())
  71. }
  72. return status, nil
  73. }