metadata.go 3.7 KB

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