cmd/k8s-operator: fix Tailscale Service API errors check (#16020)

Updates tailscale/tailscale#15895

Signed-off-by: Irbe Krumina <irbe@tailscale.com>
This commit is contained in:
Irbe Krumina 2025-05-20 11:30:45 +01:00 committed by GitHub
parent 3cc80cce6a
commit c4fb380f3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 11 deletions

View File

@ -1105,13 +1105,11 @@ func isErrorFeatureFlagNotEnabled(err error) bool {
// Tailscale control plane when a Tailscale Service API call is made for a // Tailscale control plane when a Tailscale Service API call is made for a
// tailnet that does not have the Tailscale Services feature flag enabled. // tailnet that does not have the Tailscale Services feature flag enabled.
const messageFFNotEnabled = "feature unavailable for tailnet" const messageFFNotEnabled = "feature unavailable for tailnet"
var errResp *tailscale.ErrResponse return err != nil && strings.Contains(err.Error(), messageFFNotEnabled)
ok := errors.As(err, &errResp)
return ok && strings.Contains(errResp.Message, messageFFNotEnabled)
} }
func isErrorTailscaleServiceNotFound(err error) bool { func isErrorTailscaleServiceNotFound(err error) bool {
var errResp *tailscale.ErrResponse var errResp tailscale.ErrResponse
ok := errors.As(err, &errResp) ok := errors.As(err, &errResp)
return ok && errResp.Status == http.StatusNotFound return ok && errResp.Status == http.StatusNotFound
} }

View File

@ -8,10 +8,8 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"math/rand/v2" "math/rand/v2"
"net/http"
"net/netip" "net/netip"
"testing" "testing"
@ -23,7 +21,6 @@ import (
"k8s.io/client-go/tools/record" "k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake" "sigs.k8s.io/controller-runtime/pkg/client/fake"
"tailscale.com/internal/client/tailscale"
"tailscale.com/ipn/ipnstate" "tailscale.com/ipn/ipnstate"
tsoperator "tailscale.com/k8s-operator" tsoperator "tailscale.com/k8s-operator"
tsapi "tailscale.com/k8s-operator/apis/v1alpha1" tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
@ -108,8 +105,7 @@ func TestServicePGReconciler_UpdateHostname(t *testing.T) {
if err == nil { if err == nil {
t.Fatalf("svc:default-%s not cleaned up", svc.Name) t.Fatalf("svc:default-%s not cleaned up", svc.Name)
} }
var errResp *tailscale.ErrResponse if !isErrorTailscaleServiceNotFound(err) {
if !errors.As(err, &errResp) || errResp.Status != http.StatusNotFound {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
} }

View File

@ -901,11 +901,11 @@ func (c *fakeTSClient) GetVIPService(ctx context.Context, name tailcfg.ServiceNa
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
if c.vipServices == nil { if c.vipServices == nil {
return nil, &tailscale.ErrResponse{Status: http.StatusNotFound} return nil, tailscale.ErrResponse{Status: http.StatusNotFound}
} }
svc, ok := c.vipServices[name] svc, ok := c.vipServices[name]
if !ok { if !ok {
return nil, &tailscale.ErrResponse{Status: http.StatusNotFound} return nil, tailscale.ErrResponse{Status: http.StatusNotFound}
} }
return svc, nil return svc, nil
} }