clusterexternalsecret_controller_test.go 21 KB

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