externalsecret_crd_defaults_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 externalsecret
  14. import (
  15. "context"
  16. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  19. ctest "github.com/external-secrets/external-secrets/pkg/controllers/commontest"
  20. . "github.com/onsi/ginkgo/v2"
  21. . "github.com/onsi/gomega"
  22. )
  23. // Guards issue #6797: the API server must not inject values into these optional
  24. // strategy fields, because an injected value is a permanent diff for any
  25. // apply-based reconciler comparing against the source manifest.
  26. var _ = Describe("ExternalSecret CRD defaulting", func() {
  27. var namespace string
  28. BeforeEach(func() {
  29. var err error
  30. namespace, err = ctest.CreateNamespace("test-crd-defaults", k8sClient)
  31. Expect(err).ToNot(HaveOccurred())
  32. })
  33. It("leaves omitted strategy fields absent", func() {
  34. es := &esv1.ExternalSecret{
  35. ObjectMeta: metav1.ObjectMeta{
  36. Name: "no-defaults",
  37. Namespace: namespace,
  38. },
  39. Spec: esv1.ExternalSecretSpec{
  40. SecretStoreRef: esv1.SecretStoreRef{Name: "unused"},
  41. Target: esv1.ExternalSecretTarget{
  42. Template: &esv1.ExternalSecretTemplate{
  43. TemplateFrom: []esv1.TemplateFrom{{
  44. Secret: &esv1.TemplateRef{
  45. Name: "tpl",
  46. Items: []esv1.TemplateRefItem{{Key: "key"}},
  47. },
  48. }},
  49. },
  50. },
  51. Data: []esv1.ExternalSecretData{{
  52. SecretKey: "key",
  53. RemoteRef: esv1.ExternalSecretDataRemoteRef{Key: "remote-key"},
  54. }},
  55. DataFrom: []esv1.ExternalSecretDataFromRemoteRef{
  56. {Extract: &esv1.ExternalSecretDataRemoteRef{Key: "remote-key"}},
  57. {Find: &esv1.ExternalSecretFind{Path: new("some/path")}},
  58. },
  59. },
  60. }
  61. Expect(k8sClient.Create(context.Background(), es)).To(Succeed())
  62. stored := &esv1.ExternalSecret{}
  63. Expect(k8sClient.Get(context.Background(), types.NamespacedName{
  64. Name: es.Name,
  65. Namespace: es.Namespace,
  66. }, stored)).To(Succeed())
  67. By("checking data[].remoteRef")
  68. remoteRef := stored.Spec.Data[0].RemoteRef
  69. Expect(remoteRef.MetadataPolicy).To(BeEmpty())
  70. Expect(remoteRef.ConversionStrategy).To(BeEmpty())
  71. Expect(remoteRef.DecodingStrategy).To(BeEmpty())
  72. By("checking dataFrom[].extract")
  73. extract := stored.Spec.DataFrom[0].Extract
  74. Expect(extract.MetadataPolicy).To(BeEmpty())
  75. Expect(extract.ConversionStrategy).To(BeEmpty())
  76. Expect(extract.DecodingStrategy).To(BeEmpty())
  77. By("checking dataFrom[].find")
  78. find := stored.Spec.DataFrom[1].Find
  79. Expect(find.ConversionStrategy).To(BeEmpty())
  80. Expect(find.DecodingStrategy).To(BeEmpty())
  81. By("checking target.template.templateFrom[]")
  82. Expect(stored.Spec.Target.Template.TemplateFrom[0].ValuesDecodingStrategy).To(BeEmpty())
  83. })
  84. })