metadata.go 3.7 KB

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