mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-02 14:42:34 -08:00
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 <[email protected]> * 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 <[email protected]> --------- Signed-off-by: chaosinthecrd <[email protected]>
This commit is contained in:
1 parent
ba01cd8c93
commit
7edc089a07
5 files changed
+194
-8
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user