util.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 secretstore
  13. import (
  14. v1 "k8s.io/api/core/v1"
  15. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  16. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  17. "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/metrics"
  18. )
  19. // NewSecretStoreCondition a set of default options for creating an External Secret Condition.
  20. func NewSecretStoreCondition(condType esapi.SecretStoreConditionType, status v1.ConditionStatus, reason, message string) *esapi.SecretStoreStatusCondition {
  21. return &esapi.SecretStoreStatusCondition{
  22. Type: condType,
  23. Status: status,
  24. LastTransitionTime: metav1.Now(),
  25. Reason: reason,
  26. Message: message,
  27. }
  28. }
  29. // GetSecretStoreCondition returns the condition with the provided type.
  30. func GetSecretStoreCondition(status esapi.SecretStoreStatus, condType esapi.SecretStoreConditionType) *esapi.SecretStoreStatusCondition {
  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. // SetExternalSecretCondition updates the external secret to include the provided
  40. // condition.
  41. func SetExternalSecretCondition(gs esapi.GenericStore, condition esapi.SecretStoreStatusCondition, gaugeVecGetter metrics.GaugeVevGetter) {
  42. metrics.UpdateStatusCondition(gs, condition, gaugeVecGetter)
  43. status := gs.GetStatus()
  44. currentCond := GetSecretStoreCondition(status, condition.Type)
  45. if currentCond != nil && currentCond.Status == condition.Status &&
  46. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  47. return
  48. }
  49. // Do not update lastTransitionTime if the status of the condition doesn't change.
  50. if currentCond != nil && currentCond.Status == condition.Status {
  51. condition.LastTransitionTime = currentCond.LastTransitionTime
  52. }
  53. status.Conditions = append(filterOutCondition(status.Conditions, condition.Type), condition)
  54. gs.SetStatus(status)
  55. }
  56. // filterOutCondition returns an empty set of conditions with the provided type.
  57. func filterOutCondition(conditions []esapi.SecretStoreStatusCondition, condType esapi.SecretStoreConditionType) []esapi.SecretStoreStatusCondition {
  58. newConditions := make([]esapi.SecretStoreStatusCondition, 0, len(conditions))
  59. for _, c := range conditions {
  60. if c.Type == condType {
  61. continue
  62. }
  63. newConditions = append(newConditions, c)
  64. }
  65. return newConditions
  66. }