-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathpostgrescluster_controller_test.go
More file actions
367 lines (319 loc) · 12.4 KB
/
postgrescluster_controller_test.go
File metadata and controls
367 lines (319 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
Copyright 2026.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/client-go/tools/record"
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
enterprisev4 "github.com/splunk/splunk-operator/api/v4"
"github.com/splunk/splunk-operator/pkg/postgresql/cluster/core"
"github.com/splunk/splunk-operator/pkg/postgresql/shared/adapter/prometheus"
)
/*
* Test cases:
* PC-01 creates managed resources and status refs
* PC-02 adds finalizer on reconcile
* PC-07 is idempotent across repeated reconciles
* PC-03 Delete policy removes children and finalizer
* PC-04 Retain policy preserves children and removes ownerRefs
* PC-05 fails when PostgresClusterClass is missing
* PC-06 restores drifted managed spec
* PC-08 triggers on generation/finalizer/deletion changes
* PC-09 ignores no-op updates
*/
func containsEvent(events []string, recorder *record.FakeRecorder, eventType string, event string) bool {
for {
select {
case e := <-recorder.Events:
events = append(events, e)
return strings.Contains(e, eventType) && strings.Contains(e, event)
default:
return false
}
}
}
var _ = Describe("PostgresCluster Controller", Label("postgres"), func() {
const (
postgresVersion = "15.10"
clusterMemberCount = int32(2)
storageAmount = "1Gi"
poolerEnabled = false
deletePolicy = "Delete"
retainPolicy = "Retain"
namespace = "default"
classNamePrefix = "postgresql-dev-"
clusterNamePrefix = "postgresql-cluster-dev-"
provisioner = "postgresql.cnpg.io"
)
var (
ctx context.Context
clusterName string
className string
pgCluster *enterprisev4.PostgresCluster
pgClusterClass *enterprisev4.PostgresClusterClass
pgClusterKey types.NamespacedName
pgClusterClassKey types.NamespacedName
reconciler *PostgresClusterReconciler
req reconcile.Request
fakeRecorder *record.FakeRecorder
)
reconcileNTimes := func(times int) {
for i := 0; i < times; i++ {
_, err := reconciler.Reconcile(ctx, req)
Expect(err).NotTo(HaveOccurred())
}
}
BeforeEach(func() {
nameSuffix := fmt.Sprintf("%d-%d-%d",
GinkgoParallelProcess(),
GinkgoRandomSeed(),
CurrentSpecReport().LeafNodeLocation.LineNumber,
)
ctx = context.Background()
clusterName = clusterNamePrefix + nameSuffix
className = classNamePrefix + nameSuffix
pgClusterKey = types.NamespacedName{Name: clusterName, Namespace: namespace}
pgClusterClassKey = types.NamespacedName{Name: className, Namespace: namespace}
pgClusterClass = &enterprisev4.PostgresClusterClass{
ObjectMeta: metav1.ObjectMeta{Name: className},
Spec: enterprisev4.PostgresClusterClassSpec{
Provisioner: provisioner,
Config: &enterprisev4.PostgresClusterClassConfig{
Instances: &[]int32{clusterMemberCount}[0],
Storage: &[]resource.Quantity{resource.MustParse(storageAmount)}[0],
PostgresVersion: &[]string{postgresVersion}[0],
ConnectionPoolerEnabled: &[]bool{poolerEnabled}[0],
},
},
}
Expect(k8sClient.Create(ctx, pgClusterClass)).To(Succeed())
pgCluster = &enterprisev4.PostgresCluster{
ObjectMeta: metav1.ObjectMeta{Name: clusterName, Namespace: namespace},
Spec: enterprisev4.PostgresClusterSpec{
Class: className,
ClusterDeletionPolicy: &[]string{deletePolicy}[0],
},
}
fakeRecorder = record.NewFakeRecorder(100)
reconciler = &PostgresClusterReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
Recorder: fakeRecorder,
Metrics: &prometheus.NoopRecorder{},
}
req = reconcile.Request{NamespacedName: types.NamespacedName{Name: clusterName, Namespace: namespace}}
})
AfterEach(func() {
By("Deleting PostgresCluster and letting reconcile run finalizer cleanup")
// Best-effort delete (object might already be gone in some specs)
err := k8sClient.Get(ctx, pgClusterKey, pgCluster)
if err == nil {
Expect(k8sClient.Delete(ctx, pgCluster)).To(Succeed())
} else {
Expect(apierrors.IsNotFound(err)).To(BeTrue())
}
// Drive delete reconcile path until finalizer is removed and object disappears
Eventually(func() bool {
_, recErr := reconciler.Reconcile(ctx, req)
if recErr != nil {
// Some envtest runs may not have CNPG CRDs installed in the API server.
// In that case, remove finalizer directly so fixture teardown remains deterministic.
if meta.IsNoMatchError(recErr) {
current := &enterprisev4.PostgresCluster{}
getErr := k8sClient.Get(ctx, pgClusterKey, current)
if apierrors.IsNotFound(getErr) {
return true
}
if getErr != nil {
return false
}
controllerutil.RemoveFinalizer(current, core.PostgresClusterFinalizerName)
if err := k8sClient.Update(ctx, current); err != nil && !apierrors.IsNotFound(err) {
return false
}
if err := k8sClient.Delete(ctx, current); err != nil && !apierrors.IsNotFound(err) {
return false
}
} else {
return false
}
}
getErr := k8sClient.Get(ctx, pgClusterKey, &enterprisev4.PostgresCluster{})
return apierrors.IsNotFound(getErr)
}, "10s", "500ms").Should(BeTrue())
By("Cleaning up PostgresClusterClass fixture")
err = k8sClient.Get(ctx, pgClusterClassKey, pgClusterClass)
if err == nil {
Expect(k8sClient.Delete(ctx, pgClusterClass)).To(Succeed())
} else {
Expect(apierrors.IsNotFound(err)).To(BeTrue())
}
})
When("under typical usage and expecting healthy PostgresCluster state", func() {
Context("when reconciling", func() {
// PC-02
It("adds finalizer on reconcile", func() {
Expect(k8sClient.Create(ctx, pgCluster)).To(Succeed())
reconcileNTimes(1)
pc := &enterprisev4.PostgresCluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, pc)).To(Succeed())
Expect(controllerutil.ContainsFinalizer(pc, core.PostgresClusterFinalizerName)).To(BeTrue())
})
// PC-01
It("creates managed resources and status refs", func() {
Expect(k8sClient.Create(ctx, pgCluster)).To(Succeed())
// pass 1: add finalizer; pass 2: create CNPG cluster/secret/status.
reconcileNTimes(2)
pc := &enterprisev4.PostgresCluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, pc)).To(Succeed())
cond := meta.FindStatusCondition(pc.Status.Conditions, "ClusterReady")
Expect(cond).NotTo(BeNil())
Expect(cond.Status).To(Equal(metav1.ConditionFalse))
Expect(cond.Reason).To(Equal("CNPGClusterProvisioning"))
// Simulate external CNPG controller status progression.
cnpg := &cnpgv1.Cluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, cnpg)).To(Succeed())
cnpg.Status.Phase = cnpgv1.PhaseHealthy
Expect(k8sClient.Status().Update(ctx, cnpg)).To(Succeed())
reconcileNTimes(1)
// Expect cnpg status progression propagation
Expect(k8sClient.Get(ctx, pgClusterKey, pc)).To(Succeed())
cond = meta.FindStatusCondition(pc.Status.Conditions, "ClusterReady")
Expect(cond).NotTo(BeNil())
Expect(cond.Status).To(Equal(metav1.ConditionTrue))
Expect(cond.Reason).To(Equal("CNPGClusterHealthy"))
Expect(pc.Status.Resources).NotTo(BeNil())
Expect(pc.Status.Resources.SuperUserSecretRef).NotTo(BeNil())
Expect(pc.Status.Resources.ConfigMapRef).NotTo(BeNil())
received := make([]string, 0, 8)
Eventually(func() bool {
return containsEvent(
received, fakeRecorder,
v1.EventTypeNormal, core.EventClusterReady)
}, "5s", "100ms").Should(BeTrue(), "events seen: %v", received)
})
// PC-07
It("is idempotent across repeated reconciles", func() {
Expect(k8sClient.Create(ctx, pgCluster)).To(Succeed())
reconcileNTimes(2)
reconcileNTimes(3)
cnpg := &cnpgv1.Cluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, cnpg)).To(Succeed())
Expect(cnpg.Spec.Instances).To(Equal(int(clusterMemberCount)))
pc := &enterprisev4.PostgresCluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, pc)).To(Succeed())
cond := meta.FindStatusCondition(pc.Status.Conditions, "ClusterReady")
Expect(cond).NotTo(BeNil())
Expect(cond.ObservedGeneration).To(Equal(pc.Generation))
})
})
})
When("deleting a PostgresCluster", func() {
// PC-03
Context("and clusterDeletionPolicy is set to Delete", func() {
It("removes children and finalizer", func() {
Expect(k8sClient.Create(ctx, pgCluster)).To(Succeed())
reconcileNTimes(2)
pc := &enterprisev4.PostgresCluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, pc)).To(Succeed())
Expect(k8sClient.Delete(ctx, pc)).To(Succeed())
Eventually(func() bool {
_, err := reconciler.Reconcile(ctx, req)
if err != nil {
return false
}
getErr := k8sClient.Get(ctx, pgClusterKey, &enterprisev4.PostgresCluster{})
return apierrors.IsNotFound(getErr)
}, "30s", "250ms").Should(BeTrue())
})
})
// PC-04
Context("when clusterDeletionPolicy is set to Retain", func() {
It("preserves retained resources and removes owner refs", func() {
Expect(k8sClient.Create(ctx, pgCluster)).To(Succeed())
reconcileNTimes(2)
pc := &enterprisev4.PostgresCluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, pc)).To(Succeed())
Expect(k8sClient.Delete(ctx, pc)).To(Succeed())
Eventually(func() bool {
_, err := reconciler.Reconcile(ctx, req)
if err != nil {
return false
}
getErr := k8sClient.Get(ctx, pgClusterKey, &enterprisev4.PostgresCluster{})
return apierrors.IsNotFound(getErr)
}, "30s", "250ms").Should(BeTrue())
})
})
})
When("reconciling with invalid or drifted dependencies", func() {
// PC-05
Context("when referenced class does not exist", func() {
It("fails with class-not-found condition and emits a warning event", func() {
badName := "bad-" + clusterName
badKey := types.NamespacedName{Name: badName, Namespace: namespace}
bad := &enterprisev4.PostgresCluster{
ObjectMeta: metav1.ObjectMeta{Name: badName, Namespace: namespace},
Spec: enterprisev4.PostgresClusterSpec{Class: "missing-class"},
}
Expect(k8sClient.Create(ctx, bad)).To(Succeed())
DeferCleanup(func() { _ = k8sClient.Delete(ctx, bad) })
// pass 1 adds finalizer, pass 2 reaches class lookup and sets failure condition.
_, err := reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: badKey})
Expect(err).NotTo(HaveOccurred())
_, err = reconciler.Reconcile(ctx, reconcile.Request{NamespacedName: badKey})
Expect(err).To(HaveOccurred())
Eventually(func() bool {
current := &enterprisev4.PostgresCluster{}
if err := k8sClient.Get(ctx, badKey, current); err != nil {
return false
}
cond := meta.FindStatusCondition(current.Status.Conditions, "ClusterReady")
return cond != nil && cond.Reason == "ClusterClassNotFound"
}, "20s", "250ms").Should(BeTrue())
received := make([]string, 0, 8)
Eventually(func() bool {
return containsEvent(
received, fakeRecorder,
v1.EventTypeWarning, core.EventClusterClassNotFound)
}, "5s", "100ms").Should(BeTrue(), "events seen: %v", received)
})
})
// PC-06
Context("when managed child spec drifts from desired state", func() {
It("restores drifted managed spec", func() {
Expect(k8sClient.Create(ctx, pgCluster)).To(Succeed())
reconcileNTimes(2)
cnpg := &cnpgv1.Cluster{}
Expect(k8sClient.Get(ctx, pgClusterKey, cnpg)).To(Succeed())
cnpg.Spec.Instances = 8
Expect(k8sClient.Update(ctx, cnpg)).To(Succeed())
reconcileNTimes(2)
Expect(k8sClient.Get(ctx, pgClusterKey, cnpg)).To(Succeed())
Expect(cnpg.Spec.Instances).To(Equal(int(clusterMemberCount)))
})
})
})
})