Skip to content

Commit

Permalink
[380] enhance SonataFlowClusterPlatform.spec (apache#389)
Browse files Browse the repository at this point in the history
Signed-off-by: Tommy Hughes <[email protected]>
  • Loading branch information
tchughesiv authored and rgdoliveira committed Mar 11, 2024
1 parent 6d822b4 commit 1e8644b
Show file tree
Hide file tree
Showing 28 changed files with 937 additions and 13 deletions.
12 changes: 12 additions & 0 deletions api/v1alpha08/sonataflowclusterplatform_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ type SonataFlowClusterPlatformSpec struct {
// PlatformRef defines which existing SonataFlowPlatform's supporting services should be used cluster-wide.
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="PlatformRef"
PlatformRef SonataFlowPlatformRef `json:"platformRef"`
// Capabilities defines which platform capabilities should be applied cluster-wide. If nil, defaults to `capabilities.workflows["services"]`
//+operator-sdk:csv:customresourcedefinitions:type=spec,displayName="Capabilities"
Capabilities *SonataFlowClusterPlatformCapSpec `json:"capabilities,omitempty"`
}

// SonataFlowClusterPlatformCapSpec defines which platform capabilities should be applied cluster-wide
type SonataFlowClusterPlatformCapSpec struct {
// Workflows defines which platform capabilities should be applied to workflows cluster-wide.
Workflows []WorkFlowCapability `json:"workflows,omitempty"`
}

// +kubebuilder:validation:Enum=services
type WorkFlowCapability string

// SonataFlowPlatformRef defines which existing SonataFlowPlatform's supporting services should be used cluster-wide.
type SonataFlowPlatformRef struct {
// Name of the SonataFlowPlatform
Expand Down
27 changes: 26 additions & 1 deletion api/v1alpha08/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ spec:
name: A SonataFlow Platform
version: sonataflow.org/v1alpha08
specDescriptors:
- description: Capabilities defines which platform capabilities should be applied
cluster-wide. If nil, defaults to `capabilities.workflows["services"]`
displayName: Capabilities
path: capabilities
- description: PlatformRef defines which existing SonataFlowPlatform's supporting
services should be used cluster-wide.
displayName: PlatformRef
Expand Down
13 changes: 13 additions & 0 deletions bundle/manifests/sonataflow.org_sonataflowclusterplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ spec:
description: SonataFlowClusterPlatformSpec defines the desired state of
SonataFlowClusterPlatform
properties:
capabilities:
description: Capabilities defines which platform capabilities should
be applied cluster-wide. If nil, defaults to `capabilities.workflows["services"]`
properties:
workflows:
description: Workflows defines which platform capabilities should
be applied to workflows cluster-wide.
items:
enum:
- services
type: string
type: array
type: object
platformRef:
description: PlatformRef defines which existing SonataFlowPlatform's
supporting services should be used cluster-wide.
Expand Down
13 changes: 13 additions & 0 deletions config/crd/bases/sonataflow.org_sonataflowclusterplatforms.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ spec:
description: SonataFlowClusterPlatformSpec defines the desired state of
SonataFlowClusterPlatform
properties:
capabilities:
description: Capabilities defines which platform capabilities should
be applied cluster-wide. If nil, defaults to `capabilities.workflows["services"]`
properties:
workflows:
description: Workflows defines which platform capabilities should
be applied to workflows cluster-wide.
items:
enum:
- services
type: string
type: array
type: object
platformRef:
description: PlatformRef defines which existing SonataFlowPlatform's
supporting services should be used cluster-wide.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ spec:
name: A SonataFlow Platform
version: sonataflow.org/v1alpha08
specDescriptors:
- description: Capabilities defines which platform capabilities should be applied
cluster-wide. If nil, defaults to `capabilities.workflows["services"]`
displayName: Capabilities
path: capabilities
- description: PlatformRef defines which existing SonataFlowPlatform's supporting
services should be used cluster-wide.
displayName: PlatformRef
Expand Down
4 changes: 4 additions & 0 deletions controllers/clusterplatform/clusterplatform.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import (
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
)

const (
PlatformServices operatorapi.WorkFlowCapability = "services"
)

// GetActiveClusterPlatform returns the currently installed active cluster platform.
func GetActiveClusterPlatform(ctx context.Context, c ctrl.Client) (*operatorapi.SonataFlowClusterPlatform, error) {
return getClusterPlatform(ctx, c, true)
Expand Down
55 changes: 55 additions & 0 deletions controllers/clusterplatform/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 clusterplatform

import (
"context"

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/log"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
)

func configureDefaults(ctx context.Context, c client.Client, cp *operatorapi.SonataFlowClusterPlatform, verbose bool) error {
if cp.Spec.Capabilities == nil {
cp.Spec.Capabilities = &operatorapi.SonataFlowClusterPlatformCapSpec{
Workflows: []operatorapi.WorkFlowCapability{PlatformServices},
}
}

return updateClusterPlatform(ctx, c, cp)
}

func updateClusterPlatform(ctx context.Context, c client.Client, cp *operatorapi.SonataFlowClusterPlatform) error {
sfcPlatform := operatorapi.SonataFlowClusterPlatform{}
if err := c.Get(ctx, ctrl.ObjectKey{Namespace: cp.Namespace, Name: cp.Name}, &sfcPlatform); err != nil {
klog.V(log.E).ErrorS(err, "Error reading the Cluster Platform")
return err
}

sfcPlatform.Spec = cp.Spec
if err := c.Update(ctx, &sfcPlatform); err != nil {
klog.V(log.E).ErrorS(err, "Error updating the Cluster Platform")
}

return nil
}
4 changes: 4 additions & 0 deletions controllers/clusterplatform/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (action *initializeAction) Handle(ctx context.Context, cPlatform *operatora
}
return nil
}

if err = configureDefaults(ctx, action.client, cPlatform, true); err != nil {
return err
}
cPlatform.Status.Version = metadata.SpecVersion
platformRef := cPlatform.Spec.PlatformRef

Expand Down
8 changes: 4 additions & 4 deletions controllers/platform/services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (d DataIndexHandler) GetServiceBaseUrl() string {
}

func (d DataIndexHandler) GetLocalServiceBaseUrl() string {
return generateServiceURL(constants.KogitoServiceURLProtocol, d.platform.Namespace, d.GetServiceName())
return GenerateServiceURL(constants.KogitoServiceURLProtocol, d.platform.Namespace, d.GetServiceName())
}

func (d DataIndexHandler) GetEnvironmentVariables() []corev1.EnvVar {
Expand Down Expand Up @@ -327,7 +327,7 @@ func (j JobServiceHandler) GetServiceBaseUrl() string {
}

func (j JobServiceHandler) GetLocalServiceBaseUrl() string {
return generateServiceURL(constants.JobServiceURLProtocol, j.platform.Namespace, j.GetServiceName())
return GenerateServiceURL(constants.JobServiceURLProtocol, j.platform.Namespace, j.GetServiceName())
}

func (j JobServiceHandler) GetEnvironmentVariables() []corev1.EnvVar {
Expand Down Expand Up @@ -395,7 +395,7 @@ func (j JobServiceHandler) MergePodSpec(podSpec corev1.PodSpec) (corev1.PodSpec,

func (j JobServiceHandler) GenerateServiceProperties() (*properties.Properties, error) {
props := properties.NewProperties()
props.Set(constants.KogitoServiceURLProperty, generateServiceURL(constants.KogitoServiceURLProtocol, j.platform.Namespace, j.GetServiceName()))
props.Set(constants.KogitoServiceURLProperty, GenerateServiceURL(constants.KogitoServiceURLProtocol, j.platform.Namespace, j.GetServiceName()))
props.Set(constants.JobServiceKafkaSmallRyeHealthProperty, "false")
// add data source reactive URL
if j.hasPostgreSQLConfigured() {
Expand Down Expand Up @@ -447,7 +447,7 @@ func isServicesSet(platform *operatorapi.SonataFlowPlatform) bool {
return platform != nil && platform.Spec.Services != nil
}

func generateServiceURL(protocol string, namespace string, name string) string {
func GenerateServiceURL(protocol string, namespace string, name string) string {
var serviceUrl string
if len(namespace) > 0 {
serviceUrl = fmt.Sprintf("%s://%s.%s", protocol, name, namespace)
Expand Down
19 changes: 15 additions & 4 deletions controllers/sonataflowplatform_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,13 @@ func (r *SonataFlowPlatformReconciler) SonataFlowPlatformUpdateStatus(ctx contex
},
}

tpsDI := services.NewDataIndexHandler(target)
tpsDI.SetServiceUrlInStatus(sfPlatform)
if sfcPlatform.Spec.Capabilities != nil && contains(sfcPlatform.Spec.Capabilities.Workflows, clusterplatform.PlatformServices) {
tpsDI := services.NewDataIndexHandler(target)
tpsDI.SetServiceUrlInStatus(sfPlatform)

tpsJS := services.NewJobServiceHandler(target)
tpsJS.SetServiceUrlInStatus(sfPlatform)
tpsJS := services.NewJobServiceHandler(target)
tpsJS.SetServiceUrlInStatus(sfPlatform)
}
} else {
target.Status.ClusterPlatformRef = nil
}
Expand Down Expand Up @@ -273,3 +275,12 @@ func (r *SonataFlowPlatformReconciler) platformRequests(ctx context.Context, sfc
}
return requests
}

func contains(slice []operatorapi.WorkFlowCapability, s operatorapi.WorkFlowCapability) bool {
for _, a := range slice {
if a == s {
return true
}
}
return false
}
24 changes: 24 additions & 0 deletions controllers/sonataflowplatform_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/clusterplatform"
"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/platform/services"
"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/common/constants"
"github.com/apache/incubator-kie-kogito-serverless-operator/test"
Expand Down Expand Up @@ -753,6 +754,8 @@ func TestSonataFlowPlatformController(t *testing.T) {
assert.Equal(t, kscp.Name, ksp.Status.ClusterPlatformRef.Name)
assert.Equal(t, kscp.Spec.PlatformRef.Name, ksp.Status.ClusterPlatformRef.PlatformRef.Name)
assert.Equal(t, kscp.Spec.PlatformRef.Namespace, ksp.Status.ClusterPlatformRef.PlatformRef.Namespace)
assert.NotNil(t, kscp.Spec.Capabilities)
assert.Contains(t, kscp.Spec.Capabilities.Workflows, clusterplatform.PlatformServices)

assert.NotNil(t, ksp.Status.ClusterPlatformRef)
assert.Nil(t, ksp.Status.ClusterPlatformRef.Services)
Expand Down Expand Up @@ -802,5 +805,26 @@ func TestSonataFlowPlatformController(t *testing.T) {
assert.NotNil(t, ksp2.Status.ClusterPlatformRef)
assert.Equal(t, kscp.Spec.PlatformRef.Name, ksp2.Status.ClusterPlatformRef.PlatformRef.Name)
assert.Equal(t, kscp.Spec.PlatformRef.Namespace, ksp2.Status.ClusterPlatformRef.PlatformRef.Namespace)
assert.Nil(t, ksp2.Status.ClusterPlatformRef.Services)

kscp.Spec.Capabilities = &v1alpha08.SonataFlowClusterPlatformCapSpec{}
assert.NoError(t, cl.Update(context.TODO(), kscp))
_, err = cr.Reconcile(context.TODO(), cReq)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}

_, err = r.Reconcile(context.TODO(), req2)
if err != nil {
t.Fatalf("reconcile: (%v)", err)
}

assert.NoError(t, cl.Get(context.TODO(), types.NamespacedName{Name: kscp.Name}, kscp))
assert.NoError(t, cl.Get(context.TODO(), types.NamespacedName{Name: ksp2.Name, Namespace: ksp2.Namespace}, ksp2))

assert.NotNil(t, kscp.Spec.Capabilities)
assert.Empty(t, kscp.Spec.Capabilities.Workflows)
assert.NotNil(t, ksp2.Status.ClusterPlatformRef)
assert.Nil(t, ksp2.Status.ClusterPlatformRef.Services)
})
}
13 changes: 13 additions & 0 deletions operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ spec:
description: SonataFlowClusterPlatformSpec defines the desired state of
SonataFlowClusterPlatform
properties:
capabilities:
description: Capabilities defines which platform capabilities should
be applied cluster-wide. If nil, defaults to `capabilities.workflows["services"]`
properties:
workflows:
description: Workflows defines which platform capabilities should
be applied to workflows cluster-wide.
items:
enum:
- services
type: string
type: array
type: object
platformRef:
description: PlatformRef defines which existing SonataFlowPlatform's
supporting services should be used cluster-wide.
Expand Down
Loading

0 comments on commit 1e8644b

Please sign in to comment.