clusterexternalsecret_controller_test.go 26 KB

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