diff --git a/pkg/controller/webhooks/postgresql.go b/pkg/controller/webhooks/postgresql.go index a2eb98849..f00a9ec28 100644 --- a/pkg/controller/webhooks/postgresql.go +++ b/pkg/controller/webhooks/postgresql.go @@ -101,15 +101,14 @@ func (p *PostgreSQLWebhookHandler) validatePostgreSQL(ctx context.Context, newOb } // Validate major upgrades - allErrs = append(allErrs, validateMajorVersionUpgrade(newPg, oldPg)) + if err := validateMajorVersionUpgrade(newPg, oldPg); err != nil { + allErrs = append(allErrs, err) + } } // Validate Vacuum and Repack settings if err := validateVacuumRepack(newPg.Spec.Parameters.Service.VacuumEnabled, newPg.Spec.Parameters.Service.RepackEnabled); err != nil { - allErrs = append(allErrs, field.Forbidden( - field.NewPath("spec.parameters.service"), - fmt.Sprintf("pg.Spec.Parameters.Service.VacuumEnabled and pg.Spec.Parameters.Service.RepackEnabled settings can't be both disabled: %s", err.Error()), - )) + allErrs = append(allErrs, err) } // Validate quotas if enabled @@ -244,9 +243,12 @@ func (p *PostgreSQLWebhookHandler) checkGuaranteedAvailability(pg *vshnv1.VSHNPo return allErrs } -func validateVacuumRepack(vacuum, repack bool) error { +func validateVacuumRepack(vacuum, repack bool) *field.Error { if !vacuum && !repack { - return fmt.Errorf("repack cannot be enabled without vacuum") + return field.Forbidden( + field.NewPath("spec.parameters.service"), + "pg.Spec.Parameters.Service.VacuumEnabled and pg.Spec.Parameters.Service.RepackEnabled settings can't be both disabled", + ) } return nil } diff --git a/pkg/controller/webhooks/postgresql_test.go b/pkg/controller/webhooks/postgresql_test.go index 5602a3f3a..999ffbeb2 100644 --- a/pkg/controller/webhooks/postgresql_test.go +++ b/pkg/controller/webhooks/postgresql_test.go @@ -2,6 +2,7 @@ package webhooks import ( "context" + "k8s.io/apimachinery/pkg/util/validation/field" "testing" "github.com/go-logr/logr" @@ -492,3 +493,113 @@ func TestPostgreSQLWebhookHandler_ValidateDelete(t *testing.T) { assert.NoError(t, err) } + +func TestPostgreSQLWebhookHandler_ValidateMajorVersionUpgrade(t *testing.T) { + tests := []struct { + name string + new *vshnv1.VSHNPostgreSQL + old *vshnv1.VSHNPostgreSQL + expectErr *field.Error + }{ + { + name: "GivenSameMajorVersion_ThenNoError", + new: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "15", + }, + }, + }, + }, + old: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "15", + }, + }, + }, + }, + expectErr: nil, + }, + { + name: "GivenOneMajorVersionUpdate_ThenNoError", + new: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "16", + }, + }, + }, + }, + old: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "15", + }, + }, + }, + }, + expectErr: nil, + }, + { + name: "GivenTwoMajorVersionsUpdate_ThenError", + new: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "17", + }, + }, + }, + }, + old: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "15", + }, + }, + }, + }, + expectErr: field.Forbidden( + field.NewPath("spec.parameters.service.majorVersion"), + "only one major version upgrade at a time is allowed", + ), + }, + { + name: "GivenOneMajorVersionsBehind_ThenError", + new: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "14", + }, + }, + }, + }, + old: &vshnv1.VSHNPostgreSQL{ + Spec: vshnv1.VSHNPostgreSQLSpec{ + Parameters: vshnv1.VSHNPostgreSQLParameters{ + Service: vshnv1.VSHNPostgreSQLServiceSpec{ + MajorVersion: "15", + }, + }, + }, + }, + expectErr: field.Forbidden( + field.NewPath("spec.parameters.service.majorVersion"), + "only one major version upgrade at a time is allowed", + ), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := validateMajorVersionUpgrade(tt.new, tt.old) + assert.Equal(t, tt.expectErr, err) + }) + } +}