metadata.go 4.0 KB

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