From 99ff75310dddc7b1dfb236e97c64b66af1fd699d Mon Sep 17 00:00:00 2001 From: Nicolas Bigler Date: Thu, 28 Nov 2024 14:10:52 +0200 Subject: [PATCH] Set the correct version for the SGCluster We need to ensure that the SGCluster always contains the "correct" version and that we don't override it with just a major version. The reason is, that Stackgres doesn't allow changing the version number in the `SGCluster` object. Since stackgres changes the version field during a minor upgrade, it won't be possible anymore to "revert" this change to a major version only setting. Signed-off-by: Nicolas Bigler --- .../functions/vshnpostgres/maintenance.go | 24 +++++++++++++++++++ .../vshnpostgres/postgresql_deploy_test.go | 1 - .../vshn-postgres/deploy/01_default.yaml | 2 +- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/comp-functions/functions/vshnpostgres/maintenance.go b/pkg/comp-functions/functions/vshnpostgres/maintenance.go index 8703cb1a5..47ae24a4d 100644 --- a/pkg/comp-functions/functions/vshnpostgres/maintenance.go +++ b/pkg/comp-functions/functions/vshnpostgres/maintenance.go @@ -14,6 +14,7 @@ import ( "github.com/vshn/appcat/v4/pkg/comp-functions/runtime" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" ) @@ -129,6 +130,11 @@ func addSchedules(ctx context.Context, comp *vshnv1.VSHNPostgreSQL, svc *runtime }, }...) + err = setPsqlMinorVersion(svc, cluster) + if err != nil { + return runtime.NewWarningResult(fmt.Errorf("cannot set the minor version for the PostgreSQL instance: %w", err).Error()) + } + backups := *cluster.Spec.Configurations.Backups backups[0].CronSchedule = ptr.To(comp.GetBackupSchedule()) cluster.Spec.Configurations.Backups = &backups @@ -178,3 +184,21 @@ func createMaintenanceSecret(instanceNamespace, sgNamespace, resourceName string }, } } + +func setPsqlMinorVersion(svc *runtime.ServiceRuntime, desiredCluster *stackgresv1.SGCluster) error { + observedCluster := &stackgresv1.SGCluster{} + err := svc.GetObservedKubeObject(observedCluster, "cluster") + if errors.IsNotFound(err) { + // Cluster doesn't exist yet. So let's ignore it here + return nil + } + if err != nil { + return fmt.Errorf("cannot get observed cluster object: %w", err) + } + + pgVersion := observedCluster.Spec.Postgres.Version + + desiredCluster.Spec.Postgres.Version = pgVersion + + return nil +} diff --git a/pkg/comp-functions/functions/vshnpostgres/postgresql_deploy_test.go b/pkg/comp-functions/functions/vshnpostgres/postgresql_deploy_test.go index 8ce0cee33..9b494ed15 100644 --- a/pkg/comp-functions/functions/vshnpostgres/postgresql_deploy_test.go +++ b/pkg/comp-functions/functions/vshnpostgres/postgresql_deploy_test.go @@ -59,7 +59,6 @@ func TestPostgreSqlDeploy(t *testing.T) { assert.Equal(t, comp.GetName(), cluster.Name) assert.Equal(t, comp.GetInstanceNamespace(), cluster.Namespace) assert.Equal(t, comp.Spec.Parameters.Instances, cluster.Spec.Instances) - assert.Equal(t, comp.Spec.Parameters.Service.MajorVersion, cluster.Spec.Postgres.Version) assert.Nil(t, cluster.Spec.InitialData.Restore) assert.Equal(t, comp.GetName(), *cluster.Spec.SgInstanceProfile) assert.Equal(t, comp.GetName(), *cluster.Spec.Configurations.SgPostgresConfig) diff --git a/test/functions/vshn-postgres/deploy/01_default.yaml b/test/functions/vshn-postgres/deploy/01_default.yaml index 927522219..5d6822765 100644 --- a/test/functions/vshn-postgres/deploy/01_default.yaml +++ b/test/functions/vshn-postgres/deploy/01_default.yaml @@ -282,7 +282,7 @@ observed: privateKeySecretKeySelector: key: tls.key name: tls-certificate - version: "15" + version: "15.1" sgInstanceProfile: psql-gc9x4 managementPolicy: Default providerConfigRef: