Skip to content

Commit e5aeca8

Browse files
authored
fix(switchover): detect missing WAL-archiver native sidecars (#11059)
archiverSidecarMissingOnPrimary compared only Spec.Containers, so a WAL-archiver plugin that injects its sidecar as a native sidecar (an init container with RestartPolicy=Always, as plugin-barman-cloud does) was never detected. The primary was then rolled out through a switchover that dead-locks, because a clean demotion archives pending WAL with the very sidecar that is still missing. Compare native-sidecar init containers as well, so the missing sidecar is detected and the primary is recreated in place instead. This completes the fix from #11032, which addressed #10981 but missed plugins that inject the archiver as a native sidecar. Signed-off-by: Marco Nenciarini <[email protected]>
1 parent eddf794 commit e5aeca8

2 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎internal/controller/cluster_upgrade.go‎

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,10 @@ func (r *ClusterReconciler) recreatePrimaryInPlace(
770770
// so it must be recreated to gain the sidecar rather than demoted via a
771771
// switchover, which would dead-lock on archiving (see issue #10981).
772772
//
773-
// Detection diffs the Pod's containers against a freshly evaluated instance
774-
// spec: NewInstance runs the plugins' OperationVerbEvaluate lifecycle hook, so
775-
// its containers already include any injected sidecars. The check is a
773+
// Detection diffs the Pod's containers (regular and native-sidecar init
774+
// containers) against a freshly evaluated instance spec: NewInstance runs the
775+
// plugins' OperationVerbEvaluate lifecycle hook, so its containers already
776+
// include any injected sidecars. The check is a
776777
// deliberate over-approximation. We cannot map a sidecar back to the specific
777778
// plugin that injected it, so when several sidecar-injecting plugins are enabled
778779
// this may recreate the primary even if only a non-archiver sidecar is missing.
@@ -798,12 +799,29 @@ func archiverSidecarMissingOnPrimary(
798799
return false, err
799800
}
800801

801-
current := make(map[string]struct{}, len(pod.Spec.Containers))
802-
for _, container := range pod.Spec.Containers {
803-
current[container.Name] = struct{}{}
802+
// Plugins inject their sidecars either as regular containers or, for native
803+
// sidecars, as init containers with RestartPolicy=Always. Compare both, so a
804+
// missing native sidecar is detected too. Run-once init containers (including
805+
// the bootstrap controller) are skipped: they are not long-running, so they
806+
// are never the archiver sidecar.
807+
collectContainerNames := func(target *corev1.Pod) map[string]struct{} {
808+
names := make(map[string]struct{}, len(target.Spec.Containers))
809+
for _, container := range target.Spec.Containers {
810+
names[container.Name] = struct{}{}
811+
}
812+
for _, container := range target.Spec.InitContainers {
813+
if container.RestartPolicy == nil ||
814+
*container.RestartPolicy != corev1.ContainerRestartPolicyAlways {
815+
continue
816+
}
817+
names[container.Name] = struct{}{}
818+
}
819+
return names
804820
}
805-
for _, container := range targetPod.Spec.Containers {
806-
if _, found := current[container.Name]; !found {
821+
822+
current := collectContainerNames(pod)
823+
for name := range collectContainerNames(targetPod) {
824+
if _, found := current[name]; !found {
807825
return true, nil
808826
}
809827
}

‎internal/controller/cluster_upgrade_test.go‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,11 +1198,14 @@ var _ = Describe("archiverSidecarMissingOnPrimary", func() {
11981198

11991199
// withArchiverSidecar returns a copy of pod with an extra container,
12001200
// mimicking the sidecar a WAL-archiver plugin injects at evaluation time.
1201+
// Real plugins (e.g. plugin-barman-cloud) inject it as a native sidecar: an
1202+
// init container with RestartPolicy=Always.
12011203
withArchiverSidecar := func(pod *corev1.Pod) *corev1.Pod {
12021204
out := pod.DeepCopy()
1203-
out.Spec.Containers = append(out.Spec.Containers, corev1.Container{
1204-
Name: sidecarName,
1205-
Image: "plugin-barman-cloud:latest",
1205+
out.Spec.InitContainers = append(out.Spec.InitContainers, corev1.Container{
1206+
Name: sidecarName,
1207+
Image: "plugin-barman-cloud:latest",
1208+
RestartPolicy: ptr.To(corev1.ContainerRestartPolicyAlways),
12061209
})
12071210
return out
12081211
}
@@ -1250,6 +1253,26 @@ var _ = Describe("archiverSidecarMissingOnPrimary", func() {
12501253
Expect(archiverSidecarMissingOnPrimary(ctx, podWithSidecar, &cluster)).To(BeFalse())
12511254
})
12521255

1256+
It("ignores a missing run-once init container, only native sidecars matter", func() {
1257+
enableArchiverPlugin()
1258+
1259+
pod, err := specs.NewInstance(context.TODO(), cluster, 1, true)
1260+
Expect(err).ToNot(HaveOccurred())
1261+
1262+
// The evaluated target adds a plain run-once init container (no
1263+
// RestartPolicy=Always). A missing one of those is not an archiver
1264+
// sidecar and must not force an in-place recreate.
1265+
target := pod.DeepCopy()
1266+
target.Spec.InitContainers = append(target.Spec.InitContainers, corev1.Container{
1267+
Name: "some-run-once-init",
1268+
Image: "init:latest",
1269+
})
1270+
ctx := pluginClient.SetPluginClientInContext(context.TODO(),
1271+
fakePluginClientRollout{returnedPod: target})
1272+
1273+
Expect(archiverSidecarMissingOnPrimary(ctx, pod, &cluster)).To(BeFalse())
1274+
})
1275+
12531276
It("propagates evaluation errors instead of degrading to a switchover decision", func() {
12541277
enableArchiverPlugin()
12551278

0 commit comments

Comments
 (0)