metadata.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 kubernetes
  14. import (
  15. "fmt"
  16. v1 "k8s.io/api/core/v1"
  17. "github.com/external-secrets/external-secrets/pkg/utils/metadata"
  18. )
  19. type PushSecretMetadataSpec struct {
  20. TargetMergePolicy targetMergePolicy `json:"targetMergePolicy,omitempty"`
  21. SourceMergePolicy sourceMergePolicy `json:"sourceMergePolicy,omitempty"`
  22. Labels map[string]string `json:"labels,omitempty"`
  23. Annotations map[string]string `json:"annotations,omitempty"`
  24. RemoteNamespace string `json:"remoteNamespace,omitempty"`
  25. }
  26. type targetMergePolicy string
  27. const (
  28. targetMergePolicyMerge targetMergePolicy = "Merge"
  29. targetMergePolicyReplace targetMergePolicy = "Replace"
  30. targetMergePolicyIgnore targetMergePolicy = "Ignore"
  31. )
  32. type sourceMergePolicy string
  33. const (
  34. sourceMergePolicyMerge sourceMergePolicy = "Merge"
  35. sourceMergePolicyReplace sourceMergePolicy = "Replace"
  36. )
  37. // Takes the local secret metadata and merges it with the push metadata.
  38. // The push metadata takes precedence.
  39. // Depending on the policy, we either merge or overwrite the metadata from the local secret.
  40. func mergeSourceMetadata(localSecret *v1.Secret, pushMeta *metadata.PushSecretMetadata[PushSecretMetadataSpec]) (map[string]string, map[string]string, error) {
  41. labels := localSecret.ObjectMeta.Labels
  42. annotations := localSecret.ObjectMeta.Annotations
  43. if pushMeta == nil {
  44. return labels, annotations, nil
  45. }
  46. if labels == nil {
  47. labels = make(map[string]string)
  48. }
  49. if annotations == nil {
  50. annotations = make(map[string]string)
  51. }
  52. switch pushMeta.Spec.SourceMergePolicy {
  53. case "", sourceMergePolicyMerge:
  54. for k, v := range pushMeta.Spec.Labels {
  55. labels[k] = v
  56. }
  57. for k, v := range pushMeta.Spec.Annotations {
  58. annotations[k] = v
  59. }
  60. case sourceMergePolicyReplace:
  61. labels = pushMeta.Spec.Labels
  62. annotations = pushMeta.Spec.Annotations
  63. default:
  64. return nil, nil, fmt.Errorf("unexpected source merge policy %q", pushMeta.Spec.SourceMergePolicy)
  65. }
  66. return labels, annotations, nil
  67. }
  68. // Takes the remote secret metadata and merges it with the source metadata.
  69. // The source metadata may replace the existing labels/annotations
  70. // or merge into it depending on policy.
  71. func mergeTargetMetadata(remoteSecret *v1.Secret, pushMeta *metadata.PushSecretMetadata[PushSecretMetadataSpec], sourceLabels, sourceAnnotations map[string]string) (map[string]string, map[string]string, error) {
  72. labels := remoteSecret.ObjectMeta.Labels
  73. annotations := remoteSecret.ObjectMeta.Annotations
  74. if labels == nil {
  75. labels = make(map[string]string)
  76. }
  77. if annotations == nil {
  78. annotations = make(map[string]string)
  79. }
  80. var targetMergePolicy targetMergePolicy
  81. if pushMeta != nil {
  82. targetMergePolicy = pushMeta.Spec.TargetMergePolicy
  83. }
  84. switch targetMergePolicy {
  85. case "", targetMergePolicyMerge:
  86. for k, v := range sourceLabels {
  87. labels[k] = v
  88. }
  89. for k, v := range sourceAnnotations {
  90. annotations[k] = v
  91. }
  92. case targetMergePolicyReplace:
  93. labels = sourceLabels
  94. annotations = sourceAnnotations
  95. case targetMergePolicyIgnore:
  96. // leave the target metadata as is
  97. // this is useful when we only want to push data
  98. // and the user does not want to touch the metadata
  99. default:
  100. return nil, nil, fmt.Errorf("unexpected target merge policy %q", targetMergePolicy)
  101. }
  102. return labels, annotations, nil
  103. }