util.go 2.3 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 clusterpushsecret
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  17. "github.com/external-secrets/external-secrets/pkg/controllers/clusterpushsecret/cpsmetrics"
  18. )
  19. // NewClusterPushSecretCondition creates a new PushSecretStatusCondition 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 NewClusterPushSecretCondition(failedNamespaces map[string]error) *v1alpha1.PushSecretStatusCondition {
  23. if len(failedNamespaces) == 0 {
  24. return &v1alpha1.PushSecretStatusCondition{
  25. Type: v1alpha1.PushSecretReady,
  26. Status: v1.ConditionTrue,
  27. }
  28. }
  29. condition := &v1alpha1.PushSecretStatusCondition{
  30. Type: v1alpha1.PushSecretReady,
  31. Status: v1.ConditionFalse,
  32. Message: errNamespacesFailed,
  33. }
  34. return condition
  35. }
  36. // SetClusterPushSecretCondition updates the conditions on the ClusterPushSecret status
  37. // and updates the corresponding metrics.
  38. func SetClusterPushSecretCondition(ces *v1alpha1.ClusterPushSecret, condition v1alpha1.PushSecretStatusCondition) {
  39. ces.Status.Conditions = append(filterOutCondition(ces.Status.Conditions, condition.Type), condition)
  40. cpsmetrics.UpdateClusterPushSecretCondition(ces, &condition)
  41. }
  42. // filterOutCondition returns an empty set of conditions with the provided type.
  43. func filterOutCondition(conditions []v1alpha1.PushSecretStatusCondition, condType v1alpha1.PushSecretConditionType) []v1alpha1.PushSecretStatusCondition {
  44. newConditions := make([]v1alpha1.PushSecretStatusCondition, 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. }