Go (RE2)

Kubernetes Pod Name (RFC 1123) in GO

Validate Kubernetes pod names per the RFC 1123 DNS label rules — lowercase, ≤63 chars, no leading/trailing hyphen.

Try it in the GO tester →

Pattern

regexGO
^[a-z0-9](?:[a-z0-9\-]{0,61}[a-z0-9])?$

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`^[a-z0-9](?:[a-z0-9\-]{0,61}[a-z0-9])?$`)
	input := `nginx-deployment-66b6c48dd5-abcd1`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

Stricter than the general hostname pattern: pod names must be LOWERCASE only (per K8s spec). [a-z0-9] for first char. (?:[a-z0-9\-]{0,61}[a-z0-9])? optionally allows up to 61 more chars ending in a non-hyphen. Total length ≤63 chars per RFC 1123 label limits.

Examples

Input

nginx-deployment-66b6c48dd5-abcd1

Matches

  • nginx-deployment-66b6c48dd5-abcd1

Input

my-app

Matches

  • my-app

Input

MyApp

No match

Same pattern, other engines

← Back to Kubernetes Pod Name (RFC 1123) overview (all engines)