From a16223e2fbabc2d8b628b79531be7fbafe37b9ec Mon Sep 17 00:00:00 2001 From: rhkp <87712456+rhkp@users.noreply.github.com> Date: Wed, 21 Aug 2024 14:51:23 -0400 Subject: [PATCH] Add migrateDBOnStart property under persistence (#507) --- Makefile | 4 ++-- api/v1alpha08/sonataflow_persistence_types.go | 5 ++++- .../sonataflow.org_sonataflowplatforms.yaml | 14 +++++++++++-- .../manifests/sonataflow.org_sonataflows.yaml | 7 ++++++- .../sonataflow.org_sonataflowplatforms.yaml | 14 +++++++++++-- .../crd/bases/sonataflow.org_sonataflows.yaml | 7 ++++++- controllers/platform/services/services.go | 8 +++++-- .../sonataflowplatform_controller_test.go | 12 +++++++++-- operator.yaml | 21 ++++++++++++++++--- .../02-sonataflow_platform.yaml | 4 ++++ .../02-sonataflow_platform.yaml | 2 ++ .../postgreSQL/02-sonataflow_platform.yaml | 2 ++ .../postgreSQL/02-sonataflow_platform.yaml | 2 ++ ...4-sonataflow_callbackstatetimeouts.sw.yaml | 1 + ...3-sonataflow_callbackstatetimeouts.sw.yaml | 1 + .../02-sonataflow_platform.yaml | 4 ++++ ...llbackstatetimeouts-no-persistence.sw.yaml | 4 +++- 17 files changed, 95 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index f88a8759e..ddb2ff8b3 100644 --- a/Makefile +++ b/Makefile @@ -154,7 +154,7 @@ build-4-debug: generate ## Build manager binary with debug options. .PHONY: run run: manifests generate ## Run a controller from your host. - go run ./main.go + go run ./main.go -v=2 -controller-cfg-path=$(CURDIR)/config/manager/controllers_cfg.yaml .PHONY: debug debug: build-4-debug ## Run a controller from your host from binary @@ -338,7 +338,7 @@ generate-all: generate generate-deploy bundle addheaders vet fmt .PHONY: test-e2e # You will need to have a Minikube/Kind cluster up in running to run this target, and run container-builder before the test test-e2e: - go test ./test/e2e/* -v -ginkgo.v -ginkgo.no-color -ginkgo.junit-report=./e2e-test-report.xml -timeout 60m + go test ./test/e2e/* -v -ginkgo.v -ginkgo.no-color -ginkgo.junit-report=./e2e-test-report.xml -timeout 120m .PHONY: before-pr before-pr: test generate-all diff --git a/api/v1alpha08/sonataflow_persistence_types.go b/api/v1alpha08/sonataflow_persistence_types.go index d0b08e467..e32e027c9 100644 --- a/api/v1alpha08/sonataflow_persistence_types.go +++ b/api/v1alpha08/sonataflow_persistence_types.go @@ -45,11 +45,14 @@ type PlatformPersistencePostgreSQL struct { // the operator will add the necessary JDBC properties to in the workflow's application.properties so that it can communicate // with the persistence service based on the spec provided here. // +optional -// +kubebuilder:validation:MaxProperties=1 +// +kubebuilder:validation:MaxProperties=2 type PersistenceOptionsSpec struct { // Connect configured services to a postgresql database. // +optional PostgreSQL *PersistencePostgreSQL `json:"postgresql,omitempty"` + + // Whether to migrate database on service startup? + MigrateDBOnStartUp bool `json:"migrateDBOnStartUp"` } // PersistencePostgreSQL configure postgresql connection for service(s). diff --git a/bundle/manifests/sonataflow.org_sonataflowplatforms.yaml b/bundle/manifests/sonataflow.org_sonataflowplatforms.yaml index 09a2f2544..f9d772261 100644 --- a/bundle/manifests/sonataflow.org_sonataflowplatforms.yaml +++ b/bundle/manifests/sonataflow.org_sonataflowplatforms.yaml @@ -567,8 +567,11 @@ spec: persistence: description: Persists service to a datasource of choice. Ephemeral by default. - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. @@ -625,6 +628,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details @@ -8439,8 +8444,11 @@ spec: persistence: description: Persists service to a datasource of choice. Ephemeral by default. - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. @@ -8497,6 +8505,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details diff --git a/bundle/manifests/sonataflow.org_sonataflows.yaml b/bundle/manifests/sonataflow.org_sonataflows.yaml index 5ff75cae2..effa0cd83 100644 --- a/bundle/manifests/sonataflow.org_sonataflows.yaml +++ b/bundle/manifests/sonataflow.org_sonataflows.yaml @@ -2105,8 +2105,11 @@ spec: persistence: description: Persistence defines the database persistence configuration for the workflow - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. maxProperties: 2 @@ -2160,6 +2163,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details of this diff --git a/config/crd/bases/sonataflow.org_sonataflowplatforms.yaml b/config/crd/bases/sonataflow.org_sonataflowplatforms.yaml index 648b1bcde..c4587db75 100644 --- a/config/crd/bases/sonataflow.org_sonataflowplatforms.yaml +++ b/config/crd/bases/sonataflow.org_sonataflowplatforms.yaml @@ -568,8 +568,11 @@ spec: persistence: description: Persists service to a datasource of choice. Ephemeral by default. - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. @@ -626,6 +629,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details @@ -8440,8 +8445,11 @@ spec: persistence: description: Persists service to a datasource of choice. Ephemeral by default. - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. @@ -8498,6 +8506,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details diff --git a/config/crd/bases/sonataflow.org_sonataflows.yaml b/config/crd/bases/sonataflow.org_sonataflows.yaml index 7ee4675cf..fffbbbb17 100644 --- a/config/crd/bases/sonataflow.org_sonataflows.yaml +++ b/config/crd/bases/sonataflow.org_sonataflows.yaml @@ -2106,8 +2106,11 @@ spec: persistence: description: Persistence defines the database persistence configuration for the workflow - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. maxProperties: 2 @@ -2161,6 +2164,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details of this diff --git a/controllers/platform/services/services.go b/controllers/platform/services/services.go index 58e068daa..f2455d8a5 100644 --- a/controllers/platform/services/services.go +++ b/controllers/platform/services/services.go @@ -21,6 +21,7 @@ package services import ( "fmt" + "strconv" "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/cfg" "github.com/apache/incubator-kie-kogito-serverless-operator/utils/kubernetes" @@ -224,8 +225,10 @@ func (d DataIndexHandler) ConfigurePersistence(containerSpec *corev1.Container) c := containerSpec.DeepCopy() c.Image = d.GetServiceImageName(constants.PersistenceTypePostgreSQL) c.Env = append(c.Env, persistence.ConfigurePostgreSQLEnv(p.PostgreSQL, d.GetServiceName(), d.platform.Namespace)...) + migrateDBOnStart := strconv.FormatBool(d.platform.Spec.Services.DataIndex.Persistence.MigrateDBOnStartUp) // specific to DataIndex - c.Env = append(c.Env, corev1.EnvVar{Name: quarkusHibernateORMDatabaseGeneration, Value: "update"}, corev1.EnvVar{Name: quarkusFlywayMigrateAtStart, Value: "true"}) + c.Env = append(c.Env, corev1.EnvVar{Name: quarkusHibernateORMDatabaseGeneration, Value: "update"}, corev1.EnvVar{Name: quarkusFlywayMigrateAtStart, Value: migrateDBOnStart}) + return c } return containerSpec @@ -389,7 +392,8 @@ func (j JobServiceHandler) ConfigurePersistence(containerSpec *corev1.Container) p := persistence.RetrievePostgreSQLConfiguration(j.platform.Spec.Services.JobService.Persistence, j.platform.Spec.Persistence, j.GetServiceName()) c.Env = append(c.Env, persistence.ConfigurePostgreSQLEnv(p.PostgreSQL, j.GetServiceName(), j.platform.Namespace)...) // Specific to Job Service - c.Env = append(c.Env, corev1.EnvVar{Name: "QUARKUS_FLYWAY_MIGRATE_AT_START", Value: "true"}) + migrateDBOnStart := strconv.FormatBool(j.platform.Spec.Services.JobService.Persistence.MigrateDBOnStartUp) + c.Env = append(c.Env, corev1.EnvVar{Name: "QUARKUS_FLYWAY_MIGRATE_AT_START", Value: migrateDBOnStart}) c.Env = append(c.Env, corev1.EnvVar{Name: "KOGITO_JOBS_SERVICE_LOADJOBERRORSTRATEGY", Value: "FAIL_SERVICE"}) return c } diff --git a/controllers/sonataflowplatform_controller_test.go b/controllers/sonataflowplatform_controller_test.go index 022c76063..23230d8fc 100644 --- a/controllers/sonataflowplatform_controller_test.go +++ b/controllers/sonataflowplatform_controller_test.go @@ -260,8 +260,16 @@ func TestSonataFlowPlatformController(t *testing.T) { // Check with persistence set ksp.Spec = v1alpha08.SonataFlowPlatformSpec{ Services: &v1alpha08.ServicesPlatformSpec{ - DataIndex: &v1alpha08.ServiceSpec{}, - JobService: &v1alpha08.ServiceSpec{}, + DataIndex: &v1alpha08.ServiceSpec{ + Persistence: &v1alpha08.PersistenceOptionsSpec{ + MigrateDBOnStartUp: false, + }, + }, + JobService: &v1alpha08.ServiceSpec{ + Persistence: &v1alpha08.PersistenceOptionsSpec{ + MigrateDBOnStartUp: false, + }, + }, }, Persistence: &v1alpha08.PlatformPersistenceOptionsSpec{ PostgreSQL: &v1alpha08.PlatformPersistencePostgreSQL{ diff --git a/operator.yaml b/operator.yaml index aed41049f..a1b679ac6 100644 --- a/operator.yaml +++ b/operator.yaml @@ -1059,8 +1059,11 @@ spec: persistence: description: Persists service to a datasource of choice. Ephemeral by default. - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. @@ -1117,6 +1120,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details @@ -8931,8 +8936,11 @@ spec: persistence: description: Persists service to a datasource of choice. Ephemeral by default. - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. @@ -8989,6 +8997,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details @@ -19002,8 +19012,11 @@ spec: persistence: description: Persistence defines the database persistence configuration for the workflow - maxProperties: 1 + maxProperties: 2 properties: + migrateDBOnStartUp: + description: Whether to migrate database on service startup? + type: boolean postgresql: description: Connect configured services to a postgresql database. maxProperties: 2 @@ -19057,6 +19070,8 @@ spec: required: - secretRef type: object + required: + - migrateDBOnStartUp type: object podTemplate: description: PodTemplate describes the deployment details of this diff --git a/test/testdata/platform/persistence/generic_from_platform_cr/02-sonataflow_platform.yaml b/test/testdata/platform/persistence/generic_from_platform_cr/02-sonataflow_platform.yaml index d746ac94b..cce1782e6 100644 --- a/test/testdata/platform/persistence/generic_from_platform_cr/02-sonataflow_platform.yaml +++ b/test/testdata/platform/persistence/generic_from_platform_cr/02-sonataflow_platform.yaml @@ -30,6 +30,8 @@ spec: services: dataIndex: enabled: false + persistence: + migrateDBOnStartUp: true podTemplate: initContainers: - name: init-postgres @@ -38,6 +40,8 @@ spec: command: [ 'sh', '-c', 'until (echo 1 > /dev/tcp/postgres.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local/5432) >/dev/null 2>&1; do echo "Waiting for postgres server"; sleep 3; done;' ] jobService: enabled: false + persistence: + migrateDBOnStartUp: true podTemplate: initContainers: - name: init-postgres diff --git a/test/testdata/platform/persistence/overwritten_by_services/02-sonataflow_platform.yaml b/test/testdata/platform/persistence/overwritten_by_services/02-sonataflow_platform.yaml index 90e361b86..14b0cef9b 100644 --- a/test/testdata/platform/persistence/overwritten_by_services/02-sonataflow_platform.yaml +++ b/test/testdata/platform/persistence/overwritten_by_services/02-sonataflow_platform.yaml @@ -35,6 +35,7 @@ spec: dataIndex: enabled: false persistence: + migrateDBOnStartUp: true postgresql: jdbcUrl: jdbc:postgresql://postgres:5432/sonataflow?currentSchema=data-index-service secretRef: @@ -50,6 +51,7 @@ spec: jobService: enabled: false persistence: + migrateDBOnStartUp: true postgresql: jdbcUrl: jdbc:postgresql://postgres:5432/sonataflow?currentSchema=jobs-service secretRef: diff --git a/test/testdata/platform/services/dev/postgreSQL/02-sonataflow_platform.yaml b/test/testdata/platform/services/dev/postgreSQL/02-sonataflow_platform.yaml index 77d95c969..2437863ff 100644 --- a/test/testdata/platform/services/dev/postgreSQL/02-sonataflow_platform.yaml +++ b/test/testdata/platform/services/dev/postgreSQL/02-sonataflow_platform.yaml @@ -26,6 +26,7 @@ spec: dataIndex: enabled: false persistence: + migrateDBOnStartUp: true postgresql: jdbcUrl: jdbc:postgresql://postgres:5432/sonataflow?currentSchema=data-index-service secretRef: @@ -41,6 +42,7 @@ spec: jobService: enabled: false persistence: + migrateDBOnStartUp: true postgresql: jdbcUrl: jdbc:postgresql://postgres:5432/sonataflow?currentSchema=jobs-service secretRef: diff --git a/test/testdata/platform/services/preview/postgreSQL/02-sonataflow_platform.yaml b/test/testdata/platform/services/preview/postgreSQL/02-sonataflow_platform.yaml index 9514b81ac..837f91161 100644 --- a/test/testdata/platform/services/preview/postgreSQL/02-sonataflow_platform.yaml +++ b/test/testdata/platform/services/preview/postgreSQL/02-sonataflow_platform.yaml @@ -25,6 +25,7 @@ spec: dataIndex: enabled: true persistence: + migrateDBOnStartUp: true postgresql: jdbcUrl: jdbc:postgresql://postgres:5432/sonataflow?currentSchema=data-index-service secretRef: @@ -47,6 +48,7 @@ spec: jobService: enabled: true persistence: + migrateDBOnStartUp: true postgresql: jdbcUrl: jdbc:postgresql://postgres:5432/sonataflow?currentSchema=jobs-service secretRef: diff --git a/test/testdata/workflow/persistence/by_service/04-sonataflow_callbackstatetimeouts.sw.yaml b/test/testdata/workflow/persistence/by_service/04-sonataflow_callbackstatetimeouts.sw.yaml index ec738e946..58c8ce4c7 100644 --- a/test/testdata/workflow/persistence/by_service/04-sonataflow_callbackstatetimeouts.sw.yaml +++ b/test/testdata/workflow/persistence/by_service/04-sonataflow_callbackstatetimeouts.sw.yaml @@ -21,6 +21,7 @@ metadata: sonataflow.org/version: 0.0.1 spec: persistence: + migrateDBOnStartUp: false postgresql: secretRef: name: postgres-secrets diff --git a/test/testdata/workflow/persistence/from_platform_overwritten_by_service/03-sonataflow_callbackstatetimeouts.sw.yaml b/test/testdata/workflow/persistence/from_platform_overwritten_by_service/03-sonataflow_callbackstatetimeouts.sw.yaml index fb696bff4..d39a521ed 100644 --- a/test/testdata/workflow/persistence/from_platform_overwritten_by_service/03-sonataflow_callbackstatetimeouts.sw.yaml +++ b/test/testdata/workflow/persistence/from_platform_overwritten_by_service/03-sonataflow_callbackstatetimeouts.sw.yaml @@ -21,6 +21,7 @@ metadata: sonataflow.org/version: 0.0.1 spec: persistence: + migrateDBOnStartUp: false postgresql: secretRef: name: postgres-secrets diff --git a/test/testdata/workflow/persistence/from_platform_with_di_and_js_services/02-sonataflow_platform.yaml b/test/testdata/workflow/persistence/from_platform_with_di_and_js_services/02-sonataflow_platform.yaml index 2a7db22a6..fb397b1e7 100644 --- a/test/testdata/workflow/persistence/from_platform_with_di_and_js_services/02-sonataflow_platform.yaml +++ b/test/testdata/workflow/persistence/from_platform_with_di_and_js_services/02-sonataflow_platform.yaml @@ -34,6 +34,8 @@ spec: services: dataIndex: enabled: true + persistence: + migrateDBOnStartUp: true podTemplate: initContainers: - name: init-postgres @@ -42,6 +44,8 @@ spec: command: [ 'sh', '-c', 'until (echo 1 > /dev/tcp/postgres.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local/5432) >/dev/null 2>&1; do echo "Waiting for postgres server"; sleep 3; done;' ] jobService: enabled: true + persistence: + migrateDBOnStartUp: true podTemplate: initContainers: - name: init-postgres diff --git a/test/testdata/workflow/persistence/from_platform_with_no_persistence_required/03-sonataflow_callbackstatetimeouts-no-persistence.sw.yaml b/test/testdata/workflow/persistence/from_platform_with_no_persistence_required/03-sonataflow_callbackstatetimeouts-no-persistence.sw.yaml index 7a8226c62..f7f4720fa 100644 --- a/test/testdata/workflow/persistence/from_platform_with_no_persistence_required/03-sonataflow_callbackstatetimeouts-no-persistence.sw.yaml +++ b/test/testdata/workflow/persistence/from_platform_with_no_persistence_required/03-sonataflow_callbackstatetimeouts-no-persistence.sw.yaml @@ -20,7 +20,9 @@ metadata: sonataflow.org/description: Callback State Timeouts Example k8s sonataflow.org/version: 0.0.1 spec: - persistence: {} + persistence: { + migrateDBOnStartUp: true + } podTemplate: container: env: