Skip to content

Commit c377050

Browse files
committed
cr check and improve unit test
1 parent eda1aa4 commit c377050

File tree

2 files changed

+163
-35
lines changed

2 files changed

+163
-35
lines changed

internal/patroni/reconcile.go

+36-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package patroni
66

77
import (
88
"context"
9+
"k8s.io/apimachinery/pkg/util/intstr"
910
"strings"
1011

1112
corev1 "k8s.io/api/core/v1"
@@ -149,9 +150,7 @@ func instanceProbes(cluster *v1beta1.PostgresCluster, container *corev1.Containe
149150
// TODO(cbandy): Consider if a PreStop hook is necessary.
150151
container.LivenessProbe = probeTiming(cluster.Spec.Patroni)
151152
container.LivenessProbe.InitialDelaySeconds = 3
152-
container.LivenessProbe.Exec = &corev1.ExecAction{
153-
Command: []string{"bash", "-c", "/usr/local/bin/postgres-liveness-check.sh"},
154-
}
153+
container.LivenessProbe.ProbeHandler = livenessProbe(cluster)
155154

156155
// Readiness is reflected in the controlling object's status (e.g. ReadyReplicas)
157156
// and allows our controller to react when Patroni bootstrap completes.
@@ -160,8 +159,40 @@ func instanceProbes(cluster *v1beta1.PostgresCluster, container *corev1.Containe
160159
// of the leader Pod in the leader Service.
161160
container.ReadinessProbe = probeTiming(cluster.Spec.Patroni)
162161
container.ReadinessProbe.InitialDelaySeconds = 3
163-
container.ReadinessProbe.Exec = &corev1.ExecAction{
164-
Command: []string{"bash", "-c", "/usr/local/bin/postgres-readiness-check.sh"},
162+
container.ReadinessProbe.ProbeHandler = readinessProbe(cluster)
163+
}
164+
165+
func livenessProbe(cluster *v1beta1.PostgresCluster) corev1.ProbeHandler {
166+
if cluster.CompareVersion("2.7.0") >= 0 {
167+
return corev1.ProbeHandler{
168+
Exec: &corev1.ExecAction{
169+
Command: []string{"bash", "-c", "/usr/local/bin/postgres-liveness-check.sh"},
170+
},
171+
}
172+
}
173+
return corev1.ProbeHandler{
174+
HTTPGet: &corev1.HTTPGetAction{
175+
Path: "/liveness",
176+
Port: intstr.FromInt(int(*cluster.Spec.Patroni.Port)),
177+
Scheme: corev1.URISchemeHTTPS,
178+
},
179+
}
180+
}
181+
182+
func readinessProbe(cluster *v1beta1.PostgresCluster) corev1.ProbeHandler {
183+
if cluster.CompareVersion("2.7.0") >= 0 {
184+
return corev1.ProbeHandler{
185+
Exec: &corev1.ExecAction{
186+
Command: []string{"bash", "-c", "/usr/local/bin/postgres-readiness-check.sh"},
187+
},
188+
}
189+
}
190+
return corev1.ProbeHandler{
191+
HTTPGet: &corev1.HTTPGetAction{
192+
Path: "/readiness",
193+
Port: intstr.FromInt(int(*cluster.Spec.Patroni.Port)),
194+
Scheme: corev1.URISchemeHTTPS,
195+
},
165196
}
166197
}
167198

internal/patroni/reconcile_test.go

+127-30
Original file line numberDiff line numberDiff line change
@@ -114,35 +114,12 @@ func TestInstanceConfigMap(t *testing.T) {
114114
func TestInstancePod(t *testing.T) {
115115
t.Parallel()
116116

117-
cluster := new(v1beta1.PostgresCluster)
118-
err := cluster.Default(context.Background(), nil)
119-
assert.NilError(t, err)
120-
cluster.Name = "some-such"
121-
cluster.Spec.PostgresVersion = 11
122-
cluster.Spec.Image = "image"
123-
cluster.Spec.ImagePullPolicy = corev1.PullAlways
124-
clusterConfigMap := new(corev1.ConfigMap)
125-
clusterPodService := new(corev1.Service)
126-
instanceCertificates := new(corev1.Secret)
127-
instanceConfigMap := new(corev1.ConfigMap)
128-
instanceSpec := new(v1beta1.PostgresInstanceSetSpec)
129-
patroniLeaderService := new(corev1.Service)
130-
template := new(corev1.PodTemplateSpec)
131-
template.Spec.Containers = []corev1.Container{{Name: "database"}}
132-
133-
call := func() error {
134-
return InstancePod(context.Background(),
135-
cluster, clusterConfigMap, clusterPodService, patroniLeaderService,
136-
instanceSpec, instanceCertificates, instanceConfigMap, template)
137-
}
138-
139-
assert.NilError(t, call())
140-
141-
assert.DeepEqual(t, template.ObjectMeta, metav1.ObjectMeta{
142-
Labels: map[string]string{naming.LabelPatroni: "some-such-ha"},
143-
})
144-
145-
assert.Assert(t, cmp.MarshalMatches(template.Spec, `
117+
tests := map[string]struct {
118+
expectedSpec string
119+
labels map[string]string
120+
}{
121+
"version >=2.7.0 specified": {
122+
expectedSpec: `
146123
containers:
147124
- command:
148125
- patroni
@@ -221,7 +198,127 @@ volumes:
221198
path: ~postgres-operator/patroni.ca-roots
222199
- key: patroni.crt-combined
223200
path: ~postgres-operator/patroni.crt+key
224-
`))
201+
`,
202+
labels: map[string]string{
203+
"pgv2.percona.com/version": "2.7.0",
204+
},
205+
},
206+
"version <2.7.0 specified": {
207+
labels: map[string]string{
208+
"pgv2.percona.com/version": "2.6.0",
209+
},
210+
expectedSpec: `
211+
containers:
212+
- command:
213+
- patroni
214+
- /etc/patroni
215+
env:
216+
- name: PATRONI_NAME
217+
valueFrom:
218+
fieldRef:
219+
apiVersion: v1
220+
fieldPath: metadata.name
221+
- name: PATRONI_KUBERNETES_POD_IP
222+
valueFrom:
223+
fieldRef:
224+
apiVersion: v1
225+
fieldPath: status.podIP
226+
- name: PATRONI_KUBERNETES_PORTS
227+
value: |
228+
[]
229+
- name: PATRONI_POSTGRESQL_CONNECT_ADDRESS
230+
value: $(PATRONI_NAME).:5432
231+
- name: PATRONI_POSTGRESQL_LISTEN
232+
value: '*:5432'
233+
- name: PATRONI_POSTGRESQL_CONFIG_DIR
234+
value: /pgdata/pg11
235+
- name: PATRONI_POSTGRESQL_DATA_DIR
236+
value: /pgdata/pg11
237+
- name: PATRONI_RESTAPI_CONNECT_ADDRESS
238+
value: $(PATRONI_NAME).:8008
239+
- name: PATRONI_RESTAPI_LISTEN
240+
value: '*:8008'
241+
- name: PATRONICTL_CONFIG_FILE
242+
value: /etc/patroni
243+
livenessProbe:
244+
failureThreshold: 3
245+
httpGet:
246+
path: /liveness
247+
port: 8008
248+
scheme: HTTPS
249+
initialDelaySeconds: 3
250+
periodSeconds: 10
251+
successThreshold: 1
252+
timeoutSeconds: 5
253+
name: database
254+
readinessProbe:
255+
failureThreshold: 3
256+
httpGet:
257+
path: /readiness
258+
port: 8008
259+
scheme: HTTPS
260+
initialDelaySeconds: 3
261+
periodSeconds: 10
262+
successThreshold: 1
263+
timeoutSeconds: 5
264+
resources: {}
265+
volumeMounts:
266+
- mountPath: /etc/patroni
267+
name: patroni-config
268+
readOnly: true
269+
volumes:
270+
- name: patroni-config
271+
projected:
272+
sources:
273+
- configMap:
274+
items:
275+
- key: patroni.yaml
276+
path: ~postgres-operator_cluster.yaml
277+
- configMap:
278+
items:
279+
- key: patroni.yaml
280+
path: ~postgres-operator_instance.yaml
281+
- secret:
282+
items:
283+
- key: patroni.ca-roots
284+
path: ~postgres-operator/patroni.ca-roots
285+
- key: patroni.crt-combined
286+
path: ~postgres-operator/patroni.crt+key
287+
`,
288+
},
289+
}
290+
for name, tt := range tests {
291+
t.Run(name, func(t *testing.T) {
292+
cluster := new(v1beta1.PostgresCluster)
293+
err := cluster.Default(context.Background(), nil)
294+
assert.NilError(t, err)
295+
cluster.Name = "some-such"
296+
cluster.Spec.PostgresVersion = 11
297+
cluster.Spec.Image = "image"
298+
cluster.Spec.ImagePullPolicy = corev1.PullAlways
299+
clusterConfigMap := new(corev1.ConfigMap)
300+
clusterPodService := new(corev1.Service)
301+
instanceCertificates := new(corev1.Secret)
302+
instanceConfigMap := new(corev1.ConfigMap)
303+
instanceSpec := new(v1beta1.PostgresInstanceSetSpec)
304+
patroniLeaderService := new(corev1.Service)
305+
template := new(corev1.PodTemplateSpec)
306+
template.Spec.Containers = []corev1.Container{{Name: "database"}}
307+
cluster.Labels = tt.labels
308+
309+
call := func() error {
310+
return InstancePod(context.Background(),
311+
cluster, clusterConfigMap, clusterPodService, patroniLeaderService,
312+
instanceSpec, instanceCertificates, instanceConfigMap, template)
313+
}
314+
assert.NilError(t, call())
315+
316+
assert.DeepEqual(t, template.ObjectMeta, metav1.ObjectMeta{
317+
Labels: map[string]string{naming.LabelPatroni: "some-such-ha"},
318+
})
319+
assert.Assert(t, cmp.MarshalMatches(template.Spec, tt.expectedSpec))
320+
})
321+
}
225322
}
226323

227324
func TestPodIsPrimary(t *testing.T) {

0 commit comments

Comments
 (0)