Skip to content

Commit 0e8e7e3

Browse files
committed
Remove server-side apply tests for old Kubernetes
Kubernetes 1.22 and OpenShift 4.8 have been out of commission for some time now.
1 parent 44edf36 commit 0e8e7e3

File tree

4 files changed

+29
-171
lines changed

4 files changed

+29
-171
lines changed

internal/controller/postgrescluster/apply.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ func (r *Reconciler) apply(ctx context.Context, object client.Object) error {
5454
func applyServiceSpec(
5555
patch *kubeapi.JSON6902, actual, intent corev1.ServiceSpec, path ...string,
5656
) {
57-
// Service.Spec.Selector is not +mapType=atomic until Kubernetes 1.22.
58-
// - https://issue.k8s.io/97970
57+
// Service.Spec.Selector cannot be unset; perhaps https://issue.k8s.io/117447
5958
if !equality.Semantic.DeepEqual(actual.Selector, intent.Selector) {
6059
patch.Replace(append(path, "selector")...)(intent.Selector)
6160
}

internal/controller/postgrescluster/apply_test.go

Lines changed: 3 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/google/go-cmp/cmp"
1616
"gotest.tools/v3/assert"
17-
appsv1 "k8s.io/api/apps/v1"
1817
corev1 "k8s.io/api/core/v1"
1918
"k8s.io/apimachinery/pkg/api/equality"
2019
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -144,41 +143,6 @@ func TestServerSideApply(t *testing.T) {
144143
)
145144
})
146145

