Skip to content

Commit 7761e2f

Browse files
committed
feat: support custom CA certificates
This patch enables the use of custom CA certificates when connecting to the object store in the barman-cloud plugin. The certificates are injected into the sidecar via a projected volume and used by the barman-cloud tool suite. If the barman object name or the key name changes, users must trigger a Pod rollout to apply the new values. Signed-off-by: Leonardo Cecchi <[email protected]>
1 parent 0872cf2 commit 7761e2f

File tree

11 files changed

+252
-82
lines changed

11 files changed

+252
-82
lines changed

docs/examples/minio-store.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ metadata:
44
name: minio-store
55
spec:
66
configuration:
7+
endpointCA:
8+
name: minio-server-tls
9+
key: tls.crt
710
destinationPath: s3://backups/
8-
endpointURL: http://minio:9000
11+
endpointURL: https://minio:9000
912
s3Credentials:
1013
accessKeyId:
1114
name: minio

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,5 @@ require (
136136
sigs.k8s.io/structured-merge-diff/v4 v4.5.0 // indirect
137137
sigs.k8s.io/yaml v1.4.0 // indirect
138138
)
139+
140+
replace github.com/cloudnative-pg/barman-cloud => github.com/leonardoce/barman-cloud v0.0.0-20250310163530-1a3fac818111

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UF
2020
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
2121
github.com/cloudnative-pg/api v1.25.1 h1:uNjKiB0MIspUeH9l651SnFDcuflr1crB3t6LjxUCafQ=
2222
github.com/cloudnative-pg/api v1.25.1/go.mod h1:fwF5g4XkuNZqYXIeRR3AJvUfWlqWig+r2DXc5bEmw6U=
23-
github.com/cloudnative-pg/barman-cloud v0.1.0 h1:e/z52CehMBIh1LjZqNBJnncWJbS+1JYvRMBR8Js6Uiw=
24-
github.com/cloudnative-pg/barman-cloud v0.1.0/go.mod h1:rJUJO/f1yNckLZiVxHAyRmKY+4EPJkYRJsGbTZRJQSY=
2523
github.com/cloudnative-pg/cloudnative-pg v1.25.1 h1:Yc6T7ikQ1AiWXBQht+6C3DoihrIpUN2OkM1dIwqadTo=
2624
github.com/cloudnative-pg/cloudnative-pg v1.25.1/go.mod h1:96b9bRFLSr3uFWHjhytPdcvKIKwy9H6AG7cH0O6jefs=
2725
github.com/cloudnative-pg/cnpg-i v0.1.0 h1:QH2xTsrODMhEEc6B25GbOYe7ZIttDmSkYvXotfU5dfs=
@@ -116,6 +114,8 @@ github.com/kubernetes-csi/external-snapshotter/client/v8 v8.2.0 h1:Q3jQ1NkFqv5o+
116114
github.com/kubernetes-csi/external-snapshotter/client/v8 v8.2.0/go.mod h1:E3vdYxHj2C2q6qo8/Da4g7P+IcwqRZyy3gJBzYybV9Y=
117115
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
118116
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
117+
github.com/leonardoce/barman-cloud v0.0.0-20250310163530-1a3fac818111 h1:opN3VCteYluh6R1OUvt1AU5SuiBdYa4ZzrMf0prRHXk=
118+
github.com/leonardoce/barman-cloud v0.0.0-20250310163530-1a3fac818111/go.mod h1:rJUJO/f1yNckLZiVxHAyRmKY+4EPJkYRJsGbTZRJQSY=
119119
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
120120
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
121121
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=

internal/cnpgi/common/wal.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,14 @@ func (w WALServiceImplementation) Archive(
7777
return nil, err
7878
}
7979

