Skip to content

Commit 56e1180

Browse files
committed
Make also the liveness and readyness probes configurable
Signed-off-by: Tudor Golubenco <tudor@xata.io>
1 parent 8c2e72a commit 56e1180

6 files changed

Lines changed: 242 additions & 20 deletions

File tree

api/v1/objectstore_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ type InstanceSidecarConfiguration struct {
7070
// StartupProbe defines the configuration for the startup probe of the sidecar container.
7171
// +optional
7272
StartupProbe *ProbeConfig `json:"startupProbe,omitempty"`
73+
74+
// LivenessProbe defines the configuration for the liveness probe of the sidecar container.
75+
// +optional
76+
LivenessProbe *ProbeConfig `json:"livenessProbe,omitempty"`
77+
78+
// ReadinessProbe defines the configuration for the readiness probe of the sidecar container.
79+
// +optional
80+
ReadinessProbe *ProbeConfig `json:"readinessProbe,omitempty"`
7381
}
7482

7583
// ObjectStoreSpec defines the desired state of ObjectStore.

api/v1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/barmancloud.cnpg.io_objectstores.yaml

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.18.0
6+
controller-gen.kubebuilder.io/version: v0.16.1
77
name: objectstores.barmancloud.cnpg.io
88
spec:
99
group: barmancloud.cnpg.io
@@ -511,6 +511,78 @@ spec:
511511
- name
512512
type: object
513513
type: array
514+
livenessProbe:
515+
description: LivenessProbe defines the configuration for the liveness
516+
probe of the sidecar container.
517+
properties:
518+
failureThreshold:
519+
default: 10
520+
description: FailureThreshold is the minimum consecutive failures
521+
for the probe to be considered failed.
522+
format: int32
523+
type: integer
524+
initialDelaySeconds:
525+
default: 0
526+
description: InitialDelaySeconds is the number of seconds
527+
after the container has started before startup probes are
528+
initiated.
529+
format: int32
530+
type: integer
531+
periodSeconds:
532+
default: 10
533+
description: PeriodSeconds is how often (in seconds) to perform
534+
the probe.
535+
format: int32
536+
type: integer
537+
successThreshold:
538+
default: 1
539+
description: SuccessThreshold is the minimum consecutive successes
540+
for the probe to be considered successful.
541+
format: int32
542+
type: integer
543+
timeoutSeconds:
544+
default: 10
545+
description: TimeoutSeconds is the number of seconds after
546+
which the probe times out.
547+
format: int32
548+
type: integer
549+
type: object
550+
readinessProbe:
551+
description: ReadinessProbe defines the configuration for the
552+
readiness probe of the sidecar container.
553+
properties:
554+
failureThreshold:
555+
default: 10
556+
description: FailureThreshold is the minimum consecutive failures
557+
for the probe to be considered failed.
558+
format: int32
559+
type: integer
560+
initialDelaySeconds:
561+
default: 0
562+
description: InitialDelaySeconds is the number of seconds
563+
after the container has started before startup probes are
564+
initiated.
565+
format: int32
566+
type: integer
567+
periodSeconds:
568+
default: 10
569+
description: PeriodSeconds is how often (in seconds) to perform
570+
the probe.
571+
format: int32
572+
type: integer
573+
successThreshold:
574+
default: 1
575+
description: SuccessThreshold is the minimum consecutive successes
576+
for the probe to be considered successful.
577+
format: int32
578+
type: integer
579+
timeoutSeconds:
580+
default: 10
581+
description: TimeoutSeconds is the number of seconds after
582+
which the probe times out.
583+
format: int32
584+
type: integer
585+
type: object
514586
resources:
515587
description: Resources define cpu/memory requests and limits for
516588
the sidecar that runs in the instance pods.

