ricardozanini commented on code in PR #322: URL: https://github.com/apache/incubator-kie-kogito-serverless-operator/pull/322#discussion_r1458838423
########## test/e2e/persistence_test.go: ########## @@ -0,0 +1,146 @@ +// Copyright 2024 Apache Software Foundation (ASF) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package e2e + +import ( + "bytes" + "fmt" + "math/rand" + "os/exec" + "strings" + "time" + + "github.com/apache/incubator-kie-kogito-serverless-operator/test" + "github.com/apache/incubator-kie-kogito-serverless-operator/test/utils" + + //nolint:golint + //nolint:revive + . "github.com/onsi/ginkgo/v2" + + //nolint:golint + //nolint:revive + . "github.com/onsi/gomega" +) + +var _ = Describe("Validate the persistence ", Ordered, func() { + Review Comment: Nice one! ########## controllers/platform/services/services.go: ########## @@ -134,7 +134,9 @@ func (d DataIndex) ConfigurePersistence(containerSpec *corev1.Container) *corev1 if d.platform.Spec.Services.DataIndex.Persistence != nil && d.platform.Spec.Services.DataIndex.Persistence.PostgreSql != nil { c := containerSpec.DeepCopy() c.Image = d.GetServiceImageName(constants.PersistenceTypePostgreSQL) - c.Env = append(c.Env, d.configurePostgreSqlEnv(d.platform.Spec.Services.DataIndex.Persistence.PostgreSql, d.GetServiceName(), d.platform.Namespace)...) + c.Env = append(c.Env, persistence.ConfigurePostgreSqlEnv(d.platform.Spec.Services.DataIndex.Persistence.PostgreSql, d.GetServiceName(), d.platform.Namespace)...) + // specific to DataIndex + c.Env = append(c.Env, corev1.EnvVar{Name: "QUARKUS_HIBERNATE_ORM_DATABASE_GENERATION", Value: "update"}, corev1.EnvVar{Name: "QUARKUS_FLYWAY_MIGRATE_AT_START", Value: "true"}) Review Comment: Can you move these to private consts? ########## test/testdata/persistence/workflow/by_service/02-sonataflow_platform.yaml: ########## @@ -0,0 +1,27 @@ +# Copyright 2024 Apache Software Foundation (ASF) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: sonataflow.org/v1alpha08 +kind: SonataFlowPlatform +metadata: + name: sonataflow-platform +spec: + build: + template: + buildArgs: + - name: QUARKUS_EXTENSIONS + value: org.kie.kogito:kogito-addons-quarkus-jobs-knative-eventing:999-SNAPSHOT,org.kie.kogito:kogito-addons-quarkus-persistence-jdbc:999-SNAPSHOT,io.quarkus:quarkus-jdbc-postgresql:3.2.9.Final,io.quarkus:quarkus-agroal:3.2.9.Final Review Comment: @wmedvede I think it's worth adding those to the images, so we keep them in the cache to facilitate persistence use cases. wdyt? ########## test/testdata/persistence/workflow/by_service/03-sonataflow_callbackstatetimeouts.sw.yaml: ########## @@ -0,0 +1,101 @@ +# Copyright 2024 Apache Software Foundation (ASF) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: sonataflow.org/v1alpha08 +kind: SonataFlow +metadata: + name: callbackstatetimeouts + annotations: + sonataflow.org/description: Callback State Timeouts Example k8s + sonataflow.org/version: 0.0.1 +spec: + persistence: + postgresql: + secretRef: + name: postgres-secrets + userKey: POSTGRES_USER + passwordKey: POSTGRES_PASSWORD + serviceRef: + name: postgres + port: 5432 + databaseName: sonataflow + databaseSchema: callbackstatetimeouts + podTemplate: + container: + env: + - name: QUARKUS_FLYWAY_MIGRATE_AT_START + value: "true" + initContainers: + - name: init-postgres + image: registry.access.redhat.com/ubi9/ubi-minimal:latest Review Comment: I guess we can use https://catalog.redhat.com/software/containers/ubi9/ubi-micro/615bdf943f6014fa45ae1b58 instead. IIRC `coreutils` includes cat. It's 7 vs 36 MB. Quite a difference in storage. ########## controllers/profiles/common/object_creators_test.go: ########## @@ -169,3 +169,171 @@ func TestMergePodSpec_OverrideContainers(t *testing.T) { assert.Equal(t, int32(8080), flowContainer.Ports[0].ContainerPort) assert.Empty(t, flowContainer.Env) } + +func TestMergePodSpec_WithPostgreSQL_and_JDBC_URL_field(t *testing.T) { + workflow := test.GetBaseSonataFlow(t.Name()) + workflow.Spec = v1alpha08.SonataFlowSpec{ + PodTemplate: v1alpha08.PodTemplateSpec{ + Container: v1alpha08.ContainerSpec{ + // this one we can override + Image: "quay.io/example/my-workflow:1.0.0", + Ports: []corev1.ContainerPort{ + // let's override a immutable attribute + {Name: utils.HttpScheme, ContainerPort: 9090}, + }, + Env: []corev1.EnvVar{ + // We should be able to override this too + {Name: "ENV1", Value: "VALUE_CUSTOM"}, + }, + VolumeMounts: []corev1.VolumeMount{ + {Name: "myvolume", ReadOnly: true, MountPath: "/tmp/any/path"}, + }, + }, + PodSpec: v1alpha08.PodSpec{ + ServiceAccountName: "superuser", + Containers: []corev1.Container{ + { + Name: "sidecar", + }, + }, + Volumes: []corev1.Volume{ + { + Name: "myvolume", + VolumeSource: corev1.VolumeSource{ + ConfigMap: &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{Name: "customproperties"}, + }, + }, + }, + }, + }, + }, + Persistence: &v1alpha08.PersistenceOptions{ + PostgreSql: &v1alpha08.PersistencePostgreSql{ + SecretRef: v1alpha08.PostgreSqlSecretOptions{Name: "test"}, + JdbcUrl: "jdbc:postgresql://host:port/database?currentSchema=workflow", + }, + }, + } + + object, err := DeploymentCreator(workflow) + assert.NoError(t, err) + + deployment := object.(*appsv1.Deployment) + expectedEnvVars := []corev1.EnvVar{ + { + Name: "ENV1", + Value: "VALUE_CUSTOM", + }, + { + Name: "QUARKUS_DATASOURCE_USERNAME", + Value: "", + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{Name: "test"}, Key: "POSTGRESQL_USER", + }, + }, + }, + { + Name: "QUARKUS_DATASOURCE_PASSWORD", + Value: "", + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + LocalObjectReference: corev1.LocalObjectReference{Name: "test"}, Key: "POSTGRESQL_PASSWORD", + }, + }, + }, + { + Name: "QUARKUS_DATASOURCE_DB_KIND", + Value: "postgresql", + }, + { + Name: "QUARKUS_DATASOURCE_JDBC_URL", + Value: "jdbc:postgresql://host:port/database?currentSchema=workflow", + }, + } + assert.Len(t, deployment.Spec.Template.Spec.Containers, 2) + assert.Equal(t, "superuser", deployment.Spec.Template.Spec.ServiceAccountName) + assert.Len(t, deployment.Spec.Template.Spec.Volumes, 1) + flowContainer, _ := kubeutil.GetContainerByName(v1alpha08.DefaultContainerName, &deployment.Spec.Template.Spec) + assert.Equal(t, "quay.io/example/my-workflow:1.0.0", flowContainer.Image) + assert.Equal(t, int32(8080), flowContainer.Ports[0].ContainerPort) + assert.Equal(t, expectedEnvVars, flowContainer.Env) + assert.Len(t, flowContainer.VolumeMounts, 1) +} + +var ( + postgreSQLPort = 5432 Review Comment: nit; are you using this var outside the `TestMergePodSpec_OverrideContainers_WithPostgreSQL_and_ServiceRef` test scope? ########## test/e2e/workflow_test.go: ########## @@ -41,8 +42,8 @@ import ( . "github.com/onsi/gomega" ) -// namespace store the ns where the Operator and Operand will be executed -const namespace = "sonataflow-operator-system" +// sonataflow_operator_namespace store the ns where the Operator and Operand will be executed +const sonataflow_operator_namespace = "sonataflow-operator-system" Review Comment: 👍 ########## controllers/profiles/common/persistence/postgreSQL.go: ########## @@ -0,0 +1,89 @@ +// Copyright 2023 Apache Software Foundation (ASF) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package persistence + +import ( + "strconv" + + corev1 "k8s.io/api/core/v1" + + operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/common/constants" +) + +const ( + defaultSchemaName = "default" + defaultDatabaseName = "sonataflow" +) + +func ConfigurePostgreSqlEnv(postgresql *operatorapi.PersistencePostgreSql, databaseSchema, databaseNamespace string) []corev1.EnvVar { + dataSourcePort := constants.DefaultPostgreSQLPort + databaseName := defaultDatabaseName + dataSourceURL := postgresql.JdbcUrl + if postgresql.ServiceRef != nil { + if len(postgresql.ServiceRef.DatabaseSchema) > 0 { + databaseSchema = postgresql.ServiceRef.DatabaseSchema + } + if len(postgresql.ServiceRef.Namespace) > 0 { + databaseNamespace = postgresql.ServiceRef.Namespace + } + if postgresql.ServiceRef.Port != nil { + dataSourcePort = *postgresql.ServiceRef.Port + } + if len(postgresql.ServiceRef.DatabaseName) > 0 { + databaseName = postgresql.ServiceRef.DatabaseName + } + dataSourceURL = "jdbc:" + constants.PersistenceTypePostgreSQL + "://" + postgresql.ServiceRef.Name + "." + databaseNamespace + ":" + strconv.Itoa(dataSourcePort) + "/" + databaseName + "?currentSchema=" + databaseSchema + } + secretRef := corev1.LocalObjectReference{ + Name: postgresql.SecretRef.Name, + } + quarkusDatasourceUsername := "POSTGRESQL_USER" + if len(postgresql.SecretRef.UserKey) > 0 { + quarkusDatasourceUsername = postgresql.SecretRef.UserKey + } + quarkusDatasourcePassword := "POSTGRESQL_PASSWORD" + if len(postgresql.SecretRef.PasswordKey) > 0 { + quarkusDatasourcePassword = postgresql.SecretRef.PasswordKey + } + return []corev1.EnvVar{ + { + Name: "QUARKUS_DATASOURCE_USERNAME", + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + Key: quarkusDatasourceUsername, + LocalObjectReference: secretRef, + }, + }, + }, + { + Name: "QUARKUS_DATASOURCE_PASSWORD", + ValueFrom: &corev1.EnvVarSource{ + SecretKeyRef: &corev1.SecretKeySelector{ + Key: quarkusDatasourcePassword, + LocalObjectReference: secretRef, + }, + }, + }, + { + Name: "QUARKUS_DATASOURCE_DB_KIND", + Value: constants.PersistenceTypePostgreSQL, + }, + { + Name: "QUARKUS_DATASOURCE_JDBC_URL", + Value: dataSourceURL, + }, Review Comment: I'd rather keep all these hardcoded strings in private const. ########## test/testdata/persistence/workflow/by_service/03-sonataflow_callbackstatetimeouts.sw.yaml: ########## @@ -0,0 +1,101 @@ +# Copyright 2024 Apache Software Foundation (ASF) +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: sonataflow.org/v1alpha08 +kind: SonataFlow +metadata: + name: callbackstatetimeouts + annotations: + sonataflow.org/description: Callback State Timeouts Example k8s + sonataflow.org/version: 0.0.1 +spec: + persistence: + postgresql: + secretRef: + name: postgres-secrets + userKey: POSTGRES_USER + passwordKey: POSTGRES_PASSWORD + serviceRef: + name: postgres + port: 5432 + databaseName: sonataflow + databaseSchema: callbackstatetimeouts + podTemplate: + container: + env: + - name: QUARKUS_FLYWAY_MIGRATE_AT_START + value: "true" + initContainers: + - name: init-postgres + image: registry.access.redhat.com/ubi9/ubi-minimal:latest + imagePullPolicy: IfNotPresent + 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;' ] Review Comment: Just as a quick note; we have a JIRA to implement this in case the workflow requires persistence: https://issues.redhat.com/browse/KOGITO-9840 Can you please add this link with `TODO:` in this file? 🙏 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
