From b1298ef724a38461ff431c77c335175eceebb67f Mon Sep 17 00:00:00 2001 From: Sergii Leshchenko Date: Tue, 20 Apr 2021 16:33:29 +0300 Subject: [PATCH] Make solvers impl in charge of routing suffix --- .../devworkspacerouting/solvers/basic_solver.go | 12 ++++++++++-- .../devworkspacerouting/solvers/common.go | 16 ++++++++-------- pkg/common/naming.go | 8 ++++---- pkg/config/property.go | 8 ++++---- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/controllers/controller/devworkspacerouting/solvers/basic_solver.go b/controllers/controller/devworkspacerouting/solvers/basic_solver.go index 2fb43f27f..fb7284427 100644 --- a/controllers/controller/devworkspacerouting/solvers/basic_solver.go +++ b/controllers/controller/devworkspacerouting/solvers/basic_solver.go @@ -13,7 +13,10 @@ package solvers import ( + "errors" + controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1" + "github.com/devfile/devworkspace-operator/pkg/config" "github.com/devfile/devworkspace-operator/pkg/constants" "github.com/devfile/devworkspace-operator/pkg/infrastructure" ) @@ -53,14 +56,19 @@ func (s *BasicSolver) Finalize(*controllerv1alpha1.DevWorkspaceRouting) error { func (s *BasicSolver) GetSpecObjects(routing *controllerv1alpha1.DevWorkspaceRouting, workspaceMeta DevWorkspaceMetadata) (RoutingObjects, error) { routingObjects := RoutingObjects{} + routingSuffix := config.ControllerCfg.GetProperty(config.RoutingSuffixCfg) + if routingSuffix == nil { + return routingObjects, errors.New(config.RoutingSuffixCfg + " must be set for basic routing") + } + spec := routing.Spec services := getServicesForEndpoints(spec.Endpoints, workspaceMeta) services = append(services, GetDiscoverableServicesForEndpoints(spec.Endpoints, workspaceMeta)...) routingObjects.Services = services if infrastructure.IsOpenShift() { - routingObjects.Routes = getRoutesForSpec(spec.Endpoints, workspaceMeta) + routingObjects.Routes = getRoutesForSpec(*routingSuffix, spec.Endpoints, workspaceMeta) } else { - routingObjects.Ingresses = getIngressesForSpec(spec.Endpoints, workspaceMeta) + routingObjects.Ingresses = getIngressesForSpec(*routingSuffix, spec.Endpoints, workspaceMeta) } return routingObjects, nil diff --git a/controllers/controller/devworkspacerouting/solvers/common.go b/controllers/controller/devworkspacerouting/solvers/common.go index 90051aec3..22c8dac77 100644 --- a/controllers/controller/devworkspacerouting/solvers/common.go +++ b/controllers/controller/devworkspacerouting/solvers/common.go @@ -152,33 +152,33 @@ func getServicesForEndpoints(endpoints map[string]controllerv1alpha1.EndpointLis } } -func getRoutesForSpec(endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []routeV1.Route { +func getRoutesForSpec(routingSuffix string, endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []routeV1.Route { var routes []routeV1.Route for _, machineEndpoints := range endpoints { for _, endpoint := range machineEndpoints { if endpoint.Exposure != dw.PublicEndpointExposure { continue } - routes = append(routes, getRouteForEndpoint(endpoint, meta)) + routes = append(routes, getRouteForEndpoint(routingSuffix, endpoint, meta)) } } return routes } -func getIngressesForSpec(endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []v1beta1.Ingress { +func getIngressesForSpec(routingSuffix string, endpoints map[string]controllerv1alpha1.EndpointList, meta DevWorkspaceMetadata) []v1beta1.Ingress { var ingresses []v1beta1.Ingress for _, machineEndpoints := range endpoints { for _, endpoint := range machineEndpoints { if endpoint.Exposure != dw.PublicEndpointExposure { continue } - ingresses = append(ingresses, getIngressForEndpoint(endpoint, meta)) + ingresses = append(ingresses, getIngressForEndpoint(routingSuffix, endpoint, meta)) } } return ingresses } -func getRouteForEndpoint(endpoint dw.Endpoint, meta DevWorkspaceMetadata) routeV1.Route { +func getRouteForEndpoint(routingSuffix string, endpoint dw.Endpoint, meta DevWorkspaceMetadata) routeV1.Route { targetEndpoint := intstr.FromInt(endpoint.TargetPort) endpointName := common.EndpointName(endpoint.Name) return routeV1.Route{ @@ -191,7 +191,7 @@ func getRouteForEndpoint(endpoint dw.Endpoint, meta DevWorkspaceMetadata) routeV Annotations: routeAnnotations(endpointName), }, Spec: routeV1.RouteSpec{ - Host: common.WorkspaceHostname(meta.DevWorkspaceId), + Host: common.WorkspaceHostname(routingSuffix, meta.DevWorkspaceId), Path: common.EndpointPath(endpointName), TLS: &routeV1.TLSConfig{ InsecureEdgeTerminationPolicy: routeV1.InsecureEdgeTerminationPolicyRedirect, @@ -208,10 +208,10 @@ func getRouteForEndpoint(endpoint dw.Endpoint, meta DevWorkspaceMetadata) routeV } } -func getIngressForEndpoint(endpoint dw.Endpoint, meta DevWorkspaceMetadata) v1beta1.Ingress { +func getIngressForEndpoint(routingSuffix string, endpoint dw.Endpoint, meta DevWorkspaceMetadata) v1beta1.Ingress { targetEndpoint := intstr.FromInt(endpoint.TargetPort) endpointName := common.EndpointName(endpoint.Name) - hostname := common.EndpointHostname(meta.DevWorkspaceId, endpointName, endpoint.TargetPort) + hostname := common.EndpointHostname(routingSuffix, meta.DevWorkspaceId, endpointName, endpoint.TargetPort) ingressPathType := v1beta1.PathTypeImplementationSpecific return v1beta1.Ingress{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/common/naming.go b/pkg/common/naming.go index 6e70ae4c5..877df56f6 100644 --- a/pkg/common/naming.go +++ b/pkg/common/naming.go @@ -35,22 +35,22 @@ func ServiceAccountName(workspaceId string) string { return fmt.Sprintf("%s-%s", workspaceId, "sa") } -func EndpointHostname(workspaceId, endpointName string, endpointPort int) string { +func EndpointHostname(routingSuffix, workspaceId, endpointName string, endpointPort int) string { hostname := fmt.Sprintf("%s-%s-%d", workspaceId, endpointName, endpointPort) if len(hostname) > 63 { hostname = strings.TrimSuffix(hostname[:63], "-") } - return fmt.Sprintf("%s", hostname) + return fmt.Sprintf("%s.%s", hostname, routingSuffix) } // WorkspaceHostname evaluates a single hostname for a workspace, and should be used for routing // when endpoints are distinguished by path rules -func WorkspaceHostname(workspaceId string) string { +func WorkspaceHostname(routingSuffix, workspaceId string) string { hostname := workspaceId if len(hostname) > 63 { hostname = strings.TrimSuffix(hostname[:63], "-") } - return fmt.Sprintf("%s", hostname) + return fmt.Sprintf("%s.%s", hostname, routingSuffix) } func EndpointPath(endpointName string) string { diff --git a/pkg/config/property.go b/pkg/config/property.go index 95c7894bc..e6aee54dc 100644 --- a/pkg/config/property.go +++ b/pkg/config/property.go @@ -27,10 +27,10 @@ const ( routingClass = "devworkspace.default_routing_class" defaultRoutingClass = "basic" - // routingSuffix is the default domain for routes/ingresses created on the cluster. All - // routes/ingresses will be created with URL http(s)://. - routingSuffix = "devworkspace.routing.cluster_host_suffix" - defaultRoutingSuffix = "" + // RoutingSuffixCfg is the base domain for routes/ingresses created on the cluster. All + // routes/ingresses will be created with URL http(s)://. + // is supposed to be used by embedded routing solvers only + RoutingSuffixCfg = "devworkspace.routing.cluster_host_suffix" experimentalFeaturesEnabled = "devworkspace.experimental_features_enabled" defaultExperimentalFeaturesEnabled = "false"