Skip to content

Commit 62dfaba

Browse files
committed
minnor version check
1 parent 74dca92 commit 62dfaba

2 files changed

Lines changed: 98 additions & 13 deletions

File tree

pkg/postgresql/cluster/adapter/webhook/postgrescluster_validation.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ func validateAgainstClass(obj *enterpriseApi.PostgresCluster, reader client.Read
108108
}
109109

110110
if obj.Spec.PostgresVersion != nil && classConfig.PostgresVersion != nil {
111-
clusterMajor := parseMajorVersion(*obj.Spec.PostgresVersion)
112-
classMajor := parseMajorVersion(*classConfig.PostgresVersion)
113-
if clusterMajor > 0 && classMajor > 0 && clusterMajor < classMajor {
114-
allErrs = append(allErrs, field.Invalid(
115-
field.NewPath("spec").Child("postgresVersion"),
116-
*obj.Spec.PostgresVersion,
117-
"postgresVersion cannot be lower than class default ("+*classConfig.PostgresVersion+")"))
111+
clusterMajor, clusterMinor := parseVersion(*obj.Spec.PostgresVersion)
112+
classMajor, classMinor := parseVersion(*classConfig.PostgresVersion)
113+
if clusterMajor > 0 && classMajor > 0 {
114+
versionTooLow := clusterMajor < classMajor ||
115+
(clusterMajor == classMajor && classMinor >= 0 && clusterMinor >= 0 && clusterMinor < classMinor)
116+
if versionTooLow {
117+
allErrs = append(allErrs, field.Invalid(
118+
field.NewPath("spec").Child("postgresVersion"),
119+
*obj.Spec.PostgresVersion,
120+
"postgresVersion cannot be lower than class default ("+*classConfig.PostgresVersion+")"))
121+
}
118122
}
119123
}
120124

@@ -131,15 +135,16 @@ func validateAgainstClass(obj *enterpriseApi.PostgresCluster, reader client.Read
131135
return allErrs
132136
}
133137

134-
func parseMajorVersion(version string) int {
138+
func parseVersion(version string) (major, minor int) {
135139
for i, ch := range version {
136140
if ch == '.' {
137-
v, _ := strconv.Atoi(version[:i])
138-
return v
141+
major, _ = strconv.Atoi(version[:i])
142+
minor, _ = strconv.Atoi(version[i+1:])
143+
return major, minor
139144
}
140145
}
141-
v, _ := strconv.Atoi(version)
142-
return v
146+
major, _ = strconv.Atoi(version)
147+
return major, -1
143148
}
144149

145150
// GetPostgresClusterWarningsOnCreate returns warnings for PostgresCluster CREATE.

pkg/postgresql/cluster/adapter/webhook/postgrescluster_validation_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ func TestValidateAgainstClass(t *testing.T) {
230230
},
231231
}
232232

233+
classWithMinorVersion := &enterpriseApi.PostgresClusterClass{
234+
ObjectMeta: metav1.ObjectMeta{Name: "prod-pinned"},
235+
Spec: enterpriseApi.PostgresClusterClassSpec{
236+
Provisioner: "postgresql.cnpg.io",
237+
Config: &enterpriseApi.PostgresClusterClassConfig{
238+
Instances: ptrInt32(3),
239+
Storage: ptrQuantity("50Gi"),
240+
PostgresVersion: ptrString("17.2"),
241+
},
242+
},
243+
}
244+
233245
classWithPoolerEnabled := &enterpriseApi.PostgresClusterClass{
234246
ObjectMeta: metav1.ObjectMeta{Name: "pooler-class"},
235247
Spec: enterpriseApi.PostgresClusterClassSpec{
@@ -336,16 +348,84 @@ func TestValidateAgainstClass(t *testing.T) {
336348
wantErrField: "spec.postgresVersion",
337349
},
338350
{
339-
name: "valid - minor version higher",
351+
name: "valid - minor version ignored when class has major only",
352+
class: classWithDefaults,
353+
obj: &enterpriseApi.PostgresCluster{
354+
Spec: enterpriseApi.PostgresClusterSpec{
355+
Class: "prod",
356+
PostgresVersion: ptrString("17.2"),
357+
},
358+
},
359+
wantErrCount: 0,
360+
},
361+
{
362+
name: "valid - lower minor ignored when class has major only",
340363
class: classWithDefaults,
341364
obj: &enterpriseApi.PostgresCluster{
342365
Spec: enterpriseApi.PostgresClusterSpec{
343366
Class: "prod",
367+
PostgresVersion: ptrString("17.0"),
368+
},
369+
},
370+
wantErrCount: 0,
371+
},
372+
{
373+
name: "valid - cluster minor equal to class minor",
374+
class: classWithMinorVersion,
375+
obj: &enterpriseApi.PostgresCluster{
376+
Spec: enterpriseApi.PostgresClusterSpec{
377+
Class: "prod-pinned",
344378
PostgresVersion: ptrString("17.2"),
345379
},
346380
},
347381
wantErrCount: 0,
348382
},
383+
{
384+
name: "valid - cluster minor higher than class minor",
385+
class: classWithMinorVersion,
386+
obj: &enterpriseApi.PostgresCluster{
387+
Spec: enterpriseApi.PostgresClusterSpec{
388+
Class: "prod-pinned",
389+
PostgresVersion: ptrString("17.5"),
390+
},
391+
},
392+
wantErrCount: 0,
393+
},
394+
{
395+
name: "invalid - cluster minor lower than class minor",
396+
class: classWithMinorVersion,
397+
obj: &enterpriseApi.PostgresCluster{
398+
Spec: enterpriseApi.PostgresClusterSpec{
399+
Class: "prod-pinned",
400+
PostgresVersion: ptrString("17.1"),
401+
},
402+
},
403+
wantErrCount: 1,
404+
wantErrField: "spec.postgresVersion",
405+
},
406+
{
407+
name: "invalid - cluster major lower even with higher minor",
408+
class: classWithMinorVersion,
409+
obj: &enterpriseApi.PostgresCluster{
410+
Spec: enterpriseApi.PostgresClusterSpec{
411+
Class: "prod-pinned",
412+
PostgresVersion: ptrString("16.9"),
413+
},
414+
},
415+
wantErrCount: 1,
416+
wantErrField: "spec.postgresVersion",
417+
},
418+
{
419+
name: "valid - cluster major higher than class with minor",
420+
class: classWithMinorVersion,
421+
obj: &enterpriseApi.PostgresCluster{
422+
Spec: enterpriseApi.PostgresClusterSpec{
423+
Class: "prod-pinned",
424+
PostgresVersion: ptrString("18"),
425+
},
426+
},
427+
wantErrCount: 0,
428+
},
349429
{
350430
name: "invalid - connection pooler enabled when class disables",
351431
class: classWithDefaults,

0 commit comments

Comments
 (0)