clusterpushsecret_controller_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. var result []v1.Secret
  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. var namespaces []v1.Namespace
  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
  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. },
  378. }
  379. return v1alpha1.ClusterPushSecret{
  380. ObjectMeta: metav1.ObjectMeta{
  381. Name: created.Name,
  382. },
  383. Spec: *updatedSpec,
  384. Status: v1alpha1.ClusterPushSecretStatus{
  385. PushSecretName: created.Name,
  386. ProvisionedNamespaces: []string{namespaces[0].Name},
  387. Conditions: []v1alpha1.PushSecretStatusCondition{
  388. {
  389. Type: v1alpha1.PushSecretReady,
  390. Status: v1.ConditionTrue,
  391. },
  392. },
  393. },
  394. }
  395. },
  396. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  397. updatedSpec := created.Spec.PushSecretSpec.DeepCopy()
  398. updatedSpec.SecretStoreRefs = []v1alpha1.PushSecretStoreRef{
  399. {
  400. Name: updateStoreName,
  401. Kind: "SecretStore",
  402. },
  403. }
  404. return []v1alpha1.PushSecret{
  405. {
  406. ObjectMeta: metav1.ObjectMeta{
  407. Namespace: namespaces[0].Name,
  408. Name: created.Name,
  409. Labels: map[string]string{testLabelKey: testLabelValue},
  410. Annotations: map[string]string{testAnnotationKey: testAnnotationValue},
  411. },
  412. Spec: *updatedSpec,
  413. },
  414. }
  415. },
  416. }),
  417. Entry("Should not overwrite existing push secrets and error out if one is present", clusterPushSecretTestCase{
  418. namespaces: []v1.Namespace{
  419. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  420. },
  421. clusterPushSecret: func(namespaces []v1.Namespace) v1alpha1.ClusterPushSecret {
  422. pes := defaultClusterPushSecret()
  423. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  424. {
  425. MatchLabels: map[string]string{kubernetesMetadataLabel: namespaces[0].Name},
  426. },
  427. }
  428. es := &v1alpha1.PushSecret{
  429. ObjectMeta: metav1.ObjectMeta{
  430. Name: pes.Name,
  431. Namespace: namespaces[0].Name,
  432. },
  433. Spec: v1alpha1.PushSecretSpec{
  434. RefreshInterval: &metav1.Duration{Duration: time.Hour},
  435. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  436. {
  437. Name: updateStoreName,
  438. },
  439. },
  440. Selector: v1alpha1.PushSecretSelector{
  441. Secret: &v1alpha1.PushSecretSecret{
  442. Name: secretName,
  443. },
  444. },
  445. },
  446. }
  447. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  448. return *pes
  449. },
  450. sourceSecret: defaultSourceSecret,
  451. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  452. return v1alpha1.ClusterPushSecret{
  453. ObjectMeta: metav1.ObjectMeta{
  454. Name: created.Name,
  455. },
  456. Spec: created.Spec,
  457. Status: v1alpha1.ClusterPushSecretStatus{
  458. PushSecretName: created.Name,
  459. FailedNamespaces: []v1alpha1.ClusterPushSecretNamespaceFailure{
  460. {
  461. Namespace: namespaces[0].Name,
  462. Reason: "push secret already exists in namespace",
  463. },
  464. },
  465. Conditions: []v1alpha1.PushSecretStatusCondition{
  466. {
  467. Type: v1alpha1.PushSecretReady,
  468. Status: v1.ConditionFalse,
  469. Message: "one or more namespaces failed",
  470. },
  471. },
  472. },
  473. }
  474. },
  475. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  476. return []v1alpha1.PushSecret{
  477. {
  478. ObjectMeta: metav1.ObjectMeta{
  479. Namespace: namespaces[0].Name,
  480. Name: created.Name,
  481. },
  482. Spec: v1alpha1.PushSecretSpec{
  483. RefreshInterval: &metav1.Duration{Duration: time.Hour},
  484. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  485. {
  486. Name: updateStoreName,
  487. Kind: "SecretStore",
  488. },
  489. },
  490. UpdatePolicy: "Replace",
  491. Selector: v1alpha1.PushSecretSelector{
  492. Secret: &v1alpha1.PushSecretSecret{
  493. Name: secretName,
  494. },
  495. },
  496. DeletionPolicy: "None",
  497. },
  498. },
  499. }
  500. },
  501. }),
  502. Entry("Should crate an push secret if one with the same name has been deleted", clusterPushSecretTestCase{
  503. namespaces: []v1.Namespace{
  504. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  505. },
  506. clusterPushSecret: func(namespaces []v1.Namespace) v1alpha1.ClusterPushSecret {
  507. pes := defaultClusterPushSecret()
  508. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  509. {
  510. MatchLabels: map[string]string{kubernetesMetadataLabel: namespaces[0].Name},
  511. },
  512. }
  513. es := &v1alpha1.PushSecret{
  514. ObjectMeta: metav1.ObjectMeta{
  515. Name: pes.Name,
  516. Namespace: namespaces[0].Name,
  517. },
  518. Spec: v1alpha1.PushSecretSpec{
  519. RefreshInterval: &metav1.Duration{Duration: time.Hour},
  520. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  521. {
  522. Name: updateStoreName,
  523. },
  524. },
  525. Selector: v1alpha1.PushSecretSelector{
  526. Secret: &v1alpha1.PushSecretSecret{
  527. Name: secretName,
  528. },
  529. },
  530. },
  531. }
  532. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  533. return *pes
  534. },
  535. sourceSecret: defaultSourceSecret,
  536. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) {
  537. pes := v1alpha1.ClusterPushSecret{}
  538. Eventually(func(g Gomega) {
  539. key := types.NamespacedName{Namespace: created.Namespace, Name: created.Name}
  540. g.Expect(k8sClient.Get(ctx, key, &pes)).ShouldNot(HaveOccurred())
  541. g.Expect(len(pes.Status.FailedNamespaces)).Should(Equal(1))
  542. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  543. es := &v1alpha1.PushSecret{
  544. ObjectMeta: metav1.ObjectMeta{
  545. Name: pes.Name,
  546. Namespace: namespaces[0].Name,
  547. },
  548. }
  549. Expect(k8sClient.Delete(ctx, es)).ShouldNot(HaveOccurred())
  550. },
  551. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  552. return v1alpha1.ClusterPushSecret{
  553. ObjectMeta: metav1.ObjectMeta{
  554. Name: created.Name,
  555. },
  556. Spec: created.Spec,
  557. Status: v1alpha1.ClusterPushSecretStatus{
  558. PushSecretName: created.Name,
  559. ProvisionedNamespaces: []string{namespaces[0].Name},
  560. Conditions: []v1alpha1.PushSecretStatusCondition{
  561. {
  562. Type: v1alpha1.PushSecretReady,
  563. Status: v1.ConditionTrue,
  564. },
  565. },
  566. },
  567. }
  568. },
  569. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  570. return []v1alpha1.PushSecret{
  571. {
  572. ObjectMeta: metav1.ObjectMeta{
  573. Namespace: namespaces[0].Name,
  574. Name: created.Name,
  575. },
  576. Spec: created.Spec.PushSecretSpec,
  577. },
  578. }
  579. },
  580. }),
  581. Entry("Should delete push secrets when namespaces no longer match", clusterPushSecretTestCase{
  582. namespaces: []v1.Namespace{
  583. {
  584. ObjectMeta: metav1.ObjectMeta{
  585. Name: randomNamespaceName(),
  586. Labels: map[string]string{noneMatchingAnnotationKey: noneMatchingAnnotationVal},
  587. },
  588. },
  589. {
  590. ObjectMeta: metav1.ObjectMeta{
  591. Name: randomNamespaceName(),
  592. Labels: map[string]string{noneMatchingAnnotationKey: noneMatchingAnnotationVal},
  593. },
  594. },
  595. },
  596. sourceSecret: defaultSourceSecret,
  597. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  598. pes := defaultClusterPushSecret()
  599. pes.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  600. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  601. {
  602. MatchLabels: map[string]string{noneMatchingAnnotationKey: noneMatchingAnnotationVal},
  603. },
  604. }
  605. return *pes
  606. },
  607. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) {
  608. // Wait until the target ESs have been created
  609. Eventually(func(g Gomega) {
  610. for _, ns := range namespaces {
  611. key := types.NamespacedName{Namespace: ns.Name, Name: created.Name}
  612. g.Expect(k8sClient.Get(ctx, key, &v1alpha1.PushSecret{})).ShouldNot(HaveOccurred())
  613. }
  614. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  615. namespaces[0].Labels = map[string]string{}
  616. Expect(k8sClient.Update(ctx, &namespaces[0])).ShouldNot(HaveOccurred())
  617. },
  618. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  619. return v1alpha1.ClusterPushSecret{
  620. ObjectMeta: metav1.ObjectMeta{
  621. Name: created.Name,
  622. },
  623. Spec: created.Spec,
  624. Status: v1alpha1.ClusterPushSecretStatus{
  625. PushSecretName: created.Name,
  626. ProvisionedNamespaces: []string{namespaces[1].Name},
  627. Conditions: []v1alpha1.PushSecretStatusCondition{
  628. {
  629. Type: v1alpha1.PushSecretReady,
  630. Status: v1.ConditionTrue,
  631. },
  632. },
  633. },
  634. }
  635. },
  636. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  637. return []v1alpha1.PushSecret{
  638. {
  639. ObjectMeta: metav1.ObjectMeta{
  640. Namespace: namespaces[1].Name,
  641. Name: created.Name,
  642. },
  643. Spec: created.Spec.PushSecretSpec,
  644. },
  645. }
  646. },
  647. }),
  648. Entry("Should sync with match expression", clusterPushSecretTestCase{
  649. namespaces: []v1.Namespace{
  650. {
  651. ObjectMeta: metav1.ObjectMeta{
  652. Name: randomNamespaceName(),
  653. Labels: map[string]string{"prefix": "foo"},
  654. },
  655. },
  656. {
  657. ObjectMeta: metav1.ObjectMeta{
  658. Name: randomNamespaceName(),
  659. Labels: map[string]string{"prefix": "bar"},
  660. },
  661. },
  662. {
  663. ObjectMeta: metav1.ObjectMeta{
  664. Name: randomNamespaceName(),
  665. Labels: map[string]string{"prefix": "baz"},
  666. },
  667. },
  668. },
  669. sourceSecret: defaultSourceSecret,
  670. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  671. pes := defaultClusterPushSecret()
  672. pes.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  673. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  674. {
  675. MatchExpressions: []metav1.LabelSelectorRequirement{
  676. {
  677. Key: "prefix",
  678. Operator: metav1.LabelSelectorOpIn,
  679. Values: []string{"foo", "bar"}, // "baz" is excluded
  680. },
  681. },
  682. },
  683. }
  684. return *pes
  685. },
  686. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  687. provisionedNamespaces := []string{namespaces[0].Name, namespaces[1].Name}
  688. sort.Strings(provisionedNamespaces)
  689. return v1alpha1.ClusterPushSecret{
  690. ObjectMeta: metav1.ObjectMeta{
  691. Name: created.Name,
  692. },
  693. Spec: created.Spec,
  694. Status: v1alpha1.ClusterPushSecretStatus{
  695. PushSecretName: created.Name,
  696. ProvisionedNamespaces: provisionedNamespaces,
  697. Conditions: []v1alpha1.PushSecretStatusCondition{
  698. {
  699. Type: v1alpha1.PushSecretReady,
  700. Status: v1.ConditionTrue,
  701. },
  702. },
  703. },
  704. }
  705. },
  706. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  707. return []v1alpha1.PushSecret{
  708. {
  709. ObjectMeta: metav1.ObjectMeta{
  710. Namespace: namespaces[0].Name,
  711. Name: created.Name,
  712. },
  713. Spec: created.Spec.PushSecretSpec,
  714. },
  715. {
  716. ObjectMeta: metav1.ObjectMeta{
  717. Namespace: namespaces[1].Name,
  718. Name: created.Name,
  719. },
  720. Spec: created.Spec.PushSecretSpec,
  721. },
  722. }
  723. },
  724. }),
  725. Entry("Should be ready if no namespace matches", clusterPushSecretTestCase{
  726. namespaces: []v1.Namespace{
  727. {
  728. ObjectMeta: metav1.ObjectMeta{
  729. Name: randomNamespaceName(),
  730. },
  731. },
  732. },
  733. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  734. pes := defaultClusterPushSecret()
  735. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  736. {
  737. MatchLabels: map[string]string{kubernetesMetadataLabel: "no-namespace-matches"},
  738. },
  739. }
  740. return *pes
  741. },
  742. sourceSecret: defaultSourceSecret,
  743. expectedClusterPushSecret: func(_ []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  744. return v1alpha1.ClusterPushSecret{
  745. ObjectMeta: metav1.ObjectMeta{
  746. Name: created.Name,
  747. },
  748. Spec: created.Spec,
  749. Status: v1alpha1.ClusterPushSecretStatus{
  750. PushSecretName: created.Name,
  751. Conditions: []v1alpha1.PushSecretStatusCondition{
  752. {
  753. Type: v1alpha1.PushSecretReady,
  754. Status: v1.ConditionTrue,
  755. },
  756. },
  757. },
  758. }
  759. },
  760. expectedPushSecrets: func([]v1.Namespace, v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  761. return []v1alpha1.PushSecret{}
  762. },
  763. }),
  764. Entry("Should be ready if namespace is selected via the namespace selectors", clusterPushSecretTestCase{
  765. namespaces: []v1.Namespace{
  766. {
  767. ObjectMeta: metav1.ObjectMeta{
  768. Name: "namespace1",
  769. Labels: map[string]string{
  770. "key": "value1",
  771. },
  772. },
  773. },
  774. {
  775. ObjectMeta: metav1.ObjectMeta{
  776. Name: "namespace2",
  777. Labels: map[string]string{
  778. "key": "value2",
  779. },
  780. },
  781. },
  782. {
  783. ObjectMeta: metav1.ObjectMeta{
  784. Name: "namespace3",
  785. Labels: map[string]string{
  786. "key": "value3",
  787. },
  788. },
  789. },
  790. },
  791. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  792. pes := defaultClusterPushSecret()
  793. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  794. {
  795. MatchLabels: map[string]string{"key": "value1"},
  796. },
  797. {
  798. MatchLabels: map[string]string{"key": "value2"},
  799. },
  800. }
  801. return *pes
  802. },
  803. sourceSecret: defaultSourceSecret,
  804. expectedClusterPushSecret: func(_ []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  805. return v1alpha1.ClusterPushSecret{
  806. ObjectMeta: metav1.ObjectMeta{
  807. Name: created.Name,
  808. },
  809. Spec: created.Spec,
  810. Status: v1alpha1.ClusterPushSecretStatus{
  811. PushSecretName: created.Name,
  812. ProvisionedNamespaces: []string{
  813. "namespace1",
  814. "namespace2",
  815. },
  816. Conditions: []v1alpha1.PushSecretStatusCondition{
  817. {
  818. Type: v1alpha1.PushSecretReady,
  819. Status: v1.ConditionTrue,
  820. },
  821. },
  822. },
  823. }
  824. },
  825. expectedPushSecrets: func(_ []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  826. return []v1alpha1.PushSecret{
  827. {
  828. ObjectMeta: metav1.ObjectMeta{
  829. Namespace: "namespace1",
  830. Name: created.Name,
  831. },
  832. Spec: created.Spec.PushSecretSpec,
  833. },
  834. {
  835. ObjectMeta: metav1.ObjectMeta{
  836. Namespace: "namespace2",
  837. Name: created.Name,
  838. },
  839. Spec: created.Spec.PushSecretSpec,
  840. },
  841. }
  842. },
  843. }))
  844. })
  845. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
  846. func randString(n int) string {
  847. b := make([]rune, n)
  848. for i := range b {
  849. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  850. }
  851. return string(b)
  852. }
  853. func randomNamespaceName() string {
  854. return fmt.Sprintf("testns-%s", randString(10))
  855. }