Skip to content

Commit

Permalink
(regex): update to allow full alphaNumeric and convert /(#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose-Matsuda authored Jan 24, 2025
1 parent 0982ecc commit 3344a13
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions cmd/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,26 @@ func createPatch(pod *corev1.Pod, sidecarConfigTemplate *Config, annotations map

// Used for variable insertion, as dashes are no good but underscores are
func cleanAndSanitizeName(name string) string {
// Define the allowed regex pattern: lowercase letters, numbers, and dashes
validNameRegex := regexp.MustCompile(`[^a-z0-9-]`)
// Define the allowed regex pattern: Alphanumeric and dashes
name = strings.ReplaceAll(name, "/", "-")
validNameRegex := regexp.MustCompile(`[^a-zA-Z0-9-]`)

// Replace any character that does not match the allowed pattern with an empty string
name = validNameRegex.ReplaceAllString(name, "")

// Remove trailing dashes
name = strings.TrimRight(name, "-")

// Remove leading dashes
name = strings.TrimLeft(name, "-")

// Replace double dashes with a single dash
pattern := regexp.MustCompile(`-+`)
name = pattern.ReplaceAllString(name, "-")

// Replace dashes with underscores
name = strings.ReplaceAll(name, "-", "_")

// Remove trailing dashes
name = strings.TrimRight(name, "-")

// Remove leading dashes
name = strings.TrimLeft(name, "-")

return name
}

Expand Down

0 comments on commit 3344a13

Please sign in to comment.