hack/examples/minio-store.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@ spec:
1919
periodSeconds: 1
2020
failureThreshold: 10
2121
successThreshold: 1
22+
livenessProbe:
23+
initialDelaySeconds: 30
24+
timeoutSeconds: 5
25+
periodSeconds: 10
26+
failureThreshold: 3
27+
successThreshold: 1
28+
readinessProbe:
29+
initialDelaySeconds: 5
30+
timeoutSeconds: 5
31+
periodSeconds: 5
32+
failureThreshold: 3
33+
successThreshold: 1
2234
configuration:
2335
endpointCA:
2436
name: minio-server-tls

internal/cnpgi/operator/lifecycle.go

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,33 @@ func (impl LifecycleImplementation) reconcileJob(
131131
return nil, err
132132
}
133133

134+
livenessProbe, err := impl.collectSidecarLivenessProbeForRecoveryJob(ctx, pluginConfiguration)
135+
if err != nil {
136+
return nil, err
137+
}
138+
139+
readinessProbe, err := impl.collectSidecarReadinessProbeForRecoveryJob(ctx, pluginConfiguration)
140+
if err != nil {
141+
return nil, err
142+
}
143+
134144
return reconcileJob(ctx, cluster, request, sidecarConfiguration{
135-
env: env,
136-
certificates: certificates,
137-
resources: resources,
138-
startupProbe: startupProbe,
145+
env: env,
146+
certificates: certificates,
147+
resources: resources,
148+
startupProbe: startupProbe,
149+
livenessProbe: livenessProbe,
150+
readinessProbe: readinessProbe,
139151
})
140152
}
141153

142154
type sidecarConfiguration struct {
143-
env []corev1.EnvVar
144-
certificates []corev1.VolumeProjection
145-
resources corev1.ResourceRequirements
146-
startupProbe *barmancloudv1.ProbeConfig
155+
env []corev1.EnvVar
156+
certificates []corev1.VolumeProjection
157+
resources corev1.ResourceRequirements
158+
startupProbe *barmancloudv1.ProbeConfig
159+
livenessProbe *barmancloudv1.ProbeConfig
160+
readinessProbe *barmancloudv1.ProbeConfig
147161
}
148162

149163
func reconcileJob(
@@ -230,11 +244,23 @@ func (impl LifecycleImplementation) reconcilePod(
230244
return nil, err
231245
}
232246

247+
livenessProbe, err := impl.collectSidecarLivenessProbeForInstancePod(ctx, pluginConfiguration)
248+
if err != nil {
249+
return nil, err
250+
}
251+
252+
readinessProbe, err := impl.collectSidecarReadinessProbeForInstancePod(ctx, pluginConfiguration)
253+
if err != nil {
254+
return nil, err
255+
}
256+
233257
return reconcilePod(ctx, cluster, request, pluginConfiguration, sidecarConfiguration{
234-
env: env,
235-
certificates: certificates,
236-
resources: resources,
237-
startupProbe: startupProbe,
258+
env: env,
259+
certificates: certificates,
260+
resources: resources,
261+
startupProbe: startupProbe,
262+
livenessProbe: livenessProbe,
263+
readinessProbe: readinessProbe,
238264
})
239265
}
240266

@@ -339,11 +365,41 @@ func reconcilePodSpec(
339365
baseProbe.TimeoutSeconds = 10
340366
}
341367