80-
envArchive, err := barmanCredentials.EnvSetBackupCloudCredentials(
80+
envArchive, err := barmanCredentials.EnvSetCloudCredentialsAndCertificates(
8181
ctx,
8282
w.Client,
8383
objectStore.Namespace,
8484
&objectStore.Spec.Configuration,
85-
os.Environ())
85+
os.Environ(),
86+
path.Join(metadata.BarmanCertificatesPath, objectStore.Name, metadata.BarmanCertificatesFileName),
87+
)
8688
if err != nil {
8789
if apierrors.IsForbidden(err) {
8890
return nil, errors.New("backup credentials don't yet have access permissions. Will retry reconciliation loop")
@@ -191,12 +193,13 @@ func (w WALServiceImplementation) restoreFromBarmanObjectStore(
191193
barmanConfiguration := &objectStore.Spec.Configuration
192194

193195
env := GetRestoreCABundleEnv(barmanConfiguration)
194-
credentialsEnv, err := barmanCredentials.EnvSetBackupCloudCredentials(
196+
credentialsEnv, err := barmanCredentials.EnvSetCloudCredentialsAndCertificates(
195197
ctx,
196198
w.Client,
197199
objectStore.Namespace,
198200
&objectStore.Spec.Configuration,
199201
os.Environ(),
202+
path.Join(metadata.BarmanCertificatesPath, objectStore.Name, metadata.BarmanCertificatesFileName),
200203
)
201204
if err != nil {
202205
return fmt.Errorf("while getting recover credentials: %w", err)

internal/cnpgi/instance/backup.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path"
78
"strconv"
89
"time"
910

@@ -98,12 +99,14 @@ func (b BackupServiceImplementation) Backup(
9899
// PGHOST (and the like) to be available
99100
osEnvironment := os.Environ()
100101
caBundleEnvironment := common.GetRestoreCABundleEnv(&objectStore.Spec.Configuration)
101-
env, err := barmanCredentials.EnvSetBackupCloudCredentials(
102+
env, err := barmanCredentials.EnvSetCloudCredentialsAndCertificates(
102103
ctx,
103104
b.Client,
104105
objectStore.Namespace,
105106
&objectStore.Spec.Configuration,
106-
common.MergeEnv(osEnvironment, caBundleEnvironment))
107+
common.MergeEnv(osEnvironment, caBundleEnvironment),
108+
path.Join(metadata.BarmanCertificatesPath, objectStore.Name, metadata.BarmanCertificatesFileName),
109+
)
107110
if err != nil {
108111
contextLogger.Error(err, "while setting backup cloud credentials")
109112
return nil, err

internal/cnpgi/metadata/constants.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ const (
1111
// if present, requires the WAL archiver to check that the backup object
1212
// store is empty.
1313
CheckEmptyWalArchiveFile = ".check-empty-wal-archive"
14+
15+
// BarmanCertificatesPath is the path where the Barman
16+
// certificates will be installed
17+
BarmanCertificatesPath = "/barman-certificates"
18+
19+
// BarmanCertificatesFileName is the path where the Barman
20+
// certificates will be used
21+
BarmanCertificatesFileName = "barman-ca.crt"
1422
)
1523

1624
// Data is the metadata of this plugin.

internal/cnpgi/operator/lifecycle.go

Lines changed: 53 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ import (
1414
"github.com/spf13/viper"
1515
batchv1 "k8s.io/api/batch/v1"
1616
corev1 "k8s.io/api/core/v1"
17-
"k8s.io/apimachinery/pkg/types"
1817
"k8s.io/utils/ptr"
1918
"sigs.k8s.io/controller-runtime/pkg/client"
2019

21-
barmancloudv1 "github.com/cloudnative-pg/plugin-barman-cloud/api/v1"
2220
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/metadata"
2321
"github.com/cloudnative-pg/plugin-barman-cloud/internal/cnpgi/operator/config"
2422
)
@@ -107,56 +105,6 @@ func (impl LifecycleImplementation) LifecycleHook(
107105
}
108106
}
109107

110-
func (impl LifecycleImplementation) collectAdditionalEnvs(
111-
ctx context.Context,
112-
namespace string,
113-
pluginConfiguration *config.PluginConfiguration,
114-
) ([]corev1.EnvVar, error) {
115-
var result []corev1.EnvVar
116-
117-
if len(pluginConfiguration.BarmanObjectName) > 0 {
118-
envs, err := impl.collectObjectStoreEnvs(
119-
ctx,
120-
types.NamespacedName{
121-
Name: pluginConfiguration.BarmanObjectName,
122-
Namespace: namespace,
123-
},
124-
)
125-
if err != nil {
126-
return nil, err
127-
}
128-
result = append(result, envs...)
129-
}
130-
131-
if len(pluginConfiguration.RecoveryBarmanObjectName) > 0 {
132-
envs, err := impl.collectObjectStoreEnvs(
133-
ctx,
134-
types.NamespacedName{
135-
Name: pluginConfiguration.RecoveryBarmanObjectName,
136-
Namespace: namespace,
137-
},
138-
)
139-
if err != nil {
140-
return nil, err
141-
}
142-
result = append(result, envs...)
143-
}
144-
145-
return result, nil
146-
}
147-
148-
func (impl LifecycleImplementation) collectObjectStoreEnvs(
149-
ctx context.Context,
150-
barmanObjectKey types.NamespacedName,
151-
) ([]corev1.EnvVar, error) {
152-
var objectStore barmancloudv1.ObjectStore
153-
if err := impl.Client.Get(ctx, barmanObjectKey, &objectStore); err != nil {
154-
return nil, err
155-
}
156-
157-
return objectStore.Spec.InstanceSidecarConfiguration.Env, nil
158-
}
159-
160108
func (impl LifecycleImplementation) reconcileJob(
161109
ctx context.Context,
162110
cluster *cnpgv1.Cluster,
@@ -165,17 +113,23 @@ func (impl LifecycleImplementation) reconcileJob(
165113
) (*lifecycle.OperatorLifecycleResponse, error) {
166114
env, err := impl.collectAdditionalEnvs(ctx, cluster.Namespace, pluginConfiguration)
167115
if err != nil {
168-
return nil, nil
116+
return nil, err
169117
}
170118

171-
return reconcileJob(ctx, cluster, request, env)
119+
certificates, err := impl.collectAdditionalCertificates(ctx, cluster.Namespace, pluginConfiguration)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
return reconcileJob(ctx, cluster, request, env, certificates)
172125
}
173126

174127
func reconcileJob(
175128
ctx context.Context,
176129
cluster *cnpgv1.Cluster,
177130
request *lifecycle.OperatorLifecycleRequest,
178131
env []corev1.EnvVar,
132+
certificates []corev1.VolumeProjection,
179133
) (*lifecycle.OperatorLifecycleResponse, error) {
180134
contextLogger := log.FromContext(ctx).WithName("lifecycle")
181135
if pluginConfig := cluster.GetRecoverySourcePlugin(); pluginConfig == nil || pluginConfig.Name != metadata.PluginName {
@@ -212,6 +166,7 @@ func reconcileJob(
212166
Args: []string{"restore"},
213167
},
214168
env,
169+
certificates,
215170
); err != nil {
216171
return nil, fmt.Errorf("while reconciling pod spec for job: %w", err)
217172
}
@@ -235,10 +190,15 @@ func (impl LifecycleImplementation) reconcilePod(
235190
) (*lifecycle.OperatorLifecycleResponse, error) {
236191
env, err := impl.collectAdditionalEnvs(ctx, cluster.Namespace, pluginConfiguration)
237192
if err != nil {
238-
return nil, nil
193+
return nil, err
194+
}
195+
196+
certificates, err := impl.collectAdditionalCertificates(ctx, cluster.Namespace, pluginConfiguration)
197+
if err != nil {
198+
return nil, err
239199
}
240200

241-
return reconcilePod(ctx, cluster, request, pluginConfiguration, env)
201+
return reconcilePod(ctx, cluster, request, pluginConfiguration, env, certificates)
242202
}
243203

244204
func reconcilePod(
@@ -247,6 +207,7 @@ func reconcilePod(
247207
request *lifecycle.OperatorLifecycleRequest,
248208
pluginConfiguration *config.PluginConfiguration,
249209
env []corev1.EnvVar,
210+
certificates []corev1.VolumeProjection,
250211
) (*lifecycle.OperatorLifecycleResponse, error) {
251212
pod, err := decoder.DecodePodJSON(request.GetObjectDefinition())
252213
if err != nil {
@@ -267,6 +228,7 @@ func reconcilePod(
267228
Args: []string{"instance"},
268229
},
269230
env,
231+
certificates,
270232
); err != nil {
271233
return nil, fmt.Errorf("while reconciling pod spec for pod: %w", err)
272234
}
@@ -291,6 +253,7 @@ func reconcilePodSpec(
291253
mainContainerName string,
292254
sidecarConfig corev1.Container,
293255
additionalEnvs []corev1.EnvVar,
256+
certificates []corev1.VolumeProjection,
294257
) error {
295258
envs := []corev1.EnvVar{
296259
{
@@ -360,10 +323,22 @@ func reconcilePodSpec(
360323
}
361324
}
362325

363-
if err := InjectPluginSidecarPodSpec(spec, &sidecarConfig, mainContainerName, true); err != nil {
326+
if err := injectPluginSidecarPodSpec(spec, &sidecarConfig, mainContainerName); err != nil {
364327
return err
365328
}
366329

330+
// inject the volume containing the certificates if needed
331+
if !volumeListHasVolume(spec.Volumes, barmanCertificatesVolumeName) {
332+
spec.Volumes = append(spec.Volumes, corev1.Volume{
333+
Name: barmanCertificatesVolumeName,
334+
VolumeSource: corev1.VolumeSource{
335+
Projected: &corev1.ProjectedVolumeSource{
336+
Sources: certificates,
337+
},
338+
},
339+
})
340+
}
341+
367342
return nil
368343
}
369344

@@ -407,16 +382,15 @@ func InjectPluginVolumePodSpec(spec *corev1.PodSpec, mainContainerName string) {
407382
}
408383
}
409384

410-
// InjectPluginSidecarPodSpec injects a plugin sidecar into a CNPG Pod spec.
385+
// injectPluginSidecarPodSpec injects a plugin sidecar into a CNPG Pod spec.
411386
//
412387
// If the "injectMainContainerVolumes" flag is true, this will append all the volume
413388
// mounts that are used in the instance manager Pod to the passed sidecar
414389
// container, granting it superuser access to the PostgreSQL instance.
415-
func InjectPluginSidecarPodSpec(
390+
func injectPluginSidecarPodSpec(
416391
spec *corev1.PodSpec,
417392
sidecar *corev1.Container,
418393
mainContainerName string,
419-
injectMainContainerVolumes bool,
420394
) error {
421395
sidecar = sidecar.DeepCopy()
422396
InjectPluginVolumePodSpec(spec, mainContainerName)
@@ -447,11 +421,27 @@ func InjectPluginSidecarPodSpec(
447421
}
448422

449423
// Do not modify the passed sidecar definition
450-
if injectMainContainerVolumes {
451-
sidecar.VolumeMounts = append(sidecar.VolumeMounts, volumeMounts...)
452-
}
424+
sidecar.VolumeMounts = append(
425+
sidecar.VolumeMounts,
426+
corev1.VolumeMount{
427+
Name: barmanCertificatesVolumeName,
428+
MountPath: metadata.BarmanCertificatesPath,
429+
})
430+
sidecar.VolumeMounts = append(sidecar.VolumeMounts, volumeMounts...)
453431
sidecar.RestartPolicy = ptr.To(corev1.ContainerRestartPolicyAlways)
454432
spec.InitContainers = append(spec.InitContainers, *sidecar)
455433

456434
return nil
457435
}
436+
437+
// volumeListHasVolume check if a volume with a known name exists
438+
// in the volume list
439+
func volumeListHasVolume(volumes []corev1.Volume, name string) bool {
440+
for i := range volumes {
441+
if volumes[i].Name == name {
442+
return true
443+
}
444+
}
445+
446+
return false
447+
}

0 commit comments

Comments
 (0)