This is an automated email from the ASF dual-hosted git repository.

ricardozanini pushed a commit to branch main
in repository 
https://gitbox.apache.org/repos/asf/incubator-kie-kogito-serverless-operator.git


The following commit(s) were added to refs/heads/main by this push:
     new 706ecb79 kie-kogito-serverless-operator-476: Supporting service don't 
inherit platform persistence when the current service persistence is empty 
(#478)
706ecb79 is described below

commit 706ecb79b1baa6f345c00212b958606adc29ea80
Author: Walter Medvedeo <[email protected]>
AuthorDate: Wed Jun 5 20:54:41 2024 +0200

    kie-kogito-serverless-operator-476: Supporting service don't inherit 
platform persistence when the current service persistence is empty (#478)
---
 controllers/platform/services/services.go          |   7 +-
 .../common/persistence/persistence_suite_test.go   |  27 ++++
 .../profiles/common/persistence/postgresql.go      |  13 ++
 .../profiles/common/persistence/postgresql_test.go | 146 +++++++++++++++++++++
 4 files changed, 189 insertions(+), 4 deletions(-)

diff --git a/controllers/platform/services/services.go 
b/controllers/platform/services/services.go
index 4cd9c02b..aead391e 100644
--- a/controllers/platform/services/services.go
+++ b/controllers/platform/services/services.go
@@ -227,7 +227,7 @@ func (d DataIndexHandler) hasPostgreSQLConfigured() bool {
 
 func (d DataIndexHandler) ConfigurePersistence(containerSpec 
*corev1.Container) *corev1.Container {
        if d.hasPostgreSQLConfigured() {
-               p := 
persistence.RetrieveConfiguration(d.platform.Spec.Services.DataIndex.Persistence,
 d.platform.Spec.Persistence, d.GetServiceName())
+               p := 
persistence.RetrievePostgreSQLConfiguration(d.platform.Spec.Services.DataIndex.Persistence,
 d.platform.Spec.Persistence, d.GetServiceName())
                c := containerSpec.DeepCopy()
                c.Image = 
d.GetServiceImageName(constants.PersistenceTypePostgreSQL)
                c.Env = append(c.Env, 
persistence.ConfigurePostgreSQLEnv(p.PostgreSQL, d.GetServiceName(), 
d.platform.Namespace)...)
@@ -397,11 +397,10 @@ func (j JobServiceHandler) hasPostgreSQLConfigured() bool 
{
 }
 
 func (j JobServiceHandler) ConfigurePersistence(containerSpec 
*corev1.Container) *corev1.Container {
-
        if j.hasPostgreSQLConfigured() {
                c := containerSpec.DeepCopy()
                c.Image = 
j.GetServiceImageName(constants.PersistenceTypePostgreSQL)
-               p := 
persistence.RetrieveConfiguration(j.platform.Spec.Services.JobService.Persistence,
 j.platform.Spec.Persistence, j.GetServiceName())
+               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"})
@@ -423,7 +422,7 @@ func (j JobServiceHandler) GenerateServiceProperties() 
(*properties.Properties,
        props.Set(constants.JobServiceKafkaSmallRyeHealthProperty, "false")
        // add data source reactive URL
        if j.hasPostgreSQLConfigured() {
-               p := 
persistence.RetrieveConfiguration(j.platform.Spec.Services.JobService.Persistence,
 j.platform.Spec.Persistence, j.GetServiceName())
+               p := 
persistence.RetrievePostgreSQLConfiguration(j.platform.Spec.Services.JobService.Persistence,
 j.platform.Spec.Persistence, j.GetServiceName())
                dataSourceReactiveURL, err := generateReactiveURL(p.PostgreSQL, 
j.GetServiceName(), j.platform.Namespace, constants.DefaultDatabaseName, 
constants.DefaultPostgreSQLPort)
                if err != nil {
                        return nil, err
diff --git a/controllers/profiles/common/persistence/persistence_suite_test.go 
b/controllers/profiles/common/persistence/persistence_suite_test.go
new file mode 100644
index 00000000..75d328d9
--- /dev/null
+++ b/controllers/profiles/common/persistence/persistence_suite_test.go
@@ -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.
+
+package persistence
+
+import (
+       "testing"
+
+       . "github.com/onsi/ginkgo/v2"
+       . "github.com/onsi/gomega"
+)
+
+func TestPersistence(t *testing.T) {
+       RegisterFailHandler(Fail)
+       RunSpecs(t, "Persistence Suite")
+}
diff --git a/controllers/profiles/common/persistence/postgresql.go 
b/controllers/profiles/common/persistence/postgresql.go
index 068d85ca..b6d23ccc 100644
--- a/controllers/profiles/common/persistence/postgresql.go
+++ b/controllers/profiles/common/persistence/postgresql.go
@@ -112,6 +112,19 @@ func RetrieveConfiguration(primary 
*v1alpha08.PersistenceOptionsSpec, platformPe
        if platformPersistence == nil {
                return nil
        }
+       return buildPersistenceOptionsSpec(platformPersistence, schema)
+}
+
+// RetrievePostgreSQLConfiguration return the PersistenceOptionsSpec 
considering that postgresql is the database manager
+// to look for. Gives priority to the primary configuration.
+func RetrievePostgreSQLConfiguration(primary 
*v1alpha08.PersistenceOptionsSpec, platformPersistence 
*v1alpha08.PlatformPersistenceOptionsSpec, schema string) 
*v1alpha08.PersistenceOptionsSpec {
+       if primary != nil && primary.PostgreSQL != nil {
+               return primary
+       }
+       return buildPersistenceOptionsSpec(platformPersistence, schema)
+}
+
+func buildPersistenceOptionsSpec(platformPersistence 
*v1alpha08.PlatformPersistenceOptionsSpec, schema string) 
*v1alpha08.PersistenceOptionsSpec {
        c := &v1alpha08.PersistenceOptionsSpec{}
        if platformPersistence.PostgreSQL != nil {
                c.PostgreSQL = &v1alpha08.PersistencePostgreSQL{
diff --git a/controllers/profiles/common/persistence/postgresql_test.go 
b/controllers/profiles/common/persistence/postgresql_test.go
new file mode 100644
index 00000000..6108937f
--- /dev/null
+++ b/controllers/profiles/common/persistence/postgresql_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 persistence
+
+import (
+       operatorapi 
"github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
+       . "github.com/onsi/ginkgo/v2"
+       . "github.com/onsi/gomega"
+)
+
+const (
+       primaryPostgreSQLJdbc = 
"jdbc:postgresql://host:port/database?currentSchema=primary-database"
+
+       platformPostgreSQLJdbc = 
"jdbc:postgresql://host:port/database?currentSchema=platform-database"
+       schemaName             = "my-schema"
+)
+
+var (
+       primaryPostgreSQLSecret = operatorapi.PostgreSQLSecretOptions{
+               Name: "primary-secret",
+       }
+       primaryPostreSQLService = operatorapi.PostgreSQLServiceOptions{
+               SQLServiceOptions: &operatorapi.SQLServiceOptions{Name: 
"primary-service"},
+               DatabaseSchema:    "primary-schema",
+       }
+       plaformPostgreSQLSecret = operatorapi.PostgreSQLSecretOptions{
+               Name: "platform-secret",
+       }
+       platformPostreSQLService = operatorapi.SQLServiceOptions{
+               Name: "platform-service",
+       }
+)
+
+var _ = Describe("RetrievePostgreSQLConfiguration", func() {
+       DescribeTable("calculation",
+               func(primary *operatorapi.PersistenceOptionsSpec,
+                       platformPersistence 
*operatorapi.PlatformPersistenceOptionsSpec,
+                       schema string,
+                       expectedConfig *operatorapi.PersistenceOptionsSpec) {
+                       result := RetrievePostgreSQLConfiguration(primary, 
platformPersistence, schema)
+                       Expect(expectedConfig).To(Equal(result))
+               },
+               Entry("primary is postgresql with JdbcUrl", 
buildPrimaryIsPostgreSQLWithJdbcUrl(),
+                       buildPlatformIsPostgreSQLWithJdbcUrl(),
+                       schemaName,
+                       buildPrimaryIsPostgreSQLWithJdbcUrl()),
+               Entry("primary is postgresql ServiceRef", 
buildPrimaryIsPostgreSQLWithServiceRef(),
+                       buildPlatformIsPostgreSQLWithJdbcUrl(),
+                       schemaName,
+                       buildPrimaryIsPostgreSQLWithServiceRef()),
+               Entry("primary is nil, platform with JdbcUrl",
+                       nil,
+                       buildPlatformIsPostgreSQLWithJdbcUrl(),
+                       schemaName,
+                       &operatorapi.PersistenceOptionsSpec{
+                               PostgreSQL: &operatorapi.PersistencePostgreSQL{
+                                       SecretRef: plaformPostgreSQLSecret,
+                                       JdbcUrl:   platformPostgreSQLJdbc,
+                               },
+                       }),
+               Entry("primary is empty, platform with JdbcUrl",
+                       &operatorapi.PersistenceOptionsSpec{},
+                       buildPlatformIsPostgreSQLWithJdbcUrl(),
+                       schemaName,
+                       &operatorapi.PersistenceOptionsSpec{
+                               PostgreSQL: &operatorapi.PersistencePostgreSQL{
+                                       SecretRef: plaformPostgreSQLSecret,
+                                       JdbcUrl:   platformPostgreSQLJdbc,
+                               },
+                       }),
+               Entry("primary is nil, platform with ServiceRef",
+                       nil,
+                       buildPlatformIsPostgreSQLWithServiceRef(),
+                       schemaName,
+                       &operatorapi.PersistenceOptionsSpec{
+                               PostgreSQL: &operatorapi.PersistencePostgreSQL{
+                                       ServiceRef: 
&operatorapi.PostgreSQLServiceOptions{
+                                               SQLServiceOptions: 
&platformPostreSQLService,
+                                               DatabaseSchema:    schemaName,
+                                       },
+                                       SecretRef: plaformPostgreSQLSecret,
+                               },
+                       }),
+               Entry("primary is empty, platform with ServiceRef",
+                       &operatorapi.PersistenceOptionsSpec{},
+                       buildPlatformIsPostgreSQLWithServiceRef(),
+                       schemaName,
+                       &operatorapi.PersistenceOptionsSpec{
+                               PostgreSQL: &operatorapi.PersistencePostgreSQL{
+                                       ServiceRef: 
&operatorapi.PostgreSQLServiceOptions{
+                                               SQLServiceOptions: 
&platformPostreSQLService,
+                                               DatabaseSchema:    schemaName,
+                                       },
+                                       SecretRef: plaformPostgreSQLSecret,
+                               },
+                       }),
+       )
+})
+
+func buildPrimaryIsPostgreSQLWithJdbcUrl() *operatorapi.PersistenceOptionsSpec 
{
+       return &operatorapi.PersistenceOptionsSpec{
+               PostgreSQL: &operatorapi.PersistencePostgreSQL{
+                       JdbcUrl:   primaryPostgreSQLJdbc,
+                       SecretRef: primaryPostgreSQLSecret,
+               },
+       }
+}
+
+func buildPrimaryIsPostgreSQLWithServiceRef() 
*operatorapi.PersistenceOptionsSpec {
+       return &operatorapi.PersistenceOptionsSpec{
+               PostgreSQL: &operatorapi.PersistencePostgreSQL{
+                       ServiceRef: &primaryPostreSQLService,
+                       SecretRef:  primaryPostgreSQLSecret,
+               },
+       }
+}
+
+func buildPlatformIsPostgreSQLWithJdbcUrl() 
*operatorapi.PlatformPersistenceOptionsSpec {
+       return &operatorapi.PlatformPersistenceOptionsSpec{
+               PostgreSQL: &operatorapi.PlatformPersistencePostgreSQL{
+                       JdbcUrl:   platformPostgreSQLJdbc,
+                       SecretRef: plaformPostgreSQLSecret,
+               },
+       }
+}
+
+func buildPlatformIsPostgreSQLWithServiceRef() 
*operatorapi.PlatformPersistenceOptionsSpec {
+       return &operatorapi.PlatformPersistenceOptionsSpec{
+               PostgreSQL: &operatorapi.PlatformPersistencePostgreSQL{
+                       ServiceRef: &platformPostreSQLService,
+                       SecretRef:  plaformPostgreSQLSecret,
+               },
+       }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to