util.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 clusterexternalsecret
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  17. "github.com/external-secrets/external-secrets/pkg/controllers/clusterexternalsecret/cesmetrics"
  18. )
  19. // NewClusterExternalSecretCondition creates a new ClusterExternalSecret condition based on failed namespaces.
  20. // If there are no failed namespaces, it returns a Ready condition with True status.
  21. // Otherwise, it returns a Ready condition with False status and an error message.
  22. func NewClusterExternalSecretCondition(failedNamespaces map[string]error) *esv1.ClusterExternalSecretStatusCondition {
  23. if len(failedNamespaces) == 0 {
  24. return &esv1.ClusterExternalSecretStatusCondition{
  25. Type: esv1.ClusterExternalSecretReady,
  26. Status: v1.ConditionTrue,
  27. }
  28. }
  29. condition := &esv1.ClusterExternalSecretStatusCondition{
  30. Type: esv1.ClusterExternalSecretReady,
  31. Status: v1.ConditionFalse,
  32. Message: errNamespacesFailed,
  33. }
  34. return condition
  35. }
  36. // SetClusterExternalSecretCondition updates the conditions on the ClusterExternalSecret status
  37. // and updates the corresponding metrics.
  38. func SetClusterExternalSecretCondition(ces *esv1.ClusterExternalSecret, condition esv1.ClusterExternalSecretStatusCondition) {
  39. ces.Status.Conditions = append(filterOutCondition(ces.Status.Conditions, condition.Type), condition)
  40. cesmetrics.UpdateClusterExternalSecretCondition(ces, &condition)
  41. }
  42. // filterOutCondition returns an empty set of conditions with the provided type.
  43. func filterOutCondition(conditions []esv1.ClusterExternalSecretStatusCondition, condType esv1.ClusterExternalSecretConditionType) []esv1.ClusterExternalSecretStatusCondition {
  44. newConditions := make([]esv1.ClusterExternalSecretStatusCondition, 0, len(conditions))
  45. for _, c := range conditions {
  46. if c.Type == condType {
  47. continue
  48. }
  49. newConditions = append(newConditions, c)
  50. }
  51. return newConditions
  52. }