From 7d36fe9e2ccea387451230bee86da7a89941fb65 Mon Sep 17 00:00:00 2001 From: Knative Prow Robot Date: Thu, 19 Oct 2023 08:51:41 +0100 Subject: [PATCH] Copy pull secrets to SA for eventshub (#615) Co-authored-by: Martin Gencur --- pkg/environment/namespace.go | 20 +++++++++++++++++--- pkg/eventshub/rbac/100-sa.yaml | 6 ++++++ pkg/eventshub/rbac/rbac.go | 27 ++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/pkg/environment/namespace.go b/pkg/environment/namespace.go index 18c73c8e..939f382e 100644 --- a/pkg/environment/namespace.go +++ b/pkg/environment/namespace.go @@ -122,12 +122,26 @@ func (mr *MagicEnvironment) CreateNamespaceIfNeeded() error { return fmt.Errorf("error copying the image pull Secret: %s", err) } - _, err = c.CoreV1().ServiceAccounts(mr.namespace).Patch(context.Background(), sa.Name, types.StrategicMergePatchType, - []byte(`{"imagePullSecrets":[{"name":"`+mr.imagePullSecretName+`"}]}`), metav1.PatchOptions{}) + for _, secret := range sa.ImagePullSecrets { + if secret.Name == mr.imagePullSecretName { + return nil + } + } + + // Prevent overwriting existing imagePullSecrets + patch := `[{"op":"add","path":"/imagePullSecrets/-","value":{"name":"` + mr.imagePullSecretName + `"}}]` + if len(sa.ImagePullSecrets) == 0 { + patch = `[{"op":"add","path":"/imagePullSecrets","value":[{"name":"` + mr.imagePullSecretName + `"}]}]` + } + + _, err = c.CoreV1().ServiceAccounts(mr.namespace).Patch(context.Background(), sa.Name, types.JSONPatchType, + []byte(patch), metav1.PatchOptions{}) if err != nil { - return fmt.Errorf("patch failed on NS/SA (%s/%s): %s", mr.namespace, sa.Name, err) + return fmt.Errorf("patch failed on NS/SA (%s/%s): %w", + mr.namespace, sa.Name, err) } } + return nil } diff --git a/pkg/eventshub/rbac/100-sa.yaml b/pkg/eventshub/rbac/100-sa.yaml index f86b5239..2cafc9ab 100644 --- a/pkg/eventshub/rbac/100-sa.yaml +++ b/pkg/eventshub/rbac/100-sa.yaml @@ -17,3 +17,9 @@ kind: ServiceAccount metadata: name: {{ .name }} namespace: {{ .namespace }} +{{ if .withPullSecrets }} +imagePullSecrets: + {{ range $_, $value := .withPullSecrets.secrets }} + - name: {{ $value }} + {{ end }} +{{ end }} diff --git a/pkg/eventshub/rbac/rbac.go b/pkg/eventshub/rbac/rbac.go index de8a2cfb..5c749423 100644 --- a/pkg/eventshub/rbac/rbac.go +++ b/pkg/eventshub/rbac/rbac.go @@ -21,6 +21,9 @@ import ( "embed" apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kubeclient "knative.dev/pkg/client/injection/kube/client" + "knative.dev/reconciler-test/pkg/environment" "knative.dev/reconciler-test/pkg/feature" "knative.dev/reconciler-test/pkg/manifest" @@ -30,11 +33,33 @@ import ( var templates embed.FS // Install creates the necessary ServiceAccount, Role, RoleBinding for the eventshub. -// The resources are named according to the current namespace defined in the environment. func Install(cfg map[string]interface{}) feature.StepFn { return func(ctx context.Context, t feature.T) { + WithPullSecrets(ctx, t)(cfg) if _, err := manifest.InstallYamlFS(ctx, templates, cfg); err != nil && !apierrors.IsAlreadyExists(err) { t.Fatal(err) } } } + +func WithPullSecrets(ctx context.Context, t feature.T) manifest.CfgFn { + namespace := environment.FromContext(ctx).Namespace() + serviceAccount, err := kubeclient.Get(ctx).CoreV1().ServiceAccounts(namespace).Get(ctx, "default", metav1.GetOptions{}) + if err != nil { + t.Fatalf("Failed to read default SA in %s namespace: %v", namespace, err) + } + + return func(cfg map[string]interface{}) { + if len(serviceAccount.ImagePullSecrets) == 0 { + return + } + if _, set := cfg["withPullSecrets"]; !set { + cfg["withPullSecrets"] = map[string]interface{}{} + } + withPullSecrets := cfg["withPullSecrets"].(map[string]interface{}) + withPullSecrets["secrets"] = []string{} + for _, secret := range serviceAccount.ImagePullSecrets { + withPullSecrets["secrets"] = append(withPullSecrets["secrets"].([]string), secret.Name) + } + } +}