Skip to content

Commit

Permalink
Merge pull request #65 from rgdoliveira/sync_main
Browse files Browse the repository at this point in the history
Sync main branch with Apache main branch
  • Loading branch information
rgdoliveira authored Aug 7, 2024
2 parents c0584f0 + 6fcaa8c commit 79bf871
Show file tree
Hide file tree
Showing 32 changed files with 335 additions and 105 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,4 @@ create-cluster: install-kind

.PHONY: delete-cluster
delete-cluster: install-kind
kind delete cluster && docker rm -f kind-registry
kind delete cluster && docker rm -f kind-registry
2 changes: 1 addition & 1 deletion bddframework/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
toolchain go1.21.6

// Internal dependencies
replace github.com/apache/incubator-kie-kogito-serverless-operator v0.0.0 => ../
replace github.com/apache/incubator-kie-kogito-serverless-operator => ../

require (
github.com/cucumber/gherkin-go/v19 v19.0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ spec:
serviceAccountName: sonataflow-operator-controller-manager
deployments:
- label:
app.kubernetes.io/name: sonataflow-operator
control-plane: sonataflow-operator
name: sonataflow-operator-controller-manager
spec:
Expand Down
1 change: 1 addition & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ metadata:
namespace: system
labels:
control-plane: sonataflow-operator
app.kubernetes.io/name: sonataflow-operator
spec:
selector:
matchLabels:
Expand Down
22 changes: 11 additions & 11 deletions controllers/clusterplatform/clusterplatform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ import (
"github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata"
operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
"github.com/apache/incubator-kie-kogito-serverless-operator/log"
"github.com/apache/incubator-kie-kogito-serverless-operator/utils"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/klog/v2"
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)
func GetActiveClusterPlatform(ctx context.Context) (*operatorapi.SonataFlowClusterPlatform, error) {
return getClusterPlatform(ctx, true)
}

// getClusterPlatform returns the currently active cluster platform or any cluster platform existing in the cluster.
func getClusterPlatform(ctx context.Context, c ctrl.Client, active bool) (*operatorapi.SonataFlowClusterPlatform, error) {
func getClusterPlatform(ctx context.Context, active bool) (*operatorapi.SonataFlowClusterPlatform, error) {
klog.V(log.D).InfoS("Finding available cluster platforms")

lst, err := listPrimaryClusterPlatforms(ctx, c)
lst, err := listPrimaryClusterPlatforms(ctx)
if err != nil {
return nil, err
}
Expand All @@ -66,8 +66,8 @@ func getClusterPlatform(ctx context.Context, c ctrl.Client, active bool) (*opera
}

// listPrimaryClusterPlatforms returns all non-secondary cluster platforms installed (only one will be active).
func listPrimaryClusterPlatforms(ctx context.Context, c ctrl.Reader) (*operatorapi.SonataFlowClusterPlatformList, error) {
lst, err := listAllClusterPlatforms(ctx, c)
func listPrimaryClusterPlatforms(ctx context.Context) (*operatorapi.SonataFlowClusterPlatformList, error) {
lst, err := listAllClusterPlatforms(ctx)
if err != nil {
return nil, err
}
Expand All @@ -83,8 +83,8 @@ func listPrimaryClusterPlatforms(ctx context.Context, c ctrl.Reader) (*operatora
}

// allDuplicatedClusterPlatforms returns true if every cluster platform has a "Duplicated" status set
func allDuplicatedClusterPlatforms(ctx context.Context, c ctrl.Reader) bool {
lst, err := listAllClusterPlatforms(ctx, c)
func allDuplicatedClusterPlatforms(ctx context.Context) bool {
lst, err := listAllClusterPlatforms(ctx)
if err != nil {
return false
}
Expand All @@ -99,9 +99,9 @@ func allDuplicatedClusterPlatforms(ctx context.Context, c ctrl.Reader) bool {
}

// listAllClusterPlatforms returns all clusterplatforms installed.
func listAllClusterPlatforms(ctx context.Context, c ctrl.Reader) (*operatorapi.SonataFlowClusterPlatformList, error) {
func listAllClusterPlatforms(ctx context.Context) (*operatorapi.SonataFlowClusterPlatformList, error) {
lst := operatorapi.NewSonataFlowClusterPlatformList()
if err := c.List(ctx, &lst); err != nil {
if err := utils.GetClient().List(ctx, &lst); err != nil {
return nil, err
}
return &lst, nil
Expand Down
4 changes: 2 additions & 2 deletions controllers/clusterplatform/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (action *initializeAction) Name() string {
}

func (action *initializeAction) CanHandle(ctx context.Context, cPlatform *operatorapi.SonataFlowClusterPlatform) bool {
return !cPlatform.Status.IsDuplicated() || allDuplicatedClusterPlatforms(ctx, action.client)
return !cPlatform.Status.IsDuplicated() || allDuplicatedClusterPlatforms(ctx)
}

func (action *initializeAction) Handle(ctx context.Context, cPlatform *operatorapi.SonataFlowClusterPlatform) error {
Expand Down Expand Up @@ -107,7 +107,7 @@ func (action *initializeAction) isPrimaryDuplicate(ctx context.Context, cPlatfor
// Always reconcile secondary cluster platforms
return false, nil
}
platforms, err := listPrimaryClusterPlatforms(ctx, action.client)
platforms, err := listPrimaryClusterPlatforms(ctx)
if err != nil {
return false, err
}
Expand Down
44 changes: 7 additions & 37 deletions controllers/platform/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/apache/incubator-kie-kogito-serverless-operator/container-builder/client"
"github.com/apache/incubator-kie-kogito-serverless-operator/log"
"github.com/apache/incubator-kie-kogito-serverless-operator/utils"

Expand All @@ -36,7 +34,7 @@ import (

const defaultSonataFlowPlatformName = "sonataflow-platform"

func ConfigureDefaults(ctx context.Context, c client.Client, p *operatorapi.SonataFlowPlatform, verbose bool) error {
func CreateOrUpdateWithDefaults(ctx context.Context, p *operatorapi.SonataFlowPlatform, verbose bool) error {
// update missing fields in the resource
if p.Status.Cluster == "" || utils.IsOpenShift() {
p.Status.Cluster = operatorapi.PlatformClusterOpenShift
Expand All @@ -52,7 +50,7 @@ func ConfigureDefaults(ctx context.Context, c client.Client, p *operatorapi.Sona
return err
}

err = configureRegistry(ctx, c, p, verbose)
err = configureRegistry(ctx, p, verbose)
if err != nil {
return err
}
Expand All @@ -61,53 +59,25 @@ func ConfigureDefaults(ctx context.Context, c client.Client, p *operatorapi.Sona
klog.V(log.I).InfoS("Maven Timeout set", "timeout", p.Spec.Build.Config.Timeout.Duration)
}

return createOrUpdatePlatform(ctx, c, p)
return createOrUpdatePlatform(ctx, p)
}

func createOrUpdatePlatform(ctx context.Context, c client.Client, p *operatorapi.SonataFlowPlatform) error {
func createOrUpdatePlatform(ctx context.Context, p *operatorapi.SonataFlowPlatform) error {
config := operatorapi.SonataFlowPlatform{}
err := c.Get(ctx, ctrl.ObjectKey{Namespace: p.Namespace, Name: p.Name}, &config)
err := utils.GetClient().Get(ctx, ctrl.ObjectKey{Namespace: p.Namespace, Name: p.Name}, &config)
if errors.IsNotFound(err) {
klog.V(log.D).ErrorS(err, "Platform not found, creating it")
return c.Create(ctx, p)
return utils.GetClient().Create(ctx, p)
} else if err != nil {
klog.V(log.E).ErrorS(err, "Error reading the Platform")
return err
}

config.Spec = p.Spec
config.Status.Cluster = p.Status.Cluster
err = c.Update(ctx, &config)
err = utils.GetClient().Update(ctx, &config)
if err != nil {
klog.V(log.E).ErrorS(err, "Error updating the BuildPlatform")
}
return err
}

func newDefaultSonataFlowPlatform(namespace string) *operatorapi.SonataFlowPlatform {
if utils.IsOpenShift() {
return &operatorapi.SonataFlowPlatform{
ObjectMeta: metav1.ObjectMeta{Name: defaultSonataFlowPlatformName, Namespace: namespace},
Spec: operatorapi.SonataFlowPlatformSpec{
Build: operatorapi.BuildPlatformSpec{
Config: operatorapi.BuildPlatformConfig{
BuildStrategy: operatorapi.PlatformBuildStrategy,
},
},
},
}
}

return &operatorapi.SonataFlowPlatform{
ObjectMeta: metav1.ObjectMeta{Name: defaultSonataFlowPlatformName, Namespace: namespace},
Spec: operatorapi.SonataFlowPlatformSpec{
Build: operatorapi.BuildPlatformSpec{
Config: operatorapi.BuildPlatformConfig{
BuildStrategyOptions: map[string]string{
kanikoBuildCacheEnabled: "true",
},
},
},
},
}
}
2 changes: 1 addition & 1 deletion controllers/platform/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (action *initializeAction) Handle(ctx context.Context, platform *operatorap
return nil, nil
}

if err = ConfigureDefaults(ctx, action.client, platform, true); err != nil {
if err = CreateOrUpdateWithDefaults(ctx, platform, true); err != nil {
return nil, err
}
// nolint: staticcheck
Expand Down
9 changes: 6 additions & 3 deletions controllers/platform/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (action *serviceAction) CanHandle(platform *operatorapi.SonataFlowPlatform)

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 {
if err := CreateOrUpdateWithDefaults(ctx, platform, false); err != nil {
return nil, err
}

Expand Down Expand Up @@ -233,8 +233,11 @@ func createOrUpdateService(ctx context.Context, client client.Client, platform *

func getLabels(platform *operatorapi.SonataFlowPlatform, psh services.PlatformServiceHandler) (map[string]string, map[string]string) {
lbl := map[string]string{
workflowproj.LabelApp: platform.Name,
workflowproj.LabelService: psh.GetServiceName(),
workflowproj.LabelService: psh.GetServiceName(),
workflowproj.LabelK8SName: psh.GetContainerName(),
workflowproj.LabelK8SComponent: psh.GetServiceName(),
workflowproj.LabelK8SPartOF: platform.Name,
workflowproj.LabelK8SManagedBy: "sonataflow-operator",
}
selectorLbl := map[string]string{
workflowproj.LabelService: psh.GetServiceName(),
Expand Down
2 changes: 1 addition & 1 deletion controllers/platform/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (action *monitorAction) Handle(ctx context.Context, platform *operatorapi.S
}

// Refresh applied configuration
if err := ConfigureDefaults(ctx, action.client, platform, false); err != nil {
if err := CreateOrUpdateWithDefaults(ctx, platform, false); err != nil {
return nil, err
}

Expand Down
33 changes: 31 additions & 2 deletions controllers/platform/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"strings"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"

coordination "k8s.io/api/coordination/v1"
Expand Down Expand Up @@ -147,13 +148,41 @@ func getLocalPlatform(ctx context.Context, c ctrl.Client, namespace string, acti
}
klog.V(log.I).InfoS("Not found a local build platform", "Namespace", namespace)
klog.V(log.I).InfoS("Creating a default SonataFlowPlatform", "Namespace", namespace)
sfp := newDefaultSonataFlowPlatform(namespace)
if err = c.Create(ctx, sfp); err != nil {
sfp := newEmptySonataFlowPlatform(namespace)
if err = CreateOrUpdateWithDefaults(ctx, sfp, false); err != nil {
return nil, err
}
return sfp, nil
}

func newEmptySonataFlowPlatform(namespace string) *operatorapi.SonataFlowPlatform {
if utils.IsOpenShift() {
return &operatorapi.SonataFlowPlatform{
ObjectMeta: metav1.ObjectMeta{Name: defaultSonataFlowPlatformName, Namespace: namespace},
Spec: operatorapi.SonataFlowPlatformSpec{
Build: operatorapi.BuildPlatformSpec{
Config: operatorapi.BuildPlatformConfig{
BuildStrategy: operatorapi.PlatformBuildStrategy,
},
},
},
}
}

return &operatorapi.SonataFlowPlatform{
ObjectMeta: metav1.ObjectMeta{Name: defaultSonataFlowPlatformName, Namespace: namespace},
Spec: operatorapi.SonataFlowPlatformSpec{
Build: operatorapi.BuildPlatformSpec{
Config: operatorapi.BuildPlatformConfig{
BuildStrategyOptions: map[string]string{
kanikoBuildCacheEnabled: "true",
},
},
},
},
}
}

// listPrimaryPlatforms returns all non-secondary platforms installed in a given namespace (only one will be active).
func listPrimaryPlatforms(ctx context.Context, c ctrl.Reader, namespace string) (*operatorapi.SonataFlowPlatformList, error) {
lst, err := listAllPlatforms(ctx, c, namespace)
Expand Down
10 changes: 5 additions & 5 deletions controllers/platform/platformutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"time"

"github.com/apache/incubator-kie-kogito-serverless-operator/utils"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -35,7 +36,6 @@ import (

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

"github.com/apache/incubator-kie-kogito-serverless-operator/container-builder/client"
"github.com/apache/incubator-kie-kogito-serverless-operator/log"

operatorapi "github.com/apache/incubator-kie-kogito-serverless-operator/api/v1alpha08"
Expand All @@ -46,7 +46,7 @@ var builderDockerfileFromRE = regexp.MustCompile(`FROM (.*) AS builder`)
// ResourceCustomizer can be used to inject code that changes the objects before they are created.
type ResourceCustomizer func(object ctrl.Object) ctrl.Object

func configureRegistry(ctx context.Context, c client.Client, p *operatorapi.SonataFlowPlatform, verbose bool) error {
func configureRegistry(ctx context.Context, p *operatorapi.SonataFlowPlatform, verbose bool) error {
if p.Spec.Build.Config.BuildStrategy == operatorapi.PlatformBuildStrategy && p.Status.Cluster == operatorapi.PlatformClusterOpenShift {
p.Spec.Build.Config.Registry = operatorapi.RegistrySpec{}
klog.V(log.D).InfoS("Platform registry not set and ignored on openshift cluster")
Expand All @@ -55,7 +55,7 @@ func configureRegistry(ctx context.Context, c client.Client, p *operatorapi.Sona

if p.Spec.Build.Config.Registry.Address == "" && p.Status.Cluster == operatorapi.PlatformClusterKubernetes {
// try KEP-1755
address, err := GetRegistryAddress(ctx, c)
address, err := GetRegistryAddress(ctx)
if err != nil && verbose {
klog.V(log.E).ErrorS(err, "Cannot find a registry where to push images via KEP-1755")
} else if err == nil && address != nil {
Expand Down Expand Up @@ -139,9 +139,9 @@ func setStatusAdditionalInfo(platform *operatorapi.SonataFlowPlatform) {

// GetRegistryAddress KEP-1755
// https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
func GetRegistryAddress(ctx context.Context, c client.Client) (*string, error) {
func GetRegistryAddress(ctx context.Context) (*string, error) {
config := corev1.ConfigMap{}
err := c.Get(ctx, ctrl.ObjectKey{Namespace: "kube-public", Name: "local-registry-hosting"}, &config)
err := utils.GetClient().Get(ctx, ctrl.ObjectKey{Namespace: "kube-public", Name: "local-registry-hosting"}, &config)
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, nil
Expand Down
28 changes: 27 additions & 1 deletion controllers/profiles/common/mutate_visitors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@ package common

import (
"context"
"maps"
"reflect"
"slices"

"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/discovery"
"github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/common/properties"
"github.com/imdario/mergo"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
servingv1 "knative.dev/serving/pkg/apis/serving/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand Down Expand Up @@ -86,7 +91,28 @@ func DeploymentMutateVisitor(workflow *operatorapi.SonataFlow, plf *operatorapi.
if err != nil {
return err
}
return EnsureDeployment(original.(*appsv1.Deployment), object.(*appsv1.Deployment))
src := original.(*appsv1.Deployment)
dst := object.(*appsv1.Deployment)
// merge new and old labels, but prevent overriding to keep exiting immutable selector working.
mergo.Merge(&dst.ObjectMeta.Labels, src.ObjectMeta.Labels, mergo.WithAppendSlice)
// to prevent furhter merge conflcts set the same lables on both src and dst
src.ObjectMeta.Labels = dst.ObjectMeta.Labels
if !maps.Equal(dst.Spec.Selector.MatchLabels, src.Spec.Selector.MatchLabels) {
// mutating selector labels is not supported so to prevent merge conflicts we set src and dst
// values to be identical
src.Spec.Selector.MatchLabels = dst.Spec.Selector.MatchLabels
}
if !slices.EqualFunc(
dst.Spec.Selector.MatchExpressions,
src.Spec.Selector.MatchExpressions,
func(lsr1, lsr2 metav1.LabelSelectorRequirement) bool {
return reflect.DeepEqual(lsr1, lsr2)
}) {
// mutating selector matchExpressions is not supported so to prevent merge conflicts we set src and dst
// values to be identical
src.Spec.Selector.MatchExpressions = dst.Spec.Selector.MatchExpressions
}
return EnsureDeployment(src, dst)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/profiles/common/object_creators.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func DeploymentCreator(workflow *operatorapi.SonataFlow, plf *operatorapi.Sonata
Spec: appsv1.DeploymentSpec{
Replicas: getReplicasOrDefault(workflow),
Selector: &metav1.LabelSelector{
MatchLabels: lbl,
MatchLabels: workflowproj.GetSelectorLabels(workflow),
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Expand Down
Loading

0 comments on commit 79bf871

Please sign in to comment.