147-
t.Run("StatefulSetStatus", func(t *testing.T) {
148-
constructor := func(name string) *appsv1.StatefulSet {
149-
var sts appsv1.StatefulSet
150-
sts.SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind("StatefulSet"))
151-
sts.Namespace, sts.Name = ns.Name, name
152-
sts.Spec.Selector = &metav1.LabelSelector{
153-
MatchLabels: map[string]string{"select": name},
154-
}
155-
sts.Spec.Template.Labels = map[string]string{"select": name}
156-
sts.Spec.Template.Spec.Containers = []corev1.Container{{Name: "test", Image: "test"}}
157-
return &sts
158-
}
159-
160-
cc := client.WithFieldOwner(cc, t.Name())
161-
reconciler := Reconciler{Writer: cc}
162-
upstream := constructor("status-upstream")
163-
164-
// The structs defined in "k8s.io/api/apps/v1" marshal empty status fields.
165-
switch {
166-
case serverVersion.LessThan(version.MustParseGeneric("1.22")):
167-
assert.ErrorContains(t,
168-
cc.Patch(ctx, upstream, client.Apply, client.ForceOwnership),
169-
"field not declared in schema",
170-
"expected https://issue.k8s.io/109210")
171-
172-
default:
173-
assert.NilError(t,
174-
cc.Patch(ctx, upstream, client.Apply, client.ForceOwnership))
175-
}
176-
177-
// Our apply method generates the correct apply-patch.
178-
again := constructor("status-local")
179-
assert.NilError(t, reconciler.apply(ctx, again))
180-
})
181-
182146
t.Run("ServiceSelector", func(t *testing.T) {
183147
constructor := func(name string) *corev1.Service {
184148
var service corev1.Service
@@ -190,61 +154,6 @@ func TestServerSideApply(t *testing.T) {
190154
return &service
191155
}
192156

193-
t.Run("wrong-keys", func(t *testing.T) {
194-
cc := client.WithFieldOwner(cc, t.Name())
195-
reconciler := Reconciler{Writer: cc}
196-
197-
intent := constructor("some-selector")
198-
intent.Spec.Selector = map[string]string{"k1": "v1"}
199-
200-
// Create the Service.
201-
before := intent.DeepCopy()
202-
assert.NilError(t,
203-
cc.Patch(ctx, before, client.Apply, client.ForceOwnership))
204-
205-
// Something external mucks it up.
206-
assert.NilError(t,
207-
cc.Patch(ctx, before,
208-
client.RawPatch(client.Merge.Type(), []byte(`{"spec":{"selector":{"bad":"v2"}}}`)),
209-
client.FieldOwner("wrong")))
210-
211-
// client.Apply cannot correct it in old versions of Kubernetes.
212-
after := intent.DeepCopy()
213-
assert.NilError(t,
214-
cc.Patch(ctx, after, client.Apply, client.ForceOwnership))
215-
216-
switch {
217-
case serverVersion.LessThan(version.MustParseGeneric("1.22")):
218-
219-
assert.Assert(t, len(after.Spec.Selector) != len(intent.Spec.Selector),
220-
"expected https://issue.k8s.io/97970, got %v", after.Spec.Selector)
221-
222-
default:
223-
assert.DeepEqual(t, after.Spec.Selector, intent.Spec.Selector)
224-
}
225-
226-
// Our apply method corrects it.
227-
again := intent.DeepCopy()
228-
assert.NilError(t, reconciler.apply(ctx, again))
229-
assert.DeepEqual(t, again.Spec.Selector, intent.Spec.Selector)
230-
231-
var count int
232-
var managed *metav1.ManagedFieldsEntry
233-
for i := range again.ManagedFields {
234-
if again.ManagedFields[i].Manager == t.Name() {
235-
count++
236-
managed = &again.ManagedFields[i]
237-
}
238-
}
239-
240-
assert.Equal(t, count, 1, "expected manager once in %v", again.ManagedFields)
241-
assert.Equal(t, managed.Operation, metav1.ManagedFieldsOperationApply)
242-
243-
assert.Assert(t, managed.FieldsV1 != nil)
244-
assert.Assert(t, strings.Contains(string(managed.FieldsV1.Raw), `"f:selector":{`),
245-
"expected f:selector in %s", managed.FieldsV1.Raw)
246-
})
247-
248157
for _, tt := range []struct {
249158
name string
250159
selector map[string]string
@@ -275,6 +184,9 @@ func TestServerSideApply(t *testing.T) {
275184
assert.NilError(t,
276185
cc.Patch(ctx, after, client.Apply, client.ForceOwnership))
277186

187+
// Perhaps one of:
188+
// - https://issue.k8s.io/117447
189+
// - https://github.com/kubernetes-sigs/structured-merge-diff/issues/259
278190
assert.Assert(t, len(after.Spec.Selector) != len(intent.Spec.Selector),
279191
"got %v", after.Spec.Selector)
280192

internal/controller/postgrescluster/controller_test.go

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"k8s.io/apimachinery/pkg/api/meta"
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
"k8s.io/apimachinery/pkg/util/rand"
24-
"k8s.io/apimachinery/pkg/util/version"
2524
"k8s.io/client-go/tools/record"
2625
"sigs.k8s.io/controller-runtime/pkg/client"
2726
"sigs.k8s.io/controller-runtime/pkg/reconcile"
@@ -340,59 +339,32 @@ spec:
340339
//
341340
// The "metadata.finalizers" field is also okay.
342341
// - https://book.kubebuilder.io/reference/using-finalizers.html
343-
//
344-
// NOTE(cbandy): Kubernetes prior to v1.16.10 and v1.17.6 does not track
345-
// managed fields on the status subresource: https://issue.k8s.io/88901
346-
switch {
347-
case suite.ServerVersion.LessThan(version.MustParseGeneric("1.22")):
348-
349-
// Kubernetes 1.22 began tracking subresources in managed fields.
350-
// - https://pr.k8s.io/100970
351-
Expect(existing.ManagedFields).To(ContainElement(
352-
MatchFields(IgnoreExtras, Fields{
353-
"Manager": Equal(test.Owner),
354-
"FieldsV1": PointTo(MatchAllFields(Fields{
355-
"Raw": WithTransform(func(in []byte) (out map[string]any) {
356-
Expect(yaml.Unmarshal(in, &out)).To(Succeed())
357-
return out
358-
}, MatchAllKeys(Keys{
359-
"f:metadata": MatchAllKeys(Keys{
360-
"f:finalizers": Not(BeZero()),
361-
}),
362-
"f:status": Not(BeZero()),
363-
})),
364-
})),
365-
}),
366-
), `controller should manage only "finalizers" and "status"`)
367-
368-
default:
369-
Expect(existing.ManagedFields).To(ContainElements(
370-
MatchFields(IgnoreExtras, Fields{
371-
"Manager": Equal(test.Owner),
372-
"FieldsV1": PointTo(MatchAllFields(Fields{
373-
"Raw": WithTransform(func(in []byte) (out map[string]any) {
374-
Expect(yaml.Unmarshal(in, &out)).To(Succeed())
375-
return out
376-
}, MatchAllKeys(Keys{
377-
"f:metadata": MatchAllKeys(Keys{
378-
"f:finalizers": Not(BeZero()),
379-
}),
380-
})),
342+
Expect(existing.ManagedFields).To(ContainElements(
343+
MatchFields(IgnoreExtras, Fields{
344+
"Manager": Equal(test.Owner),
345+
"FieldsV1": PointTo(MatchAllFields(Fields{
346+
"Raw": WithTransform(func(in []byte) (out map[string]any) {
347+
Expect(yaml.Unmarshal(in, &out)).To(Succeed())
348+
return out
349+
}, MatchAllKeys(Keys{
350+
"f:metadata": MatchAllKeys(Keys{
351+
"f:finalizers": Not(BeZero()),
352+
}),
381353
})),
382-
}),
383-
MatchFields(IgnoreExtras, Fields{
384-
"Manager": Equal(test.Owner),
385-
"FieldsV1": PointTo(MatchAllFields(Fields{
386-
"Raw": WithTransform(func(in []byte) (out map[string]any) {
387-
Expect(yaml.Unmarshal(in, &out)).To(Succeed())
388-
return out
389-
}, MatchAllKeys(Keys{
390-
"f:status": Not(BeZero()),
391-
})),
354+
})),
355+
}),
356+
MatchFields(IgnoreExtras, Fields{
357+
"Manager": Equal(test.Owner),
358+
"FieldsV1": PointTo(MatchAllFields(Fields{
359+
"Raw": WithTransform(func(in []byte) (out map[string]any) {
360+
Expect(yaml.Unmarshal(in, &out)).To(Succeed())
361+
return out
362+
}, MatchAllKeys(Keys{
363+
"f:status": Not(BeZero()),
392364
})),
393-
}),
394-
), `controller should manage only "finalizers" and "status"`)
395-
}
365+
})),
366+
}),
367+
), `controller should manage only "finalizers" and "status"`)
396368
})
397369

398370
Specify("Patroni Distributed Configuration", func() {

internal/controller/postgrescluster/suite_test.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,19 @@ package postgrescluster
66

77
import (
88
"context"
9-
"os"
10-
"strings"
119
"testing"
1210

1311
. "github.com/onsi/ginkgo/v2"
1412
. "github.com/onsi/gomega"
15-
"k8s.io/apimachinery/pkg/util/version"
16-
"k8s.io/client-go/discovery"
17-
"k8s.io/client-go/rest"
1813
"sigs.k8s.io/controller-runtime/pkg/client"
1914
"sigs.k8s.io/controller-runtime/pkg/log"
20-
"sigs.k8s.io/controller-runtime/pkg/manager"
2115

2216
"github.com/crunchydata/postgres-operator/internal/logging"
2317
"github.com/crunchydata/postgres-operator/internal/testing/require"
2418
)
2519

2620
var suite struct {
2721
Client client.Client
28-
Config *rest.Config
29-
30-
ServerVersion *version.Version
31-
32-
Manager manager.Manager
3322
}
3423

3524
func TestAPIs(t *testing.T) {
@@ -39,24 +28,10 @@ func TestAPIs(t *testing.T) {
3928
}
4029

4130
var _ = BeforeSuite(func() {
42-
if os.Getenv("KUBEBUILDER_ASSETS") == "" && !strings.EqualFold(os.Getenv("USE_EXISTING_CLUSTER"), "true") {
43-
Skip("skipping")
44-
}
31+
suite.Client = require.Kubernetes(GinkgoT())
4532

4633
logging.SetLogSink(logging.Logrus(GinkgoWriter, "test", 1, 1))
4734
log.SetLogger(logging.FromContext(context.Background()))
48-
49-
By("bootstrapping test environment")
50-
suite.Config, suite.Client = require.Kubernetes2(GinkgoT())
51-
52-
dc, err := discovery.NewDiscoveryClientForConfig(suite.Config)
53-
Expect(err).ToNot(HaveOccurred())
54-
55-
server, err := dc.ServerVersion()
56-
Expect(err).ToNot(HaveOccurred())
57-
58-
suite.ServerVersion, err = version.ParseGeneric(server.GitVersion)
59-
Expect(err).ToNot(HaveOccurred())
6035
})
6136

6237
var _ = AfterSuite(func() {

0 commit comments

Comments
 (0)