util.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "bytes"
  16. "crypto/sha3"
  17. "fmt"
  18. "sort"
  19. v1 "k8s.io/api/core/v1"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  22. "github.com/external-secrets/external-secrets/pkg/controllers/externalsecret/esmetrics"
  23. )
  24. // NewExternalSecretCondition a set of default options for creating an External Secret Condition.
  25. func NewExternalSecretCondition(condType esv1.ExternalSecretConditionType, status v1.ConditionStatus, reason, message string) *esv1.ExternalSecretStatusCondition {
  26. return &esv1.ExternalSecretStatusCondition{
  27. Type: condType,
  28. Status: status,
  29. LastTransitionTime: metav1.Now(),
  30. Reason: reason,
  31. Message: message,
  32. }
  33. }
  34. // SetExternalSecretCondition updates the external secret to include the provided
  35. // condition.
  36. func SetExternalSecretCondition(es *esv1.ExternalSecret, condition esv1.ExternalSecretStatusCondition) {
  37. currentCond := esv1.GetExternalSecretCondition(es.Status, condition.Type)
  38. if currentCond != nil && currentCond.Status == condition.Status &&
  39. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  40. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  41. return
  42. }
  43. // Do not update lastTransitionTime if the status of the condition doesn't change.
  44. if currentCond != nil && currentCond.Status == condition.Status {
  45. condition.LastTransitionTime = currentCond.LastTransitionTime
  46. }
  47. es.Status.Conditions = append(filterOutCondition(es.Status.Conditions, condition.Type), condition)
  48. if currentCond != nil {
  49. esmetrics.UpdateExternalSecretCondition(es, currentCond, 0.0)
  50. }
  51. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  52. }
  53. // filterOutCondition returns an empty set of conditions with the provided type.
  54. func filterOutCondition(conditions []esv1.ExternalSecretStatusCondition, condType esv1.ExternalSecretConditionType) []esv1.ExternalSecretStatusCondition {
  55. newConditions := make([]esv1.ExternalSecretStatusCondition, 0, len(conditions))
  56. for _, c := range conditions {
  57. if c.Type == condType {
  58. continue
  59. }
  60. newConditions = append(newConditions, c)
  61. }
  62. return newConditions
  63. }
  64. func fqdnFor(name string) string {
  65. fqdn := fmt.Sprintf(fieldOwnerTemplate, name)
  66. // If secret name is just too big, use the SHA3 hash of the secret name
  67. // Done this way for backwards compatibility thus avoiding breaking changes
  68. if len(fqdn) > 63 {
  69. fqdn = fmt.Sprintf(fieldOwnerTemplateSha, sha3.Sum224([]byte(name)))
  70. }
  71. return fqdn
  72. }
  73. // diffSecretDataKeys compares a secret's data keys before and after a mutation
  74. // and returns sorted lists of the key names that were added, updated, removed,
  75. // or newly set to an empty value. "emptied" is a subset of added/updated whose
  76. // new value has zero length, which is the "value replaced with empty string"
  77. // case. It returns only key names, never the secret values, so the result is
  78. // safe to log.
  79. func diffSecretDataKeys(oldData, newData map[string][]byte) (added, updated, removed, emptied []string) {
  80. for key, newVal := range newData {
  81. oldVal, existed := oldData[key]
  82. switch {
  83. case !existed:
  84. added = append(added, key)
  85. case !bytes.Equal(oldVal, newVal):
  86. updated = append(updated, key)
  87. default:
  88. // unchanged key, nothing to report
  89. continue
  90. }
  91. if len(newVal) == 0 {
  92. emptied = append(emptied, key)
  93. }
  94. }
  95. for key := range oldData {
  96. if _, ok := newData[key]; !ok {
  97. removed = append(removed, key)
  98. }
  99. }
  100. sort.Strings(added)
  101. sort.Strings(updated)
  102. sort.Strings(removed)
  103. sort.Strings(emptied)
  104. return added, updated, removed, emptied
  105. }