util.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright © The ESO Authors
  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 externalsecret
  14. import (
  15. "crypto/sha3"
  16. "fmt"
  17. v1 "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. "github.com/external-secrets/external-secrets/pkg/controllers/externalsecret/esmetrics"
  21. )
  22. // NewExternalSecretCondition a set of default options for creating an External Secret Condition.
  23. func NewExternalSecretCondition(condType esv1.ExternalSecretConditionType, status v1.ConditionStatus, reason, message string) *esv1.ExternalSecretStatusCondition {
  24. return &esv1.ExternalSecretStatusCondition{
  25. Type: condType,
  26. Status: status,
  27. LastTransitionTime: metav1.Now(),
  28. Reason: reason,
  29. Message: message,
  30. }
  31. }
  32. // GetExternalSecretCondition returns the condition with the provided type.
  33. func GetExternalSecretCondition(status esv1.ExternalSecretStatus, condType esv1.ExternalSecretConditionType) *esv1.ExternalSecretStatusCondition {
  34. for _, c := range status.Conditions {
  35. if c.Type == condType {
  36. return &c
  37. }
  38. }
  39. return nil
  40. }
  41. // SetExternalSecretCondition updates the external secret to include the provided
  42. // condition.
  43. func SetExternalSecretCondition(es *esv1.ExternalSecret, condition esv1.ExternalSecretStatusCondition) {
  44. currentCond := GetExternalSecretCondition(es.Status, condition.Type)
  45. if currentCond != nil && currentCond.Status == condition.Status &&
  46. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  47. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  48. return
  49. }
  50. // Do not update lastTransitionTime if the status of the condition doesn't change.
  51. if currentCond != nil && currentCond.Status == condition.Status {
  52. condition.LastTransitionTime = currentCond.LastTransitionTime
  53. }
  54. es.Status.Conditions = append(filterOutCondition(es.Status.Conditions, condition.Type), condition)
  55. if currentCond != nil {
  56. esmetrics.UpdateExternalSecretCondition(es, currentCond, 0.0)
  57. }
  58. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  59. }
  60. // filterOutCondition returns an empty set of conditions with the provided type.
  61. func filterOutCondition(conditions []esv1.ExternalSecretStatusCondition, condType esv1.ExternalSecretConditionType) []esv1.ExternalSecretStatusCondition {
  62. newConditions := make([]esv1.ExternalSecretStatusCondition, 0, len(conditions))
  63. for _, c := range conditions {
  64. if c.Type == condType {
  65. continue
  66. }
  67. newConditions = append(newConditions, c)
  68. }
  69. return newConditions
  70. }
  71. func fqdnFor(name string) string {
  72. fqdn := fmt.Sprintf(fieldOwnerTemplate, name)
  73. // If secret name is just too big, use the SHA3 hash of the secret name
  74. // Done this way for backwards compatibility thus avoiding breaking changes
  75. if len(fqdn) > 63 {
  76. fqdn = fmt.Sprintf(fieldOwnerTemplateSha, sha3.Sum224([]byte(name)))
  77. }
  78. return fqdn
  79. }