Skip to content

Commit 1eee520

Browse files
committed
e2e: Test pull secrets from other namespaces
deploy-bink now creates an extra namespace and adds it to the release's pullSecretNamespaces, and passes its name to the tests. Configuring it at deploy time, rather than from the test, avoids restarting the controller during the run, so the new test can run in parallel. In TestPullSecretNamespaces, a pool referencing a secret in a namespace the test creates must be Degraded with SecretError and claim no node; switched to a secret in the allowed namespace, its node must update through the authenticated registry. The test is skipped if the operator does not allow the namespace, e.g. when it was deployed some other way. Assisted-by: AI Signed-off-by: Alice Frosi <afrosi@redhat.com>
1 parent 317979c commit 1eee520

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

‎Makefile‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ BINK_NODE_DISK_IMAGE ?= ghcr.io/bootc-dev/bink/node:v$(DEFAULT_KUBE_MINOR)-fedor
1717
BINK_LOCAL_REGISTRY_NODE_IMAGE ?= registry.cluster.local:5000/node
1818
E2E_REGISTRY_USER ?= e2e-user
1919
E2E_REGISTRY_PASSWORD ?= e2e-password
20+
# Extra namespace deploy-bink allows pull secrets from (TestPullSecretNamespaces).
21+
E2E_PULL_SECRET_NAMESPACE ?= bootc-e2e-pull-secrets
2022
# Released operator for upgrade testing (TestOperatorUpgrade).
2123
# Override RELEASED_OPERATOR_IMG for downstream images.
2224
RELEASED_OPERATOR_TAG ?= v0.2.2
@@ -119,6 +121,7 @@ e2e: ## Run e2e tests (requires: make deploy-bink). V=1 for verbose. RUN=<regex>
119121
E2E_NODE_IMAGE_UPDATE_DIGEST=$$(skopeo inspect --tls-verify=false docker://localhost:5000/node:update | jq -r '.Digest') \
120122
E2E_NODE_IMAGE_UPDATE2_DIGEST=$$(skopeo inspect --tls-verify=false docker://localhost:5000/node:update2 | jq -r '.Digest') \
121123
E2E_REGISTRY_USER=$(E2E_REGISTRY_USER) E2E_REGISTRY_PASSWORD=$(E2E_REGISTRY_PASSWORD) \
124+
E2E_PULL_SECRET_NAMESPACE=$(E2E_PULL_SECRET_NAMESPACE) \
122125
$(if $(RELEASED_OPERATOR_TAG),E2E_OPERATOR_RELEASE_TAG=$(RELEASED_OPERATOR_TAG)) \
123126
$(if $(RELEASED_OPERATOR_IMG),E2E_OPERATOR_RELEASED_IMG=$(IMG_BINK_RELEASED)) \
124127
go test -timeout 90m -count=1 -parallel 2 $(if $(V),-v) $(if $(RUN),-run '$(RUN)') .
@@ -259,13 +262,16 @@ start-bink: seed-node-image ## Start a bink cluster (idempotent).
259262
deploy-bink: start-bink build-update-image $(if $(RELEASED_OPERATOR_IMG),push-released-operator-image) helm ## Deploy to a bink cluster (requires: buildimg).
260263
podman push --tls-verify=false $(IMG) localhost:5000/bootc-operator-e2e:latest
261264
helm dependency build ./chart/bootc-operator
265+
kubectl --kubeconfig $(KUBECONFIG_BINK) create namespace $(E2E_PULL_SECRET_NAMESPACE) \
266+
--dry-run=client -o yaml | kubectl --kubeconfig $(KUBECONFIG_BINK) apply -f -
262267
img="$(IMG_BINK)" && \
263268
helm upgrade --install bootc-operator ./chart/bootc-operator \
264269
--kubeconfig $(abspath $(KUBECONFIG_BINK)) \
265270
--create-namespace --namespace bootc-operator \
266271
--set-string image.repository="$${img%:*}" \
267272
--set-string image.tag="$${img##*:}" \
268273
--set-string image.pullPolicy=Always \
274+
--set-json 'pullSecretNamespaces=["$(E2E_PULL_SECRET_NAMESPACE)"]' \
269275
--set-json 'controller.extraArgs=["--allow-insecure-registry","--tag-resolution-interval=10s"]'
270276
kubectl --kubeconfig $(KUBECONFIG_BINK) -n bootc-operator rollout restart deployment/bootc-operator-controller
271277
kubectl --kubeconfig $(KUBECONFIG_BINK) -n bootc-operator rollout status deployment/bootc-operator-controller --timeout=3m
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package e2e
4+
5+
import (
6+
"context"
7+
"encoding/base64"
8+
"fmt"
9+
"os"
10+
"slices"
11+
"testing"
12+
"time"
13+
14+
. "github.com/onsi/gomega"
15+
corev1 "k8s.io/api/core/v1"
16+
apierrors "k8s.io/apimachinery/pkg/api/errors"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
"sigs.k8s.io/controller-runtime/pkg/client"
19+
20+
bootcv1alpha1 "github.com/bootc-dev/bootc-operator/api/v1alpha1"
21+
"github.com/bootc-dev/bootc-operator/test/e2e/e2eutil"
22+
testutil "github.com/bootc-dev/bootc-operator/test/util"
23+
)
24+
25+
// TestPullSecretNamespaces verifies that pools can only use pull secrets
26+
// from the namespaces the operator is configured to read from. The
27+
// operator must already allow E2E_PULL_SECRET_NAMESPACE (make deploy-bink
28+
// does). A pool referencing a secret in a namespace the test creates must
29+
// be Degraded without claiming its node; switched to a secret in the
30+
// allowed namespace, its node must update through the authenticated
31+
// registry.
32+
func TestPullSecretNamespaces(t *testing.T) {
33+
e2eutil.Providers(t, "bink", "eks")
34+
t.Parallel()
35+
g := NewWithT(t)
36+
g.SetDefaultEventuallyTimeout(pollTimeout)
37+
g.SetDefaultEventuallyPollingInterval(pollInterval)
38+
39+
env := e2eutil.New(t)
40+
if env.RegistryUser() == "" || env.RegistryPassword() == "" {
41+
t.Skip("E2E_REGISTRY_USER / E2E_REGISTRY_PASSWORD not set")
42+
}
43+
allowedNS := os.Getenv("E2E_PULL_SECRET_NAMESPACE")
44+
if allowedNS == "" {
45+
t.Skip("E2E_PULL_SECRET_NAMESPACE not set")
46+
}
47+
48+
ctx := context.Background()
49+
if !slices.Contains(controllerPullSecretNamespaces(t, g, ctx, env.Client), allowedNS) {
50+
t.Skipf("operator does not allow pull secrets from %q", allowedNS)
51+
}
52+
53+
deniedNS := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: env.TestID() + "-denied"}}
54+
g.Expect(env.Client.Create(ctx, deniedNS)).To(Succeed())
55+
t.Cleanup(func() { _ = env.Client.Delete(context.Background(), deniedNS) })
56+
57+
nodeName := env.AddNode(t)
58+
59+
deniedSecret := registrySecret(env, env.TestID()+"-pull-secret", deniedNS.Name)
60+
g.Expect(env.Client.Create(ctx, deniedSecret)).To(Succeed())
61+
allowedSecret := registrySecret(env, env.TestID()+"-pull-secret", allowedNS)
62+
g.Expect(env.Client.Create(ctx, allowedSecret)).To(Succeed())
63+
t.Cleanup(func() { _ = env.Client.Delete(context.Background(), allowedSecret) })
64+
65+
authImageRef := env.AuthImageRef()
66+
pool := env.NewPool("psns", authImageRef,
67+
testutil.WithPullSecret(deniedSecret.Name, deniedSecret.Namespace),
68+
)
69+
g.Expect(env.Client.Create(ctx, pool)).To(Succeed())
70+
71+
// Phase 1: the secret exists, but its namespace is not allowed.
72+
g.Eventually(func() ([]metav1.Condition, error) {
73+
var p bootcv1alpha1.BootcNodePool
74+
err := env.Client.Get(ctx, client.ObjectKeyFromObject(pool), &p)
75+
return p.Status.Conditions, err
76+
}).Should(ContainElement(And(
77+
HaveField("Type", bootcv1alpha1.PoolDegraded),
78+
HaveField("Status", metav1.ConditionTrue),
79+
HaveField("Reason", bootcv1alpha1.PoolSecretError),
80+
HaveField("Message", ContainSubstring(fmt.Sprintf("%q", deniedNS.Name))),
81+
)))
82+
g.Consistently(func() bool {
83+
var bn bootcv1alpha1.BootcNode
84+
err := env.Client.Get(ctx, client.ObjectKey{Name: nodeName}, &bn)
85+
return apierrors.IsNotFound(err)
86+
}).WithTimeout(10*time.Second).Should(BeTrue(),
87+
"no BootcNode should be created for a pool with a denied secret")
88+
89+
t.Logf("Pool %q is Degraded for secret in namespace %q", pool.Name, deniedNS.Name)
90+
91+
// Phase 2: a secret in the allowed namespace is used to pull the image.
92+
modified := pool.DeepCopy()
93+
modified.Spec.PullSecretRef = &bootcv1alpha1.PullSecretRef{
94+
Name: allowedSecret.Name, Namespace: allowedSecret.Namespace,
95+
}
96+
g.Expect(env.Client.Patch(ctx, modified, client.MergeFrom(pool))).To(Succeed())
97+
*pool = *modified
98+
99+
testutil.WaitForNodeIdle(t, g, ctx, env.Client, nodeName, 8*time.Minute,
100+
HaveField("Booted", HaveField("Image", Equal(authImageRef))),
101+
)
102+
g.Eventually(fetchPoolStatus(ctx, env.Client, pool)).
103+
Should(poolAllUpdated(1, env.NodeImageUpdateDigest()))
104+
105+
t.Logf("Node %q updated with a pull secret from namespace %q", nodeName, allowedNS)
106+
}
107+
108+
// registrySecret returns a dockerconfigjson Secret with the credentials of
109+
// the authenticated e2e registry.
110+
func registrySecret(env *e2eutil.Env, name, namespace string) *corev1.Secret {
111+
auth := base64.StdEncoding.EncodeToString(
112+
[]byte(env.RegistryUser() + ":" + env.RegistryPassword()),
113+
)
114+
return &corev1.Secret{
115+
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace},
116+
Type: corev1.SecretTypeDockerConfigJson,
117+
Data: map[string][]byte{
118+
corev1.DockerConfigJsonKey: fmt.Appendf(nil,
119+
`{"auths":{%q:{"auth":%q}}}`, env.RegistryHost(), auth),
120+
},
121+
}
122+
}

0 commit comments

Comments
 (0)