From 7edc089a07f2b274fdda94bc52305319819305c2 Mon Sep 17 00:00:00 2001 From: Tom Meadows Date: Wed, 2 Dec 2026 10:59:06 +0000 Subject: [PATCH] cmd/k8s-operator,k8s-operator: give ProxyGroups the chart imagePullSecrets (#22785) * cmd/k8s-operator,k8s-operator: give ProxyGroups the chart imagePullSecrets The operator copies the Helm chart imagePullSecrets value from the proxies ServiceAccount onto the ServiceAccount of each Service, Ingress, Connector and PeerRelay proxy, but not onto the one it creates for each ProxyGroup, so with a private registry ProxyGroup Pods cannot pull the proxy image. Its update of that ServiceAccount also removed all annotations, while OpenShift adds an annotation and a pull secret to every ServiceAccount, adds them again when they are removed, and the operator watches this one. This fixes this by adding the imagePullSecrets of the proxies ServiceAccount to the ProxyGroup ServiceAccount and keeping the annotations and pull secrets that others add. Entries removed from the chart value stay on existing ProxyGroup ServiceAccounts. Fixes #22783 Fixes #22784 Change-Id: Ided67a2972b81c1a95281cf8ef85eae34fee4a3c Signed-off-by: chaosinthecrd * cmd/k8s-operator: test ProxyGroup ServiceAccount pull secrets and annotations Adds a test that egress, ingress and kube-apiserver ProxyGroups get the imagePullSecrets of the proxies ServiceAccount without losing entries or annotations that others add, and that kube-apiserver ProxyGroups in auth mode leave the static ServiceAccount alone. It also checks that reconcile fails without creating the ServiceAccount when the proxies ServiceAccount cannot be read. Updates #22783 Updates #22784 Change-Id: Ibf73086cdf0f622cc7761cc5f0a6cee482a3b5c8 Signed-off-by: chaosinthecrd --------- Signed-off-by: chaosinthecrd --- .../deploy/chart/templates/proxy-rbac.yaml | 3 +- cmd/k8s-operator/proxygroup.go | 14 +- cmd/k8s-operator/proxygroup_specs.go | 3 +- cmd/k8s-operator/proxygroup_test.go | 176 +++++++++++++++++- k8s-operator/reconciler/tailscaled/rbac.go | 6 +- 5 files changed, 194 insertions(+), 8 deletions(-) diff --git a/cmd/k8s-operator/deploy/chart/templates/proxy-rbac.yaml b/cmd/k8s-operator/deploy/chart/templates/proxy-rbac.yaml index 455e4cc82..872d7c063 100644 --- a/cmd/k8s-operator/deploy/chart/templates/proxy-rbac.yaml +++ b/cmd/k8s-operator/deploy/chart/templates/proxy-rbac.yaml @@ -2,7 +2,8 @@ # SPDX-License-Identifier: BSD-3-Clause # The operator gives each Service, Ingress, Connector and PeerRelay proxy its own -# ServiceAccount and copies imagePullSecrets from this one onto it. +# ServiceAccount and copies imagePullSecrets from this one onto it, and onto the +# ServiceAccount of each ProxyGroup. apiVersion: v1 kind: ServiceAccount metadata: diff --git a/cmd/k8s-operator/proxygroup.go b/cmd/k8s-operator/proxygroup.go index 1513d48a7..a30718bb7 100644 --- a/cmd/k8s-operator/proxygroup.go +++ b/cmd/k8s-operator/proxygroup.go @@ -372,11 +372,21 @@ func (r *ProxyGroupReconciler) maybeProvision(ctx context.Context, tsClient tscl // ServiceAccount to keep ClusterRole creation permissions limited to the // helm chart installer. if !isAuthAPIServerProxy(pg) { - sa := pgServiceAccount(pg, r.tsNamespace) + pullSecrets, err := tailscaled.ProxiesImagePullSecrets(ctx, r.Client, r.tsNamespace) + if err != nil { + return r.notReadyErrf(pg, logger, "error provisioning ServiceAccount: %w", err) + } + + sa := pgServiceAccount(pg, r.tsNamespace, pullSecrets) if _, err := createOrUpdate(ctx, r.Client, r.tsNamespace, sa, func(s *corev1.ServiceAccount) { s.ObjectMeta.Labels = sa.ObjectMeta.Labels - s.ObjectMeta.Annotations = sa.ObjectMeta.Annotations s.ObjectMeta.OwnerReferences = sa.ObjectMeta.OwnerReferences + // Keep annotations and pull secrets added by others: OpenShift adds both to every ServiceAccount and we watch this one, so a replace would loop. + for _, ps := range sa.ImagePullSecrets { + if !slices.Contains(s.ImagePullSecrets, ps) { + s.ImagePullSecrets = append(s.ImagePullSecrets, ps) + } + } }); err != nil { return r.notReadyErrf(pg, logger, "error provisioning ServiceAccount: %w", err) } diff --git a/cmd/k8s-operator/proxygroup_specs.go b/cmd/k8s-operator/proxygroup_specs.go index 95310b3be..e010b235b 100644 --- a/cmd/k8s-operator/proxygroup_specs.go +++ b/cmd/k8s-operator/proxygroup_specs.go @@ -429,7 +429,7 @@ func kubeAPIServerStatefulSet(pg *tsapi.ProxyGroup, namespace, image string, por return sts, nil } -func pgServiceAccount(pg *tsapi.ProxyGroup, namespace string) *corev1.ServiceAccount { +func pgServiceAccount(pg *tsapi.ProxyGroup, namespace string, pullSecrets []corev1.LocalObjectReference) *corev1.ServiceAccount { return &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ Name: pg.Name, @@ -437,6 +437,7 @@ func pgServiceAccount(pg *tsapi.ProxyGroup, namespace string) *corev1.ServiceAcc Labels: pgLabels(pg.Name, nil), OwnerReferences: pgOwnerReference(pg), }, + ImagePullSecrets: pullSecrets, } } diff --git a/cmd/k8s-operator/proxygroup_test.go b/cmd/k8s-operator/proxygroup_test.go index f920926bb..baad68af4 100644 --- a/cmd/k8s-operator/proxygroup_test.go +++ b/cmd/k8s-operator/proxygroup_test.go @@ -1291,6 +1291,180 @@ func TestProxyGroupRoleRules(t *testing.T) { } } +// TestProxyGroupServiceAccountImagePullSecrets checks that every type of ProxyGroup gets the imagePullSecrets that +// the Helm chart sets on the proxies ServiceAccount. +func TestProxyGroupServiceAccount(t *testing.T) { + regcred := []corev1.LocalObjectReference{{Name: "regcred"}} + serviceAccount := func(name string, pullSecrets []corev1.LocalObjectReference) *corev1.ServiceAccount { + return &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: tsNamespace}, + ImagePullSecrets: pullSecrets, + } + } + + tests := []struct { + name string + pgType tsapi.ProxyGroupType + apiServerMode tsapi.APIServerProxyMode + existing []client.Object + interceptors interceptor.Funcs + wantErr bool + wantEvents []string + wantPullSecrets []corev1.LocalObjectReference + wantAnnotations map[string]string + }{ + { + name: "egress", + pgType: tsapi.ProxyGroupTypeEgress, + existing: []client.Object{serviceAccount("proxies", regcred)}, + wantPullSecrets: regcred, + }, + { + name: "ingress", + pgType: tsapi.ProxyGroupTypeIngress, + existing: []client.Object{serviceAccount("proxies", regcred)}, + wantPullSecrets: regcred, + }, + { + name: "kube_apiserver_noauth", + pgType: tsapi.ProxyGroupTypeKubernetesAPIServer, + apiServerMode: tsapi.APIServerProxyModeNoAuth, + existing: []client.Object{serviceAccount("proxies", regcred)}, + wantPullSecrets: regcred, + }, + { + // The Helm chart sets imagePullSecrets on the static ServiceAccount itself. + name: "kube_apiserver_auth_leaves_static_service_account", + pgType: tsapi.ProxyGroupTypeKubernetesAPIServer, + apiServerMode: tsapi.APIServerProxyModeAuth, + existing: []client.Object{ + serviceAccount("proxies", []corev1.LocalObjectReference{{Name: "other"}}), + serviceAccount(authAPIServerProxySAName, regcred), + }, + wantPullSecrets: regcred, + }, + { + name: "no_proxies_service_account", + pgType: tsapi.ProxyGroupTypeEgress, + }, + { + // OpenShift adds a pull secret to every ServiceAccount. + name: "keeps_pull_secrets_added_by_others", + pgType: tsapi.ProxyGroupTypeEgress, + existing: []client.Object{ + serviceAccount("proxies", regcred), + serviceAccount("test", []corev1.LocalObjectReference{{Name: "test-dockercfg-abcde"}}), + }, + wantPullSecrets: []corev1.LocalObjectReference{{Name: "test-dockercfg-abcde"}, {Name: "regcred"}}, + }, + { + name: "keeps_pull_secrets_added_by_others_when_proxies_service_account_has_none", + pgType: tsapi.ProxyGroupTypeEgress, + existing: []client.Object{ + serviceAccount("proxies", nil), + serviceAccount("test", []corev1.LocalObjectReference{{Name: "test-dockercfg-abcde"}}), + }, + wantPullSecrets: []corev1.LocalObjectReference{{Name: "test-dockercfg-abcde"}}, + }, + { + name: "does_not_duplicate_pull_secrets", + pgType: tsapi.ProxyGroupTypeEgress, + existing: []client.Object{ + serviceAccount("proxies", regcred), + serviceAccount("test", regcred), + }, + wantPullSecrets: regcred, + }, + { + // OpenShift 4.16 and later adds an annotation to every ServiceAccount. + name: "keeps_annotations_added_by_others", + pgType: tsapi.ProxyGroupTypeEgress, + existing: []client.Object{ + serviceAccount("proxies", regcred), + &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: tsNamespace, + Annotations: map[string]string{"openshift.io/internal-registry-pull-secret-ref": "test-dockercfg-abcde"}, + }, + }, + }, + wantPullSecrets: regcred, + wantAnnotations: map[string]string{"openshift.io/internal-registry-pull-secret-ref": "test-dockercfg-abcde"}, + }, + { + name: "fails_when_proxies_service_account_get_fails", + pgType: tsapi.ProxyGroupTypeEgress, + existing: []client.Object{serviceAccount("proxies", regcred)}, + interceptors: interceptor.Funcs{ + Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error { + if key.Name == "proxies" { + return errors.New("internal error") + } + return c.Get(ctx, key, obj, opts...) + }, + }, + wantErr: true, + wantEvents: []string{`Warning ProxyGroupCreationFailed error provisioning ServiceAccount: failed to get ServiceAccount "proxies": internal error`}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pg := &tsapi.ProxyGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Finalizers: []string{FinalizerName}, + Generation: 1, + }, + Spec: tsapi.ProxyGroupSpec{ + Type: tt.pgType, + Replicas: new(int32(1)), + }, + } + if tt.apiServerMode != "" { + pg.Spec.KubeAPIServer = &tsapi.KubeAPIServerConfig{Mode: new(tt.apiServerMode)} + } + fc := fake.NewClientBuilder(). + WithScheme(tsapi.GlobalScheme). + WithObjects(append(tt.existing, pg)...). + WithStatusSubresource(pg). + WithInterceptorFuncs(tt.interceptors). + Build() + fr := record.NewFakeRecorder(10) + r := &ProxyGroupReconciler{ + tsNamespace: tsNamespace, + tsProxyImage: testProxyImage, + Client: fc, + recorder: fr, + log: zap.NewNop().Sugar(), + clients: tsclient.NewProvider(&fakeTSClient{}), + clock: tstest.NewClock(tstest.ClockOpts{}), + reissuer: tailscaled.NewReissuer(), + } + + if tt.wantErr { + expectError(t, r, "", pg.Name) + expectEvents(t, fr, tt.wantEvents) + expectMissing[corev1.ServiceAccount](t, fc, tsNamespace, pg.Name) + return + } + expectReconciled(t, r, "", pg.Name) + + sa := new(corev1.ServiceAccount) + if err := fc.Get(t.Context(), client.ObjectKey{Namespace: tsNamespace, Name: pgServiceAccountName(pg)}, sa); err != nil { + t.Fatalf("failed to get ServiceAccount: %v", err) + } + if diff := cmp.Diff(tt.wantPullSecrets, sa.ImagePullSecrets); diff != "" { + t.Errorf("ServiceAccount imagePullSecrets mismatch (-want +got):\n%s", diff) + } + if diff := cmp.Diff(tt.wantAnnotations, sa.Annotations); diff != "" { + t.Errorf("ServiceAccount annotations mismatch (-want +got):\n%s", diff) + } + }) + } +} + func TestProxyGroupTypes(t *testing.T) { pc := &tsapi.ProxyClass{ ObjectMeta: metav1.ObjectMeta{ @@ -2457,7 +2631,7 @@ func expectProxyGroupResources(t *testing.T, fc client.WithWatch, pg *tsapi.Prox shareACMEAccount := pg.Annotations[AnnotationShareACMEAccount] == "true" role := pgRole(pg, tsNamespace, shareACMEAccount) roleBinding := pgRoleBinding(pg, tsNamespace) - serviceAccount := pgServiceAccount(pg, tsNamespace) + serviceAccount := pgServiceAccount(pg, tsNamespace, nil) statefulSet, err := pgStatefulSet(pg, tsNamespace, testProxyImage, "auto", nil, proxyClass, shareACMEAccount) if err != nil { t.Fatal(err) diff --git a/k8s-operator/reconciler/tailscaled/rbac.go b/k8s-operator/reconciler/tailscaled/rbac.go index 0d3a78288..f7496addb 100644 --- a/k8s-operator/reconciler/tailscaled/rbac.go +++ b/k8s-operator/reconciler/tailscaled/rbac.go @@ -38,7 +38,7 @@ type RBACOptions struct { // EnsureRBAC creates or updates the ServiceAccount, Role and RoleBinding for opts. The ServiceAccount gets the // imagePullSecrets of the proxies ServiceAccount, where the Helm chart sets its imagePullSecrets value. func EnsureRBAC(ctx context.Context, cl client.Client, opts RBACOptions) error { - pullSecrets, err := proxiesImagePullSecrets(ctx, cl, opts.Namespace) + pullSecrets, err := ProxiesImagePullSecrets(ctx, cl, opts.Namespace) if err != nil { return err } @@ -135,9 +135,9 @@ func newRoleBinding(opts RBACOptions) *rbacv1.RoleBinding { } } -// proxiesImagePullSecrets returns the imagePullSecrets of the proxies ServiceAccount in namespace, or nil if it does +// ProxiesImagePullSecrets returns the imagePullSecrets of the proxies ServiceAccount in namespace, or nil if it does // not exist. -func proxiesImagePullSecrets(ctx context.Context, cl client.Client, namespace string) ([]corev1.LocalObjectReference, error) { +func ProxiesImagePullSecrets(ctx context.Context, cl client.Client, namespace string) ([]corev1.LocalObjectReference, error) { var sa corev1.ServiceAccount err := cl.Get(ctx, types.NamespacedName{Namespace: namespace, Name: proxiesServiceAccountName}, &sa) switch {