tailscale/k8s-operator/conditions_test.go
Tom Proctor 3099323976
cmd/k8s-operator,k8s-operator,go.{mod,sum}: publish proxy status condition for annotated services (#12463)
Adds a new TailscaleProxyReady condition type for use in corev1.Service
conditions.

Also switch our CRDs to use metav1.Condition instead of
ConnectorCondition. The Go structs are seralized identically, but it
updates some descriptions and validation rules. Update k8s
controller-tools and controller-runtime deps to fix the documentation
generation for metav1.Condition so that it excludes comments and
TODOs.

Stop expecting the fake client to populate TypeMeta in tests. See
kubernetes-sigs/controller-runtime#2633 for details of the change.

Finally, make some minor improvements to validation for service hostnames.

Fixes #12216

Co-authored-by: Irbe Krumina <irbe@tailscale.com>
Signed-off-by: Tom Proctor <tomhjp@users.noreply.github.com>
2024-06-18 19:01:40 +01:00

102 lines
2.9 KiB
Go

// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
//go:build !plan9
package kube
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
tsapi "tailscale.com/k8s-operator/apis/v1alpha1"
"tailscale.com/tstest"
)
func TestSetConnectorCondition(t *testing.T) {
cn := tsapi.Connector{}
clock := tstest.NewClock(tstest.ClockOpts{})
fakeNow := metav1.NewTime(clock.Now().Truncate(time.Second))
fakePast := metav1.NewTime(clock.Now().Truncate(time.Second).Add(-5 * time.Minute))
zl, err := zap.NewDevelopment()
assert.Nil(t, err)
// Set up a new condition
SetConnectorCondition(&cn, tsapi.ConnectorReady, metav1.ConditionTrue, "someReason", "someMsg", 1, clock, zl.Sugar())
assert.Equal(t, cn, tsapi.Connector{
Status: tsapi.ConnectorStatus{
Conditions: []metav1.Condition{
{
Type: string(tsapi.ConnectorReady),
Status: metav1.ConditionTrue,
Reason: "someReason",
Message: "someMsg",
ObservedGeneration: 1,
LastTransitionTime: fakeNow,
},
},
},
})
// Modify status of an existing condition
cn.Status = tsapi.ConnectorStatus{
Conditions: []metav1.Condition{
{
Type: string(tsapi.ConnectorReady),
Status: metav1.ConditionFalse,
Reason: "someReason",
Message: "someMsg",
ObservedGeneration: 1,
LastTransitionTime: fakePast,
},
},
}
SetConnectorCondition(&cn, tsapi.ConnectorReady, metav1.ConditionTrue, "anotherReason", "anotherMsg", 2, clock, zl.Sugar())
assert.Equal(t, cn, tsapi.Connector{
Status: tsapi.ConnectorStatus{
Conditions: []metav1.Condition{
{
Type: string(tsapi.ConnectorReady),
Status: metav1.ConditionTrue,
Reason: "anotherReason",
Message: "anotherMsg",
ObservedGeneration: 2,
LastTransitionTime: fakeNow,
},
},
},
})
// Don't modify last transition time if status hasn't changed
cn.Status = tsapi.ConnectorStatus{
Conditions: []metav1.Condition{
{
Type: string(tsapi.ConnectorReady),
Status: metav1.ConditionTrue,
Reason: "someReason",
Message: "someMsg",
ObservedGeneration: 1,
LastTransitionTime: fakePast,
},
},
}
SetConnectorCondition(&cn, tsapi.ConnectorReady, metav1.ConditionTrue, "anotherReason", "anotherMsg", 2, clock, zl.Sugar())
assert.Equal(t, cn, tsapi.Connector{
Status: tsapi.ConnectorStatus{
Conditions: []metav1.Condition{
{
Type: string(tsapi.ConnectorReady),
Status: metav1.ConditionTrue,
Reason: "anotherReason",
Message: "anotherMsg",
ObservedGeneration: 2,
LastTransitionTime: fakePast,
},
},
},
})
}