util.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 externalsecret
  13. import (
  14. v1 "k8s.io/api/core/v1"
  15. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  16. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  17. esmetrics "github.com/external-secrets/external-secrets/pkg/controllers/externalsecret/esmetrics"
  18. )
  19. // NewExternalSecretCondition a set of default options for creating an External Secret Condition.
  20. func NewExternalSecretCondition(condType esv1beta1.ExternalSecretConditionType, status v1.ConditionStatus, reason, message string) *esv1beta1.ExternalSecretStatusCondition {
  21. return &esv1beta1.ExternalSecretStatusCondition{
  22. Type: condType,
  23. Status: status,
  24. LastTransitionTime: metav1.Now(),
  25. Reason: reason,
  26. Message: message,
  27. }
  28. }
  29. // GetExternalSecretCondition returns the condition with the provided type.
  30. func GetExternalSecretCondition(status esv1beta1.ExternalSecretStatus, condType esv1beta1.ExternalSecretConditionType) *esv1beta1.ExternalSecretStatusCondition {
  31. for _, c := range status.Conditions {
  32. if c.Type == condType {
  33. return &c
  34. }
  35. }
  36. return nil
  37. }
  38. // SetExternalSecretCondition updates the external secret to include the provided
  39. // condition.
  40. func SetExternalSecretCondition(es *esv1beta1.ExternalSecret, condition esv1beta1.ExternalSecretStatusCondition) {
  41. currentCond := GetExternalSecretCondition(es.Status, condition.Type)
  42. if currentCond != nil && currentCond.Status == condition.Status &&
  43. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  44. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  45. return
  46. }
  47. // Do not update lastTransitionTime if the status of the condition doesn't change.
  48. if currentCond != nil && currentCond.Status == condition.Status {
  49. condition.LastTransitionTime = currentCond.LastTransitionTime
  50. }
  51. es.Status.Conditions = append(filterOutCondition(es.Status.Conditions, condition.Type), condition)
  52. if currentCond != nil {
  53. esmetrics.UpdateExternalSecretCondition(es, currentCond, 0.0)
  54. }
  55. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  56. }
  57. // filterOutCondition returns an empty set of conditions with the provided type.
  58. func filterOutCondition(conditions []esv1beta1.ExternalSecretStatusCondition, condType esv1beta1.ExternalSecretConditionType) []esv1beta1.ExternalSecretStatusCondition {
  59. newConditions := make([]esv1beta1.ExternalSecretStatusCondition, 0, len(conditions))
  60. for _, c := range conditions {
  61. if c.Type == condType {
  62. continue
  63. }
  64. newConditions = append(newConditions, c)
  65. }
  66. return newConditions
  67. }