clusterexternalsecret_controller_test.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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 clusterexternalsecret
  14. import (
  15. "context"
  16. "fmt"
  17. "math/rand"
  18. "sort"
  19. "time"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. crclient "sigs.k8s.io/controller-runtime/pkg/client"
  24. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  25. "github.com/external-secrets/external-secrets/pkg/controllers/clusterexternalsecret/cesmetrics"
  26. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  27. . "github.com/onsi/ginkgo/v2"
  28. . "github.com/onsi/gomega"
  29. )
  30. func init() {
  31. ctrlmetrics.SetUpLabelNames(false)
  32. cesmetrics.SetUpMetrics()
  33. }
  34. const (
  35. metadataLabelName = "kubernetes.io/metadata.name"
  36. testLabelKey = "test-label-key"
  37. testAnnotationKey = "test-annotation-key"
  38. testLabelValue = "test-label-value"
  39. testAnnotationValue = "test-annotation-value"
  40. updatedTestStore = "updated-test-store"
  41. noLongerMatchLabelKey = "no-longer-match-label-key"
  42. noLongerMatchLabelValue = "no-longer-match-label-value"
  43. )
  44. var (
  45. timeout = time.Second * 10
  46. interval = time.Millisecond * 250
  47. )
  48. type testCase struct {
  49. namespaces []v1.Namespace
  50. clusterExternalSecret func(namespaces []v1.Namespace) esv1.ClusterExternalSecret
  51. beforeCheck func(ctx context.Context, namespaces []v1.Namespace, created esv1.ClusterExternalSecret)
  52. expectedClusterExternalSecret func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret
  53. expectedExternalSecrets func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret
  54. }
  55. var _ = Describe("ClusterExternalSecret controller", func() {
  56. defaultClusterExternalSecret := func() *esv1.ClusterExternalSecret {
  57. return &esv1.ClusterExternalSecret{
  58. ObjectMeta: metav1.ObjectMeta{
  59. Name: fmt.Sprintf("test-ces-%s", randString(10)),
  60. },
  61. Spec: esv1.ClusterExternalSecretSpec{
  62. ExternalSecretSpec: esv1.ExternalSecretSpec{
  63. SecretStoreRef: esv1.SecretStoreRef{
  64. Name: "test-store",
  65. },
  66. Target: esv1.ExternalSecretTarget{
  67. Name: "test-secret",
  68. },
  69. Data: []esv1.ExternalSecretData{
  70. {
  71. SecretKey: "test-secret-key",
  72. RemoteRef: esv1.ExternalSecretDataRemoteRef{
  73. Key: "test-remote-key",
  74. NullBytePolicy: esv1.ExternalSecretNullBytePolicyIgnore,
  75. },
  76. },
  77. },
  78. },
  79. },
  80. }
  81. }
  82. DescribeTable("When reconciling a ClusterExternal Secret",
  83. func(tc testCase) {
  84. ctx := context.Background()
  85. By("creating namespaces")
  86. namespaces := make([]v1.Namespace, 0, len(tc.namespaces))
  87. for _, ns := range tc.namespaces {
  88. err := k8sClient.Create(ctx, &ns)
  89. Expect(err).ShouldNot(HaveOccurred())
  90. namespaces = append(namespaces, ns)
  91. }
  92. By("creating a cluster external secret")
  93. ces := tc.clusterExternalSecret(tc.namespaces)
  94. err := k8sClient.Create(ctx, &ces)
  95. Expect(err).ShouldNot(HaveOccurred())
  96. By("running before check")
  97. if tc.beforeCheck != nil {
  98. tc.beforeCheck(ctx, namespaces, ces)
  99. }
  100. // the before check above may have updated the namespaces, so refresh them
  101. for i, ns := range namespaces {
  102. err := k8sClient.Get(ctx, types.NamespacedName{Name: ns.Name}, &ns)
  103. Expect(err).ShouldNot(HaveOccurred())
  104. namespaces[i] = ns
  105. }
  106. By("checking the cluster external secret")
  107. expectedCES := tc.expectedClusterExternalSecret(namespaces, ces)
  108. Eventually(func(g Gomega) {
  109. key := types.NamespacedName{Name: expectedCES.Name}
  110. var gotCes esv1.ClusterExternalSecret
  111. err = k8sClient.Get(ctx, key, &gotCes)
  112. g.Expect(err).ShouldNot(HaveOccurred())
  113. g.Expect(gotCes.Labels).To(Equal(expectedCES.Labels))
  114. g.Expect(gotCes.Annotations).To(Equal(expectedCES.Annotations))
  115. g.Expect(gotCes.Spec).To(Equal(expectedCES.Spec))
  116. g.Expect(gotCes.Status).To(Equal(expectedCES.Status))
  117. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  118. By("checking the external secrets")
  119. expectedESs := tc.expectedExternalSecrets(namespaces, ces)
  120. Eventually(func(g Gomega) {
  121. var gotESs []esv1.ExternalSecret //nolint:prealloc // size unknown before loop
  122. for _, ns := range namespaces {
  123. var externalSecrets esv1.ExternalSecretList
  124. err := k8sClient.List(ctx, &externalSecrets, crclient.InNamespace(ns.Name))
  125. g.Expect(err).ShouldNot(HaveOccurred())
  126. gotESs = append(gotESs, externalSecrets.Items...)
  127. }
  128. g.Expect(len(gotESs)).Should(Equal(len(expectedESs)))
  129. for _, gotES := range gotESs {
  130. found := false
  131. for _, expectedES := range expectedESs {
  132. if gotES.Namespace == expectedES.Namespace && gotES.Name == expectedES.Name {
  133. found = true
  134. g.Expect(gotES.Labels).To(Equal(expectedES.Labels))
  135. g.Expect(gotES.Annotations).To(Equal(expectedES.Annotations))
  136. g.Expect(gotES.Spec).To(Equal(expectedES.Spec))
  137. }
  138. }
  139. g.Expect(found).To(Equal(true))
  140. }
  141. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  142. },
  143. Entry("Should use cluster external secret name if external secret name isn't defined", testCase{
  144. namespaces: []v1.Namespace{
  145. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  146. },
  147. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  148. ces := defaultClusterExternalSecret()
  149. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  150. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  151. }
  152. return *ces
  153. },
  154. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  155. return esv1.ClusterExternalSecret{
  156. ObjectMeta: metav1.ObjectMeta{
  157. Name: created.Name,
  158. },
  159. Spec: created.Spec,
  160. Status: esv1.ClusterExternalSecretStatus{
  161. ExternalSecretName: created.Name,
  162. ProvisionedNamespaces: []string{namespaces[0].Name},
  163. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  164. {
  165. Type: esv1.ClusterExternalSecretReady,
  166. Status: v1.ConditionTrue,
  167. },
  168. },
  169. },
  170. }
  171. },
  172. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  173. return []esv1.ExternalSecret{
  174. {
  175. ObjectMeta: metav1.ObjectMeta{
  176. Namespace: namespaces[0].Name,
  177. Name: created.Name,
  178. },
  179. Spec: created.Spec.ExternalSecretSpec,
  180. },
  181. }
  182. },
  183. }),
  184. Entry("Should set external secret name and metadata if the fields are set", testCase{
  185. namespaces: []v1.Namespace{
  186. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  187. },
  188. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  189. ces := defaultClusterExternalSecret()
  190. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  191. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  192. }
  193. ces.Spec.ExternalSecretName = "test-es"
  194. ces.Spec.ExternalSecretMetadata = esv1.ExternalSecretMetadata{
  195. Labels: map[string]string{"test-label-key1": "test-label-value1", "test-label-key2": "test-label-value2"},
  196. Annotations: map[string]string{"test-annotation-key1": "test-annotation-value1", "test-annotation-key2": "test-annotation-value2"},
  197. }
  198. return *ces
  199. },
  200. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  201. return esv1.ClusterExternalSecret{
  202. ObjectMeta: metav1.ObjectMeta{
  203. Name: created.Name,
  204. },
  205. Spec: created.Spec,
  206. Status: esv1.ClusterExternalSecretStatus{
  207. ExternalSecretName: "test-es",
  208. ProvisionedNamespaces: []string{namespaces[0].Name},
  209. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  210. {
  211. Type: esv1.ClusterExternalSecretReady,
  212. Status: v1.ConditionTrue,
  213. },
  214. },
  215. },
  216. }
  217. },
  218. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  219. return []esv1.ExternalSecret{
  220. {
  221. ObjectMeta: metav1.ObjectMeta{
  222. Namespace: namespaces[0].Name,
  223. Name: "test-es",
  224. Labels: map[string]string{"test-label-key1": "test-label-value1", "test-label-key2": "test-label-value2"},
  225. Annotations: map[string]string{"test-annotation-key1": "test-annotation-value1", "test-annotation-key2": "test-annotation-value2"},
  226. },
  227. Spec: created.Spec.ExternalSecretSpec,
  228. },
  229. }
  230. },
  231. }),
  232. Entry("Should delete old external secrets if name has changed", testCase{
  233. namespaces: []v1.Namespace{
  234. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  235. },
  236. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  237. ces := defaultClusterExternalSecret()
  238. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  239. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  240. }
  241. ces.Spec.ExternalSecretName = "old-es-name"
  242. return *ces
  243. },
  244. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1.ClusterExternalSecret) {
  245. // Wait until the external secret is provisioned
  246. var es esv1.ExternalSecret
  247. Eventually(func(g Gomega) {
  248. key := types.NamespacedName{Namespace: namespaces[0].Name, Name: "old-es-name"}
  249. g.Expect(k8sClient.Get(ctx, key, &es)).ShouldNot(HaveOccurred())
  250. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  251. copied := created.DeepCopy()
  252. copied.Spec.ExternalSecretName = "new-es-name"
  253. Expect(k8sClient.Patch(ctx, copied, crclient.MergeFrom(created.DeepCopy()))).ShouldNot(HaveOccurred())
  254. },
  255. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  256. updatedSpec := created.Spec.DeepCopy()
  257. updatedSpec.ExternalSecretName = "new-es-name"
  258. return esv1.ClusterExternalSecret{
  259. ObjectMeta: metav1.ObjectMeta{
  260. Name: created.Name,
  261. },
  262. Spec: *updatedSpec,
  263. Status: esv1.ClusterExternalSecretStatus{
  264. ExternalSecretName: "new-es-name",
  265. ProvisionedNamespaces: []string{namespaces[0].Name},
  266. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  267. {
  268. Type: esv1.ClusterExternalSecretReady,
  269. Status: v1.ConditionTrue,
  270. },
  271. },
  272. },
  273. }
  274. },
  275. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  276. return []esv1.ExternalSecret{
  277. {
  278. ObjectMeta: metav1.ObjectMeta{
  279. Namespace: namespaces[0].Name,
  280. Name: "new-es-name",
  281. },
  282. Spec: created.Spec.ExternalSecretSpec,
  283. },
  284. }
  285. },
  286. }),
  287. Entry("Should update external secret if the fields change", testCase{
  288. namespaces: []v1.Namespace{
  289. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  290. },
  291. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  292. ces := defaultClusterExternalSecret()
  293. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  294. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  295. }
  296. return *ces
  297. },
  298. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1.ClusterExternalSecret) {
  299. // Wait until the external secret is provisioned
  300. var es esv1.ExternalSecret
  301. Eventually(func(g Gomega) {
  302. key := types.NamespacedName{Namespace: namespaces[0].Name, Name: created.Name}
  303. g.Expect(k8sClient.Get(ctx, key, &es)).ShouldNot(HaveOccurred())
  304. g.Expect(len(es.Labels)).Should(Equal(0))
  305. g.Expect(len(es.Annotations)).Should(Equal(0))
  306. g.Expect(es.Spec).Should(Equal(created.Spec.ExternalSecretSpec))
  307. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  308. copied := created.DeepCopy()
  309. copied.Spec.ExternalSecretMetadata = esv1.ExternalSecretMetadata{
  310. Labels: map[string]string{testLabelKey: testLabelValue},
  311. Annotations: map[string]string{testAnnotationKey: testAnnotationValue},
  312. }
  313. copied.Spec.ExternalSecretSpec.SecretStoreRef.Name = updatedTestStore
  314. Expect(k8sClient.Patch(ctx, copied, crclient.MergeFrom(created.DeepCopy()))).ShouldNot(HaveOccurred())
  315. },
  316. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  317. updatedSpec := created.Spec.DeepCopy()
  318. updatedSpec.ExternalSecretMetadata = esv1.ExternalSecretMetadata{
  319. Labels: map[string]string{testLabelKey: testLabelValue},
  320. Annotations: map[string]string{testAnnotationKey: testAnnotationValue},
  321. }
  322. updatedSpec.ExternalSecretSpec.SecretStoreRef.Name = updatedTestStore
  323. return esv1.ClusterExternalSecret{
  324. ObjectMeta: metav1.ObjectMeta{
  325. Name: created.Name,
  326. },
  327. Spec: *updatedSpec,
  328. Status: esv1.ClusterExternalSecretStatus{
  329. ExternalSecretName: created.Name,
  330. ProvisionedNamespaces: []string{namespaces[0].Name},
  331. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  332. {
  333. Type: esv1.ClusterExternalSecretReady,
  334. Status: v1.ConditionTrue,
  335. },
  336. },
  337. },
  338. }
  339. },
  340. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  341. updatedSpec := created.Spec.ExternalSecretSpec.DeepCopy()
  342. updatedSpec.SecretStoreRef.Name = updatedTestStore
  343. return []esv1.ExternalSecret{
  344. {
  345. ObjectMeta: metav1.ObjectMeta{
  346. Namespace: namespaces[0].Name,
  347. Name: created.Name,
  348. Labels: map[string]string{testLabelKey: testLabelValue},
  349. Annotations: map[string]string{testAnnotationKey: testAnnotationValue},
  350. },
  351. Spec: *updatedSpec,
  352. },
  353. }
  354. },
  355. }),
  356. Entry("Should not overwrite existing external secrets and error out if one is present", testCase{
  357. namespaces: []v1.Namespace{
  358. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  359. },
  360. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  361. ces := defaultClusterExternalSecret()
  362. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  363. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  364. }
  365. es := &esv1.ExternalSecret{
  366. ObjectMeta: metav1.ObjectMeta{
  367. Name: ces.Name,
  368. Namespace: namespaces[0].Name,
  369. },
  370. }
  371. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  372. return *ces
  373. },
  374. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  375. return esv1.ClusterExternalSecret{
  376. ObjectMeta: metav1.ObjectMeta{
  377. Name: created.Name,
  378. },
  379. Spec: created.Spec,
  380. Status: esv1.ClusterExternalSecretStatus{
  381. ExternalSecretName: created.Name,
  382. FailedNamespaces: []esv1.ClusterExternalSecretNamespaceFailure{
  383. {
  384. Namespace: namespaces[0].Name,
  385. Reason: "external secret already exists in namespace",
  386. },
  387. },
  388. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  389. {
  390. Type: esv1.ClusterExternalSecretReady,
  391. Status: v1.ConditionFalse,
  392. Message: "one or more namespaces failed",
  393. },
  394. },
  395. },
  396. }
  397. },
  398. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  399. return []esv1.ExternalSecret{
  400. {
  401. ObjectMeta: metav1.ObjectMeta{
  402. Namespace: namespaces[0].Name,
  403. Name: created.Name,
  404. },
  405. Spec: esv1.ExternalSecretSpec{
  406. Target: esv1.ExternalSecretTarget{
  407. CreationPolicy: "Owner",
  408. DeletionPolicy: "Retain",
  409. },
  410. RefreshInterval: &metav1.Duration{Duration: time.Hour},
  411. },
  412. },
  413. }
  414. },
  415. }),
  416. Entry("Should crate an external secret if one with the same name has been deleted", testCase{
  417. namespaces: []v1.Namespace{
  418. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  419. },
  420. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  421. ces := defaultClusterExternalSecret()
  422. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  423. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  424. }
  425. es := &esv1.ExternalSecret{
  426. ObjectMeta: metav1.ObjectMeta{
  427. Name: ces.Name,
  428. Namespace: namespaces[0].Name,
  429. },
  430. }
  431. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  432. return *ces
  433. },
  434. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1.ClusterExternalSecret) {
  435. ces := esv1.ClusterExternalSecret{}
  436. Eventually(func(g Gomega) {
  437. key := types.NamespacedName{Namespace: created.Namespace, Name: created.Name}
  438. g.Expect(k8sClient.Get(ctx, key, &ces)).ShouldNot(HaveOccurred())
  439. g.Expect(len(ces.Status.FailedNamespaces)).Should(Equal(1))
  440. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  441. es := &esv1.ExternalSecret{
  442. ObjectMeta: metav1.ObjectMeta{
  443. Name: ces.Name,
  444. Namespace: namespaces[0].Name,
  445. },
  446. }
  447. Expect(k8sClient.Delete(ctx, es)).ShouldNot(HaveOccurred())
  448. },
  449. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  450. return esv1.ClusterExternalSecret{
  451. ObjectMeta: metav1.ObjectMeta{
  452. Name: created.Name,
  453. },
  454. Spec: created.Spec,
  455. Status: esv1.ClusterExternalSecretStatus{
  456. ExternalSecretName: created.Name,
  457. ProvisionedNamespaces: []string{namespaces[0].Name},
  458. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  459. {
  460. Type: esv1.ClusterExternalSecretReady,
  461. Status: v1.ConditionTrue,
  462. },
  463. },
  464. },
  465. }
  466. },
  467. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  468. return []esv1.ExternalSecret{
  469. {
  470. ObjectMeta: metav1.ObjectMeta{
  471. Namespace: namespaces[0].Name,
  472. Name: created.Name,
  473. },
  474. Spec: created.Spec.ExternalSecretSpec,
  475. },
  476. }
  477. },
  478. }),
  479. Entry("Should delete external secrets when namespaces no longer match", testCase{
  480. namespaces: []v1.Namespace{
  481. {
  482. ObjectMeta: metav1.ObjectMeta{
  483. Name: randomNamespaceName(),
  484. Labels: map[string]string{noLongerMatchLabelKey: noLongerMatchLabelValue},
  485. },
  486. },
  487. {
  488. ObjectMeta: metav1.ObjectMeta{
  489. Name: randomNamespaceName(),
  490. Labels: map[string]string{noLongerMatchLabelKey: noLongerMatchLabelValue},
  491. },
  492. },
  493. },
  494. clusterExternalSecret: func(_ []v1.Namespace) esv1.ClusterExternalSecret {
  495. ces := defaultClusterExternalSecret()
  496. ces.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  497. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  498. MatchLabels: map[string]string{noLongerMatchLabelKey: noLongerMatchLabelValue},
  499. }
  500. return *ces
  501. },
  502. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1.ClusterExternalSecret) {
  503. // Wait until the target ESs have been created
  504. Eventually(func(g Gomega) {
  505. for _, ns := range namespaces {
  506. key := types.NamespacedName{Namespace: ns.Name, Name: created.Name}
  507. g.Expect(k8sClient.Get(ctx, key, &esv1.ExternalSecret{})).ShouldNot(HaveOccurred())
  508. }
  509. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  510. // Retry on conflict since controller may be updating namespace with finalizers
  511. Eventually(func() error {
  512. var ns v1.Namespace
  513. if err := k8sClient.Get(ctx, types.NamespacedName{Name: namespaces[0].Name}, &ns); err != nil {
  514. return err
  515. }
  516. ns.Labels = map[string]string{}
  517. return k8sClient.Update(ctx, &ns)
  518. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  519. },
  520. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  521. return esv1.ClusterExternalSecret{
  522. ObjectMeta: metav1.ObjectMeta{
  523. Name: created.Name,
  524. },
  525. Spec: created.Spec,
  526. Status: esv1.ClusterExternalSecretStatus{
  527. ExternalSecretName: created.Name,
  528. ProvisionedNamespaces: []string{namespaces[1].Name},
  529. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  530. {
  531. Type: esv1.ClusterExternalSecretReady,
  532. Status: v1.ConditionTrue,
  533. },
  534. },
  535. },
  536. }
  537. },
  538. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  539. return []esv1.ExternalSecret{
  540. {
  541. ObjectMeta: metav1.ObjectMeta{
  542. Namespace: namespaces[1].Name,
  543. Name: created.Name,
  544. },
  545. Spec: created.Spec.ExternalSecretSpec,
  546. },
  547. }
  548. },
  549. }),
  550. Entry("Should sync with match expression", testCase{
  551. namespaces: []v1.Namespace{
  552. {
  553. ObjectMeta: metav1.ObjectMeta{
  554. Name: randomNamespaceName(),
  555. Labels: map[string]string{"prefix": "foo"},
  556. },
  557. },
  558. {
  559. ObjectMeta: metav1.ObjectMeta{
  560. Name: randomNamespaceName(),
  561. Labels: map[string]string{"prefix": "bar"},
  562. },
  563. },
  564. {
  565. ObjectMeta: metav1.ObjectMeta{
  566. Name: randomNamespaceName(),
  567. Labels: map[string]string{"prefix": "baz"},
  568. },
  569. },
  570. },
  571. clusterExternalSecret: func(_ []v1.Namespace) esv1.ClusterExternalSecret {
  572. ces := defaultClusterExternalSecret()
  573. ces.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  574. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  575. MatchExpressions: []metav1.LabelSelectorRequirement{
  576. {
  577. Key: "prefix",
  578. Operator: metav1.LabelSelectorOpIn,
  579. Values: []string{"foo", "bar"}, // "baz" is excluded
  580. },
  581. },
  582. }
  583. return *ces
  584. },
  585. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  586. provisionedNamespaces := []string{namespaces[0].Name, namespaces[1].Name}
  587. sort.Strings(provisionedNamespaces)
  588. return esv1.ClusterExternalSecret{
  589. ObjectMeta: metav1.ObjectMeta{
  590. Name: created.Name,
  591. },
  592. Spec: created.Spec,
  593. Status: esv1.ClusterExternalSecretStatus{
  594. ExternalSecretName: created.Name,
  595. ProvisionedNamespaces: provisionedNamespaces,
  596. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  597. {
  598. Type: esv1.ClusterExternalSecretReady,
  599. Status: v1.ConditionTrue,
  600. },
  601. },
  602. },
  603. }
  604. },
  605. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  606. return []esv1.ExternalSecret{
  607. {
  608. ObjectMeta: metav1.ObjectMeta{
  609. Namespace: namespaces[0].Name,
  610. Name: created.Name,
  611. },
  612. Spec: created.Spec.ExternalSecretSpec,
  613. },
  614. {
  615. ObjectMeta: metav1.ObjectMeta{
  616. Namespace: namespaces[1].Name,
  617. Name: created.Name,
  618. },
  619. Spec: created.Spec.ExternalSecretSpec,
  620. },
  621. }
  622. },
  623. }),
  624. Entry("Should be ready if no namespace matches", testCase{
  625. namespaces: []v1.Namespace{
  626. {
  627. ObjectMeta: metav1.ObjectMeta{
  628. Name: randomNamespaceName(),
  629. },
  630. },
  631. },
  632. clusterExternalSecret: func(_ []v1.Namespace) esv1.ClusterExternalSecret {
  633. ces := defaultClusterExternalSecret()
  634. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  635. MatchLabels: map[string]string{metadataLabelName: "no-namespace-matches"},
  636. }
  637. return *ces
  638. },
  639. expectedClusterExternalSecret: func(_ []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  640. return esv1.ClusterExternalSecret{
  641. ObjectMeta: metav1.ObjectMeta{
  642. Name: created.Name,
  643. },
  644. Spec: created.Spec,
  645. Status: esv1.ClusterExternalSecretStatus{
  646. ExternalSecretName: created.Name,
  647. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  648. {
  649. Type: esv1.ClusterExternalSecretReady,
  650. Status: v1.ConditionTrue,
  651. },
  652. },
  653. },
  654. }
  655. },
  656. expectedExternalSecrets: func(_ []v1.Namespace, _ esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  657. return []esv1.ExternalSecret{}
  658. },
  659. }),
  660. Entry("Should be ready if namespace is selected via the namespace selectors", testCase{
  661. namespaces: []v1.Namespace{
  662. {
  663. ObjectMeta: metav1.ObjectMeta{
  664. Name: "namespace1",
  665. Labels: map[string]string{
  666. "key": "value1",
  667. },
  668. },
  669. },
  670. {
  671. ObjectMeta: metav1.ObjectMeta{
  672. Name: "namespace2",
  673. Labels: map[string]string{
  674. "key": "value2",
  675. },
  676. },
  677. },
  678. {
  679. ObjectMeta: metav1.ObjectMeta{
  680. Name: "namespace3",
  681. Labels: map[string]string{
  682. "key": "value3",
  683. },
  684. },
  685. },
  686. },
  687. clusterExternalSecret: func(_ []v1.Namespace) esv1.ClusterExternalSecret {
  688. ces := defaultClusterExternalSecret()
  689. ces.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  690. {
  691. MatchLabels: map[string]string{"key": "value1"},
  692. },
  693. {
  694. MatchLabels: map[string]string{"key": "value2"},
  695. },
  696. }
  697. return *ces
  698. },
  699. expectedClusterExternalSecret: func(_ []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  700. return esv1.ClusterExternalSecret{
  701. ObjectMeta: metav1.ObjectMeta{
  702. Name: created.Name,
  703. },
  704. Spec: created.Spec,
  705. Status: esv1.ClusterExternalSecretStatus{
  706. ExternalSecretName: created.Name,
  707. ProvisionedNamespaces: []string{
  708. "namespace1",
  709. "namespace2",
  710. },
  711. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  712. {
  713. Type: esv1.ClusterExternalSecretReady,
  714. Status: v1.ConditionTrue,
  715. },
  716. },
  717. },
  718. }
  719. },
  720. expectedExternalSecrets: func(_ []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  721. return []esv1.ExternalSecret{
  722. {
  723. ObjectMeta: metav1.ObjectMeta{
  724. Namespace: "namespace1",
  725. Name: created.Name,
  726. },
  727. Spec: created.Spec.ExternalSecretSpec,
  728. },
  729. {
  730. ObjectMeta: metav1.ObjectMeta{
  731. Namespace: "namespace2",
  732. Name: created.Name,
  733. },
  734. Spec: created.Spec.ExternalSecretSpec,
  735. },
  736. }
  737. },
  738. }),
  739. Entry("Should be ready if namespace is selected via namespaces", testCase{
  740. namespaces: []v1.Namespace{
  741. {
  742. ObjectMeta: metav1.ObjectMeta{
  743. Name: "not-matching-namespace",
  744. },
  745. },
  746. },
  747. clusterExternalSecret: func(_ []v1.Namespace) esv1.ClusterExternalSecret {
  748. ces := defaultClusterExternalSecret()
  749. // does-not-exists tests that we would continue on to the next and not stop if the
  750. // namespace hasn't been created yet.
  751. ces.Spec.Namespaces = []string{"does-not-exist", "not-matching-namespace"}
  752. return *ces
  753. },
  754. expectedClusterExternalSecret: func(_ []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  755. return esv1.ClusterExternalSecret{
  756. ObjectMeta: metav1.ObjectMeta{
  757. Name: created.Name,
  758. },
  759. Spec: created.Spec,
  760. Status: esv1.ClusterExternalSecretStatus{
  761. ExternalSecretName: created.Name,
  762. ProvisionedNamespaces: []string{
  763. "not-matching-namespace",
  764. },
  765. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  766. {
  767. Type: esv1.ClusterExternalSecretReady,
  768. Status: v1.ConditionTrue,
  769. },
  770. },
  771. },
  772. }
  773. },
  774. expectedExternalSecrets: func(_ []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  775. return []esv1.ExternalSecret{
  776. {
  777. ObjectMeta: metav1.ObjectMeta{
  778. Namespace: "not-matching-namespace",
  779. Name: created.Name,
  780. },
  781. Spec: created.Spec.ExternalSecretSpec,
  782. },
  783. }
  784. },
  785. }),
  786. Entry("Should propagate the force-sync annotation", testCase{
  787. namespaces: []v1.Namespace{
  788. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  789. },
  790. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  791. ces := defaultClusterExternalSecret()
  792. ces.Annotations = map[string]string{esv1.AnnotationForceSync: "true"}
  793. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  794. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  795. }
  796. return *ces
  797. },
  798. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  799. return esv1.ClusterExternalSecret{
  800. ObjectMeta: metav1.ObjectMeta{
  801. Name: created.Name,
  802. Annotations: map[string]string{esv1.AnnotationForceSync: "true"},
  803. },
  804. Spec: created.Spec,
  805. Status: esv1.ClusterExternalSecretStatus{
  806. ExternalSecretName: created.Name,
  807. ProvisionedNamespaces: []string{namespaces[0].Name},
  808. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  809. {
  810. Type: esv1.ClusterExternalSecretReady,
  811. Status: v1.ConditionTrue,
  812. },
  813. },
  814. },
  815. }
  816. },
  817. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  818. return []esv1.ExternalSecret{
  819. {
  820. ObjectMeta: metav1.ObjectMeta{
  821. Namespace: namespaces[0].Name,
  822. Name: created.Name,
  823. Annotations: map[string]string{esv1.AnnotationForceSync: "true"},
  824. },
  825. Spec: created.Spec.ExternalSecretSpec,
  826. },
  827. }
  828. },
  829. }),
  830. Entry("Should prune the force-sync annotation", testCase{
  831. namespaces: []v1.Namespace{
  832. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  833. },
  834. clusterExternalSecret: func(namespaces []v1.Namespace) esv1.ClusterExternalSecret {
  835. ces := defaultClusterExternalSecret()
  836. ces.Annotations = map[string]string{esv1.AnnotationForceSync: "true"}
  837. ces.Spec.NamespaceSelector = &metav1.LabelSelector{
  838. MatchLabels: map[string]string{metadataLabelName: namespaces[0].Name},
  839. }
  840. return *ces
  841. },
  842. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1.ClusterExternalSecret) {
  843. // Wait until the external secret is provisioned and has
  844. // the force-sync annotation
  845. var es esv1.ExternalSecret
  846. Eventually(func(g Gomega) {
  847. key := types.NamespacedName{Namespace: namespaces[0].Name, Name: created.Name}
  848. g.Expect(k8sClient.Get(ctx, key, &es)).ShouldNot(HaveOccurred())
  849. g.Expect(len(es.Annotations)).Should(Equal(1))
  850. g.Expect(es.Spec).Should(Equal(created.Spec.ExternalSecretSpec))
  851. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  852. // Prune the force-sync annotation
  853. copied := created.DeepCopy()
  854. delete(copied.Annotations, esv1.AnnotationForceSync)
  855. Expect(k8sClient.Patch(ctx, copied, crclient.MergeFrom(created.DeepCopy()))).ShouldNot(HaveOccurred())
  856. },
  857. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) esv1.ClusterExternalSecret {
  858. return esv1.ClusterExternalSecret{
  859. ObjectMeta: metav1.ObjectMeta{
  860. Name: created.Name,
  861. },
  862. Spec: created.Spec,
  863. Status: esv1.ClusterExternalSecretStatus{
  864. ExternalSecretName: created.Name,
  865. ProvisionedNamespaces: []string{namespaces[0].Name},
  866. Conditions: []esv1.ClusterExternalSecretStatusCondition{
  867. {
  868. Type: esv1.ClusterExternalSecretReady,
  869. Status: v1.ConditionTrue,
  870. },
  871. },
  872. },
  873. }
  874. },
  875. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1.ClusterExternalSecret) []esv1.ExternalSecret {
  876. return []esv1.ExternalSecret{
  877. {
  878. ObjectMeta: metav1.ObjectMeta{
  879. Namespace: namespaces[0].Name,
  880. Name: created.Name,
  881. },
  882. Spec: created.Spec.ExternalSecretSpec,
  883. },
  884. }
  885. },
  886. }),
  887. )
  888. })
  889. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
  890. func randString(n int) string {
  891. b := make([]rune, n)
  892. for i := range b {
  893. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  894. }
  895. return string(b)
  896. }
  897. func randomNamespaceName() string {
  898. return fmt.Sprintf("testns-%s", randString(10))
  899. }