util.go 3.3 KB

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