util.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package clusterexternalsecret
  13. import (
  14. v1 "k8s.io/api/core/v1"
  15. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  16. "github.com/external-secrets/external-secrets/pkg/controllers/clusterexternalsecret/cesmetrics"
  17. )
  18. func NewClusterExternalSecretCondition(failedNamespaces map[string]error) *esv1.ClusterExternalSecretStatusCondition {
  19. if len(failedNamespaces) == 0 {
  20. return &esv1.ClusterExternalSecretStatusCondition{
  21. Type: esv1.ClusterExternalSecretReady,
  22. Status: v1.ConditionTrue,
  23. }
  24. }
  25. condition := &esv1.ClusterExternalSecretStatusCondition{
  26. Type: esv1.ClusterExternalSecretReady,
  27. Status: v1.ConditionFalse,
  28. Message: errNamespacesFailed,
  29. }
  30. return condition
  31. }
  32. func SetClusterExternalSecretCondition(ces *esv1.ClusterExternalSecret, condition esv1.ClusterExternalSecretStatusCondition) {
  33. ces.Status.Conditions = append(filterOutCondition(ces.Status.Conditions, condition.Type), condition)
  34. cesmetrics.UpdateClusterExternalSecretCondition(ces, &condition)
  35. }
  36. // filterOutCondition returns an empty set of conditions with the provided type.
  37. func filterOutCondition(conditions []esv1.ClusterExternalSecretStatusCondition, condType esv1.ClusterExternalSecretConditionType) []esv1.ClusterExternalSecretStatusCondition {
  38. newConditions := make([]esv1.ClusterExternalSecretStatusCondition, 0, len(conditions))
  39. for _, c := range conditions {
  40. if c.Type == condType {
  41. continue
  42. }
  43. newConditions = append(newConditions, c)
  44. }
  45. return newConditions
  46. }