Skip to content

Commit

Permalink
kie-kogito-serverless-operator-335: Operator driven service discovery…
Browse files Browse the repository at this point in the history
… API Phase4 (apache#338)

* kie-kogito-serverless-operator-335: Operator driven service discovery API Phase4
    - Add the discovery of Openshift DeploymentConfigs and Routes to the service discovery API

* kie-kogito-serverless-operator-335: Operator driven service discovery API Phase4
    - Code review suggestions 1

* kie-kogito-serverless-operator-335: Operator driven service discovery API Phase4
    - Code review suggestions 2

* kie-kogito-serverless-operator-335: Operator driven service discovery API Phase4
    - Augment the service uri query parameters
  • Loading branch information
wmedvede authored and rgdoliveira committed Jan 29, 2024
1 parent 711679d commit a396205
Show file tree
Hide file tree
Showing 14 changed files with 482 additions and 23 deletions.
16 changes: 16 additions & 0 deletions bundle/manifests/sonataflow-operator.clusterserviceversion.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,22 @@ spec:
- get
- list
- watch
- apiGroups:
- apps.openshift.io
resources:
- deploymentconfigs
verbs:
- get
- list
- watch
- apiGroups:
- route.openshift.io
resources:
- routes
verbs:
- get
- list
- watch
serviceAccountName: sonataflow-operator-controller-manager
deployments:
- label:
Expand Down
16 changes: 16 additions & 0 deletions config/rbac/service_discovery_role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,19 @@ rules:
- get
- list
- watch
- apiGroups:
- apps.openshift.io
resources:
- deploymentconfigs
verbs:
- get
- list
- watch
- apiGroups:
- route.openshift.io
resources:
- routes
verbs:
- get
- list
- watch
4 changes: 3 additions & 1 deletion controllers/builder/openshiftbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"strings"

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

buildv1 "github.com/openshift/api/build/v1"
imgv1 "github.com/openshift/api/image/v1"
buildclientv1 "github.com/openshift/client-go/build/clientset/versioned/typed/build/v1"
Expand Down Expand Up @@ -78,7 +80,7 @@ type openshiftBuilderManager struct {
}

func newOpenShiftBuilderManager(managerContext buildManagerContext, cliConfig *rest.Config) (BuildManager, error) {
buildClient, err := buildclientv1.NewForConfig(cliConfig)
buildClient, err := openshift.NewOpenShiftBuildClient(cliConfig)
if err != nil {
return nil, err
}
Expand Down
6 changes: 4 additions & 2 deletions controllers/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ type sonataFlowServiceCatalog struct {
}

// NewServiceCatalog returns a new ServiceCatalog configured to resolve kubernetes, knative, and openshift resource addresses.
func NewServiceCatalog(cli client.Client, knDiscoveryClient *KnDiscoveryClient) ServiceCatalog {
func NewServiceCatalog(cli client.Client, knDiscoveryClient *KnDiscoveryClient, openShiftDiscoveryClient *OpenShiftDiscoveryClient) ServiceCatalog {
return &sonataFlowServiceCatalog{
kubernetesCatalog: newK8SServiceCatalog(cli),
knativeCatalog: newKnServiceCatalog(knDiscoveryClient),
openshiftCatalog: newOpenShiftServiceCatalog(openShiftDiscoveryClient),
}
}

func NewServiceCatalogForConfig(cli client.Client, cfg *rest.Config) ServiceCatalog {
return &sonataFlowServiceCatalog{
kubernetesCatalog: newK8SServiceCatalog(cli),
knativeCatalog: newKnServiceCatalogForConfig(cfg),
openshiftCatalog: newOpenShiftServiceCatalogForClientAndConfig(cli, cfg),
}
}

Expand All @@ -108,7 +110,7 @@ func (c *sonataFlowServiceCatalog) Query(ctx context.Context, uri ResourceUri, o
case KnativeScheme:
return c.knativeCatalog.Query(ctx, uri, outputFormat)
case OpenshiftScheme:
return "", fmt.Errorf("openshift service discovery is not yet implemented")
return c.openshiftCatalog.Query(ctx, uri, outputFormat)
default:
return "", fmt.Errorf("unknown scheme was provided for service discovery: %s", uri.Scheme)
}
Expand Down
8 changes: 4 additions & 4 deletions controllers/discovery/discovery_knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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), nil)
doTestQueryWithError(t, ctg, *NewResourceUriBuilder(KnativeScheme).
Kind("services").
Group("serving.knative.dev").
Expand Down Expand Up @@ -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), nil)
doTestQuery(t, ctg, *NewResourceUriBuilder(KnativeScheme).
Kind("services").
Group("serving.knative.dev").
Expand All @@ -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()), nil)
doTestQueryWithError(t, ctg, *NewResourceUriBuilder(KnativeScheme).
Kind("brokers").
Group("eventing.knative.dev").
Expand Down Expand Up @@ -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()), nil)
doTestQuery(t, ctg, *NewResourceUriBuilder(KnativeScheme).
Kind("brokers").
Group("eventing.knative.dev").
Expand Down
123 changes: 123 additions & 0 deletions controllers/discovery/discovery_openshift_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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 discovery

import (
appsv1 "github.com/openshift/api/apps/v1"
routev1 "github.com/openshift/api/route/v1"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

fakeappsclient "github.com/openshift/client-go/apps/clientset/versioned/fake"
fakerouteclient "github.com/openshift/client-go/route/clientset/versioned/fake"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"testing"
)

func Test_QueryOpenShiftRoute(t *testing.T) {
doTestQueryOpenShiftRoute(t, false, "http://openshiftroutehost1:80")
}

func Test_QueryOpenShiftRouteWithTLS(t *testing.T) {
doTestQueryOpenShiftRoute(t, true, "https://openshiftroutehost1:443")
}

func doTestQueryOpenShiftRoute(t *testing.T, tls bool, expectedUri string) {
route := &routev1.Route{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace1,
Name: openShiftRouteName1,
},
Spec: routev1.RouteSpec{
Host: openShiftRouteHost1,
},
Status: routev1.RouteStatus{},
}
if tls {
route.Spec.TLS = &routev1.TLSConfig{}
}
fakeRoutesClient := fakerouteclient.NewSimpleClientset(route)
ctg := NewServiceCatalog(nil, nil, newOpenShiftDiscoveryClient(nil, fakeRoutesClient.RouteV1(), nil))
doTestQuery(t, ctg, *NewResourceUriBuilder(OpenshiftScheme).
Kind("routes").
Group("route.openshift.io").
Version("v1").
Namespace(namespace1).
Name(openShiftRouteName1).Build(), "", expectedUri)
}

