util.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  16. "github.com/external-secrets/external-secrets/pkg/controllers/clusterexternalsecret/cesmetrics"
  17. )
  18. func NewClusterExternalSecretCondition(failedNamespaces map[string]string, namespaceList *v1.NamespaceList) *esv1beta1.ClusterExternalSecretStatusCondition {
  19. conditionType := getConditionType(failedNamespaces, namespaceList)
  20. condition := &esv1beta1.ClusterExternalSecretStatusCondition{
  21. Type: conditionType,
  22. Status: v1.ConditionTrue,
  23. }
  24. if conditionType != esv1beta1.ClusterExternalSecretReady {
  25. condition.Message = errNamespacesFailed
  26. }
  27. return condition
  28. }
  29. // GetClusterExternalSecretCondition returns the condition with the provided type.
  30. func GetClusterExternalSecretCondition(status esv1beta1.ClusterExternalSecretStatus, condType esv1beta1.ClusterExternalSecretConditionType) *esv1beta1.ClusterExternalSecretStatusCondition {
  31. for i := range status.Conditions {
  32. c := status.Conditions[i]
  33. if c.Type == condType {
  34. return &c
  35. }
  36. }
  37. return nil
  38. }
  39. func SetClusterExternalSecretCondition(ces *esv1beta1.ClusterExternalSecret, condition esv1beta1.ClusterExternalSecretStatusCondition) {
  40. ces.Status.Conditions = append(filterOutCondition(ces.Status.Conditions, condition.Type), condition)
  41. cesmetrics.UpdateClusterExternalSecretCondition(ces, &condition)
  42. }
  43. // filterOutCondition returns an empty set of conditions with the provided type.
  44. func filterOutCondition(conditions []esv1beta1.ClusterExternalSecretStatusCondition, condType esv1beta1.ClusterExternalSecretConditionType) []esv1beta1.ClusterExternalSecretStatusCondition {
  45. newConditions := make([]esv1beta1.ClusterExternalSecretStatusCondition, 0, len(conditions))
  46. for _, c := range conditions {
  47. if c.Type == condType {
  48. continue
  49. }
  50. newConditions = append(newConditions, c)
  51. }
  52. return newConditions
  53. }
  54. func ContainsNamespace(namespaces v1.NamespaceList, namespace string) bool {
  55. for _, ns := range namespaces.Items {
  56. if ns.ObjectMeta.Name == namespace {
  57. return true
  58. }
  59. }
  60. return false
  61. }
  62. func getConditionType(failedNamespaces map[string]string, namespaceList *v1.NamespaceList) esv1beta1.ClusterExternalSecretConditionType {
  63. if len(failedNamespaces) == 0 {
  64. return esv1beta1.ClusterExternalSecretReady
  65. }
  66. if len(failedNamespaces) < len(namespaceList.Items) {
  67. return esv1beta1.ClusterExternalSecretPartiallyReady
  68. }
  69. return esv1beta1.ClusterExternalSecretNotReady
  70. }