util.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // SetExternalSecretCondition updates the external secret to include the provided
  33. // condition.
  34. func SetExternalSecretCondition(es *esv1.ExternalSecret, condition esv1.ExternalSecretStatusCondition) {
  35. currentCond := esv1.GetExternalSecretCondition(es.Status, condition.Type)
  36. if currentCond != nil && currentCond.Status == condition.Status &&
  37. currentCond.Reason == condition.Reason && currentCond.Message == condition.Message {
  38. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  39. return
  40. }
  41. // Do not update lastTransitionTime if the status of the condition doesn't change.
  42. if currentCond != nil && currentCond.Status == condition.Status {
  43. condition.LastTransitionTime = currentCond.LastTransitionTime
  44. }
  45. es.Status.Conditions = append(filterOutCondition(es.Status.Conditions, condition.Type), condition)
  46. if currentCond != nil {
  47. esmetrics.UpdateExternalSecretCondition(es, currentCond, 0.0)
  48. }
  49. esmetrics.UpdateExternalSecretCondition(es, &condition, 1.0)
  50. }
  51. // filterOutCondition returns an empty set of conditions with the provided type.
  52. func filterOutCondition(conditions []esv1.ExternalSecretStatusCondition, condType esv1.ExternalSecretConditionType) []esv1.ExternalSecretStatusCondition {
  53. newConditions := make([]esv1.ExternalSecretStatusCondition, 0, len(conditions))
  54. for _, c := range conditions {
  55. if c.Type == condType {
  56. continue
  57. }
  58. newConditions = append(newConditions, c)
  59. }
  60. return newConditions
  61. }
  62. func fqdnFor(name string) string {
  63. fqdn := fmt.Sprintf(fieldOwnerTemplate, name)
  64. // If secret name is just too big, use the SHA3 hash of the secret name
  65. // Done this way for backwards compatibility thus avoiding breaking changes
  66. if len(fqdn) > 63 {
  67. fqdn = fmt.Sprintf(fieldOwnerTemplateSha, sha3.Sum224([]byte(name)))
  68. }
  69. return fqdn
  70. }