Skip to content

Commit 8c20e4f

Browse files
authored
fix: duplicate certificate projections (#331)
When referring to the same ObjectStore with custom TLS certificates multiple times, the plugin was adding the same volume projection two times. This lead to a wrong Job definition. This patch makes the plugin add a sidecar to replica cluster Pods that are using the plugin to get WALs, even if the plugin itself is not used for WAL archiving. Closes: #329 Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent 3fee90b commit 8c20e4f

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: postgresql.cnpg.io/v1
2+
kind: Cluster
3+
metadata:
4+
name: cluster-replica
5+
spec:
6+
instances: 3
7+
bootstrap:
8+
recovery:
9+
source: source
10+
replica:
11+
enabled: true
12+
source: source
13+
externalClusters:
14+
- name: source
15+
plugin:
16+
name: barman-cloud.cloudnative-pg.io
17+
parameters:
18+
barmanObjectName: minio-store
19+
serverName: cluster-example
20+
storage:
21+
size: 1Gi
22+

internal/cnpgi/operator/config/config.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
cnpgv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
77
"github.com/cloudnative-pg/cnpg-i-machinery/pkg/pluginhelper/decoder"
8+
"github.com/cloudnative-pg/machinery/pkg/stringset"
89
"k8s.io/apimachinery/pkg/types"
910

1011
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
@@ -85,16 +86,23 @@ func (config *PluginConfiguration) GetReplicaSourceBarmanObjectKey() types.Names
8586
// GetReferredBarmanObjectsKey gets the list of barman objects referred by this
8687
// plugin configuration
8788
func (config *PluginConfiguration) GetReferredBarmanObjectsKey() []types.NamespacedName {
88-
result := make([]types.NamespacedName, 0, 3)
89-
89+
objectNames := stringset.New()
9090
if len(config.BarmanObjectName) > 0 {
91-
result = append(result, config.GetBarmanObjectKey())
91+
objectNames.Put(config.BarmanObjectName)
9292
}
9393
if len(config.RecoveryBarmanObjectName) > 0 {
94-
result = append(result, config.GetRecoveryBarmanObjectKey())
94+
objectNames.Put(config.RecoveryBarmanObjectName)
9595
}
9696
if len(config.ReplicaSourceBarmanObjectName) > 0 {
97-
result = append(result, config.GetReplicaSourceBarmanObjectKey())
97+
objectNames.Put(config.ReplicaSourceBarmanObjectName)
98+
}
99+
100+
result := make([]types.NamespacedName, 0, 3)
101+
for _, name := range objectNames.ToSortedList() {
102+
result = append(result, types.NamespacedName{
103+
Name: name,
104+
Namespace: config.Cluster.Namespace,
105+
})
98106
}
99107

100108
return result

internal/cnpgi/operator/lifecycle.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (impl LifecycleImplementation) reconcileJob(
118118
return nil, err
119119
}
120120

121-
certificates, err := impl.collectAdditionalCertificates(ctx, cluster.Namespace, pluginConfiguration)
121+
certificates, err := impl.collectAdditionalCertificates(ctx, pluginConfiguration)
122122
if err != nil {
123123
return nil, err
124124
}
@@ -197,7 +197,7 @@ func (impl LifecycleImplementation) reconcilePod(
197197
return nil, err
198198
}
199199

200-
certificates, err := impl.collectAdditionalCertificates(ctx, cluster.Namespace, pluginConfiguration)
200+
certificates, err := impl.collectAdditionalCertificates(ctx, pluginConfiguration)
201201
if err != nil {
202202
return nil, err
203203
}
@@ -223,7 +223,8 @@ func reconcilePod(
223223

224224
mutatedPod := pod.DeepCopy()
225225

226-
if len(pluginConfiguration.BarmanObjectName) != 0 {
226+
if len(pluginConfiguration.BarmanObjectName) != 0 ||
227+
len(pluginConfiguration.ReplicaSourceBarmanObjectName) != 0 {
227228
if err := reconcilePodSpec(
228229
cluster,
229230
&mutatedPod.Spec,

internal/cnpgi/operator/lifecycle_certificates.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,16 @@ const barmanCertificatesVolumeName = "barman-certificates"
1818

1919
func (impl LifecycleImplementation) collectAdditionalCertificates(
2020
ctx context.Context,
21-
namespace string,
2221
pluginConfiguration *config.PluginConfiguration,
2322
) ([]corev1.VolumeProjection, error) {
2423
var result []corev1.VolumeProjection
2524

26-
if len(pluginConfiguration.BarmanObjectName) > 0 {
27-
envs, err := impl.collectObjectStoreCertificates(
28-
ctx,
29-
types.NamespacedName{
30-
Name: pluginConfiguration.BarmanObjectName,
31-
Namespace: namespace,
32-
},
33-
)
34-
if err != nil {
35-
return nil, err
36-
}
37-
result = append(result, envs...)
38-
}
39-
40-
if len(pluginConfiguration.RecoveryBarmanObjectName) > 0 &&
41-
pluginConfiguration.RecoveryBarmanObjectName != pluginConfiguration.BarmanObjectName {
42-
envs, err := impl.collectObjectStoreCertificates(
43-
ctx,
44-
types.NamespacedName{
45-
Name: pluginConfiguration.RecoveryBarmanObjectName,
46-
Namespace: namespace,
47-
},
48-
)
25+
for _, barmanObjectKey := range pluginConfiguration.GetReferredBarmanObjectsKey() {
26+
certs, err := impl.collectObjectStoreCertificates(ctx, barmanObjectKey)
4927
if err != nil {
5028
return nil, err
5129
}
52-
result = append(result, envs...)
30+
result = append(result, certs...)
5331
}
5432

5533
return result, nil

0 commit comments

Comments
 (0)