Skip to content

Commit 6cada9f

Browse files
sfc-gh-cbandybenjaminjb
authored andcommitted
FIXUP: add a helper for kube version tests
1 parent 94a3211 commit 6cada9f

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

internal/testing/require/kubernetes.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"golang.org/x/tools/go/packages"
1616
"gotest.tools/v3/assert"
1717
corev1 "k8s.io/api/core/v1"
18+
"k8s.io/apimachinery/pkg/util/version"
19+
"k8s.io/client-go/discovery"
1820
"k8s.io/client-go/rest"
1921
"sigs.k8s.io/controller-runtime/pkg/client"
2022
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -73,6 +75,34 @@ func Kubernetes(t TestingT) client.Client {
7375
return cc
7476
}
7577

78+
// KubernetesAtLeast is the same as [Kubernetes] but also calls t.Skip when
79+
// the connected Kubernetes API is earlier than minVersion, like "1.28" or "1.27.7".
80+
func KubernetesAtLeast(t TestingT, minVersion string) client.Client {
81+
t.Helper()
82+
83+
expectedVersion, err := version.ParseGeneric(minVersion)
84+
assert.NilError(t, err)
85+
86+
// Start or connect to Kubernetes
87+
env, cc := kubernetes3(t)
88+
89+
dc, err := discovery.NewDiscoveryClientForConfig(env.Config)
90+
assert.NilError(t, err)
91+
92+
serverInfo, err := dc.ServerVersion()
93+
assert.NilError(t, err)
94+
95+
serverVersion, err := version.ParseGeneric(serverInfo.GitVersion)
96+
assert.NilError(t, err)
97+
98+
if serverVersion.LessThan(expectedVersion) {
99+
t.Log("Kubernetes version", serverVersion, "is before", expectedVersion)
100+
t.SkipNow()
101+
}
102+
103+
return cc
104+
}
105+
76106
// Kubernetes2 is the same as [Kubernetes] but also returns a copy of the client
77107
// configuration.
78108
func Kubernetes2(t TestingT) (*rest.Config, client.Client) {

internal/testing/validation/postgrescluster_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,9 @@ func TestPostgresUserInterfaceAcrossVersions(t *testing.T) {
592592
})
593593

594594
t.Run("v1 is valid with pgadmin but only if unchanged from v1beta1", func(t *testing.T) {
595+
// Validation ratcheting is enabled starting in Kubernetes 1.30
596+
require.KubernetesAtLeast(t, "1.30")
597+
595598
// A v1 that has been updated from a v1beta1 with no change to the userInterface is valid
596599
assert.NilError(t, cc.Create(ctx, base),
597600
"expected this base cluster to be valid")

pkg/apis/postgres-operator.crunchydata.com/v1/config_types_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import (
88
"strings"
99
"testing"
1010

11-
v1 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1"
1211
"gotest.tools/v3/assert"
1312
"sigs.k8s.io/yaml"
13+
14+
v1 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1"
1415
)
1516

1617
func TestOptionalConfigMapKeyRefAsProjection(t *testing.T) {

0 commit comments

Comments
 (0)