masayag commented on code in PR #287: URL: https://github.com/apache/incubator-kie-kogito-serverless-operator/pull/287#discussion_r1376807043
########## api/v1alpha08/sonataflowplatform_services_types.go: ########## @@ -0,0 +1,89 @@ +// Copyright 2023 Red Hat, Inc. and/or its affiliates +// +// 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 v1alpha08 + +// ServicesPlatformSpec describes the desired service configuration for "prod" workflows. +type ServicesPlatformSpec struct { + // Deploys the Data Index service for use by "prod" profile workflows. + // +optional + DataIndex *ServicesPodTemplateSpec `json:"dataIndex,omitempty"` + // Persists service(s) to a datasource of choice. Ephemeral by default. + // +optional + Persistence *PersistenceOptions `json:"persistence,omitempty"` +} + +// ServicesPodTemplateSpec describes the desired custom Kubernetes PodTemplate definition for the deployed flow. +// +// The FlowContainer describes the container where the actual service is running. It will override any default definitions. +// For example, to override the image one can use `.spec.services.dataIndex.container.image = my/image:tag`. +type ServicesPodTemplateSpec struct { + // Container is the Kubernetes container where the service should run. + // One can change this attribute in order to override the defaults provided by the operator. + // +optional + Container *FlowContainer `json:"container,omitempty"` + // Describes the PodSpec for the internal service deployment based on the default Kubernetes PodSpec API + // +optional + FlowPodSpec `json:",inline"` + // +optional + Replicas *int32 `json:"replicas,omitempty"` + // Determines whether "prod" profile workflows should be configured to use this service Review Comment: How would the operator handle workflow that is configured to prod profile while the SonataFlowPlatform CR of the same namespace has the `.spec.services.dataIndex.enbled: false` ? ########## controllers/platform/services.go: ########## @@ -0,0 +1,360 @@ +// Copyright 2023 Red Hat, Inc. and/or its affiliates +// +// 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 platform + +import ( + "context" + "strconv" + "strings" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08" + "github.com/apache/incubator-kie-kogito-serverless-operator/container-builder/client" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/common" + "github.com/apache/incubator-kie-kogito-serverless-operator/log" + "github.com/apache/incubator-kie-kogito-serverless-operator/workflowproj" + "github.com/imdario/mergo" +) + +var httpPort = strings.ToLower(string(corev1.URISchemeHTTP)) + +// NewServiceAction returns an action that deploys the services. +func NewServiceAction() Action { + return &serviceAction{} +} + +type serviceAction struct { + baseAction +} + +func (action *serviceAction) Name() string { + return "service" +} + +func (action *serviceAction) CanHandle(platform *operatorapi.SonataFlowPlatform) bool { + return platform.Status.IsReady() +} + +func (action *serviceAction) Handle(ctx context.Context, platform *operatorapi.SonataFlowPlatform) (*operatorapi.SonataFlowPlatform, error) { + // Refresh applied configuration + if err := ConfigureDefaults(ctx, action.client, platform, false); err != nil { + return nil, err + } + + if err := createDataIndexComponents(ctx, action.client, platform); err != nil { + return nil, err + } + + return platform, nil +} + +func createDataIndexComponents(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform) error { + if platform.Spec.Services.DataIndex != nil { + if err := createDataIndexConfigMap(ctx, client, platform); err != nil { + return err + } + if err := createDataIndexDeployment(ctx, client, platform); err != nil { + return err + } + if err := createDataIndexService(ctx, client, platform); err != nil { + return err + } + } + + return nil +} + +func createDataIndexDeployment(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform) error { + probe := &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/q/health/ready", + Port: common.DefaultHTTPWorkflowPortIntStr, + Scheme: corev1.URISchemeHTTP, + }, + }, + InitialDelaySeconds: int32(15), + TimeoutSeconds: int32(10), + PeriodSeconds: int32(30), + SuccessThreshold: int32(1), + FailureThreshold: int32(3), + } + dataDeployContainer := &corev1.Container{ + Name: common.DataIndexName, + Image: common.DataIndexImageBase + common.PersistenceTypeEphemeral, + Env: []corev1.EnvVar{ + { + Name: "KOGITO_DATA_INDEX_QUARKUS_PROFILE", + Value: "http-events-support", + }, + { + Name: "QUARKUS_HTTP_PORT", + Value: "8080", + }, + { + Name: "QUARKUS_HTTP_CORS", + Value: "true", + }, + { + Name: "QUARKUS_HTTP_CORS_ORIGINS", + Value: "/.*/", + }, + { + Name: "QUARKUS_KAFKA_HEALTH_ENABLE", + Value: "false", + }, + }, + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("100m"), + corev1.ResourceMemory: resource.MustParse("256Mi"), + }, + }, + ReadinessProbe: probe, + LivenessProbe: probe, + Ports: []corev1.ContainerPort{ + { + Name: httpPort, + ContainerPort: int32(common.DefaultHTTPWorkflowPortInt), + Protocol: corev1.ProtocolTCP, + }, + }, + ImagePullPolicy: corev1.PullAlways, + VolumeMounts: []corev1.VolumeMount{ + { + Name: "application-config", + MountPath: "/home/kogito/config", + }, + }, + } + configurePersistence(ctx, client, dataDeployContainer, platform) + if platform.Spec.Services.DataIndex.Container != nil { + if err := mergo.Merge(dataDeployContainer, platform.Spec.Services.DataIndex.Container.ToContainer(), mergo.WithOverride); err != nil { + return err + } + } + + var replicas int32 = 1 + if platform.Spec.Services.DataIndex.Replicas != nil { + replicas = *platform.Spec.Services.DataIndex.Replicas + } + lbl := map[string]string{ + workflowproj.LabelApp: platform.Name, + } + dataDeploySpec := appsv1.DeploymentSpec{ + Selector: &metav1.LabelSelector{ + MatchLabels: lbl, + }, + Replicas: &replicas, + Template: corev1.PodTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Labels: lbl, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{*dataDeployContainer}, + Volumes: []corev1.Volume{ + { + Name: "application-config", + VolumeSource: corev1.VolumeSource{ + ConfigMap: &corev1.ConfigMapVolumeSource{ + LocalObjectReference: corev1.LocalObjectReference{ + Name: platform.Name + "-" + common.DataIndexName, + }, + }, + }, + }, + }, + }, + }, + } + if err := mergo.Merge(&dataDeploySpec.Template.Spec, platform.Spec.Services.DataIndex.FlowPodSpec.ToPodSpec(), mergo.WithOverride); err != nil { + return err + } + + dataDeploy := &appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: platform.Namespace, + Name: platform.Name + "-" + common.DataIndexName, + Labels: lbl, + }} + if err := controllerutil.SetControllerReference(platform, dataDeploy, client.Scheme()); err != nil { + return err + } + + // Create or Update the deployment + if op, err := controllerutil.CreateOrUpdate(ctx, client, dataDeploy, func() error { + dataDeploy.Spec = dataDeploySpec + + return nil + }); err != nil { + return err + } else { + klog.V(log.I).InfoS("Deployment successfully reconciled", "operation", op) + } + + return nil +} + +func configurePersistence(ctx context.Context, client client.Client, dataDeployContainer *corev1.Container, platform *operatorapi.SonataFlowPlatform) { + if platform.Spec.Services.Persistence != nil { + if platform.Spec.Services.Persistence.PostgreSql != nil { + configurePostgreSql(ctx, client, dataDeployContainer, platform) + } + } +} + +func configurePostgreSql(ctx context.Context, client client.Client, dataDeployContainer *corev1.Container, platform *operatorapi.SonataFlowPlatform) { + persistenceType := common.PersistenceTypePostgressql + dataDeployContainer.Image = common.DataIndexImageBase + persistenceType + + databaseNamespace := platform.Namespace + if len(platform.Spec.Services.Persistence.PostgreSql.ServiceRef.Namespace) > 0 { + databaseNamespace = platform.Spec.Services.Persistence.PostgreSql.ServiceRef.Namespace + } + dataSourcePort := 5432 + if platform.Spec.Services.Persistence.PostgreSql.ServiceRef.Port != nil { + dataSourcePort = *platform.Spec.Services.Persistence.PostgreSql.ServiceRef.Port + } + databaseName := "sonataflow" + if len(platform.Spec.Services.Persistence.PostgreSql.DatabaseName) > 0 { + databaseName = platform.Spec.Services.Persistence.PostgreSql.DatabaseName + } + dataSourceUrl := "jdbc:" + persistenceType + "://" + platform.Spec.Services.Persistence.PostgreSql.ServiceRef.Name + "." + databaseNamespace + ":" + strconv.Itoa(dataSourcePort) + "/" + databaseName + "?currentSchema=data-index-service" + + secretRef := corev1.LocalObjectReference{ + Name: platform.Spec.Services.Persistence.PostgreSql.SecretRef.Name, + } + quarkusDatasourceUsername := "POSTGRESQL_USER" + if len(platform.Spec.Services.Persistence.PostgreSql.SecretRef.UserKey) > 0 { + quarkusDatasourceUsername = platform.Spec.Services.Persistence.PostgreSql.SecretRef.UserKey + } + quarkusDatasourcePassword := "POSTGRESQL_PASSWORD" + if len(platform.Spec.Services.Persistence.PostgreSql.SecretRef.PasswordKey) > 0 { + quarkusDatasourcePassword = platform.Spec.Services.Persistence.PostgreSql.SecretRef.PasswordKey + } + persistEnvVars := []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: persistenceType, + }, + { + Name: "QUARKUS_HIBERNATE_ORM_DATABASE_GENERATION", + Value: "update", + }, + { + Name: "QUARKUS_FLYWAY_MIGRATE_AT_START", + Value: "true", + }, + { + Name: "QUARKUS_DATASOURCE_JDBC_URL", + Value: dataSourceUrl, + }, + } + dataDeployContainer.Env = append(dataDeployContainer.Env, persistEnvVars...) +} + +func createDataIndexService(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform) error { + lbl := map[string]string{ + workflowproj.LabelApp: platform.Name, + } + dataSvcSpec := corev1.ServiceSpec{ + Ports: []corev1.ServicePort{ + { + Name: httpPort, + Protocol: corev1.ProtocolTCP, + Port: 80, + TargetPort: common.DefaultHTTPWorkflowPortIntStr, + }, + }, + Selector: lbl, + } + dataSvc := &corev1.Service{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: platform.Namespace, + Name: platform.Name + "-" + common.DataIndexName, Review Comment: `platform.Name + "-" + common.DataIndexName` repeats several times. perhaps replace it with a util function to determine the format of the name for all occurrences, since it is used both for creating the resource and referring to it. ########## api/v1alpha08/sonataflowplatform_services_types.go: ########## @@ -0,0 +1,89 @@ +// Copyright 2023 Red Hat, Inc. and/or its affiliates +// +// 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 v1alpha08 + +// ServicesPlatformSpec describes the desired service configuration for "prod" workflows. +type ServicesPlatformSpec struct { + // Deploys the Data Index service for use by "prod" profile workflows. + // +optional + DataIndex *ServicesPodTemplateSpec `json:"dataIndex,omitempty"` + // Persists service(s) to a datasource of choice. Ephemeral by default. + // +optional + Persistence *PersistenceOptions `json:"persistence,omitempty"` +} + +// ServicesPodTemplateSpec describes the desired custom Kubernetes PodTemplate definition for the deployed flow. +// +// The FlowContainer describes the container where the actual service is running. It will override any default definitions. +// For example, to override the image one can use `.spec.services.dataIndex.container.image = my/image:tag`. +type ServicesPodTemplateSpec struct { + // Container is the Kubernetes container where the service should run. + // One can change this attribute in order to override the defaults provided by the operator. + // +optional + Container *FlowContainer `json:"container,omitempty"` + // Describes the PodSpec for the internal service deployment based on the default Kubernetes PodSpec API + // +optional + FlowPodSpec `json:",inline"` Review Comment: +1 for PodSpec. By looking at `platform.Spec.Services.DataIndex.FlowPodSpec` it seems that the `Flow` prefix to the attribute `FlowPodSpec` doesn't make it more meaningful in this context and can be removed. (ServicePodSpec would fit better here, but PodSpec is good as well). ########## controllers/platform/services.go: ########## @@ -0,0 +1,360 @@ +// Copyright 2023 Red Hat, Inc. and/or its affiliates +// +// 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 platform + +import ( + "context" + "strconv" + "strings" + + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/klog/v2" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + + operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08" + "github.com/apache/incubator-kie-kogito-serverless-operator/container-builder/client" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/common" + "github.com/apache/incubator-kie-kogito-serverless-operator/log" + "github.com/apache/incubator-kie-kogito-serverless-operator/workflowproj" + "github.com/imdario/mergo" +) + +var httpPort = strings.ToLower(string(corev1.URISchemeHTTP)) + +// NewServiceAction returns an action that deploys the services. +func NewServiceAction() Action { + return &serviceAction{} +} + +type serviceAction struct { + baseAction +} + +func (action *serviceAction) Name() string { + return "service" +} + +func (action *serviceAction) CanHandle(platform *operatorapi.SonataFlowPlatform) bool { + return platform.Status.IsReady() +} + +func (action *serviceAction) Handle(ctx context.Context, platform *operatorapi.SonataFlowPlatform) (*operatorapi.SonataFlowPlatform, error) { + // Refresh applied configuration + if err := ConfigureDefaults(ctx, action.client, platform, false); err != nil { + return nil, err + } + + if err := createDataIndexComponents(ctx, action.client, platform); err != nil { + return nil, err + } + + return platform, nil +} + +func createDataIndexComponents(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform) error { + if platform.Spec.Services.DataIndex != nil { + if err := createDataIndexConfigMap(ctx, client, platform); err != nil { + return err + } + if err := createDataIndexDeployment(ctx, client, platform); err != nil { + return err + } + if err := createDataIndexService(ctx, client, platform); err != nil { + return err + } + } + + return nil +} + +func createDataIndexDeployment(ctx context.Context, client client.Client, platform *operatorapi.SonataFlowPlatform) error { + probe := &corev1.Probe{ + ProbeHandler: corev1.ProbeHandler{ + HTTPGet: &corev1.HTTPGetAction{ + Path: "/q/health/ready", + Port: common.DefaultHTTPWorkflowPortIntStr, + Scheme: corev1.URISchemeHTTP, + }, + }, + InitialDelaySeconds: int32(15), + TimeoutSeconds: int32(10), + PeriodSeconds: int32(30), + SuccessThreshold: int32(1), + FailureThreshold: int32(3), + } + dataDeployContainer := &corev1.Container{ + Name: common.DataIndexName, + Image: common.DataIndexImageBase + common.PersistenceTypeEphemeral, + Env: []corev1.EnvVar{ + { + Name: "KOGITO_DATA_INDEX_QUARKUS_PROFILE", + Value: "http-events-support", + }, + { + Name: "QUARKUS_HTTP_PORT", + Value: "8080", + }, + { + Name: "QUARKUS_HTTP_CORS", + Value: "true", + }, + { + Name: "QUARKUS_HTTP_CORS_ORIGINS", + Value: "/.*/", + }, + { + Name: "QUARKUS_KAFKA_HEALTH_ENABLE", + Value: "false", + }, + }, + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("100m"), + corev1.ResourceMemory: resource.MustParse("256Mi"), Review Comment: will this be configurable via FlowPodSpec? Meaning, the option to override service configuration (such as resources). -- 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]
