-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KOGITO-8648] [KSW-Operator] Implement the Knative Addressable interf… (
#124) * [KOGITO-8648] [KSW-Operator] Implement the Knative Addressable interface in dev profile Signed-off-by: Davide Salerno <dsalerno@redhat.com> Update controllers/profiles/status_enricher_dev_test.go Co-authored-by: Ricardo Zanini <1538000+ricardozanini@users.noreply.github.com> Fixing namespace into unit tests Signed-off-by: Davide Salerno <dsalerno@redhat.com> Update utils/kubernetes/service.go Co-authored-by: Filippe Spolti <filippespolti@gmail.com> Changes coming from code review Signed-off-by: Davide Salerno <dsalerno@redhat.com> * fix generate-all --------- Signed-off-by: Davide Salerno <dsalerno@redhat.com> Co-authored-by: radtriste <tradisso@redhat.com>
- Loading branch information
1 parent
0190a5f
commit 46e17e7
Showing
9 changed files
with
260 additions
and
2 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
config/samples/sw.kogito_v1alpha08_kogitoserverlessplatform_minikube.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: sw.kogito.kie.org/v1alpha08 | ||
kind: KogitoServerlessPlatform | ||
metadata: | ||
name: kogito-workflow-platform | ||
spec: | ||
platform: | ||
buildStrategy: operator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 profiles | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
openshiftv1 "github.com/openshift/api/route/v1" | ||
"github.com/stretchr/testify/assert" | ||
"knative.dev/pkg/apis" | ||
|
||
apiv08 "github.com/kiegroup/kogito-serverless-operator/api/v1alpha08" | ||
"github.com/kiegroup/kogito-serverless-operator/test" | ||
) | ||
|
||
func Test_enrichmentStatusOnK8s(t *testing.T) { | ||
t.Run("verify that the service URL is returned with the default cluster name on default namespace", func(t *testing.T) { | ||
|
||
workflow := test.GetKogitoServerlessWorkflow("../../config/samples/"+test.KogitoServerlessWorkflowSampleDevModeYamlCR, t.Name()) | ||
workflow.Namespace = toK8SNamespace(t.Name()) | ||
service, err := defaultServiceCreator(workflow) | ||
client := test.NewKogitoClientBuilder().WithRuntimeObjects(workflow, service).Build() | ||
obj, err := defaultDevStatusEnricher(context.TODO(), client, workflow) | ||
|
||
reflectWorkflow := obj.(*apiv08.KogitoServerlessWorkflow) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, obj) | ||
assert.NotNil(t, reflectWorkflow.Status.Address) | ||
assert.Equal(t, reflectWorkflow.Status.Address.URL.String(), "http://"+workflow.Name+"."+workflow.Namespace+".svc.cluster.local/"+workflow.Name) | ||
|
||
}) | ||
|
||
t.Run("verify that the service URL won't be generated if an invalid namespace is used", func(t *testing.T) { | ||
|
||
workflow := test.GetKogitoServerlessWorkflow("../../config/samples/"+test.KogitoServerlessWorkflowSampleDevModeYamlCR, t.Name()) | ||
workflow.Namespace = t.Name() | ||
service, err := defaultServiceCreator(workflow) | ||
client := test.NewKogitoClientBuilder().WithRuntimeObjects(workflow, service).Build() | ||
_, err = defaultDevStatusEnricher(context.TODO(), client, workflow) | ||
assert.Error(t, err) | ||
|
||
}) | ||
} | ||
|
||
func Test_enrichmentStatusOnOCP(t *testing.T) { | ||
t.Run("verify that the service URL is returned with the default cluster name on default namespace", func(t *testing.T) { | ||
workflow := test.GetKogitoServerlessWorkflow("../../config/samples/"+test.KogitoServerlessWorkflowSampleDevModeYamlCR, t.Name()) | ||
workflow.Namespace = toK8SNamespace(t.Name()) | ||
service, err := defaultServiceCreator(workflow) | ||
route := &openshiftv1.Route{} | ||
route.Name = workflow.Name | ||
route.Namespace = workflow.Namespace | ||
route.Spec.Host = workflow.Name + "." + workflow.Namespace + ".apps-crc.testing" | ||
client := test.NewKogitoClientBuilderWithOpenShift().WithRuntimeObjects(workflow, service, route).Build() | ||
obj, err := devStatusEnricherForOpenShift(context.TODO(), client, workflow) | ||
|
||
reflectWorkflow := obj.(*apiv08.KogitoServerlessWorkflow) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, obj) | ||
assert.NotNil(t, reflectWorkflow.Status.Address) | ||
expectedURL := apis.HTTP(route.Spec.Host) | ||
expectedURL.Path = workflow.Name | ||
assert.Equal(t, reflectWorkflow.Status.Address.URL.String(), expectedURL.String()) | ||
|
||
}) | ||
} | ||
|
||
func toK8SNamespace(testName string) string { | ||
return strings.ToLower(strings.Replace(strings.Split(testName, "/")[0], "_", "-", 1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 kubernetes | ||
|
||
import ( | ||
"net/url" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
// TODO: retrieve the cluster domain from the /etc/resolve inside the pod or from the Platform CRD - will be addressed by KOGITO-9198 | ||
var defaultClusterDomain = "svc.cluster.local" | ||
|
||
// retrieveServiceHost function that based on the service name, namespace and eventually the nodeport, will provide the service URI | ||
func retrieveServiceHost(service *v1.Service) string { | ||
namespace := service.Namespace | ||
if len(namespace) == 0 { | ||
namespace = "default" | ||
} | ||
// TODO: Retrieve the cluster domain or use the default one | ||
return service.Name + "." + namespace + "." + defaultClusterDomain | ||
} | ||
|
||
// RetrieveServiceURL function that based on the service name, namespace and eventually the nodeport, will provide the service URI | ||
func RetrieveServiceURL(service *v1.Service) (*apis.URL, error) { | ||
url := url.URL{ | ||
Scheme: "http", | ||
Host: retrieveServiceHost(service), | ||
Path: service.Name} | ||
return apis.ParseURL(url.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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 kubernetes | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
v1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func Test_retrievingKubernetesServiceHost(t *testing.T) { | ||
t.Run("verify that the service host is returned with the default cluster name on default namespace", func(t *testing.T) { | ||
svc := &v1.Service{} | ||
svc.Name = "workflow" | ||
host := retrieveServiceHost(svc) | ||
|
||
assert.NotNil(t, host) | ||
assert.Equal(t, host, svc.Name+".default.svc.cluster.local") | ||
|
||
}) | ||
|
||
t.Run("verify that the service host is returned with the default cluster name on non-default namespace", func(t *testing.T) { | ||
svc := &v1.Service{} | ||
svc.Name = "workflow" | ||
svc.Namespace = "ns" | ||
host := retrieveServiceHost(svc) | ||
|
||
assert.NotNil(t, host) | ||
assert.Equal(t, host, svc.Name+"."+svc.Namespace+".svc.cluster.local") | ||
|
||
}) | ||
} | ||
|
||
func Test_retrievingKubernetesServiceURL(t *testing.T) { | ||
t.Run("verify that the service URL is returned with the default cluster name on default namespace", func(t *testing.T) { | ||
svc := &v1.Service{} | ||
svc.Name = "workflow" | ||
RetrieveServiceURL(svc) | ||
|
||
url, err := RetrieveServiceURL(svc) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, url) | ||
assert.Equal(t, url.String(), "http://"+svc.Name+".default.svc.cluster.local/"+svc.Name) | ||
|
||
}) | ||
|
||
t.Run("verify that the service URL is returned with the default cluster name on non-default namespace", func(t *testing.T) { | ||
svc := &v1.Service{} | ||
svc.Name = "workflow" | ||
svc.Namespace = "ns" | ||
RetrieveServiceURL(svc) | ||
|
||
url, err := RetrieveServiceURL(svc) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, url) | ||
assert.Equal(t, url.String(), "http://"+svc.Name+"."+svc.Namespace+".svc.cluster.local/"+svc.Name) | ||
|
||
}) | ||
|
||
} |