cmd/k8s-operator: migrate to tailscale-client-go-v2 (#19010)

This commit modifies the kubernetes operator to use the `tailscale-client-go-v2`
package instead of the internal tailscale client it was previously using. This
now gives us the ability to expand out custom resources and features as they
become available via the API module.

The tailnet reconciler has also been modified to manage clients as tailnets
are created and removed, providing each subsequent reconciler with a single
`ClientProvider` that obtains a tailscale client for the respective tailnet
by name, or the operator's default when presented with a blank string.

Fixes: https://github.com/tailscale/corp/issues/38418

Signed-off-by: David Bond <davidsbond93@gmail.com>
This commit is contained in:
David Bond
2026-04-09 14:39:46 +01:00
committed by GitHub
parent b25920dfc0
commit 85d6ba9473
33 changed files with 916 additions and 940 deletions
+42 -40
View File
@@ -24,8 +24,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"tailscale.com/k8s-operator/apis/v1alpha1"
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
"tailscale.com/k8s-operator/tsclient"
"tailscale.com/kube/kubetypes"
"tailscale.com/net/dns/resolvconffile"
"tailscale.com/tstest"
@@ -43,7 +45,7 @@ func TestLoadBalancerClass(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -62,7 +64,7 @@ func TestLoadBalancerClass(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationTailnetTargetFQDN: "invalid.example.com",
},
@@ -203,7 +205,7 @@ func TestLoadBalancerClass(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -223,7 +225,7 @@ func TestTailnetTargetFQDNAnnotation(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -241,7 +243,7 @@ func TestTailnetTargetFQDNAnnotation(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationTailnetTargetFQDN: tailnetTargetFQDN,
},
@@ -333,7 +335,7 @@ func TestTailnetTargetIPAnnotation(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -351,7 +353,7 @@ func TestTailnetTargetIPAnnotation(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationTailnetTargetIP: tailnetTargetIP,
},
@@ -442,7 +444,7 @@ func TestTailnetTargetIPAnnotation_IPCouldNotBeParsed(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -457,7 +459,7 @@ func TestTailnetTargetIPAnnotation_IPCouldNotBeParsed(t *testing.T) {
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationTailnetTargetIP: tailnetTargetIP,
},
@@ -510,7 +512,7 @@ func TestTailnetTargetIPAnnotation_InvalidIP(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -525,7 +527,7 @@ func TestTailnetTargetIPAnnotation_InvalidIP(t *testing.T) {
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationTailnetTargetIP: tailnetTargetIP,
},
@@ -578,7 +580,7 @@ func TestAnnotations(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -596,7 +598,7 @@ func TestAnnotations(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
"tailscale.com/expose": "true",
},
@@ -663,7 +665,7 @@ func TestAnnotations(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -682,7 +684,7 @@ func TestAnnotationIntoLB(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -700,7 +702,7 @@ func TestAnnotationIntoLB(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
"tailscale.com/expose": "true",
},
@@ -779,7 +781,7 @@ func TestAnnotationIntoLB(t *testing.T) {
Name: "test",
Namespace: "default",
Finalizers: []string{"tailscale.com/finalizer"},
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -812,7 +814,7 @@ func TestLBIntoAnnotation(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -830,7 +832,7 @@ func TestLBIntoAnnotation(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -925,7 +927,7 @@ func TestLBIntoAnnotation(t *testing.T) {
Annotations: map[string]string{
"tailscale.com/expose": "true",
},
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -947,7 +949,7 @@ func TestCustomHostname(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -965,7 +967,7 @@ func TestCustomHostname(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
"tailscale.com/expose": "true",
"tailscale.com/hostname": "reindeer-flotilla",
@@ -1034,7 +1036,7 @@ func TestCustomHostname(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
"tailscale.com/hostname": "reindeer-flotilla",
},
@@ -1056,7 +1058,7 @@ func TestCustomPriorityClassName(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1075,7 +1077,7 @@ func TestCustomPriorityClassName(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
"tailscale.com/expose": "true",
"tailscale.com/hostname": "tailscale-critical",
@@ -1212,7 +1214,7 @@ func TestServiceProxyClassAnnotation(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1308,7 +1310,7 @@ func TestProxyClassForService(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1326,7 +1328,7 @@ func TestProxyClassForService(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -1397,7 +1399,7 @@ func TestDefaultLoadBalancer(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1416,7 +1418,7 @@ func TestDefaultLoadBalancer(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -1451,7 +1453,7 @@ func TestProxyFirewallMode(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1471,7 +1473,7 @@ func TestProxyFirewallMode(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -1545,7 +1547,7 @@ func Test_HeadlessService(t *testing.T) {
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationExpose: "true",
},
@@ -1829,7 +1831,7 @@ func Test_authKeyRemoval(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1842,7 +1844,7 @@ func Test_authKeyRemoval(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
UID: types.UID("1234-UID"),
UID: "1234-UID",
},
Spec: corev1.ServiceSpec{
ClusterIP: "10.20.30.40",
@@ -1894,7 +1896,7 @@ func Test_externalNameService(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -1912,7 +1914,7 @@ func Test_externalNameService(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
AnnotationExpose: "true",
},
@@ -1988,7 +1990,7 @@ func Test_metricsResourceCreation(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
operatorNamespace: "operator-ns",
},
logger: zl.Sugar(),
@@ -2059,7 +2061,7 @@ func TestIgnorePGService(t *testing.T) {
Client: fc,
ssr: &tailscaleSTSReconciler{
Client: fc,
tsClient: ft,
clients: tsclient.NewProvider(ft),
defaultTags: []string{"tag:k8s"},
operatorNamespace: "operator-ns",
proxyImage: "tailscale/tailscale",
@@ -2077,7 +2079,7 @@ func TestIgnorePGService(t *testing.T) {
// The apiserver is supposed to set the UID, but the fake client
// doesn't. So, set it explicitly because other code later depends
// on it being set.
UID: types.UID("1234-UID"),
UID: "1234-UID",
Annotations: map[string]string{
"tailscale.com/proxygroup": "test-pg",
},