368+
// Create liveness probe
369+
livenessProbe := baseProbe.DeepCopy()
370+
if config.livenessProbe != nil {
371+
livenessProbe.InitialDelaySeconds = config.livenessProbe.InitialDelaySeconds
372+
livenessProbe.TimeoutSeconds = config.livenessProbe.TimeoutSeconds
373+
livenessProbe.PeriodSeconds = config.livenessProbe.PeriodSeconds
374+
livenessProbe.FailureThreshold = config.livenessProbe.FailureThreshold
375+
livenessProbe.SuccessThreshold = config.livenessProbe.SuccessThreshold
376+
} else {
377+
// Fallback to default values for liveness probe
378+
livenessProbe.FailureThreshold = 3
379+
livenessProbe.TimeoutSeconds = 10
380+
}
381+
382+
// Create readiness probe
383+
readinessProbe := baseProbe.DeepCopy()
384+
if config.readinessProbe != nil {
385+
readinessProbe.InitialDelaySeconds = config.readinessProbe.InitialDelaySeconds
386+
readinessProbe.TimeoutSeconds = config.readinessProbe.TimeoutSeconds
387+
readinessProbe.PeriodSeconds = config.readinessProbe.PeriodSeconds
388+
readinessProbe.FailureThreshold = config.readinessProbe.FailureThreshold
389+
readinessProbe.SuccessThreshold = config.readinessProbe.SuccessThreshold
390+
} else {
391+
// Fallback to default values for readiness probe
392+
readinessProbe.FailureThreshold = 3
393+
readinessProbe.TimeoutSeconds = 10
394+
}
395+
342396
// fixed values
343397
sidecarTemplate.Name = "plugin-barman-cloud"
344398
sidecarTemplate.Image = viper.GetString("sidecar-image")
345399
sidecarTemplate.ImagePullPolicy = cluster.Spec.ImagePullPolicy
346400
sidecarTemplate.StartupProbe = baseProbe.DeepCopy()
401+
sidecarTemplate.LivenessProbe = livenessProbe
402+
sidecarTemplate.ReadinessProbe = readinessProbe
347403
sidecarTemplate.SecurityContext = &corev1.SecurityContext{
348404
AllowPrivilegeEscalation: ptr.To(false),
349405
RunAsNonRoot: ptr.To(true),

internal/cnpgi/operator/lifecycle_probes.go

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,117 @@ import (
77
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
88
)
99

10-
func (impl LifecycleImplementation) collectSidecarStartupProbeForRecoveryJob(
10+
// probeAccessor is a function type that extracts a specific probe configuration from an ObjectStore
11+
type probeAccessor func(*barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig
12+
13+
// collectSidecarProbeForRecoveryJob is a generic function to collect probe configurations for recovery jobs
14+
func (impl LifecycleImplementation) collectSidecarProbeForRecoveryJob(
1115
ctx context.Context,
1216
configuration *config.PluginConfiguration,
17+
accessor probeAccessor,
1318
) (*barmancloudv1.ProbeConfig, error) {
1419
if len(configuration.RecoveryBarmanObjectName) > 0 {
1520
var barmanObjectStore barmancloudv1.ObjectStore
1621
if err := impl.Client.Get(ctx, configuration.GetRecoveryBarmanObjectKey(), &barmanObjectStore); err != nil {
1722
return nil, err
1823
}
1924

20-
return barmanObjectStore.Spec.InstanceSidecarConfiguration.StartupProbe, nil
25+
return accessor(&barmanObjectStore), nil
2126
}
2227

2328
return nil, nil
2429
}
2530

26-
func (impl LifecycleImplementation) collectSidecarStartupProbeForInstancePod(
31+
// collectSidecarProbeForInstancePod is a generic function to collect probe configurations for instance pods
32+
func (impl LifecycleImplementation) collectSidecarProbeForInstancePod(
2733
ctx context.Context,
2834
configuration *config.PluginConfiguration,
35+
accessor probeAccessor,
36+
probeType string,
2937
) (*barmancloudv1.ProbeConfig, error) {
3038
if len(configuration.BarmanObjectName) > 0 {
3139
// On a replica cluster that also archives, the designated primary
3240
// will use both the replica source object store and the object store
3341
// of the cluster.
3442
// In this case, we use the cluster object store for configuring
35-
// the startup probe of the sidecar container.
43+
// the probe of the sidecar container.
3644

3745
var barmanObjectStore barmancloudv1.ObjectStore
3846
if err := impl.Client.Get(ctx, configuration.GetBarmanObjectKey(), &barmanObjectStore); err != nil {
3947
return nil, err
4048
}
4149

42-
return barmanObjectStore.Spec.InstanceSidecarConfiguration.StartupProbe, nil
50+
return accessor(&barmanObjectStore), nil
4351
}
4452

4553
if len(configuration.RecoveryBarmanObjectName) > 0 {
4654
// On a replica cluster that doesn't archive, the designated primary
4755
// uses only the replica source object store.
4856
// In this case, we use the replica source object store for configuring
49-
// the startup probe of the sidecar container.
57+
// the probe of the sidecar container.
5058
var barmanObjectStore barmancloudv1.ObjectStore
5159
if err := impl.Client.Get(ctx, configuration.GetRecoveryBarmanObjectKey(), &barmanObjectStore); err != nil {
5260
return nil, err
5361
}
5462

55-
return barmanObjectStore.Spec.InstanceSidecarConfiguration.StartupProbe, nil
63+
return accessor(&barmanObjectStore), nil
5664
}
5765

5866
return nil, nil
5967
}
68+
69+
// Specific probe collection methods that use the generic functions
70+
71+
func (impl LifecycleImplementation) collectSidecarStartupProbeForRecoveryJob(
72+
ctx context.Context,
73+
configuration *config.PluginConfiguration,
74+
) (*barmancloudv1.ProbeConfig, error) {
75+
return impl.collectSidecarProbeForRecoveryJob(ctx, configuration, func(store *barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig {
76+
return store.Spec.InstanceSidecarConfiguration.StartupProbe
77+
})
78+
}
79+
80+
func (impl LifecycleImplementation) collectSidecarStartupProbeForInstancePod(
81+
ctx context.Context,
82+
configuration *config.PluginConfiguration,
83+
) (*barmancloudv1.ProbeConfig, error) {
84+
return impl.collectSidecarProbeForInstancePod(ctx, configuration, func(store *barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig {
85+
return store.Spec.InstanceSidecarConfiguration.StartupProbe
86+
}, "startup")
87+
}
88+
89+
func (impl LifecycleImplementation) collectSidecarLivenessProbeForRecoveryJob(
90+
ctx context.Context,
91+
configuration *config.PluginConfiguration,
92+
) (*barmancloudv1.ProbeConfig, error) {
93+
return impl.collectSidecarProbeForRecoveryJob(ctx, configuration, func(store *barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig {
94+
return store.Spec.InstanceSidecarConfiguration.LivenessProbe
95+
})
96+
}
97+
98+
func (impl LifecycleImplementation) collectSidecarLivenessProbeForInstancePod(
99+
ctx context.Context,
100+
configuration *config.PluginConfiguration,
101+
) (*barmancloudv1.ProbeConfig, error) {
102+
return impl.collectSidecarProbeForInstancePod(ctx, configuration, func(store *barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig {
103+
return store.Spec.InstanceSidecarConfiguration.LivenessProbe
104+
}, "liveness")
105+
}
106+
107+
func (impl LifecycleImplementation) collectSidecarReadinessProbeForRecoveryJob(
108+
ctx context.Context,
109+
configuration *config.PluginConfiguration,
110+
) (*barmancloudv1.ProbeConfig, error) {
111+
return impl.collectSidecarProbeForRecoveryJob(ctx, configuration, func(store *barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig {
112+
return store.Spec.InstanceSidecarConfiguration.ReadinessProbe
113+
})
114+
}
115+
116+
func (impl LifecycleImplementation) collectSidecarReadinessProbeForInstancePod(
117+
ctx context.Context,
118+
configuration *config.PluginConfiguration,
119+
) (*barmancloudv1.ProbeConfig, error) {
120+
return impl.collectSidecarProbeForInstancePod(ctx, configuration, func(store *barmancloudv1.ObjectStore) *barmancloudv1.ProbeConfig {
121+
return store.Spec.InstanceSidecarConfiguration.ReadinessProbe
122+
}, "readiness")
123+
}

0 commit comments

Comments
 (0)