util.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 storeutil provides utility functions for SecretStore operations
  14. package storeutil
  15. import (
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. )
  20. const (
  21. errSecretStoreNotReady = "%s %q is not ready"
  22. )
  23. // ShouldProcessStore returns true if the store should be processed.
  24. func ShouldProcessStore(store esv1.GenericStore, class string) bool {
  25. if store == nil || store.GetSpec().Controller == "" || store.GetSpec().Controller == class {
  26. return true
  27. }
  28. return false
  29. }
  30. // AssertStoreIsUsable asserts that the store is ready to use.
  31. func AssertStoreIsUsable(store esv1.GenericStore) error {
  32. if store == nil {
  33. return nil
  34. }
  35. condition := GetSecretStoreCondition(store.GetStatus(), esv1.SecretStoreReady)
  36. if condition == nil || condition.Status != v1.ConditionTrue {
  37. return fmt.Errorf(errSecretStoreNotReady, store.GetKind(), store.GetName())
  38. }
  39. return nil
  40. }
  41. // GetSecretStoreCondition returns the condition with the provided type.
  42. func GetSecretStoreCondition(status esv1.SecretStoreStatus, condType esv1.SecretStoreConditionType) *esv1.SecretStoreStatusCondition {
  43. for i := range status.Conditions {
  44. c := status.Conditions[i]
  45. if c.Type == condType {
  46. return &c
  47. }
  48. }
  49. return nil
  50. }