Skip to content
Draft
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
2 changes: 1 addition & 1 deletion internal/readme/readme.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func isPrimitive(t string) bool {
switch t {
case "string", "bool", "boolean", "int", "int32", "int64",
"float32", "float64", "number", "integer", "nil",
"quantity":
"quantity", "duration":
return true
default:
return false
Expand Down
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (g *gen) addImp(p string) {
func isPrimitive(t string) bool {
switch t {
case "string", "bool", "int", "int32", "int64", "float32", "float64",
"quantity":
"quantity", "duration":
return true
default:
return false
Expand Down Expand Up @@ -269,7 +269,7 @@ func (g *gen) resolve(raw string) string {
}
if strings.Contains(raw, ".") { // external
switch raw {
case "quantity":
case "quantity", "duration":
return "string"
default:
if idx := strings.LastIndex(raw, "."); idx != -1 {
Expand Down Expand Up @@ -344,6 +344,8 @@ func (g *gen) writeStruct(n *node) {
// +kubebuilder:validation:XIntOrString
type quantity string

// +kubebuilder:validation:Pattern=` + "`" + `^-?(\d+h(\d+m)?(\d+s)?(\d+ms)?(\d+us)?(\d+µs)?(\d+ns)?|\d+m(\d+s)?(\d+ms)?(\d+us)?(\d+µs)?(\d+ns)?|\d+s(\d+ms)?(\d+us)?(\d+µs)?(\d+ns)?|\d+ms(\d+us)?(\d+µs)?(\d+ns)?|\d+us(\d+µs)?(\d+ns)?|\d+µs(\d+ns)?|\d+ns)$` + "`" + `
type duration string
Comment on lines +347 to +348

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The regular expression for duration validation is incorrect as it allows both us and µs to be present in the same duration string (e.g., 1us1µs). This is invalid for Go's time.ParseDuration since us and µs are aliases for microseconds and cannot be used together.

Suggested change
// +kubebuilder:validation:Pattern=` + "`" + `^-?(\d+h(\d+m)?(\d+s)?(\d+ms)?(\d+us)?(\d+µs)?(\d+ns)?|\d+m(\d+s)?(\d+ms)?(\d+us)?(\d+µs)?(\d+ns)?|\d+s(\d+ms)?(\d+us)?(\d+µs)?(\d+ns)?|\d+ms(\d+us)?(\d+µs)?(\d+ns)?|\d+us(\d+µs)?(\d+ns)?|\d+µs(\d+ns)?|\d+ns)$` + "`" + `
type duration string
// +kubebuilder:validation:Pattern=` + "`" + `^-?(\d+h(\d+m)?(\d+s)?(\d+ms)?(\d+(?:us|µs))?(\d+ns)?|\d+m(\d+s)?(\d+ms)?(\d+(?:us|µs))?(\d+ns)?|\d+s(\d+ms)?(\d+(?:us|µs))?(\d+ns)?|\d+ms(\d+(?:us|µs))?(\d+ns)?|\d+(?:us|µs)(\d+ns)?|\d+ns)$` + "`" + `
type duration string

`)
return
}
Expand Down Expand Up @@ -749,7 +751,7 @@ func populateDefaults(n *node, y interface{}, aliases map[string]*node) {
func formatDefault(val, typ string) string {
t := strings.TrimPrefix(typ, "*")
switch {
case t == "string" || t == "quantity":
case t == "string" || t == "quantity" || t == "duration":
return fmt.Sprintf("%q", val)
case strings.HasPrefix(t, "[]"), strings.HasPrefix(t, "map["):
return "" // no defaults for composite types
Expand Down