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 {