util.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. esv1alpha1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  17. )
  18. func NewExternalSecretCondition(condType esv1alpha1.ExternalSecretConditionType, status v1.ConditionStatus, reason, message string) *esv1alpha1.ExternalSecretStatusCondition {
  19. return &esv1alpha1.ExternalSecretStatusCondition{
  20. Type: condType,
  21. Status: status,
  22. LastTransitionTime: metav1.Now(),
  23. Reason: reason,
  24. Message: message,
  25. }
  26. }
  27. // GetExternalSecretCondition returns the condition with the provided type.
  28. func GetExternalSecretCondition(status esv1alpha1.ExternalSecretStatus, condType esv1alpha1.ExternalSecretConditionType) *esv1alpha1.ExternalSecretStatusCondition {
  29. for i := range status.Conditions {
  30. c := status.Conditions[i]
  31. if c.Type == condType {
  32. return &c
  33. }
  34. }
  35. return nil
  36. }
  37. // SetExternalSecretCondition updates the external secret to include the provided
  38. // condition.
  39. func SetExternalSecretCondition(es *esv1alpha1.ExternalSecret, condition esv1alpha1.ExternalSecretStatusCondition) {
  40. currentCond := GetExternalSecretCondition(es.Status, condition.Type)
  41. if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason {
  42. updateExternalSecretCondition(es, &condition, 1.0)
  43. return
  44. }
  45. // Do not update lastTransitionTime if the status of the condition doesn't change.
  46. if currentCond != nil && currentCond.Status == condition.Status {
  47. condition.LastTransitionTime = currentCond.LastTransitionTime
  48. }
  49. es.Status.Conditions = append(filterOutCondition(es.Status.Conditions, condition.Type), condition)
  50. if currentCond != nil {
  51. updateExternalSecretCondition(es, currentCond, 0.0)
  52. }
  53. updateExternalSecretCondition(es, &condition, 1.0)
  54. }
  55. func filterOutCondition(conditions []esv1alpha1.ExternalSecretStatusCondition, condType esv1alpha1.ExternalSecretConditionType) []esv1alpha1.ExternalSecretStatusCondition {
  56. newConditions := make([]esv1alpha1.ExternalSecretStatusCondition, 0, len(conditions))
  57. for _, c := range conditions {
  58. if c.Type == condType {
  59. continue
  60. }
  61. newConditions = append(newConditions, c)
  62. }
  63. return newConditions
  64. }