Skip to content

Commit

Permalink
kie-kogito-serverless-operator-520: Error when deploying a WF with th…
Browse files Browse the repository at this point in the history
…e dev profile in a SPF that has persistence configured (apache#531)
  • Loading branch information
wmedvede committed Sep 11, 2024
1 parent 1352620 commit 5014592
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 6 deletions.
16 changes: 10 additions & 6 deletions controllers/profiles/common/object_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"fmt"
"strings"

"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles"

"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/workflowdef"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"

Expand Down Expand Up @@ -200,12 +202,14 @@ func defaultContainer(workflow *operatorapi.SonataFlow, plf *operatorapi.SonataF
if err := mergo.Merge(defaultFlowContainer, workflow.Spec.PodTemplate.Container.ToContainer(), mergo.WithOverride); err != nil {
return nil, err
}
var pper *operatorapi.PlatformPersistenceOptionsSpec
if plf != nil && plf.Spec.Persistence != nil {
pper = plf.Spec.Persistence
}
if p := persistence.RetrieveConfiguration(workflow.Spec.Persistence, pper, workflow.Name); p != nil {
defaultFlowContainer = persistence.ConfigurePersistence(defaultFlowContainer, p, workflow.Name, workflow.Namespace)
if !profiles.IsDevProfile(workflow) {
var pper *operatorapi.PlatformPersistenceOptionsSpec
if plf != nil && plf.Spec.Persistence != nil {
pper = plf.Spec.Persistence
}
if p := persistence.RetrieveConfiguration(workflow.Spec.Persistence, pper, workflow.Name); p != nil {
defaultFlowContainer = persistence.ConfigurePersistence(defaultFlowContainer, p, workflow.Name, workflow.Namespace)
}
}
// immutable
defaultFlowContainer.Name = operatorapi.DefaultContainerName
Expand Down
181 changes: 181 additions & 0 deletions controllers/profiles/common/object_creators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"context"
"testing"

"github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata"
"k8s.io/apimachinery/pkg/util/intstr"

sourcesv1 "knative.dev/eventing/pkg/apis/sources/v1"

"github.com/magiconair/properties"
Expand Down Expand Up @@ -609,3 +612,181 @@ func TestMergePodSpec_WithServicedPostgreSQL_In_Platform_But_Workflow_CR_Not_Req
assert.Equal(t, int32(8080), flowContainer.Ports[0].ContainerPort)
assert.Nil(t, flowContainer.Env)
}

func TestDefaultContainer_WithPlatformPersistenceWorkflowWithDefaultProfile(t *testing.T) {
workflow := test.GetBaseSonataFlow(t.Name())
doTestDefaultContainer_WithPlatformPersistence(t, workflow, true)
}

func TestDefaultContainer_WithPlatformPersistenceWorkflowWithPreviewProfile(t *testing.T) {
workflow := test.GetBaseSonataFlow(t.Name())
workflowproj.SetWorkflowProfile(workflow, metadata.PreviewProfile)
doTestDefaultContainer_WithPlatformPersistence(t, workflow, true)
}

func TestDefaultContainer_WithPlatformPersistenceWorkflowWithGitOpsProfile(t *testing.T) {
workflow := test.GetBaseSonataFlow(t.Name())
workflowproj.SetWorkflowProfile(workflow, metadata.GitOpsProfile)
doTestDefaultContainer_WithPlatformPersistence(t, workflow, true)
}

func TestDefaultContainer_WithPlatformPersistenceWorkflowWithDevProfile(t *testing.T) {
workflow := test.GetBaseSonataFlow(t.Name())
workflowproj.SetWorkflowProfile(workflow, metadata.DevProfile)
doTestDefaultContainer_WithPlatformPersistence(t, workflow, false)
}

func doTestDefaultContainer_WithPlatformPersistence(t *testing.T, workflow *v1alpha08.SonataFlow, checkPersistence bool) {
platform := &v1alpha08.SonataFlowPlatform{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "default",
},
Spec: v1alpha08.SonataFlowPlatformSpec{
Persistence: &v1alpha08.PlatformPersistenceOptionsSpec{
PostgreSQL: &v1alpha08.PlatformPersistencePostgreSQL{
SecretRef: v1alpha08.PostgreSQLSecretOptions{
Name: "foo_secret",
UserKey: "username",
PasswordKey: "password",
},
ServiceRef: &v1alpha08.SQLServiceOptions{
Name: "service_name",
Namespace: "service_namespace",
Port: &postgreSQLPort,
DatabaseName: "foo",
},
},
},
},
}

container, err := defaultContainer(workflow, platform)
assert.Nil(t, err)
assert.NotNil(t, container)
assert.Equal(t, "workflow", container.Name)

//verify default container port.
assert.Equal(t, 1, len(container.Ports))
assert.Equal(t, "h2c", container.Ports[0].Name)
assert.Equal(t, int32(0), container.Ports[0].HostPort)
assert.Equal(t, int32(8080), container.Ports[0].ContainerPort)
assert.Equal(t, corev1.Protocol("TCP"), container.Ports[0].Protocol)
assert.Equal(t, "", container.Ports[0].HostIP)

//verify default container health checks
assert.Equal(t, &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: nil,
HTTPGet: &corev1.HTTPGetAction{
Path: "/q/health/live",
Port: intstr.IntOrString{
Type: 0,
IntVal: 8080,
StrVal: "",
},
Host: "",
Scheme: "",
HTTPHeaders: nil,
},
TCPSocket: nil,
GRPC: nil,
},
InitialDelaySeconds: 0,
TimeoutSeconds: 3,
PeriodSeconds: 15,
SuccessThreshold: 0,
FailureThreshold: 0,
TerminationGracePeriodSeconds: nil,
}, container.LivenessProbe)

