refactor: deduplicate barman object store presence check

Validate() and the instance sidecar injection condition each
independently re-encoded "is any of BarmanObjectName,
RecoveryBarmanObjectName or ReplicaSourceBarmanObjectName set". The
injection condition had already drifted from Validate() once, missing
RecoveryBarmanObjectName until this PR added it back. Extract a single
HasAnyBarmanObjectStore() method so the two checks can no longer drift
apart the same way again.

Signed-off-by: Marco Nenciarini <marco.nenciarini@enterprisedb.com>
This commit is contained in:
Marco Nenciarini 2026-07-24 14:28:06 +02:00
parent 4acf3b7301
commit 4f5f259ee7
2 changed files with 11 additions and 6 deletions

View File

@ -102,6 +102,15 @@ func (config *PluginConfiguration) GetReplicaSourceBarmanObjectKey() types.Names
} }
} }
// HasAnyBarmanObjectStore returns true if the configuration references at least
// one barman object store, be it for backup/archiving, recovery, or as a
// replica source.
func (config *PluginConfiguration) HasAnyBarmanObjectStore() bool {
return len(config.BarmanObjectName) > 0 ||
len(config.RecoveryBarmanObjectName) > 0 ||
len(config.ReplicaSourceBarmanObjectName) > 0
}
// GetReferredBarmanObjectsKey gets the list of barman objects referred by this // GetReferredBarmanObjectsKey gets the list of barman objects referred by this
// plugin configuration // plugin configuration
func (config *PluginConfiguration) GetReferredBarmanObjectsKey() []types.NamespacedName { func (config *PluginConfiguration) GetReferredBarmanObjectsKey() []types.NamespacedName {
@ -263,9 +272,7 @@ func getReplicaSourcePlugin(cluster *cnpgv1.Cluster) *cnpgv1.PluginConfiguration
func (config *PluginConfiguration) Validate() error { func (config *PluginConfiguration) Validate() error {
err := NewConfigurationError() err := NewConfigurationError()
if len(config.BarmanObjectName) == 0 && if !config.HasAnyBarmanObjectStore() {
len(config.RecoveryBarmanObjectName) == 0 &&
len(config.ReplicaSourceBarmanObjectName) == 0 {
return err.WithMessage("no reference to barmanObjectName have been included") return err.WithMessage("no reference to barmanObjectName have been included")
} }

View File

@ -343,9 +343,7 @@ func reconcileInstancePod(
// sidecar in its instance pods: the phase-0 bootstrap restore and the WAL // sidecar in its instance pods: the phase-0 bootstrap restore and the WAL
// replay that follows both run inside the instance and rely on it. This // replay that follows both run inside the instance and rely on it. This
// condition therefore mirrors what pluginConfiguration.Validate() accepts. // condition therefore mirrors what pluginConfiguration.Validate() accepts.
if len(pluginConfiguration.BarmanObjectName) != 0 || if pluginConfiguration.HasAnyBarmanObjectStore() {
len(pluginConfiguration.RecoveryBarmanObjectName) != 0 ||
len(pluginConfiguration.ReplicaSourceBarmanObjectName) != 0 {
if err := reconcilePodSpec( if err := reconcilePodSpec(
cluster, cluster,
&mutatedPod.Spec, &mutatedPod.Spec,