metadata.go 4.1 KB

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