clusterexternalsecret_controller_test.go 32 KB

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