Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supporting service don't inherit platform persistence when the current service persistence is empty #477

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ data:
jobsServiceEphemeralImageTag: ""
# The Data Index image to use, if empty the operator will use the default Apache Community one based on the current operator's version
dataIndexPostgreSQLImageTag: ""
dataIndexEphemeralTag: ""
dataIndexEphemeralImageTag: ""
# SonataFlow base builder image used in the internal Dockerfile to build workflow applications in preview profile
# Order of precedence is:
# 1. SonataFlowPlatform in the given namespace
Expand Down
2 changes: 1 addition & 1 deletion config/manager/controllers_cfg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobsServicePostgreSQLImageTag: ""
jobsServiceEphemeralImageTag: ""
# The Data Index image to use, if empty the operator will use the default Apache Community one based on the current operator's version
dataIndexPostgreSQLImageTag: ""
dataIndexEphemeralTag: ""
dataIndexEphemeralImageTag: ""
# SonataFlow base builder image used in the internal Dockerfile to build workflow applications in preview profile
# Order of precedence is:
# 1. SonataFlowPlatform in the given namespace
Expand Down
7 changes: 3 additions & 4 deletions controllers/platform/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)
Expand Down Expand Up @@ -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"})
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
13 changes: 13 additions & 0 deletions controllers/profiles/common/persistence/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
146 changes: 146 additions & 0 deletions controllers/profiles/common/persistence/postgresql_test.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
}
2 changes: 1 addition & 1 deletion operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27011,7 +27011,7 @@ data:
jobsServiceEphemeralImageTag: ""
# The Data Index image to use, if empty the operator will use the default Apache Community one based on the current operator's version
dataIndexPostgreSQLImageTag: ""
dataIndexEphemeralTag: ""
dataIndexEphemeralImageTag: ""
# SonataFlow base builder image used in the internal Dockerfile to build workflow applications in preview profile
# Order of precedence is:
# 1. SonataFlowPlatform in the given namespace
Expand Down
Loading