Skip to content

Commit 8d3af4d

Browse files
mnenciaarmruNiccoloFei
authored
fix: prefix extension and tablespace volume names to avoid collisions (#9973)
Add namespace prefixes to extension and tablespace volume names so they cannot collide with system volumes (pgdata, shm, pg-wal, scratch-data, projected) or with each other. Extensions get an "ext-" prefix, tablespaces get a "tbs-" prefix. Webhook validation rejects names that produce duplicate volume names after K8s name sanitization, for both extensions and tablespaces. Migration logic normalizes old unprefixed volume names during pod spec comparison, preventing unnecessary pod restarts on upgrade. The operator upgrade E2E fixtures now include a tablespace to exercise this path. Closes #9972 Signed-off-by: Marco Nenciarini <[email protected]> Signed-off-by: Armando Ruocco <[email protected]> Signed-off-by: Niccolò Fei <[email protected]> Co-authored-by: Armando Ruocco <[email protected]> Co-authored-by: Niccolò Fei <[email protected]>
1 parent 42261d0 commit 8d3af4d

11 files changed

Lines changed: 376 additions & 39 deletions

‎internal/webhook/v1/cluster_webhook.go‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,14 +1760,16 @@ func (v *ClusterCustomValidator) validateTablespaceNames(r *apiv1.Cluster) field
17601760
}
17611761

17621762
hasTablespace := make(map[string]bool)
1763+
sanitizedVolumeNames := stringset.New()
17631764
for idx, tbsConfig := range r.Spec.Tablespaces {
17641765
name := tbsConfig.Name
1766+
basePath := field.NewPath("spec", "tablespaces").Index(idx)
17651767
// NOTE: postgres identifiers are case-insensitive, so we could have
17661768
// different map keys (names) which are identical for PG
17671769
_, found := hasTablespace[strings.ToLower(name)]
17681770
if found {
17691771
result = append(result, field.Invalid(
1770-
field.NewPath("spec", "tablespaces").Index(idx),
1772+
basePath,
17711773
name,
17721774
"duplicate tablespace name"))
17731775
continue
@@ -1776,10 +1778,21 @@ func (v *ClusterCustomValidator) validateTablespaceNames(r *apiv1.Cluster) field
17761778

17771779
if _, err := postgres.IsTablespaceNameValid(name); err != nil {
17781780
result = append(result, field.Invalid(
1779-
field.NewPath("spec", "tablespaces").Index(idx),
1781+
basePath,
17801782
name,
17811783
err.Error()))
17821784
}
1785+
1786+
sanitizedName := specs.VolumeMountNameForTablespace(name)
1787+
if sanitizedVolumeNames.Has(sanitizedName) {
1788+
result = append(result, field.Invalid(
1789+
basePath,
1790+
name,
1791+
fmt.Sprintf("tablespace name results in duplicate volume name %q after sanitization "+
1792+
"(special characters are converted to hyphens)", sanitizedName),
1793+
))
1794+
}
1795+
sanitizedVolumeNames.Put(sanitizedName)
17831796
}
17841797
return result
17851798
}
@@ -2826,7 +2839,7 @@ func (v *ClusterCustomValidator) validateExtensions(r *apiv1.Cluster) field.Erro
28262839
}
28272840
extensionNames.Put(v.Name)
28282841