func Test_QueryOpenShiftDeploymentConfigWithServiceDNSMode(t *testing.T) {
doTestQueryOpenShiftDeploymentConfig(t, KubernetesDNSAddress, true, "http://service1Name.namespace1.svc:80", "")
}

func Test_QueryOpenShiftDeploymentConfigWithServiceIPAddressMode(t *testing.T) {
doTestQueryOpenShiftDeploymentConfig(t, KubernetesIPAddress, true, "http://10.1.15.16:80", "")
}

func Test_QueryOpenShiftDeploymentConfigWithoutServiceDNSMode(t *testing.T) {
doTestQueryOpenShiftDeploymentConfig(t, KubernetesDNSAddress, false, "", "no service was found for the deploymentConfig: openShiftDeploymentConfigName1")
}

func Test_QueryOpenShiftDeploymentConfigWithoutServiceIPAddressMode(t *testing.T) {
doTestQueryOpenShiftDeploymentConfig(t, KubernetesIPAddress, false, "", "no service was found for the deploymentConfig: openShiftDeploymentConfigName1")
}

func doTestQueryOpenShiftDeploymentConfig(t *testing.T, outputFormat string, withService bool, expectedUri string, expectedError string) {
selector := map[string]string{
label1: valueLabel1,
label2: valueLabel2,
}
deploymentConfig := &appsv1.DeploymentConfig{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace1,
Name: openShiftDeploymentConfigName1,
},
Spec: appsv1.DeploymentConfigSpec{
Selector: selector,
},
}
fakeClientBuilder := fake.NewClientBuilder()
if withService {
service := mockServiceWithPorts(namespace1, service1Name, mockServicePort(httpProtocol, tcp, defaultHttpPort))
service.Spec.Selector = selector
service.Spec.ClusterIP = "10.1.15.16"
service.Spec.Type = corev1.ServiceTypeNodePort
fakeClientBuilder.WithRuntimeObjects(service)
}
cli := fakeClientBuilder.Build()
fakeAppsClient := fakeappsclient.NewSimpleClientset(deploymentConfig)
ctg := NewServiceCatalog(nil, nil, newOpenShiftDiscoveryClient(cli, nil, fakeAppsClient.AppsV1()))

resourceUri := *NewResourceUriBuilder(OpenshiftScheme).
Kind("deploymentconfigs").
Group("apps.openshift.io").
Version("v1").
Namespace(namespace1).
Name(openShiftDeploymentConfigName1).Build()

if withService {
doTestQuery(t, ctg, resourceUri, outputFormat, expectedUri)
} else {
doTestQueryWithError(t, ctg, resourceUri, outputFormat, expectedError)
}
}
14 changes: 7 additions & 7 deletions controllers/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, nil, nil)
doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme).
Kind("services").
Version("v1").
Expand All @@ -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, nil, nil)
doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme).
Kind("pods").
Version("v1").
Expand Down Expand Up @@ -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, nil, nil)

doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme).
Group("apps").
Expand All @@ -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(), nil, nil)

uri := *NewResourceUriBuilder(KubernetesScheme).
Group("apps").
Expand Down Expand Up @@ -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, nil, nil)

doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme).
Group("apps").
Expand All @@ -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(), nil, nil)

uri := *NewResourceUriBuilder(KubernetesScheme).
Group("apps").
Expand Down Expand Up @@ -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, nil, nil)
doTestQuery(t, ctg, *NewResourceUriBuilder(KubernetesScheme).
Kind("ingresses").
Group("networking.k8s.io").
Expand Down
Loading

0 comments on commit a396205

Please sign in to comment.