From a9f2ff1e201bfb62c9e90d8b18d63e0df132eb0c Mon Sep 17 00:00:00 2001 From: Angel Misevski Date: Thu, 27 Jan 2022 16:56:30 -0500 Subject: [PATCH] Adapt Che routing solver to DevWorkspace Operator changes Adapt code in Che RoutingSolver to accomodate changes in DWO that disconnect DevWorkspaceRouting resources from the upstream Devfile API Signed-off-by: Angel Misevski --- .../devworkspace/solver/che_routing.go | 20 ++++++------- .../devworkspace/solver/che_routing_test.go | 29 +++++++++---------- 2 files changed, 22 insertions(+), 27 deletions(-) diff --git a/controllers/devworkspace/solver/che_routing.go b/controllers/devworkspace/solver/che_routing.go index 89d0dee6e6..9f65df5d10 100644 --- a/controllers/devworkspace/solver/che_routing.go +++ b/controllers/devworkspace/solver/che_routing.go @@ -19,8 +19,6 @@ import ( "strconv" "strings" - "k8s.io/utils/pointer" - "github.com/eclipse-che/che-operator/pkg/util" "github.com/eclipse-che/che-operator/pkg/deploy/gateway" @@ -216,7 +214,7 @@ func (c *CheRoutingSolver) cheExposedEndpoints(cheCluster *v2alpha1.CheCluster, for component, endpoints := range componentEndpoints { for _, endpoint := range endpoints { - if endpoint.Exposure != dw.PublicEndpointExposure { + if dw.EndpointExposure(endpoint.Exposure) != dw.PublicEndpointExposure { continue } @@ -361,7 +359,7 @@ func exposeAllEndpoints(cheCluster *v2alpha1.CheCluster, routing *dwo.DevWorkspa order := 1 for componentName, endpoints := range routing.Spec.Endpoints { for _, e := range endpoints { - if e.Exposure != dw.PublicEndpointExposure { + if dw.EndpointExposure(e.Exposure) != dw.PublicEndpointExposure { continue } @@ -498,7 +496,7 @@ func routeForHealthzEndpoint(cfg *gateway.TraefikConfig, dwId string, endpoints } } -func addEndpointToTraefikConfig(componentName string, e dw.Endpoint, cfg *gateway.TraefikConfig, cheCluster *v2alpha1.CheCluster, routing *dwo.DevWorkspaceRouting) { +func addEndpointToTraefikConfig(componentName string, e dwo.Endpoint, cfg *gateway.TraefikConfig, cheCluster *v2alpha1.CheCluster, routing *dwo.DevWorkspaceRouting) { routeName, prefix := createEndpointPath(&e, componentName) rulePrefix := fmt.Sprintf("PathPrefix(`%s`)", prefix) @@ -531,7 +529,7 @@ func addEndpointToTraefikConfig(componentName string, e dw.Endpoint, cfg *gatewa } } -func createEndpointPath(e *dw.Endpoint, componentName string) (routeName string, path string) { +func createEndpointPath(e *dwo.Endpoint, componentName string) (routeName string, path string) { if e.Attributes.GetString(uniqueEndpointAttributeName, nil) == "true" { // if endpoint is unique, we're exposing on /componentName/ routeName = e.Name @@ -557,7 +555,7 @@ func findServiceForPort(port int32, objs *solvers.RoutingObjects) *corev1.Servic return nil } -func findIngressForEndpoint(componentName string, endpoint dw.Endpoint, objs *solvers.RoutingObjects) *networkingv1.Ingress { +func findIngressForEndpoint(componentName string, endpoint dwo.Endpoint, objs *solvers.RoutingObjects) *networkingv1.Ingress { for i := range objs.Ingresses { ingress := &objs.Ingresses[i] @@ -580,7 +578,7 @@ func findIngressForEndpoint(componentName string, endpoint dw.Endpoint, objs *so return nil } -func findRouteForEndpoint(componentName string, endpoint dw.Endpoint, objs *solvers.RoutingObjects, dwId string) *routeV1.Route { +func findRouteForEndpoint(componentName string, endpoint dwo.Endpoint, objs *solvers.RoutingObjects, dwId string) *routeV1.Route { service := findServiceForPort(int32(endpoint.TargetPort), objs) if service == nil { service = getCommonService(objs, dwId) @@ -650,7 +648,7 @@ func getServiceURL(port int32, workspaceID string, workspaceNamespace string) st return fmt.Sprintf("http://%s.%s.svc:%d", common.ServiceName(workspaceID), workspaceNamespace, port) } -func getPublicURLPrefixForEndpoint(workspaceID string, machineName string, endpoint dw.Endpoint) string { +func getPublicURLPrefixForEndpoint(workspaceID string, machineName string, endpoint dwo.Endpoint) string { endpointName := "" if endpoint.Attributes.GetString(uniqueEndpointAttributeName, nil) == "true" { endpointName = endpoint.Name @@ -666,7 +664,7 @@ func getPublicURLPrefix(workspaceID string, machineName string, port int32, uniq return fmt.Sprintf(uniqueEndpointURLPrefixPattern, workspaceID, machineName, uniqueEndpointName) } -func determineEndpointScheme(e dw.Endpoint) string { +func determineEndpointScheme(e dwo.Endpoint) string { var scheme string if e.Protocol == "" { scheme = "http" @@ -674,7 +672,7 @@ func determineEndpointScheme(e dw.Endpoint) string { scheme = string(e.Protocol) } - upgradeToSecure := pointer.BoolPtrDerefOr(e.Secure, false) + upgradeToSecure := e.Secure // gateway is always on HTTPS, so if the endpoint is served through the gateway, we need to use the TLS'd variant. if e.Attributes.GetString(urlRewriteSupportedEndpointAttributeName, nil) == "true" { diff --git a/controllers/devworkspace/solver/che_routing_test.go b/controllers/devworkspace/solver/che_routing_test.go index 551311c93b..3dbbc5fcdf 100644 --- a/controllers/devworkspace/solver/che_routing_test.go +++ b/controllers/devworkspace/solver/che_routing_test.go @@ -18,8 +18,6 @@ import ( "strings" "testing" - "k8s.io/utils/pointer" - "github.com/eclipse-che/che-operator/pkg/util" "github.com/stretchr/testify/assert" @@ -27,7 +25,6 @@ import ( "github.com/eclipse-che/che-operator/pkg/deploy/gateway" dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" - "github.com/devfile/api/v2/pkg/attributes" dwo "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" "github.com/devfile/devworkspace-operator/controllers/controller/devworkspacerouting/solvers" "github.com/devfile/devworkspace-operator/pkg/constants" @@ -137,22 +134,22 @@ func subdomainDevWorkspaceRouting() *dwo.DevWorkspaceRouting { { Name: "e1", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, + Exposure: dwo.PublicEndpointExposure, Protocol: "https", Path: "/1/", }, { Name: "e2", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, + Exposure: dwo.PublicEndpointExposure, Protocol: "http", Path: "/2.js", - Secure: pointer.BoolPtr(true), + Secure: true, }, { Name: "e3", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, + Exposure: dwo.PublicEndpointExposure, }, }, }, @@ -174,10 +171,10 @@ func relocatableDevWorkspaceRouting() *dwo.DevWorkspaceRouting { { Name: "e1", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, + Exposure: dwo.PublicEndpointExposure, Protocol: "https", Path: "/1/", - Attributes: attributes.Attributes{ + Attributes: dwo.Attributes{ urlRewriteSupportedEndpointAttributeName: apiext.JSON{Raw: []byte("\"true\"")}, string(dwo.TypeEndpointAttribute): apiext.JSON{Raw: []byte("\"main\"")}, }, @@ -185,19 +182,19 @@ func relocatableDevWorkspaceRouting() *dwo.DevWorkspaceRouting { { Name: "e2", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, + Exposure: dwo.PublicEndpointExposure, Protocol: "http", Path: "/2.js", - Secure: pointer.BoolPtr(true), - Attributes: attributes.Attributes{ + Secure: true, + Attributes: dwo.Attributes{ urlRewriteSupportedEndpointAttributeName: apiext.JSON{Raw: []byte("\"true\"")}, }, }, { Name: "e3", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, - Attributes: attributes.Attributes{ + Exposure: dwo.PublicEndpointExposure, + Attributes: dwo.Attributes{ urlRewriteSupportedEndpointAttributeName: apiext.JSON{Raw: []byte("\"true\"")}, }, }, @@ -461,10 +458,10 @@ func TestUniqueMainEndpoint(t *testing.T) { { Name: "e1", TargetPort: 9999, - Exposure: dw.PublicEndpointExposure, + Exposure: dwo.PublicEndpointExposure, Protocol: "https", Path: "/1/", - Attributes: attributes.Attributes{ + Attributes: dwo.Attributes{ urlRewriteSupportedEndpointAttributeName: apiext.JSON{Raw: []byte("\"true\"")}, string(dwo.TypeEndpointAttribute): apiext.JSON{Raw: []byte("\"main\"")}, uniqueEndpointAttributeName: apiext.JSON{Raw: []byte("\"true\"")},