2829-
sanitizedName := strings.ReplaceAll(v.Name, "_", "-")
2842+
sanitizedName := specs.SanitizeExtensionNameForVolume(v.Name)
28302843
if sanitizedVolumeNames.Has(sanitizedName) {
28312844
result = append(result, field.Invalid(
28322845
basePath.Child("name"),

‎internal/webhook/v1/cluster_webhook_test.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4789,6 +4789,47 @@ var _ = Describe("Tablespaces validation", func() {
47894789
Expect(v.validate(cluster)).To(HaveLen(1))
47904790
})
47914791

4792+
It("should produce an error when tablespace names collide after sanitization", func() {
4793+
cluster := &apiv1.Cluster{
4794+
ObjectMeta: metav1.ObjectMeta{
4795+
Name: "cluster1",
4796+
},
4797+
Spec: apiv1.ClusterSpec{
4798+
Instances: 3,
4799+
StorageConfiguration: apiv1.StorageConfiguration{
4800+
Size: "10Gi",
4801+
},
4802+
Tablespaces: []apiv1.TablespaceConfiguration{
4803+
createFakeTemporaryTbsConf("foo_bar"),
4804+
createFakeTemporaryTbsConf("foo$bar"),
4805+
},
4806+
},
4807+
}
4808+
errors := v.validate(cluster)
4809+
Expect(errors).To(HaveLen(1))
4810+
Expect(errors[0].Type).To(Equal(field.ErrorTypeInvalid))
4811+
Expect(errors[0].Detail).To(ContainSubstring("duplicate volume name"))
4812+
})
4813+
4814+
It("should not produce an error when tablespace names are distinct after sanitization", func() {
4815+
cluster := &apiv1.Cluster{
4816+
ObjectMeta: metav1.ObjectMeta{
4817+
Name: "cluster1",
4818+
},
4819+
Spec: apiv1.ClusterSpec{
4820+
Instances: 3,
4821+
StorageConfiguration: apiv1.StorageConfiguration{
4822+
Size: "10Gi",
4823+
},
4824+
Tablespaces: []apiv1.TablespaceConfiguration{
4825+
createFakeTemporaryTbsConf("foo_bar"),
4826+
createFakeTemporaryTbsConf("baz_qux"),
4827+
},
4828+
},
4829+
}
4830+
Expect(v.validate(cluster)).To(BeEmpty())
4831+
})
4832+
47924833
It("should produce an error if the storage configured for the tablespace is invalid", func() {
47934834
cluster := &apiv1.Cluster{
47944835
ObjectMeta: metav1.ObjectMeta{

‎pkg/specs/pods_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ var _ = Describe("PodSpec drift detection", func() {
699699
SubPathExpr: "",
700700
},
701701
{
702-
Name: "bar",
702+
Name: "tbs-bar",
703703
ReadOnly: false,
704704
MountPath: "/var/lib/postgresql/tablespaces/bar",
705705
SubPath: "",
@@ -717,7 +717,7 @@ var _ = Describe("PodSpec drift detection", func() {
717717
Image: "postgres:13.11",
718718
VolumeMounts: []corev1.VolumeMount{
719719
{
720-
Name: "bar",
720+
Name: "tbs-bar",
721721
ReadOnly: false,
722722
MountPath: "/var/lib/postgresql/tablespaces/bar",
723723
SubPath: "",
@@ -803,7 +803,7 @@ var _ = Describe("PodSpec drift detection", func() {
803803
SubPathExpr: "",
804804
},
805805
{
806-
Name: "bar",
806+
Name: "tbs-bar",
807807
ReadOnly: false,
808808
MountPath: "/var/lib/postgresql/tablespaces/bar",
809809
SubPath: "",
@@ -821,7 +821,7 @@ var _ = Describe("PodSpec drift detection", func() {
821821
Image: "postgres:13.11",
822822
VolumeMounts: []corev1.VolumeMount{
823823
{
824-
Name: "bar",
824+
Name: "tbs-bar",
825825
ReadOnly: false,
826826
MountPath: "/var/lib/postgresql/tablespaces/bar",
827827
SubPath: "",

‎pkg/specs/podspec_diff.go‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ package specs
2222
import (
2323
"fmt"
2424
"reflect"
25+
"strings"
2526

2627
corev1 "k8s.io/api/core/v1"
2728
"k8s.io/apimachinery/pkg/api/equality"
29+
30+
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
31+
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
2832
)
2933

3034
// ComparePodSpecs compares two pod specs, returns true iff they are equivalent, and
@@ -132,11 +136,66 @@ func compareMaps[V comparable](current, target map[string]V) (bool, string) {
132136
return true, ""
133137
}
134138

139+
// isLegacyVolumeToIgnore returns true for volumes that were removed in 1.24
140+
// but may still exist on running pods.
141+
//
142+
// TODO: delete this function after minor version 1.24 is discontinued
143+
func isLegacyVolumeToIgnore(name string) bool {
144+
return name == "superuser-secret" || name == "app-secret"
145+
}
146+
147+
// normalizeVolumeName maps old unprefixed volume names to the new prefixed
148+
// scheme (ext- for extensions, tbs- for tablespaces) to avoid spurious
149+
// pod restarts on upgrade. Names already starting with the correct prefix
150+
// are left unchanged, which causes one spurious restart for extensions
151+
// named "ext_*" or tablespaces named "tbs_*".
152+
//
153+
// TODO: delete this function after minor version 1.28 is discontinued
154+
func normalizeVolumeName(vol corev1.Volume) string {
155+
name := vol.Name
156+
157+
if vol.Image != nil && !strings.HasPrefix(name, "ext-") {
158+
return SanitizeExtensionNameForVolume(name)
159+
}
160+
161+
if vol.PersistentVolumeClaim != nil &&
162+
strings.Contains(vol.PersistentVolumeClaim.ClaimName, apiv1.TablespaceVolumeInfix) &&
163+
!strings.HasPrefix(name, "tbs-") {
164+
return "tbs-" + name
165+
}
166+
167+
return name
168+
}
169+
170+
// normalizeVolumeMountName is the VolumeMount counterpart of
171+
// normalizeVolumeName, using mount paths to detect the volume type.
172+
//
173+
// TODO: delete this function after minor version 1.28 is discontinued
174+
func normalizeVolumeMountName(mount corev1.VolumeMount) string {
175+
name := mount.Name
176+
177+
extensionPathPrefix := postgres.ExtensionsBaseDirectory + "/"
178+
if strings.HasPrefix(mount.MountPath, extensionPathPrefix) && !strings.HasPrefix(name, "ext-") {
179+
return SanitizeExtensionNameForVolume(name)
180+
}
181+
182+
if strings.HasPrefix(mount.MountPath, PgTablespaceVolumePath+"/") && !strings.HasPrefix(name, "tbs-") {
183+
return "tbs-" + name
184+
}
185+
186+
return name
187+
}
188+
135189
func compareVolumes(currentVolumes, targetVolumes []corev1.Volume) (bool, string) {
136190
current := make(map[string]corev1.Volume)
137191
target := make(map[string]corev1.Volume)
138192
for _, vol := range currentVolumes {
139-
current[vol.Name] = vol
193+
if isLegacyVolumeToIgnore(vol.Name) {
194+
continue
195+
}
196+
normalized := normalizeVolumeName(vol)
197+
vol.Name = normalized
198+
current[normalized] = vol
140199
}
141200
for _, vol := range targetVolumes {
142201
target[vol.Name] = vol
@@ -149,7 +208,12 @@ func compareVolumeMounts(currentMounts, targetMounts []corev1.VolumeMount) (bool
149208
current := make(map[string]corev1.VolumeMount)
150209
target := make(map[string]corev1.VolumeMount)
151210
for _, mount := range currentMounts {
152-
current[mount.Name] = mount
211+
if isLegacyVolumeToIgnore(mount.Name) {
212+
continue
213+
}
214+
normalized := normalizeVolumeMountName(mount)
215+
mount.Name = normalized
216+
current[normalized] = mount
153217
}
154218
for _, mount := range targetMounts {
155219
target[mount.Name] = mount

0 commit comments

Comments
 (0)