Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/webhook/appwrapper_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (w *AppWrapperWebhook) Default(ctx context.Context, obj runtime.Object) err
return err
}
userInfo := request.UserInfo
aw.Labels = utilmaps.MergeKeepFirst(map[string]string{AppWrapperUsernameLabel: userInfo.Username, AppWrapperUserIDLabel: userInfo.UID}, aw.Labels)
username := utils.SanitizeLabel(userInfo.Username)
aw.Labels = utilmaps.MergeKeepFirst(map[string]string{AppWrapperUsernameLabel: username, AppWrapperUserIDLabel: userInfo.UID}, aw.Labels)
return nil
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package utils

import (
"fmt"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -346,3 +347,18 @@ func ValidatePodSets(obj *unstructured.Unstructured, podSets []workloadv1beta2.A

return nil
}

var labelRegex = regexp.MustCompile(`[^-_.\w]`)

// SanitizeLabel sanitizes a string for use as a label
func SanitizeLabel(label string) string {
// truncate to max length
if len(label) > 63 {
label = label[0:63]
}
// replace invalid characters with underscores
label = labelRegex.ReplaceAllString(label, "_")
// trim non-alphanumeric characters at both ends
label = strings.Trim(label, "-_.")
return label
}