util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 generatorstate
  14. import (
  15. v1 "k8s.io/api/core/v1"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  18. )
  19. // NewgeneratorstateCondition a set of default options for creating an GeneratorState Condition.
  20. func NewGeneratorStateCondition(condType genv1alpha1.GeneratorStateConditionType, status v1.ConditionStatus, reason, message string) *genv1alpha1.GeneratorStateStatusCondition {
  21. return &genv1alpha1.GeneratorStateStatusCondition{
  22. Type: condType,
  23. Status: status,
  24. LastTransitionTime: metav1.Now(),
  25. Reason: reason,
  26. Message: message,
  27. }
  28. }
  29. // GetgeneratorstateCondition returns the condition with the provided type.
  30. func GetGeneratorStateCondition(status genv1alpha1.GeneratorStateStatus, condType genv1alpha1.GeneratorStateConditionType) *genv1alpha1.GeneratorStateStatusCondition {
  31. for _, c := range status.Conditions {
  32. if c.Type == condType {
  33. return &c
  34. }
  35. }
  36. return nil
  37. }
  38. // SetGeneratorStateCondition updates the GeneratorState to include the provided
  39. // condition.
  40. func SetGeneratorStateCondition(gs *genv1alpha1.GeneratorState, condition genv1alpha1.GeneratorStateStatusCondition) {
  41. currentCond := GetGeneratorStateCondition(gs.Status, condition.Type)
  42. if currentCond != nil && currentCond.Status == condition.Status &&
  43. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  44. return
  45. }
  46. // Do not update lastTransitionTime if the status of the condition doesn't change.
  47. if currentCond != nil && currentCond.Status == condition.Status {
  48. condition.LastTransitionTime = currentCond.LastTransitionTime
  49. }
  50. gs.Status.Conditions = append(filterOutCondition(gs.Status.Conditions, condition.Type), condition)
  51. }
  52. // filterOutCondition returns an empty set of conditions with the provided type.
  53. func filterOutCondition(conditions []genv1alpha1.GeneratorStateStatusCondition, condType genv1alpha1.GeneratorStateConditionType) []genv1alpha1.GeneratorStateStatusCondition {
  54. newConditions := make([]genv1alpha1.GeneratorStateStatusCondition, 0, len(conditions))
  55. for _, c := range conditions {
  56. if c.Type == condType {
  57. continue
  58. }
  59. newConditions = append(newConditions, c)
  60. }
  61. return newConditions
  62. }