clusterexternalsecret_controller_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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.MatchLabels = map[string]string{"kubernetes.io/metadata.name": namespaces[0].Name}
  138. return *ces
  139. },
  140. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  141. return esv1beta1.ClusterExternalSecret{
  142. ObjectMeta: metav1.ObjectMeta{
  143. Name: created.Name,
  144. },
  145. Spec: created.Spec,
  146. Status: esv1beta1.ClusterExternalSecretStatus{
  147. ExternalSecretName: created.Name,
  148. ProvisionedNamespaces: []string{namespaces[0].Name},
  149. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  150. {
  151. Type: esv1beta1.ClusterExternalSecretReady,
  152. Status: v1.ConditionTrue,
  153. },
  154. },
  155. },
  156. }
  157. },
  158. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  159. return []esv1beta1.ExternalSecret{
  160. {
  161. ObjectMeta: metav1.ObjectMeta{
  162. Namespace: namespaces[0].Name,
  163. Name: created.Name,
  164. },
  165. Spec: created.Spec.ExternalSecretSpec,
  166. },
  167. }
  168. },
  169. }),
  170. Entry("Should set external secret name and metadata if the fields are set", testCase{
  171. namespaces: []v1.Namespace{
  172. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  173. },
  174. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  175. ces := defaultClusterExternalSecret()
  176. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"kubernetes.io/metadata.name": namespaces[0].Name}
  177. ces.Spec.ExternalSecretName = "test-es"
  178. ces.Spec.ExternalSecretMetadata = esv1beta1.ExternalSecretMetadata{
  179. Labels: map[string]string{"test-label-key1": "test-label-value1", "test-label-key2": "test-label-value2"},
  180. Annotations: map[string]string{"test-annotation-key1": "test-annotation-value1", "test-annotation-key2": "test-annotation-value2"},
  181. }
  182. return *ces
  183. },
  184. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  185. return esv1beta1.ClusterExternalSecret{
  186. ObjectMeta: metav1.ObjectMeta{
  187. Name: created.Name,
  188. },
  189. Spec: created.Spec,
  190. Status: esv1beta1.ClusterExternalSecretStatus{
  191. ExternalSecretName: "test-es",
  192. ProvisionedNamespaces: []string{namespaces[0].Name},
  193. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  194. {
  195. Type: esv1beta1.ClusterExternalSecretReady,
  196. Status: v1.ConditionTrue,
  197. },
  198. },
  199. },
  200. }
  201. },
  202. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  203. return []esv1beta1.ExternalSecret{
  204. {
  205. ObjectMeta: metav1.ObjectMeta{
  206. Namespace: namespaces[0].Name,
  207. Name: "test-es",
  208. Labels: map[string]string{"test-label-key1": "test-label-value1", "test-label-key2": "test-label-value2"},
  209. Annotations: map[string]string{"test-annotation-key1": "test-annotation-value1", "test-annotation-key2": "test-annotation-value2"},
  210. },
  211. Spec: created.Spec.ExternalSecretSpec,
  212. },
  213. }
  214. },
  215. }),
  216. Entry("Should delete old external secrets if name has changed", testCase{
  217. namespaces: []v1.Namespace{
  218. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  219. },
  220. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  221. ces := defaultClusterExternalSecret()
  222. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"kubernetes.io/metadata.name": namespaces[0].Name}
  223. ces.Spec.ExternalSecretName = "old-es-name"
  224. return *ces
  225. },
  226. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) {
  227. // Wait until the external secret is provisioned
  228. var es esv1beta1.ExternalSecret
  229. Eventually(func(g Gomega) {
  230. key := types.NamespacedName{Namespace: namespaces[0].Name, Name: "old-es-name"}
  231. g.Expect(k8sClient.Get(ctx, key, &es)).ShouldNot(HaveOccurred())
  232. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  233. copied := created.DeepCopy()
  234. copied.Spec.ExternalSecretName = "new-es-name"
  235. Expect(k8sClient.Patch(ctx, copied, crclient.MergeFrom(created.DeepCopy()))).ShouldNot(HaveOccurred())
  236. },
  237. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  238. updatedSpec := created.Spec.DeepCopy()
  239. updatedSpec.ExternalSecretName = "new-es-name"
  240. return esv1beta1.ClusterExternalSecret{
  241. ObjectMeta: metav1.ObjectMeta{
  242. Name: created.Name,
  243. },
  244. Spec: *updatedSpec,
  245. Status: esv1beta1.ClusterExternalSecretStatus{
  246. ExternalSecretName: "new-es-name",
  247. ProvisionedNamespaces: []string{namespaces[0].Name},
  248. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  249. {
  250. Type: esv1beta1.ClusterExternalSecretReady,
  251. Status: v1.ConditionTrue,
  252. },
  253. },
  254. },
  255. }
  256. },
  257. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  258. return []esv1beta1.ExternalSecret{
  259. {
  260. ObjectMeta: metav1.ObjectMeta{
  261. Namespace: namespaces[0].Name,
  262. Name: "new-es-name",
  263. },
  264. Spec: created.Spec.ExternalSecretSpec,
  265. },
  266. }
  267. },
  268. }),
  269. Entry("Should update external secret if the fields change", testCase{
  270. namespaces: []v1.Namespace{
  271. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  272. },
  273. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  274. ces := defaultClusterExternalSecret()
  275. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"kubernetes.io/metadata.name": namespaces[0].Name}
  276. return *ces
  277. },
  278. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) {
  279. // Wait until the external secret is provisioned
  280. var es esv1beta1.ExternalSecret
  281. Eventually(func(g Gomega) {
  282. key := types.NamespacedName{Namespace: namespaces[0].Name, Name: created.Name}
  283. g.Expect(k8sClient.Get(ctx, key, &es)).ShouldNot(HaveOccurred())
  284. g.Expect(len(es.Labels)).Should(Equal(0))
  285. g.Expect(len(es.Annotations)).Should(Equal(0))
  286. g.Expect(es.Spec).Should(Equal(created.Spec.ExternalSecretSpec))
  287. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  288. copied := created.DeepCopy()
  289. copied.Spec.ExternalSecretMetadata = esv1beta1.ExternalSecretMetadata{
  290. Labels: map[string]string{"test-label-key": "test-label-value"},
  291. Annotations: map[string]string{"test-annotation-key": "test-annotation-value"},
  292. }
  293. copied.Spec.ExternalSecretSpec.SecretStoreRef.Name = "updated-test-store" //nolint:goconst
  294. Expect(k8sClient.Patch(ctx, copied, crclient.MergeFrom(created.DeepCopy()))).ShouldNot(HaveOccurred())
  295. },
  296. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  297. updatedSpec := created.Spec.DeepCopy()
  298. updatedSpec.ExternalSecretMetadata = esv1beta1.ExternalSecretMetadata{
  299. Labels: map[string]string{"test-label-key": "test-label-value"},
  300. Annotations: map[string]string{"test-annotation-key": "test-annotation-value"},
  301. }
  302. updatedSpec.ExternalSecretSpec.SecretStoreRef.Name = "updated-test-store"
  303. return esv1beta1.ClusterExternalSecret{
  304. ObjectMeta: metav1.ObjectMeta{
  305. Name: created.Name,
  306. },
  307. Spec: *updatedSpec,
  308. Status: esv1beta1.ClusterExternalSecretStatus{
  309. ExternalSecretName: created.Name,
  310. ProvisionedNamespaces: []string{namespaces[0].Name},
  311. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  312. {
  313. Type: esv1beta1.ClusterExternalSecretReady,
  314. Status: v1.ConditionTrue,
  315. },
  316. },
  317. },
  318. }
  319. },
  320. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  321. updatedSpec := created.Spec.ExternalSecretSpec.DeepCopy()
  322. updatedSpec.SecretStoreRef.Name = "updated-test-store"
  323. return []esv1beta1.ExternalSecret{
  324. {
  325. ObjectMeta: metav1.ObjectMeta{
  326. Namespace: namespaces[0].Name,
  327. Name: created.Name,
  328. Labels: map[string]string{"test-label-key": "test-label-value"},
  329. Annotations: map[string]string{"test-annotation-key": "test-annotation-value"},
  330. },
  331. Spec: *updatedSpec,
  332. },
  333. }
  334. },
  335. }),
  336. Entry("Should not overwrite existing external secrets and error out if one is present", testCase{
  337. namespaces: []v1.Namespace{
  338. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  339. },
  340. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  341. ces := defaultClusterExternalSecret()
  342. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"kubernetes.io/metadata.name": namespaces[0].Name}
  343. es := &esv1beta1.ExternalSecret{
  344. ObjectMeta: metav1.ObjectMeta{
  345. Name: ces.Name,
  346. Namespace: namespaces[0].Name,
  347. },
  348. }
  349. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  350. return *ces
  351. },
  352. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  353. return esv1beta1.ClusterExternalSecret{
  354. ObjectMeta: metav1.ObjectMeta{
  355. Name: created.Name,
  356. },
  357. Spec: created.Spec,
  358. Status: esv1beta1.ClusterExternalSecretStatus{
  359. ExternalSecretName: created.Name,
  360. FailedNamespaces: []esv1beta1.ClusterExternalSecretNamespaceFailure{
  361. {
  362. Namespace: namespaces[0].Name,
  363. Reason: "external secret already exists in namespace",
  364. },
  365. },
  366. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  367. {
  368. Type: esv1beta1.ClusterExternalSecretReady,
  369. Status: v1.ConditionFalse,
  370. Message: "one or more namespaces failed",
  371. },
  372. },
  373. },
  374. }
  375. },
  376. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  377. return []esv1beta1.ExternalSecret{
  378. {
  379. ObjectMeta: metav1.ObjectMeta{
  380. Namespace: namespaces[0].Name,
  381. Name: created.Name,
  382. },
  383. Spec: esv1beta1.ExternalSecretSpec{
  384. Target: esv1beta1.ExternalSecretTarget{
  385. CreationPolicy: "Owner",
  386. DeletionPolicy: "Retain",
  387. },
  388. RefreshInterval: &metav1.Duration{Duration: time.Hour},
  389. },
  390. },
  391. }
  392. },
  393. }),
  394. Entry("Should crate an external secret if one with the same name has been deleted", testCase{
  395. namespaces: []v1.Namespace{
  396. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  397. },
  398. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  399. ces := defaultClusterExternalSecret()
  400. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"kubernetes.io/metadata.name": namespaces[0].Name}
  401. es := &esv1beta1.ExternalSecret{
  402. ObjectMeta: metav1.ObjectMeta{
  403. Name: ces.Name,
  404. Namespace: namespaces[0].Name,
  405. },
  406. }
  407. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  408. return *ces
  409. },
  410. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) {
  411. ces := esv1beta1.ClusterExternalSecret{}
  412. Eventually(func(g Gomega) {
  413. key := types.NamespacedName{Namespace: created.Namespace, Name: created.Name}
  414. g.Expect(k8sClient.Get(ctx, key, &ces)).ShouldNot(HaveOccurred())
  415. g.Expect(len(ces.Status.FailedNamespaces)).Should(Equal(1))
  416. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  417. es := &esv1beta1.ExternalSecret{
  418. ObjectMeta: metav1.ObjectMeta{
  419. Name: ces.Name,
  420. Namespace: namespaces[0].Name,
  421. },
  422. }
  423. Expect(k8sClient.Delete(ctx, es)).ShouldNot(HaveOccurred())
  424. },
  425. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  426. return esv1beta1.ClusterExternalSecret{
  427. ObjectMeta: metav1.ObjectMeta{
  428. Name: created.Name,
  429. },
  430. Spec: created.Spec,
  431. Status: esv1beta1.ClusterExternalSecretStatus{
  432. ExternalSecretName: created.Name,
  433. ProvisionedNamespaces: []string{namespaces[0].Name},
  434. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  435. {
  436. Type: esv1beta1.ClusterExternalSecretReady,
  437. Status: v1.ConditionTrue,
  438. },
  439. },
  440. },
  441. }
  442. },
  443. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  444. return []esv1beta1.ExternalSecret{
  445. {
  446. ObjectMeta: metav1.ObjectMeta{
  447. Namespace: namespaces[0].Name,
  448. Name: created.Name,
  449. },
  450. Spec: created.Spec.ExternalSecretSpec,
  451. },
  452. }
  453. },
  454. }),
  455. Entry("Should delete external secrets when namespaces no longer match", testCase{
  456. namespaces: []v1.Namespace{
  457. {
  458. ObjectMeta: metav1.ObjectMeta{
  459. Name: randomNamespaceName(),
  460. Labels: map[string]string{"no-longer-match-label-key": "no-longer-match-label-value"},
  461. },
  462. },
  463. {
  464. ObjectMeta: metav1.ObjectMeta{
  465. Name: randomNamespaceName(),
  466. Labels: map[string]string{"no-longer-match-label-key": "no-longer-match-label-value"},
  467. },
  468. },
  469. },
  470. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  471. ces := defaultClusterExternalSecret()
  472. ces.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  473. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"no-longer-match-label-key": "no-longer-match-label-value"}
  474. return *ces
  475. },
  476. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) {
  477. // Wait until the target ESs have been created
  478. Eventually(func(g Gomega) {
  479. for _, ns := range namespaces {
  480. key := types.NamespacedName{Namespace: ns.Name, Name: created.Name}
  481. g.Expect(k8sClient.Get(ctx, key, &esv1beta1.ExternalSecret{})).ShouldNot(HaveOccurred())
  482. }
  483. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  484. namespaces[0].Labels = map[string]string{}
  485. Expect(k8sClient.Update(ctx, &namespaces[0])).ShouldNot(HaveOccurred())
  486. },
  487. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  488. return esv1beta1.ClusterExternalSecret{
  489. ObjectMeta: metav1.ObjectMeta{
  490. Name: created.Name,
  491. },
  492. Spec: created.Spec,
  493. Status: esv1beta1.ClusterExternalSecretStatus{
  494. ExternalSecretName: created.Name,
  495. ProvisionedNamespaces: []string{namespaces[1].Name},
  496. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  497. {
  498. Type: esv1beta1.ClusterExternalSecretReady,
  499. Status: v1.ConditionTrue,
  500. },
  501. },
  502. },
  503. }
  504. },
  505. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  506. return []esv1beta1.ExternalSecret{
  507. {
  508. ObjectMeta: metav1.ObjectMeta{
  509. Namespace: namespaces[1].Name,
  510. Name: created.Name,
  511. },
  512. Spec: created.Spec.ExternalSecretSpec,
  513. },
  514. }
  515. },
  516. }),
  517. Entry("Should sync with match expression", testCase{
  518. namespaces: []v1.Namespace{
  519. {
  520. ObjectMeta: metav1.ObjectMeta{
  521. Name: randomNamespaceName(),
  522. Labels: map[string]string{"prefix": "foo"},
  523. },
  524. },
  525. {
  526. ObjectMeta: metav1.ObjectMeta{
  527. Name: randomNamespaceName(),
  528. Labels: map[string]string{"prefix": "bar"},
  529. },
  530. },
  531. {
  532. ObjectMeta: metav1.ObjectMeta{
  533. Name: randomNamespaceName(),
  534. Labels: map[string]string{"prefix": "baz"},
  535. },
  536. },
  537. },
  538. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  539. ces := defaultClusterExternalSecret()
  540. ces.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  541. ces.Spec.NamespaceSelector.MatchExpressions = []metav1.LabelSelectorRequirement{
  542. {
  543. Key: "prefix",
  544. Operator: metav1.LabelSelectorOpIn,
  545. Values: []string{"foo", "bar"}, // "baz" is excluded
  546. },
  547. }
  548. return *ces
  549. },
  550. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  551. provisionedNamespaces := []string{namespaces[0].Name, namespaces[1].Name}
  552. sort.Strings(provisionedNamespaces)
  553. return esv1beta1.ClusterExternalSecret{
  554. ObjectMeta: metav1.ObjectMeta{
  555. Name: created.Name,
  556. },
  557. Spec: created.Spec,
  558. Status: esv1beta1.ClusterExternalSecretStatus{
  559. ExternalSecretName: created.Name,
  560. ProvisionedNamespaces: provisionedNamespaces,
  561. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  562. {
  563. Type: esv1beta1.ClusterExternalSecretReady,
  564. Status: v1.ConditionTrue,
  565. },
  566. },
  567. },
  568. }
  569. },
  570. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  571. return []esv1beta1.ExternalSecret{
  572. {
  573. ObjectMeta: metav1.ObjectMeta{
  574. Namespace: namespaces[0].Name,
  575. Name: created.Name,
  576. },
  577. Spec: created.Spec.ExternalSecretSpec,
  578. },
  579. {
  580. ObjectMeta: metav1.ObjectMeta{
  581. Namespace: namespaces[1].Name,
  582. Name: created.Name,
  583. },
  584. Spec: created.Spec.ExternalSecretSpec,
  585. },
  586. }
  587. },
  588. }),
  589. Entry("Should not be ready if no namespace matches", testCase{
  590. namespaces: []v1.Namespace{
  591. {
  592. ObjectMeta: metav1.ObjectMeta{
  593. Name: randomNamespaceName(),
  594. },
  595. },
  596. },
  597. clusterExternalSecret: func(namespaces []v1.Namespace) esv1beta1.ClusterExternalSecret {
  598. ces := defaultClusterExternalSecret()
  599. ces.Spec.NamespaceSelector.MatchLabels = map[string]string{"kubernetes.io/metadata.name": "no-namespace-matches"}
  600. return *ces
  601. },
  602. expectedClusterExternalSecret: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) esv1beta1.ClusterExternalSecret {
  603. return esv1beta1.ClusterExternalSecret{
  604. ObjectMeta: metav1.ObjectMeta{
  605. Name: created.Name,
  606. },
  607. Spec: created.Spec,
  608. Status: esv1beta1.ClusterExternalSecretStatus{
  609. ExternalSecretName: created.Name,
  610. Conditions: []esv1beta1.ClusterExternalSecretStatusCondition{
  611. {
  612. Type: esv1beta1.ClusterExternalSecretReady,
  613. Status: v1.ConditionFalse,
  614. Message: errNamespaceNotFound,
  615. },
  616. },
  617. },
  618. }
  619. },
  620. expectedExternalSecrets: func(namespaces []v1.Namespace, created esv1beta1.ClusterExternalSecret) []esv1beta1.ExternalSecret {
  621. return []esv1beta1.ExternalSecret{}
  622. },
  623. }))
  624. })
  625. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
  626. func randString(n int) string {
  627. b := make([]rune, n)
  628. for i := range b {
  629. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  630. }
  631. return string(b)
  632. }
  633. func randomNamespaceName() string {
  634. return fmt.Sprintf("testns-%s", randString(10))
  635. }