assert.Equal(t, &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: nil,
HTTPGet: &corev1.HTTPGetAction{
Path: "/q/health/ready",
Port: intstr.IntOrString{
Type: 0,
IntVal: 8080,
StrVal: "",
},
Host: "",
Scheme: "",
HTTPHeaders: nil,
},
TCPSocket: nil,
GRPC: nil,
},
InitialDelaySeconds: 0,
TimeoutSeconds: 3,
PeriodSeconds: 15,
SuccessThreshold: 0,
FailureThreshold: 0,
TerminationGracePeriodSeconds: nil,
}, container.ReadinessProbe)

assert.Equal(t, &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
Exec: nil,
HTTPGet: &corev1.HTTPGetAction{
Path: "/q/health/started",
Port: intstr.IntOrString{
Type: 0,
IntVal: 8080,
StrVal: "",
},
Host: "",
Scheme: "",
HTTPHeaders: nil,
},
TCPSocket: nil,
GRPC: nil,
},
InitialDelaySeconds: 10,
TimeoutSeconds: 3,
PeriodSeconds: 15,
SuccessThreshold: 0,
FailureThreshold: 5,
TerminationGracePeriodSeconds: nil,
}, container.StartupProbe)

//verify the persistence configuration is present if requested.
if checkPersistence {
expectedEnvVars := []corev1.EnvVar{
{
Name: "QUARKUS_DATASOURCE_USERNAME",
Value: "",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "foo_secret"}, Key: "username",
},
},
},
{
Name: "QUARKUS_DATASOURCE_PASSWORD",
Value: "",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: "foo_secret"}, Key: "password",
},
},
},
{
Name: "QUARKUS_DATASOURCE_DB_KIND",
Value: "postgresql",
},
{
Name: "QUARKUS_DATASOURCE_JDBC_URL",
Value: "jdbc:postgresql://service_name.service_namespace:5432/foo?currentSchema=greeting",
},
{
Name: "KOGITO_PERSISTENCE_TYPE",
Value: "jdbc",
},
}
assert.Equal(t, expectedEnvVars, container.Env)
} else {
//no persistence
assert.Nil(t, container.Env)
}
}

0 comments on commit 5014592

Please sign in to comment.