|
| 1 | +package test_core |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/loft-sh/e2e-framework/pkg/setup/cluster" |
| 7 | + "github.com/loft-sh/vcluster/e2e-next/constants" |
| 8 | + "github.com/loft-sh/vcluster/e2e-next/labels" |
| 9 | + "github.com/loft-sh/vcluster/pkg/util/random" |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + kerrors "k8s.io/apimachinery/pkg/api/errors" |
| 14 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 15 | + "k8s.io/client-go/kubernetes" |
| 16 | +) |
| 17 | + |
| 18 | +// hostSAAnnotationName is the annotation set by the SA syncer to record the virtual SA name. |
| 19 | +const hostSAAnnotationName = "vcluster.loft.sh/object-name" |
| 20 | + |
| 21 | +// hostSANamespaceLabel is the label set by the SA syncer to record the virtual namespace. |
| 22 | +const hostSANamespaceLabel = "vcluster.loft.sh/namespace" |
| 23 | + |
| 24 | +// expectedPullSecret is the imagePullSecret name configured in vcluster-sa-imagepullsecrets.yaml. |
| 25 | +const expectedPullSecret = "e2e-test-registry-secret" |
| 26 | + |
| 27 | +// selectorLabel is the label key/value that the imagePullSecretSelector matches on. |
| 28 | +const selectorLabelKey = "inject-pull-secrets" |
| 29 | +const selectorLabelValue = "true" |
| 30 | + |
| 31 | +// DescribeSAImagePullSecrets registers tests that verify workloadServiceAccount imagePullSecrets |
| 32 | +// are propagated to synced host ServiceAccounts when they match the configured selector. |
| 33 | +func DescribeSAImagePullSecrets() { |
| 34 | + Describe("ServiceAccount imagePullSecrets propagation", |
| 35 | + labels.Core, |
| 36 | + labels.PR, |
| 37 | + labels.Sync, |
| 38 | + labels.ServiceAccounts, |
| 39 | + func() { |
| 40 | + var ( |
| 41 | + hostClient kubernetes.Interface |
| 42 | + vClusterClient kubernetes.Interface |
| 43 | + ) |
| 44 | + |
| 45 | + BeforeEach(func(ctx context.Context) { |
| 46 | + hostClient = cluster.KubeClientFrom(ctx, constants.GetHostClusterName()) |
| 47 | + Expect(hostClient).NotTo(BeNil()) |
| 48 | + vClusterClient = cluster.CurrentKubeClientFrom(ctx) |
| 49 | + Expect(vClusterClient).NotTo(BeNil()) |
| 50 | + }) |
| 51 | + |
| 52 | + // findHostSA lists host ServiceAccounts for the given virtual namespace and returns |
| 53 | + // the one whose vcluster ownership annotation matches saName. |
| 54 | + findHostSA := func(ctx context.Context, g Gomega, virtualNS, saName string) *corev1.ServiceAccount { |
| 55 | + GinkgoHelper() |
| 56 | + sas, err := hostClient.CoreV1().ServiceAccounts("").List(ctx, metav1.ListOptions{ |
| 57 | + LabelSelector: hostSANamespaceLabel + "=" + virtualNS, |
| 58 | + }) |
| 59 | + g.Expect(err).To(Succeed(), "listing host SAs for virtual namespace %q", virtualNS) |
| 60 | + |
| 61 | + for i := range sas.Items { |
| 62 | + if sas.Items[i].Annotations[hostSAAnnotationName] == saName { |
| 63 | + return &sas.Items[i] |
| 64 | + } |
| 65 | + } |
| 66 | + g.Expect(false).To(BeTrue(), "host SA for virtual SA %q/%q not found yet", virtualNS, saName) |
| 67 | + return nil |
| 68 | + } |
| 69 | + |
| 70 | + It("propagates imagePullSecrets to a SA whose labels match the selector", func(ctx context.Context) { |
| 71 | + suffix := random.String(6) |
| 72 | + nsName := "sa-pull-secret-test-" + suffix |
| 73 | + saName := "sa-matching-" + suffix |
| 74 | + |
| 75 | + By("creating a virtual namespace", func() { |
| 76 | + _, err := vClusterClient.CoreV1().Namespaces().Create(ctx, |
| 77 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: nsName}}, |
| 78 | + metav1.CreateOptions{}) |
| 79 | + Expect(err).To(Succeed()) |
| 80 | + DeferCleanup(func(ctx context.Context) { |
| 81 | + err := vClusterClient.CoreV1().Namespaces().Delete(ctx, nsName, metav1.DeleteOptions{}) |
| 82 | + if !kerrors.IsNotFound(err) { |
| 83 | + Expect(err).To(Succeed()) |
| 84 | + } |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + By("creating a virtual SA with the matching label", func() { |
| 89 | + _, err := vClusterClient.CoreV1().ServiceAccounts(nsName).Create(ctx, |
| 90 | + &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{ |
| 91 | + Name: saName, |
| 92 | + Namespace: nsName, |
| 93 | + Labels: map[string]string{selectorLabelKey: selectorLabelValue}, |
| 94 | + }}, |
| 95 | + metav1.CreateOptions{}) |
| 96 | + Expect(err).To(Succeed()) |
| 97 | + }) |
| 98 | + |
| 99 | + By("waiting for the host SA to have the configured imagePullSecrets", func() { |
| 100 | + Eventually(func(g Gomega) { |
| 101 | + hostSA := findHostSA(ctx, g, nsName, saName) |
| 102 | + g.Expect(hostSA.ImagePullSecrets).To(ContainElement( |
| 103 | + corev1.LocalObjectReference{Name: expectedPullSecret}, |
| 104 | + ), "host SA imagePullSecrets should contain %q", expectedPullSecret) |
| 105 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + It("does not propagate imagePullSecrets to a SA whose labels do not match the selector", func(ctx context.Context) { |
| 110 | + suffix := random.String(6) |
| 111 | + nsName := "sa-pull-secret-test-" + suffix |
| 112 | + saName := "sa-nonmatching-" + suffix |
| 113 | + |
| 114 | + By("creating a virtual namespace", func() { |
| 115 | + _, err := vClusterClient.CoreV1().Namespaces().Create(ctx, |
| 116 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: nsName}}, |
| 117 | + metav1.CreateOptions{}) |
| 118 | + Expect(err).To(Succeed()) |
| 119 | + DeferCleanup(func(ctx context.Context) { |
| 120 | + err := vClusterClient.CoreV1().Namespaces().Delete(ctx, nsName, metav1.DeleteOptions{}) |
| 121 | + if !kerrors.IsNotFound(err) { |
| 122 | + Expect(err).To(Succeed()) |
| 123 | + } |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + By("creating a virtual SA without the matching label", func() { |
| 128 | + _, err := vClusterClient.CoreV1().ServiceAccounts(nsName).Create(ctx, |
| 129 | + &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{ |
| 130 | + Name: saName, |
| 131 | + Namespace: nsName, |
| 132 | + // no inject-pull-secrets label |
| 133 | + }}, |
| 134 | + metav1.CreateOptions{}) |
| 135 | + Expect(err).To(Succeed()) |
| 136 | + }) |
| 137 | + |
| 138 | + By("waiting for the host SA to exist and verifying it has no imagePullSecrets", func() { |
| 139 | + Consistently(func(g Gomega) { |
| 140 | + hostSA := findHostSA(ctx, g, nsName, saName) |
| 141 | + g.Expect(hostSA.ImagePullSecrets).To(BeEmpty(), |
| 142 | + "host SA imagePullSecrets should be empty for a non-matching SA") |
| 143 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeoutShort).Should(Succeed()) |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + // Ordered: the second spec removes the SA label; it must run after the first spec |
| 148 | + // has verified that imagePullSecrets were initially set. |
| 149 | + Context("when the SA label is removed after initial sync", Ordered, func() { |
| 150 | + var ( |
| 151 | + nsName string |
| 152 | + saName string |
| 153 | + ) |
| 154 | + |
| 155 | + BeforeAll(func(ctx context.Context) { |
| 156 | + suffix := random.String(6) |
| 157 | + nsName = "sa-pull-secret-test-" + suffix |
| 158 | + saName = "sa-label-removed-" + suffix |
| 159 | + |
| 160 | + _, err := vClusterClient.CoreV1().Namespaces().Create(ctx, |
| 161 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: nsName}}, |
| 162 | + metav1.CreateOptions{}) |
| 163 | + Expect(err).To(Succeed()) |
| 164 | + DeferCleanup(func(ctx context.Context) { |
| 165 | + err := vClusterClient.CoreV1().Namespaces().Delete(ctx, nsName, metav1.DeleteOptions{}) |
| 166 | + if !kerrors.IsNotFound(err) { |
| 167 | + Expect(err).To(Succeed()) |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + _, err = vClusterClient.CoreV1().ServiceAccounts(nsName).Create(ctx, |
| 172 | + &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{ |
| 173 | + Name: saName, |
| 174 | + Namespace: nsName, |
| 175 | + Labels: map[string]string{selectorLabelKey: selectorLabelValue}, |
| 176 | + }}, |
| 177 | + metav1.CreateOptions{}) |
| 178 | + Expect(err).To(Succeed()) |
| 179 | + }) |
| 180 | + |
| 181 | + It("host SA initially has imagePullSecrets while the SA matches the selector", func(ctx context.Context) { |
| 182 | + By("waiting for imagePullSecrets to appear on the host SA", func() { |
| 183 | + Eventually(func(g Gomega) { |
| 184 | + hostSA := findHostSA(ctx, g, nsName, saName) |
| 185 | + g.Expect(hostSA.ImagePullSecrets).To(ContainElement( |
| 186 | + corev1.LocalObjectReference{Name: expectedPullSecret}, |
| 187 | + ), "host SA should have imagePullSecrets while selector label is set") |
| 188 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 189 | + }) |
| 190 | + }) |
| 191 | + |
| 192 | + It("host SA loses imagePullSecrets after the selector label is removed", func(ctx context.Context) { |
| 193 | + By("removing the selector label from the virtual SA", func() { |
| 194 | + sa, err := vClusterClient.CoreV1().ServiceAccounts(nsName).Get(ctx, saName, metav1.GetOptions{}) |
| 195 | + Expect(err).To(Succeed()) |
| 196 | + delete(sa.Labels, selectorLabelKey) |
| 197 | + _, err = vClusterClient.CoreV1().ServiceAccounts(nsName).Update(ctx, sa, metav1.UpdateOptions{}) |
| 198 | + Expect(err).To(Succeed()) |
| 199 | + }) |
| 200 | + |
| 201 | + By("waiting for imagePullSecrets to be cleared on the host SA", func() { |
| 202 | + Eventually(func(g Gomega) { |
| 203 | + hostSA := findHostSA(ctx, g, nsName, saName) |
| 204 | + g.Expect(hostSA.ImagePullSecrets).To(BeEmpty(), |
| 205 | + "host SA imagePullSecrets should be cleared after selector label is removed") |
| 206 | + }).WithPolling(constants.PollingInterval).WithTimeout(constants.PollingTimeout).Should(Succeed()) |
| 207 | + }) |
| 208 | + }) |
| 209 | + }) |
| 210 | + }, |
| 211 | + ) |
| 212 | +} |
0 commit comments