Skip to content

Commit

Permalink
Normalize username and dw name
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 committed May 4, 2023
1 parent d6c5f45 commit 753b93a
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions controllers/devworkspace/solver/che_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"context"
"fmt"
"path"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -272,12 +273,12 @@ func (c *CheRoutingSolver) cheExposedEndpoints(cheCluster *chev2.CheCluster, wor
return map[string]dwo.ExposedEndpointList{}, false, nil
}

username, err := getUsername(c.client, routingObj.Services[0].Namespace)
username, err := getNormalizedUsername(c.client, routingObj.Services[0].Namespace)
if err != nil {
return map[string]dwo.ExposedEndpointList{}, false, err
}

dwName, err := getDevWorkspaceName(c.client, routingObj.Services[0].Namespace, routingObj.Services[0].ObjectMeta.OwnerReferences[0].Name)
dwName, err := getNormalizedWkspName(c.client, routingObj.Services[0].Namespace, routingObj.Services[0].ObjectMeta.OwnerReferences[0].Name)
if err != nil {
return map[string]dwo.ExposedEndpointList{}, false, err
}
Expand Down Expand Up @@ -332,12 +333,12 @@ func (c *CheRoutingSolver) getGatewayConfigsAndFillRoutingObjects(cheCluster *ch
}

configs := make([]corev1.ConfigMap, 0)
username, err := getUsername(c.client, routing.Namespace)
username, err := getNormalizedUsername(c.client, routing.Namespace)
if err != nil {
return nil, err
}

dwName, err := getDevWorkspaceName(c.client, routing.Namespace, routing.Name)
dwName, err := getNormalizedWkspName(c.client, routing.Namespace, routing.Name)
if err != nil {
return nil, err
}
Expand All @@ -361,23 +362,33 @@ func (c *CheRoutingSolver) getGatewayConfigsAndFillRoutingObjects(cheCluster *ch
return configs, nil
}

func getUsername(c client.Client, namespace string) (string, error) {
func getNormalizedUsername(c client.Client, namespace string) (string, error) {
secret := &corev1.Secret{}
err := c.Get(context.TODO(), client.ObjectKey{Name: "user-profile", Namespace: namespace}, secret)
if err != nil {
return "", err
}
username := string(secret.Data["name"])
return username, nil
return normalize(username), nil
}

func getDevWorkspaceName(c client.Client, namespace string, devworkspaceId string) (string, error) {
func getNormalizedWkspName(c client.Client, namespace string, devworkspaceId string) (string, error) {
dw := &dwo.DevWorkspaceRouting{}
err := c.Get(context.TODO(), client.ObjectKey{Name: devworkspaceId, Namespace: namespace}, dw)
if err != nil {
return "", err
}
return dw.ObjectMeta.OwnerReferences[0].Name, nil
return normalize(dw.ObjectMeta.OwnerReferences[0].Name), nil
}

func normalize(username string) string {
r1 := regexp.MustCompile(`[^-a-zA-Z0-9]`)
r2 := regexp.MustCompile(`-+`)
r3 := regexp.MustCompile(`^-|-$`)

result := r1.ReplaceAllString(username, "-") // replace invalid chars with '-'
result = r2.ReplaceAllString(result, "-") // replace multiple '-' with single ones
return r3.ReplaceAllString(result, "") // trim dashes at beginning/end of
}

func (c *CheRoutingSolver) getInfraSpecificExposer(cheCluster *chev2.CheCluster, routing *dwo.DevWorkspaceRouting, objs *solvers.RoutingObjects, username string, dwName string) (func(info *EndpointInfo), error) {
Expand Down

0 comments on commit 753b93a

Please sign in to comment.