From ae8ab89c9def772012f9c50bafc5007176a5a2ac Mon Sep 17 00:00:00 2001 From: Walter Medvedeo Date: Wed, 13 Dec 2023 16:16:42 +0100 Subject: [PATCH] kie-issues-314: Operator driven service discovery API Phase3 - Code review suggestions 2 --- controllers/discovery/discovery.go | 9 ++++ .../discovery/discovery_knative_test.go | 8 ++-- controllers/discovery/discovery_test.go | 14 +++---- controllers/discovery/knative_catalog.go | 42 ++++++++++++++++++- controllers/knative/knative.go | 14 +++---- controllers/profiles/dev/profile_dev.go | 5 ++- controllers/profiles/dev/profile_dev_test.go | 20 +++++---- controllers/profiles/factory/factory.go | 8 ++-- controllers/profiles/prod/profile_prod.go | 10 +++-- .../profiles/prod/profile_prod_test.go | 20 +++++---- controllers/sonataflow_controller.go | 37 +--------------- utils/knative/common.go | 34 --------------- 12 files changed, 104 insertions(+), 117 deletions(-) delete mode 100644 utils/knative/common.go diff --git a/controllers/discovery/discovery.go b/controllers/discovery/discovery.go index 81136699a..d5b44255e 100644 --- a/controllers/discovery/discovery.go +++ b/controllers/discovery/discovery.go @@ -23,6 +23,8 @@ import ( "context" "fmt" + "k8s.io/client-go/rest" + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -92,6 +94,13 @@ func NewServiceCatalog(cli client.Client, knDiscoveryClient *KnDiscoveryClient) } } +func NewServiceCatalogForConfig(cli client.Client, cfg *rest.Config) ServiceCatalog { + return &sonataFlowServiceCatalog{ + kubernetesCatalog: newK8SServiceCatalog(cli), + knativeCatalog: newKnServiceCatalogForConfig(cfg), + } +} + func (c *sonataFlowServiceCatalog) Query(ctx context.Context, uri ResourceUri, outputFormat string) (string, error) { switch uri.Scheme { case KubernetesScheme: diff --git a/controllers/discovery/discovery_knative_test.go b/controllers/discovery/discovery_knative_test.go index 1bb2d7279..9169bbaa8 100644 --- a/controllers/discovery/discovery_knative_test.go +++ b/controllers/discovery/discovery_knative_test.go @@ -43,7 +43,7 @@ func Test_QueryKnativeService(t *testing.T) { func Test_QueryKnativeServiceNotFound(t *testing.T) { _, client := fakeservingclient.With(context.TODO()) - ctg := NewServiceCatalog(nil, NewKnDiscoveryClient(client.ServingV1(), nil)) + ctg := NewServiceCatalog(nil, newKnDiscoveryClient(client.ServingV1(), nil)) doTestQueryWithError(t, ctg, *NewResourceUriBuilder(KnativeScheme). Kind("services"). Group("serving.knative.dev"). @@ -72,7 +72,7 @@ func doTestQueryKnativeService(t *testing.T, expectedUri string) { }, } _, client := fakeservingclient.With(context.TODO(), service) - ctg := NewServiceCatalog(nil, NewKnDiscoveryClient(client.ServingV1(), nil)) + ctg := NewServiceCatalog(nil, newKnDiscoveryClient(client.ServingV1(), nil)) doTestQuery(t, ctg, *NewResourceUriBuilder(KnativeScheme). Kind("services"). Group("serving.knative.dev"). @@ -87,7 +87,7 @@ func Test_QueryKnativeBroker(t *testing.T) { func Test_QueryKnativeBrokerNotFound(t *testing.T) { _, client := fakeeventingclient.With(context.TODO()) - ctg := NewServiceCatalog(nil, NewKnDiscoveryClient(nil, client.EventingV1())) + ctg := NewServiceCatalog(nil, newKnDiscoveryClient(nil, client.EventingV1())) doTestQueryWithError(t, ctg, *NewResourceUriBuilder(KnativeScheme). Kind("brokers"). Group("eventing.knative.dev"). @@ -115,7 +115,7 @@ func doTestQueryKnativeBroker(t *testing.T, expectedUri string) { }, } _, client := fakeeventingclient.With(context.TODO(), broker) - ctg := NewServiceCatalog(nil, NewKnDiscoveryClient(nil, client.EventingV1())) + ctg := NewServiceCatalog(nil, newKnDiscoveryClient(nil, client.EventingV1())) doTestQuery(t, ctg, *NewResourceUriBuilder(KnativeScheme). Kind("brokers"). Group("eventing.knative.dev"). diff --git a/controllers/discovery/discovery_test.go b/controllers/discovery/discovery_test.go index 8c29b0a34..1c28881b5 100644 --- a/controllers/discovery/discovery_test.go +++ b/controllers/discovery/discovery_test.go @@ -65,7 +65,7 @@ func doTestQueryKubernetesService(t *testing.T, outputFormat string, expectedUri service.Spec.Type = corev1.ServiceTypeNodePort service.Spec.ClusterIP = "10.1.5.18" cli := fake.NewClientBuilder().WithRuntimeObjects(service).Build() - ctg := NewServiceCatalog(cli, NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(cli, newKnDiscoveryClient(nil, nil)) doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme). Kind("services"). Version("v1"). @@ -86,7 +86,7 @@ func doTestQueryKubernetesPod(t *testing.T, outputFormat string, expectedUri str *mockContainerWithPorts("container1Name", mockContainerPort(httpProtocol, tcp, defaultHttpPort))) pod.Status.PodIP = "10.1.12.13" cli := fake.NewClientBuilder().WithRuntimeObjects(pod).Build() - ctg := NewServiceCatalog(cli, NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(cli, newKnDiscoveryClient(nil, nil)) doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme). Kind("pods"). Version("v1"). @@ -116,7 +116,7 @@ func doTesQueryKubernetesDeploymentWithService(t *testing.T, outputFormat string service.Spec.Type = corev1.ServiceTypeNodePort cli := fake.NewClientBuilder().WithRuntimeObjects(deployment, service).Build() - ctg := NewServiceCatalog(cli, NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(cli, newKnDiscoveryClient(nil, nil)) doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme). Group("apps"). @@ -142,7 +142,7 @@ func doTestQueryKubernetesDeploymentWithoutService(t *testing.T, outputFormat st } deployment := mockDeployment(namespace1, deployment1Name, nil, &selector) - ctg := NewServiceCatalog(fake.NewClientBuilder().WithRuntimeObjects(deployment).Build(), NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(fake.NewClientBuilder().WithRuntimeObjects(deployment).Build(), newKnDiscoveryClient(nil, nil)) uri := *NewResourceUriBuilder(KubernetesScheme). Group("apps"). @@ -176,7 +176,7 @@ func doTestQueryKubernetesStatefulSetWithService(t *testing.T, outputFormat stri service.Spec.Type = corev1.ServiceTypeNodePort cli := fake.NewClientBuilder().WithRuntimeObjects(statefulSet, service).Build() - ctg := NewServiceCatalog(cli, NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(cli, newKnDiscoveryClient(nil, nil)) doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme). Group("apps"). @@ -202,7 +202,7 @@ func doTestQueryKubernetesStatefulSetWithoutService(t *testing.T, outputFormat s } statefulSet := mockStatefulSet(namespace1, statefulSet1Name, nil, &selector) - ctg := NewServiceCatalog(fake.NewClientBuilder().WithRuntimeObjects(statefulSet).Build(), NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(fake.NewClientBuilder().WithRuntimeObjects(statefulSet).Build(), newKnDiscoveryClient(nil, nil)) uri := *NewResourceUriBuilder(KubernetesScheme). Group("apps"). @@ -237,7 +237,7 @@ func doTestQueryKubernetesIngress(t *testing.T, hostName string, ip string, tls ingress.Spec.TLS = []v1.IngressTLS{{}} } cli := fake.NewClientBuilder().WithRuntimeObjects(ingress).Build() - ctg := NewServiceCatalog(cli, NewKnDiscoveryClient(nil, nil)) + ctg := NewServiceCatalog(cli, newKnDiscoveryClient(nil, nil)) doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme). Kind("ingresses"). Group("networking.k8s.io"). diff --git a/controllers/discovery/knative_catalog.go b/controllers/discovery/knative_catalog.go index e3022182d..3b5d41bd8 100644 --- a/controllers/discovery/knative_catalog.go +++ b/controllers/discovery/knative_catalog.go @@ -23,6 +23,11 @@ import ( "context" "fmt" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/knative" + "github.com/apache/incubator-kie-kogito-serverless-operator/log" + "k8s.io/client-go/rest" + "k8s.io/klog/v2" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" clienteventingv1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1" @@ -49,7 +54,42 @@ func newKnServiceCatalog(discoveryClient *KnDiscoveryClient) knServiceCatalog { } } -func NewKnDiscoveryClient(servingClient clientservingv1.ServingV1Interface, eventingClient clienteventingv1.EventingV1Interface) *KnDiscoveryClient { +func newKnServiceCatalogForConfig(cfg *rest.Config) knServiceCatalog { + return knServiceCatalog{ + dc: newKnDiscoveryClientForConfig(cfg), + } +} + +// newKnDiscoveryClientForConfig returns a KnDiscoveryClient discovery client depending on the cluster status, if knative +// serving nor knative eventing are installed, or it was not possible to create that client, returns null. +func newKnDiscoveryClientForConfig(cfg *rest.Config) *KnDiscoveryClient { + var servingClient clientservingv1.ServingV1Interface + var eventingClient clienteventingv1.EventingV1Interface + + if avail, err := knative.GetKnativeAvailability(cfg); err != nil { + klog.V(log.E).ErrorS(err, "Unable to determine if knative is installed in the cluster") + return nil + } else { + if avail.Serving { + if servingClient, err = knative.GetKnativeServingClient(cfg); err != nil { + klog.V(log.E).ErrorS(err, "Unable to get the knative serving client") + return nil + } + } + if avail.Eventing { + if eventingClient, err = knative.GetKnativeEventingClient(cfg); err != nil { + klog.V(log.E).ErrorS(err, "Unable to get the knative eventing client") + return nil + } + } + if servingClient != nil || eventingClient != nil { + return newKnDiscoveryClient(servingClient, eventingClient) + } + } + return nil +} + +func newKnDiscoveryClient(servingClient clientservingv1.ServingV1Interface, eventingClient clienteventingv1.EventingV1Interface) *KnDiscoveryClient { return &KnDiscoveryClient{ ServingClient: servingClient, EventingClient: eventingClient, diff --git a/controllers/knative/knative.go b/controllers/knative/knative.go index ae8490cf5..929a96cc5 100644 --- a/controllers/knative/knative.go +++ b/controllers/knative/knative.go @@ -29,9 +29,9 @@ import ( var servingClient clientservingv1.ServingV1Interface var eventingClient clienteventingv1.EventingV1Interface -type Info struct { - IsKnativeEventing bool - IsKnativeServing bool +type Availability struct { + Eventing bool + Serving bool } func GetKnativeServingClient(cfg *rest.Config) (clientservingv1.ServingV1Interface, error) { @@ -64,7 +64,7 @@ func NewKnativeEventingClient(cfg *rest.Config) (*clienteventingv1.EventingV1Cli return clienteventingv1.NewForConfig(cfg) } -func GetKnativeInfo(cfg *rest.Config) (*Info, error) { +func GetKnativeAvailability(cfg *rest.Config) (*Availability, error) { if cli, err := discovery.NewDiscoveryClientForConfig(cfg); err != nil { return nil, err } else { @@ -72,13 +72,13 @@ func GetKnativeInfo(cfg *rest.Config) (*Info, error) { if err != nil { return nil, err } - result := new(Info) + result := new(Availability) for _, group := range apiList.Groups { if group.Name == "serving.knative.dev" { - result.IsKnativeServing = true + result.Serving = true } if group.Name == "eventing.knative.dev" { - result.IsKnativeEventing = true + result.Eventing = true } } return result, nil diff --git a/controllers/profiles/dev/profile_dev.go b/controllers/profiles/dev/profile_dev.go index 4a8d5dc7c..7f7e36d4b 100644 --- a/controllers/profiles/dev/profile_dev.go +++ b/controllers/profiles/dev/profile_dev.go @@ -21,6 +21,7 @@ package dev import ( "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/discovery" + "k8s.io/client-go/rest" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" @@ -41,10 +42,10 @@ func (d developmentProfile) GetProfile() metadata.ProfileType { return metadata.DevProfile } -func NewProfileReconciler(client client.Client, knDiscoveryClient *discovery.KnDiscoveryClient) profiles.ProfileReconciler { +func NewProfileReconciler(client client.Client, cfg *rest.Config) profiles.ProfileReconciler { support := &common.StateSupport{ C: client, - Catalog: discovery.NewServiceCatalog(client, knDiscoveryClient), + Catalog: discovery.NewServiceCatalogForConfig(client, cfg), } var ensurers *objectEnsurers diff --git a/controllers/profiles/dev/profile_dev_test.go b/controllers/profiles/dev/profile_dev_test.go index 2dda0cbb8..2c8e84b4c 100644 --- a/controllers/profiles/dev/profile_dev_test.go +++ b/controllers/profiles/dev/profile_dev_test.go @@ -24,6 +24,8 @@ import ( "sort" "testing" + "k8s.io/client-go/rest" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -54,7 +56,7 @@ func Test_OverrideStartupProbe(t *testing.T) { client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow).WithStatusSubresource(workflow).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) @@ -81,7 +83,7 @@ func Test_recoverFromFailureNoDeployment(t *testing.T) { workflow.Status.Manager().MarkFalse(api.RunningConditionType, api.DeploymentFailureReason, "") client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow).WithStatusSubresource(workflow).Build() - reconciler := NewProfileReconciler(client, nil) + reconciler := NewProfileReconciler(client, &rest.Config{}) // we are in failed state and have no objects result, err := reconciler.Reconcile(context.TODO(), workflow) @@ -122,7 +124,7 @@ func Test_newDevProfile(t *testing.T) { client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow).WithStatusSubresource(workflow).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) @@ -195,7 +197,7 @@ func Test_newDevProfile(t *testing.T) { func Test_devProfileImageDefaultsNoPlatform(t *testing.T) { workflow := test.GetBaseSonataFlowWithDevProfile(t.Name()) client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow).WithStatusSubresource(workflow).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) @@ -212,7 +214,7 @@ func Test_devProfileWithImageSnapshotOverrideWithPlatform(t *testing.T) { platform := test.GetBasePlatformWithDevBaseImageInReadyPhase(workflow.Namespace) client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow, platform).WithStatusSubresource(workflow, platform).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) @@ -229,7 +231,7 @@ func Test_devProfileWithWPlatformWithoutDevBaseImageAndWithBaseImage(t *testing. platform := test.GetBasePlatformWithBaseImageInReadyPhase(workflow.Namespace) client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow, platform).WithStatusSubresource(workflow, platform).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) @@ -246,7 +248,7 @@ func Test_devProfileWithPlatformWithoutDevBaseImageAndWithoutBaseImage(t *testin platform := test.GetBasePlatformInReadyPhase(workflow.Namespace) client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow, platform).WithStatusSubresource(workflow, platform).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) @@ -265,7 +267,7 @@ func Test_newDevProfileWithExternalConfigMaps(t *testing.T) { client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow).WithStatusSubresource(workflow).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) camelXmlRouteFileName := "camelroute-xml" xmlRoute := ` @@ -381,7 +383,7 @@ func Test_VolumeWithCapitalizedPaths(t *testing.T) { client := test.NewSonataFlowClientBuilder().WithRuntimeObjects(workflow, configMap).WithStatusSubresource(workflow, configMap).Build() - devReconciler := NewProfileReconciler(client, nil) + devReconciler := NewProfileReconciler(client, &rest.Config{}) result, err := devReconciler.Reconcile(context.TODO(), workflow) assert.NoError(t, err) diff --git a/controllers/profiles/factory/factory.go b/controllers/profiles/factory/factory.go index 071d3a10b..64f753586 100644 --- a/controllers/profiles/factory/factory.go +++ b/controllers/profiles/factory/factory.go @@ -20,7 +20,7 @@ package factory import ( - "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/discovery" + "k8s.io/client-go/rest" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/apache/incubator-kie-kogito-serverless-operator/api/metadata" @@ -36,7 +36,7 @@ const ( opsProfile metadata.ProfileType = "prod_for_ops" ) -type reconcilerBuilder func(client client.Client, knDiscoveryClient *discovery.KnDiscoveryClient) profiles.ProfileReconciler +type reconcilerBuilder func(client client.Client, cfg *rest.Config) profiles.ProfileReconciler var profileBuilders = map[metadata.ProfileType]reconcilerBuilder{ metadata.ProdProfile: prod.NewProfileReconciler, @@ -59,6 +59,6 @@ func profileBuilder(workflow *operatorapi.SonataFlow) reconcilerBuilder { } // NewReconciler creates a new ProfileReconciler based on the given workflow and context. -func NewReconciler(client client.Client, knDiscoveryClient *discovery.KnDiscoveryClient, workflow *operatorapi.SonataFlow) profiles.ProfileReconciler { - return profileBuilder(workflow)(client, knDiscoveryClient) +func NewReconciler(client client.Client, cfg *rest.Config, workflow *operatorapi.SonataFlow) profiles.ProfileReconciler { + return profileBuilder(workflow)(client, cfg) } diff --git a/controllers/profiles/prod/profile_prod.go b/controllers/profiles/prod/profile_prod.go index e0e95f8a6..e4c7cdc38 100644 --- a/controllers/profiles/prod/profile_prod.go +++ b/controllers/profiles/prod/profile_prod.go @@ -22,6 +22,8 @@ package prod import ( "time" + "k8s.io/client-go/rest" + "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/discovery" "sigs.k8s.io/controller-runtime/pkg/client" @@ -64,10 +66,10 @@ func newObjectEnsurers(support *common.StateSupport) *objectEnsurers { // NewProfileReconciler the default profile builder which includes a build state to run an internal build process // to have an immutable workflow image deployed -func NewProfileReconciler(client client.Client, knDiscoveryClient *discovery.KnDiscoveryClient) profiles.ProfileReconciler { +func NewProfileReconciler(client client.Client, cfg *rest.Config) profiles.ProfileReconciler { support := &common.StateSupport{ C: client, - Catalog: discovery.NewServiceCatalog(client, knDiscoveryClient), + Catalog: discovery.NewServiceCatalogForConfig(client, cfg), } // the reconciliation state machine stateMachine := common.NewReconciliationStateMachine( @@ -84,10 +86,10 @@ func NewProfileReconciler(client client.Client, knDiscoveryClient *discovery.KnD // NewProfileForOpsReconciler creates an alternative prod profile that won't require to build the workflow image in order to deploy // the workflow application. It assumes that the image has been built somewhere else. -func NewProfileForOpsReconciler(client client.Client, knDiscoveryClient *discovery.KnDiscoveryClient) profiles.ProfileReconciler { +func NewProfileForOpsReconciler(client client.Client, cfg *rest.Config) profiles.ProfileReconciler { support := &common.StateSupport{ C: client, - Catalog: discovery.NewServiceCatalog(client, knDiscoveryClient), + Catalog: discovery.NewServiceCatalogForConfig(client, cfg), } // the reconciliation state machine stateMachine := common.NewReconciliationStateMachine( diff --git a/controllers/profiles/prod/profile_prod_test.go b/controllers/profiles/prod/profile_prod_test.go index 180eabcad..080b04e3a 100644 --- a/controllers/profiles/prod/profile_prod_test.go +++ b/controllers/profiles/prod/profile_prod_test.go @@ -24,6 +24,8 @@ import ( "testing" "time" + "k8s.io/client-go/rest" + corev1 "k8s.io/api/core/v1" "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/common" @@ -47,7 +49,7 @@ func Test_Reconciler_ProdOps(t *testing.T) { client := test.NewSonataFlowClientBuilder(). WithRuntimeObjects(workflow). WithStatusSubresource(workflow, &operatorapi.SonataFlowBuild{}).Build() - result, err := NewProfileForOpsReconciler(client, nil).Reconcile(context.TODO(), workflow) + result, err := NewProfileForOpsReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) assert.NotNil(t, result.RequeueAfter) @@ -57,8 +59,8 @@ func Test_Reconciler_ProdOps(t *testing.T) { // Since we don't have it in a mocked env, the result must be ready == false assert.False(t, workflow.Status.IsReady()) - // Reconcile again to run the ddeployment handler - result, err = NewProfileForOpsReconciler(client, nil).Reconcile(context.TODO(), workflow) + // Reconcile again to run the deployment handler + result, err = NewProfileForOpsReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) // Let's check for the right creation of the workflow (one CM volume, one container with a custom image) @@ -86,7 +88,7 @@ func Test_Reconciler_ProdCustomPod(t *testing.T) { client := test.NewSonataFlowClientBuilder(). WithRuntimeObjects(workflow, build, platform). WithStatusSubresource(workflow, build, platform).Build() - _, err := NewProfileReconciler(client, nil).Reconcile(context.TODO(), workflow) + _, err := NewProfileReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) // Let's check for the right creation of the workflow (one CM volume, one container with a custom image) @@ -107,7 +109,7 @@ func Test_reconcilerProdBuildConditions(t *testing.T) { WithRuntimeObjects(workflow, platform). WithStatusSubresource(workflow, platform, &operatorapi.SonataFlowBuild{}).Build() - result, err := NewProfileReconciler(client, nil).Reconcile(context.TODO(), workflow) + result, err := NewProfileReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) assert.NotNil(t, result.RequeueAfter) @@ -115,7 +117,7 @@ func Test_reconcilerProdBuildConditions(t *testing.T) { assert.False(t, workflow.Status.IsReady()) // still building - result, err = NewProfileReconciler(client, nil).Reconcile(context.TODO(), workflow) + result, err = NewProfileReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) assert.Equal(t, requeueWhileWaitForBuild, result.RequeueAfter) assert.True(t, workflow.Status.IsBuildRunningOrUnknown()) @@ -128,7 +130,7 @@ func Test_reconcilerProdBuildConditions(t *testing.T) { assert.NoError(t, client.Status().Update(context.TODO(), build)) // last reconciliation cycle waiting for build - result, err = NewProfileReconciler(client, nil).Reconcile(context.TODO(), workflow) + result, err = NewProfileReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) assert.Equal(t, requeueWhileWaitForBuild, result.RequeueAfter) assert.False(t, workflow.Status.IsBuildRunningOrUnknown()) @@ -136,7 +138,7 @@ func Test_reconcilerProdBuildConditions(t *testing.T) { assert.Equal(t, api.WaitingForBuildReason, workflow.Status.GetTopLevelCondition().Reason) // now we create the objects - result, err = NewProfileReconciler(client, nil).Reconcile(context.TODO(), workflow) + result, err = NewProfileReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) assert.False(t, workflow.Status.IsBuildRunningOrUnknown()) assert.False(t, workflow.Status.IsReady()) @@ -154,7 +156,7 @@ func Test_reconcilerProdBuildConditions(t *testing.T) { err = client.Status().Update(context.TODO(), deployment) assert.NoError(t, err) - result, err = NewProfileReconciler(client, nil).Reconcile(context.TODO(), workflow) + result, err = NewProfileReconciler(client, &rest.Config{}).Reconcile(context.TODO(), workflow) assert.NoError(t, err) assert.False(t, workflow.Status.IsBuildRunningOrUnknown()) assert.True(t, workflow.Status.IsReady()) diff --git a/controllers/sonataflow_controller.go b/controllers/sonataflow_controller.go index ec512c7ff..3a85b037e 100644 --- a/controllers/sonataflow_controller.go +++ b/controllers/sonataflow_controller.go @@ -23,12 +23,6 @@ import ( "context" "fmt" - "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/knative" - - "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/discovery" - clienteventingv1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1" - clientservingv1 "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1" - "k8s.io/klog/v2" "github.com/apache/incubator-kie-kogito-serverless-operator/controllers/profiles/factory" @@ -101,7 +95,7 @@ func (r *SonataFlowReconciler) Reconcile(ctx context.Context, req ctrl.Request) return reconcile.Result{}, nil } - return factory.NewReconciler(r.Client, buildKnDiscoveryClient(r.Config), workflow).Reconcile(ctx, workflow) + return factory.NewReconciler(r.Client, r.Config, workflow).Reconcile(ctx, workflow) } func platformEnqueueRequestsFromMapFunc(c client.Client, p *operatorapi.SonataFlowPlatform) []reconcile.Request { @@ -197,32 +191,3 @@ func (r *SonataFlowReconciler) SetupWithManager(mgr ctrl.Manager) error { })). Complete(r) } - -// buildKnDiscoveryClient returns a KnDiscoveryClient discovery client depending on the cluster status, if knative -// serving nor knative eventing are installed, or it was not possible to create that client, returns null. -func buildKnDiscoveryClient(cfg *rest.Config) *discovery.KnDiscoveryClient { - var servingClient clientservingv1.ServingV1Interface - var eventingClient clienteventingv1.EventingV1Interface - - if info, err := knative.GetKnativeInfo(cfg); err != nil { - klog.V(log.E).ErrorS(err, "Unable to determine if knative is installed in the cluster") - return nil - } else { - if info.IsKnativeServing { - if servingClient, err = knative.GetKnativeServingClient(cfg); err != nil { - klog.V(log.E).ErrorS(err, "Unable to get the knative serving client") - return nil - } - } - if info.IsKnativeEventing { - if eventingClient, err = knative.GetKnativeEventingClient(cfg); err != nil { - klog.V(log.E).ErrorS(err, "Unable to get the knative eventing client") - return nil - } - } - if servingClient != nil || eventingClient != nil { - return discovery.NewKnDiscoveryClient(servingClient, eventingClient) - } - } - return nil -} diff --git a/utils/knative/common.go b/utils/knative/common.go deleted file mode 100644 index b0ae71592..000000000 --- a/utils/knative/common.go +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 knative - -import ( - "k8s.io/client-go/rest" - clienteventingv1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1" - clientservingv1 "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1" -) - -func NewKnativeServingClient(cfg *rest.Config) (*clientservingv1.ServingV1Client, error) { - return clientservingv1.NewForConfig(cfg) -} - -func NewKnativeEventingClient(cfg *rest.Config) (*clienteventingv1.EventingV1Client, error) { - return clienteventingv1.NewForConfig(cfg) -}