Skip to content

Commit b550f8a

Browse files
authored
fix unit test for new subPathExpr feature (zalando#2638)
* fix unit test for new subPathExpr feature * add subPathExpr flag to CRD and re-sort
1 parent 7bcb73a commit b550f8a

File tree

9 files changed

+78
-52
lines changed

9 files changed

+78
-52
lines changed

charts/postgres-operator/crds/postgresqls.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ spec:
8787
- mountPath
8888
- volumeSource
8989
properties:
90+
isSubPathExpr:
91+
type: boolean
9092
name:
9193
type: string
9294
mountPath:
9395
type: string
96+
subPath:
97+
type: string
9498
targetContainers:
9599
type: array
96100
nullable: true
@@ -99,10 +103,6 @@ spec:
99103
volumeSource:
100104
type: object
101105
x-kubernetes-preserve-unknown-fields: true
102-
subPath:
103-
type: string
104-
isSubPathExpr:
105-
type: boolean
106106
allowedSourceRanges:
107107
type: array
108108
nullable: true
@@ -636,6 +636,8 @@ spec:
636636
required:
637637
- size
638638
properties:
639+
isSubPathExpr:
640+
type: boolean
639641
iops:
640642
type: integer
641643
selector:

docs/reference/cluster_manifest.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ properties of the persistent storage that stores Postgres data.
485485
* **subPath**
486486
Subpath to use when mounting volume into Spilo container. Optional.
487487

488+
* **isSubPathExpr**
489+
Set it to true if the specified subPath is an expression. Optional.
490+
488491
* **iops**
489492
When running the operator on AWS the latest generation of EBS volumes (`gp3`)
490493
allows for configuring the number of IOPS. Maximum is 16000. Optional.

manifests/complete-postgres-manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ spec:
6868
# matchLabels:
6969
# environment: dev
7070
# service: postgres
71+
# subPath: $(NODE_NAME)/$(POD_NAME)
72+
# isSubPathExpr: true
7173
additionalVolumes:
7274
- name: empty
7375
mountPath: /opt/empty

manifests/postgresql.crd.yaml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,14 @@ spec:
8585
- mountPath
8686
- volumeSource
8787
properties:
88+
isSubPathExpr:
89+
type: boolean
8890
name:
8991
type: string
9092
mountPath:
9193
type: string
94+
subPath:
95+
type: string
9296
targetContainers:
9397
type: array
9498
nullable: true
@@ -97,10 +101,6 @@ spec:
97101
volumeSource:
98102
type: object
99103
x-kubernetes-preserve-unknown-fields: true
100-
subPath:
101-
type: string
102-
isSubPathExpr:
103-
type: boolean
104104
allowedSourceRanges:
105105
type: array
106106
nullable: true
@@ -634,6 +634,8 @@ spec:
634634
required:
635635
- size
636636
properties:
637+
isSubPathExpr:
638+
type: boolean
637639
iops:
638640
type: integer
639641
selector:

pkg/apis/acid.zalan.do/v1/crds.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,18 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
146146
Type: "object",
147147
Required: []string{"name", "mountPath", "volumeSource"},
148148
Properties: map[string]apiextv1.JSONSchemaProps{
149+
"isSubPathExpr": {
150+
Type: "boolean",
151+
},
149152
"name": {
150153
Type: "string",
151154
},
152155
"mountPath": {
153156
Type: "string",
154157
},
158+
"subPath": {
159+
Type: "string",
160+
},
155161
"targetContainers": {
156162
Type: "array",
157163
Nullable: true,
@@ -165,12 +171,6 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
165171
Type: "object",
166172
XPreserveUnknownFields: util.True(),
167173
},
168-
"subPath": {
169-
Type: "string",
170-
},
171-
"isSubPathExpr": {
172-
Type: "boolean",
173-
},
174174
},
175175
},
176176
},
@@ -1033,6 +1033,9 @@ var PostgresCRDResourceValidation = apiextv1.CustomResourceValidation{
10331033
Type: "object",
10341034
Required: []string{"size"},
10351035
Properties: map[string]apiextv1.JSONSchemaProps{
1036+
"isSubPathExpr": {
1037+
Type: "boolean",
1038+
},
10361039
"iops": {
10371040
Type: "integer",
10381041
},

pkg/apis/acid.zalan.do/v1/postgresql_type.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,22 @@ type MaintenanceWindow struct {
129129

130130
// Volume describes a single volume in the manifest.
131131
type Volume struct {
132-
Selector *metav1.LabelSelector `json:"selector,omitempty"`
133-
Size string `json:"size"`
134-
StorageClass string `json:"storageClass,omitempty"`
135-
SubPath string `json:"subPath,omitempty"`
136-
Iops *int64 `json:"iops,omitempty"`
137-
Throughput *int64 `json:"throughput,omitempty"`
138-
VolumeType string `json:"type,omitempty"`
132+
Selector *metav1.LabelSelector `json:"selector,omitempty"`
133+
Size string `json:"size"`
134+
StorageClass string `json:"storageClass,omitempty"`
135+
SubPath string `json:"subPath,omitempty"`
136+
IsSubPathExpr *bool `json:"isSubPathExpr,omitemtpy"`
137+
Iops *int64 `json:"iops,omitempty"`
138+
Throughput *int64 `json:"throughput,omitempty"`
139+
VolumeType string `json:"type,omitempty"`
139140
}
140141

141142
// AdditionalVolume specs additional optional volumes for statefulset
142143
type AdditionalVolume struct {
143144
Name string `json:"name"`
144145
MountPath string `json:"mountPath"`
145146
SubPath string `json:"subPath,omitempty"`
146-
IsSubPathExpr bool `json:"isSubPathExpr,omitemtpy"`
147+
IsSubPathExpr *bool `json:"isSubPathExpr,omitemtpy"`
147148
TargetContainers []string `json:"targetContainers"`
148149
VolumeSource v1.VolumeSource `json:"volumeSource"`
149150
}

pkg/apis/acid.zalan.do/v1/zz_generated.deepcopy.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/k8sres.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,19 @@ func isBootstrapOnlyParameter(param string) bool {
668668
}
669669

670670
func generateVolumeMounts(volume acidv1.Volume) []v1.VolumeMount {
671-
return []v1.VolumeMount{
671+
volumeMount := []v1.VolumeMount{
672672
{
673673
Name: constants.DataVolumeName,
674674
MountPath: constants.PostgresDataMount, //TODO: fetch from manifest
675-
SubPath: volume.SubPath,
676675
},
677676
}
677+
678+
if volume.IsSubPathExpr != nil && *volume.IsSubPathExpr {
679+
volumeMount[0].SubPathExpr = volume.SubPath
680+
} else {
681+
volumeMount[0].SubPath = volume.SubPath
682+
}
683+
return volumeMount
678684
}
679685

680686
func generateContainer(
@@ -1825,7 +1831,7 @@ func (c *Cluster) addAdditionalVolumes(podSpec *v1.PodSpec,
18251831
MountPath: additionalVolume.MountPath,
18261832
}
18271833

1828-
if additionalVolume.IsSubPathExpr {
1834+
if additionalVolume.IsSubPathExpr != nil && *additionalVolume.IsSubPathExpr {
18291835
v.SubPathExpr = additionalVolume.SubPath
18301836
} else {
18311837
v.SubPath = additionalVolume.SubPath

pkg/cluster/k8sres_test.go

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ func TestAdditionalVolume(t *testing.T) {
19021902
Name: "test6",
19031903
MountPath: "/test6",
19041904
SubPath: "$(POD_NAME)",
1905-
IsSubPathExpr: true,
1905+
IsSubPathExpr: util.True(),
19061906
TargetContainers: nil, // should mount only to postgres
19071907
VolumeSource: v1.VolumeSource{
19081908
EmptyDir: &v1.EmptyDirVolumeSource{},
@@ -1922,7 +1922,9 @@ func TestAdditionalVolume(t *testing.T) {
19221922
ResourceLimits: acidv1.ResourceDescription{CPU: k8sutil.StringToPointer("1"), Memory: k8sutil.StringToPointer("10")},
19231923
},
19241924
Volume: acidv1.Volume{
1925-
Size: "1G",
1925+
Size: "1G",
1926+
SubPath: "$(POD_NAME)",
1927+
IsSubPathExpr: util.True(),
19261928
},
19271929
AdditionalVolumes: additionalVolumes,
19281930
Sidecars: []acidv1.Sidecar{
@@ -1954,31 +1956,25 @@ func TestAdditionalVolume(t *testing.T) {
19541956
assert.NoError(t, err)
19551957

19561958
tests := []struct {
1957-
subTest string
1958-
container string
1959-
expectedMounts []string
1960-
expectedSubPath []string
1959+
subTest string
1960+
container string
1961+
expectedMounts []string
1962+
expectedSubPaths []string
1963+
expectedSubPathExprs []string
19611964
}{
19621965
{
1963-
subTest: "checking volume mounts of postgres container",
1964-
container: constants.PostgresContainerName,
1965-
expectedMounts: []string{"pgdata", "test1", "test3", "test4"},
1966-
},
1967-
{
1968-
subTest: "checking volume mounts of sidecar container",
1969-
container: "sidecar",
1970-
expectedMounts: []string{"pgdata", "test1", "test2"},
1966+
subTest: "checking volume mounts of postgres container",
1967+
container: constants.PostgresContainerName,
1968+
expectedMounts: []string{"pgdata", "test1", "test3", "test4", "test5", "test6"},
1969+
expectedSubPaths: []string{"", "", "", "", "subpath", ""},
1970+
expectedSubPathExprs: []string{"$(POD_NAME)", "", "", "", "", "$(POD_NAME)"},
19711971
},
19721972
{
1973-
subTest: "checking volume mounts with subPath",
1974-
container: constants.PostgresContainerName,
1975-
expectedMounts: []string{"test5"},
1976-
expectedSubPath: []string{"subpath"},
1977-
},
1978-
{
1979-
subTest: "checking volume mounts with subPathExpr",
1980-
container: constants.PostgresContainerName,
1981-
expectedMounts: []string{"test6"},
1973+
subTest: "checking volume mounts of sidecar container",
1974+
container: "sidecar",
1975+
expectedMounts: []string{"pgdata", "test1", "test2"},
1976+
expectedSubPaths: []string{"", "", ""},
1977+
expectedSubPathExprs: []string{"$(POD_NAME)", "", ""},
19821978
},
19831979
}
19841980

@@ -1990,6 +1986,7 @@ func TestAdditionalVolume(t *testing.T) {
19901986
mounts := []string{}
19911987
subPaths := []string{}
19921988
subPathExprs := []string{}
1989+
19931990
for _, volumeMounts := range container.VolumeMounts {
19941991
mounts = append(mounts, volumeMounts.Name)
19951992
subPaths = append(subPaths, volumeMounts.SubPath)
@@ -2001,14 +1998,14 @@ func TestAdditionalVolume(t *testing.T) {
20011998
t.Name(), tt.subTest, mounts, tt.expectedMounts)
20021999
}
20032000

2004-
if !util.IsEqualIgnoreOrder(subPaths, tt.expectedSubPath) {
2001+
if !util.IsEqualIgnoreOrder(subPaths, tt.expectedSubPaths) {
20052002
t.Errorf("%s %s: different volume subPaths: got %v, expected %v",
2006-
t.Name(), tt.subTest, mounts, tt.expectedSubPath)
2003+
t.Name(), tt.subTest, subPaths, tt.expectedSubPaths)
20072004
}
20082005

2009-
if !util.IsEqualIgnoreOrder(subPathExprs, []string{container.Name}) {
2006+
if !util.IsEqualIgnoreOrder(subPathExprs, tt.expectedSubPathExprs) {
20102007
t.Errorf("%s %s: different volume subPathExprs: got %v, expected %v",
2011-
t.Name(), tt.subTest, mounts, tt.expectedMounts)
2008+
t.Name(), tt.subTest, subPathExprs, tt.expectedSubPathExprs)
20122009
}
20132010
}
20142011
}

0 commit comments

Comments
 (0)