Skip to content

Commit

Permalink
Make solvers impl in charge of routing suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
sleshchenko committed Apr 20, 2021
1 parent 158bb27 commit ecc7e20
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
12 changes: 10 additions & 2 deletions controllers/controller/devworkspacerouting/solvers/basic_solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.RoutingSuffix)
if routingSuffix == nil {
return routingObjects, errors.New(config.RoutingSuffix + " 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
Expand Down
16 changes: 8 additions & 8 deletions controllers/controller/devworkspacerouting/solvers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
Expand All @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions pkg/common/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func fillOpenShiftRouteSuffixIfNecessary(nonCachedClient client.Client, configMa
host := testRoute.Spec.Host
if host != "" {
prefixToRemove := "devworkspace-controller-test-route-" + configMap.Namespace + "."
configMap.Data[routingSuffix] = strings.TrimPrefix(host, prefixToRemove)
configMap.Data[RoutingSuffix] = strings.TrimPrefix(host, prefixToRemove)
}

err = nonCachedClient.Update(context.TODO(), configMap)
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)://<unique-to-workspace-part>.<routingSuffix>
routingSuffix = "devworkspace.routing.cluster_host_suffix"
defaultRoutingSuffix = ""
// RoutingSuffix is the base domain for routes/ingresses created on the cluster. All
// routes/ingresses will be created with URL http(s)://<unique-to-workspace-part>.<RoutingSuffix>
// is supposed to be used by embedded routing solvers only
RoutingSuffix = "devworkspace.routing.cluster_host_suffix"

experimentalFeaturesEnabled = "devworkspace.experimental_features_enabled"
defaultExperimentalFeaturesEnabled = "false"
Expand Down

0 comments on commit ecc7e20

Please sign in to comment.