clusterpushsecret_controller_test.go 29 KB

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