clusterpushsecret_controller_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. },
  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. },
  488. },
  489. UpdatePolicy: "Replace",
  490. Selector: v1alpha1.PushSecretSelector{
  491. Secret: &v1alpha1.PushSecretSecret{
  492. Name: secretName,
  493. },
  494. },
  495. DeletionPolicy: "None",
  496. },
  497. },
  498. }
  499. },
  500. }),
  501. Entry("Should crate an push secret if one with the same name has been deleted", clusterPushSecretTestCase{
  502. namespaces: []v1.Namespace{
  503. {ObjectMeta: metav1.ObjectMeta{Name: randomNamespaceName()}},
  504. },
  505. clusterPushSecret: func(namespaces []v1.Namespace) v1alpha1.ClusterPushSecret {
  506. pes := defaultClusterPushSecret()
  507. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  508. {
  509. MatchLabels: map[string]string{kubernetesMetadataLabel: namespaces[0].Name},
  510. },
  511. }
  512. es := &v1alpha1.PushSecret{
  513. ObjectMeta: metav1.ObjectMeta{
  514. Name: pes.Name,
  515. Namespace: namespaces[0].Name,
  516. },
  517. Spec: v1alpha1.PushSecretSpec{
  518. RefreshInterval: &metav1.Duration{Duration: time.Hour},
  519. SecretStoreRefs: []v1alpha1.PushSecretStoreRef{
  520. {
  521. Name: updateStoreName,
  522. },
  523. },
  524. Selector: v1alpha1.PushSecretSelector{
  525. Secret: &v1alpha1.PushSecretSecret{
  526. Name: secretName,
  527. },
  528. },
  529. },
  530. }
  531. Expect(k8sClient.Create(context.Background(), es)).ShouldNot(HaveOccurred())
  532. return *pes
  533. },
  534. sourceSecret: defaultSourceSecret,
  535. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) {
  536. pes := v1alpha1.ClusterPushSecret{}
  537. Eventually(func(g Gomega) {
  538. key := types.NamespacedName{Namespace: created.Namespace, Name: created.Name}
  539. g.Expect(k8sClient.Get(ctx, key, &pes)).ShouldNot(HaveOccurred())
  540. g.Expect(len(pes.Status.FailedNamespaces)).Should(Equal(1))
  541. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  542. es := &v1alpha1.PushSecret{
  543. ObjectMeta: metav1.ObjectMeta{
  544. Name: pes.Name,
  545. Namespace: namespaces[0].Name,
  546. },
  547. }
  548. Expect(k8sClient.Delete(ctx, es)).ShouldNot(HaveOccurred())
  549. },
  550. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  551. return v1alpha1.ClusterPushSecret{
  552. ObjectMeta: metav1.ObjectMeta{
  553. Name: created.Name,
  554. },
  555. Spec: created.Spec,
  556. Status: v1alpha1.ClusterPushSecretStatus{
  557. PushSecretName: created.Name,
  558. ProvisionedNamespaces: []string{namespaces[0].Name},
  559. Conditions: []v1alpha1.PushSecretStatusCondition{
  560. {
  561. Type: v1alpha1.PushSecretReady,
  562. Status: v1.ConditionTrue,
  563. },
  564. },
  565. },
  566. }
  567. },
  568. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  569. return []v1alpha1.PushSecret{
  570. {
  571. ObjectMeta: metav1.ObjectMeta{
  572. Namespace: namespaces[0].Name,
  573. Name: created.Name,
  574. },
  575. Spec: created.Spec.PushSecretSpec,
  576. },
  577. }
  578. },
  579. }),
  580. Entry("Should delete push secrets when namespaces no longer match", clusterPushSecretTestCase{
  581. namespaces: []v1.Namespace{
  582. {
  583. ObjectMeta: metav1.ObjectMeta{
  584. Name: randomNamespaceName(),
  585. Labels: map[string]string{noneMatchingAnnotationKey: noneMatchingAnnotationVal},
  586. },
  587. },
  588. {
  589. ObjectMeta: metav1.ObjectMeta{
  590. Name: randomNamespaceName(),
  591. Labels: map[string]string{noneMatchingAnnotationKey: noneMatchingAnnotationVal},
  592. },
  593. },
  594. },
  595. sourceSecret: defaultSourceSecret,
  596. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  597. pes := defaultClusterPushSecret()
  598. pes.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  599. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  600. {
  601. MatchLabels: map[string]string{noneMatchingAnnotationKey: noneMatchingAnnotationVal},
  602. },
  603. }
  604. return *pes
  605. },
  606. beforeCheck: func(ctx context.Context, namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) {
  607. // Wait until the target ESs have been created
  608. Eventually(func(g Gomega) {
  609. for _, ns := range namespaces {
  610. key := types.NamespacedName{Namespace: ns.Name, Name: created.Name}
  611. g.Expect(k8sClient.Get(ctx, key, &v1alpha1.PushSecret{})).ShouldNot(HaveOccurred())
  612. }
  613. }).WithTimeout(timeout).WithPolling(interval).Should(Succeed())
  614. namespaces[0].Labels = map[string]string{}
  615. Expect(k8sClient.Update(ctx, &namespaces[0])).ShouldNot(HaveOccurred())
  616. },
  617. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  618. return v1alpha1.ClusterPushSecret{
  619. ObjectMeta: metav1.ObjectMeta{
  620. Name: created.Name,
  621. },
  622. Spec: created.Spec,
  623. Status: v1alpha1.ClusterPushSecretStatus{
  624. PushSecretName: created.Name,
  625. ProvisionedNamespaces: []string{namespaces[1].Name},
  626. Conditions: []v1alpha1.PushSecretStatusCondition{
  627. {
  628. Type: v1alpha1.PushSecretReady,
  629. Status: v1.ConditionTrue,
  630. },
  631. },
  632. },
  633. }
  634. },
  635. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  636. return []v1alpha1.PushSecret{
  637. {
  638. ObjectMeta: metav1.ObjectMeta{
  639. Namespace: namespaces[1].Name,
  640. Name: created.Name,
  641. },
  642. Spec: created.Spec.PushSecretSpec,
  643. },
  644. }
  645. },
  646. }),
  647. Entry("Should sync with match expression", clusterPushSecretTestCase{
  648. namespaces: []v1.Namespace{
  649. {
  650. ObjectMeta: metav1.ObjectMeta{
  651. Name: randomNamespaceName(),
  652. Labels: map[string]string{"prefix": "foo"},
  653. },
  654. },
  655. {
  656. ObjectMeta: metav1.ObjectMeta{
  657. Name: randomNamespaceName(),
  658. Labels: map[string]string{"prefix": "bar"},
  659. },
  660. },
  661. {
  662. ObjectMeta: metav1.ObjectMeta{
  663. Name: randomNamespaceName(),
  664. Labels: map[string]string{"prefix": "baz"},
  665. },
  666. },
  667. },
  668. sourceSecret: defaultSourceSecret,
  669. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  670. pes := defaultClusterPushSecret()
  671. pes.Spec.RefreshInterval = &metav1.Duration{Duration: 100 * time.Millisecond}
  672. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  673. {
  674. MatchExpressions: []metav1.LabelSelectorRequirement{
  675. {
  676. Key: "prefix",
  677. Operator: metav1.LabelSelectorOpIn,
  678. Values: []string{"foo", "bar"}, // "baz" is excluded
  679. },
  680. },
  681. },
  682. }
  683. return *pes
  684. },
  685. expectedClusterPushSecret: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  686. provisionedNamespaces := []string{namespaces[0].Name, namespaces[1].Name}
  687. sort.Strings(provisionedNamespaces)
  688. return v1alpha1.ClusterPushSecret{
  689. ObjectMeta: metav1.ObjectMeta{
  690. Name: created.Name,
  691. },
  692. Spec: created.Spec,
  693. Status: v1alpha1.ClusterPushSecretStatus{
  694. PushSecretName: created.Name,
  695. ProvisionedNamespaces: provisionedNamespaces,
  696. Conditions: []v1alpha1.PushSecretStatusCondition{
  697. {
  698. Type: v1alpha1.PushSecretReady,
  699. Status: v1.ConditionTrue,
  700. },
  701. },
  702. },
  703. }
  704. },
  705. expectedPushSecrets: func(namespaces []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  706. return []v1alpha1.PushSecret{
  707. {
  708. ObjectMeta: metav1.ObjectMeta{
  709. Namespace: namespaces[0].Name,
  710. Name: created.Name,
  711. },
  712. Spec: created.Spec.PushSecretSpec,
  713. },
  714. {
  715. ObjectMeta: metav1.ObjectMeta{
  716. Namespace: namespaces[1].Name,
  717. Name: created.Name,
  718. },
  719. Spec: created.Spec.PushSecretSpec,
  720. },
  721. }
  722. },
  723. }),
  724. Entry("Should be ready if no namespace matches", clusterPushSecretTestCase{
  725. namespaces: []v1.Namespace{
  726. {
  727. ObjectMeta: metav1.ObjectMeta{
  728. Name: randomNamespaceName(),
  729. },
  730. },
  731. },
  732. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  733. pes := defaultClusterPushSecret()
  734. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  735. {
  736. MatchLabels: map[string]string{kubernetesMetadataLabel: "no-namespace-matches"},
  737. },
  738. }
  739. return *pes
  740. },
  741. sourceSecret: defaultSourceSecret,
  742. expectedClusterPushSecret: func(_ []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  743. return v1alpha1.ClusterPushSecret{
  744. ObjectMeta: metav1.ObjectMeta{
  745. Name: created.Name,
  746. },
  747. Spec: created.Spec,
  748. Status: v1alpha1.ClusterPushSecretStatus{
  749. PushSecretName: created.Name,
  750. Conditions: []v1alpha1.PushSecretStatusCondition{
  751. {
  752. Type: v1alpha1.PushSecretReady,
  753. Status: v1.ConditionTrue,
  754. },
  755. },
  756. },
  757. }
  758. },
  759. expectedPushSecrets: func([]v1.Namespace, v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  760. return []v1alpha1.PushSecret{}
  761. },
  762. }),
  763. Entry("Should be ready if namespace is selected via the namespace selectors", clusterPushSecretTestCase{
  764. namespaces: []v1.Namespace{
  765. {
  766. ObjectMeta: metav1.ObjectMeta{
  767. Name: "namespace1",
  768. Labels: map[string]string{
  769. "key": "value1",
  770. },
  771. },
  772. },
  773. {
  774. ObjectMeta: metav1.ObjectMeta{
  775. Name: "namespace2",
  776. Labels: map[string]string{
  777. "key": "value2",
  778. },
  779. },
  780. },
  781. {
  782. ObjectMeta: metav1.ObjectMeta{
  783. Name: "namespace3",
  784. Labels: map[string]string{
  785. "key": "value3",
  786. },
  787. },
  788. },
  789. },
  790. clusterPushSecret: func(_ []v1.Namespace) v1alpha1.ClusterPushSecret {
  791. pes := defaultClusterPushSecret()
  792. pes.Spec.NamespaceSelectors = []*metav1.LabelSelector{
  793. {
  794. MatchLabels: map[string]string{"key": "value1"},
  795. },
  796. {
  797. MatchLabels: map[string]string{"key": "value2"},
  798. },
  799. }
  800. return *pes
  801. },
  802. sourceSecret: defaultSourceSecret,
  803. expectedClusterPushSecret: func(_ []v1.Namespace, created v1alpha1.ClusterPushSecret) v1alpha1.ClusterPushSecret {
  804. return v1alpha1.ClusterPushSecret{
  805. ObjectMeta: metav1.ObjectMeta{
  806. Name: created.Name,
  807. },
  808. Spec: created.Spec,
  809. Status: v1alpha1.ClusterPushSecretStatus{
  810. PushSecretName: created.Name,
  811. ProvisionedNamespaces: []string{
  812. "namespace1",
  813. "namespace2",
  814. },
  815. Conditions: []v1alpha1.PushSecretStatusCondition{
  816. {
  817. Type: v1alpha1.PushSecretReady,
  818. Status: v1.ConditionTrue,
  819. },
  820. },
  821. },
  822. }
  823. },
  824. expectedPushSecrets: func(_ []v1.Namespace, created v1alpha1.ClusterPushSecret) []v1alpha1.PushSecret {
  825. return []v1alpha1.PushSecret{
  826. {
  827. ObjectMeta: metav1.ObjectMeta{
  828. Namespace: "namespace1",
  829. Name: created.Name,
  830. },
  831. Spec: created.Spec.PushSecretSpec,
  832. },
  833. {
  834. ObjectMeta: metav1.ObjectMeta{
  835. Namespace: "namespace2",
  836. Name: created.Name,
  837. },
  838. Spec: created.Spec.PushSecretSpec,
  839. },
  840. }
  841. },
  842. }))
  843. })
  844. var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz")
  845. func randString(n int) string {
  846. b := make([]rune, n)
  847. for i := range b {
  848. b[i] = letterRunes[rand.Intn(len(letterRunes))]
  849. }
  850. return string(b)
  851. }
  852. func randomNamespaceName() string {
  853. return fmt.Sprintf("testns-%s", randString(10))
  854. }