This repository was archived by the owner on Jun 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
144 lines (127 loc) · 3.7 KB
/
Copy pathvalidate.go
File metadata and controls
144 lines (127 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package kployconfig
import (
"errors"
"fmt"
"net/mail"
"regexp"
"strings"
"text/template"
)
var (
// projectNameRE: 3–13 chars, leading lowercase letter, then
// lowercase alphanumeric or dashes. Constrains the project name
// since it becomes part of namespace names ("foo-pr-42") and
// hostnames ("foo-pr-42.bitcomplete.dev") that must remain RFC 1123
// compliant and short.
projectNameRE = regexp.MustCompile(`^[a-z][a-z0-9-]{2,12}$`)
// k8sNameRE: RFC 1123 DNS label (Kubernetes name format).
k8sNameRE = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
// envVarRE: shell-style upper snake case (e.g. AUTH_SECRET).
envVarRE = regexp.MustCompile(`^[A-Z][A-Z0-9_]*$`)
)
// Validate runs all schema rules over cfg. Returns ValidationErrors
// (non-nil) if any rule fails; nil otherwise.
//
// Rules are accumulated rather than short-circuiting so callers see
// every problem at once (matters for CLI UX).
func Validate(cfg *Config) error {
if cfg == nil {
return errors.New("nil config")
}
var errs ValidationErrors
if cfg.Project == "" {
errs = append(errs, ValidationError{"project", "required"})
} else if !projectNameRE.MatchString(cfg.Project) {
errs = append(errs, ValidationError{
"project",
fmt.Sprintf("must match %s (got %q)", projectNameRE, cfg.Project),
})
}
if cfg.Hostname != nil && cfg.Hostname.Template != "" {
if _, err := template.New("hostname").Parse(cfg.Hostname.Template); err != nil {
errs = append(errs, ValidationError{
"hostname.template",
"invalid Go template: " + err.Error(),
})
}
}
if cfg.Preview != nil && cfg.Preview.Enabled {
errs = append(errs, validatePreviewEnabled(cfg)...)
}
if len(errs) > 0 {
return errs
}
return nil
}
func validatePreviewEnabled(cfg *Config) ValidationErrors {
var errs ValidationErrors
p := cfg.Preview
if len(cfg.Admins) == 0 {
errs = append(errs, ValidationError{
"admins",
"required when preview.enabled is true",
})
}
for i, addr := range cfg.Admins {
if _, err := mail.ParseAddress(addr); err != nil {
errs = append(errs, ValidationError{
fmt.Sprintf("admins[%d]", i),
"invalid email: " + err.Error(),
})
}
}
if p.Template == "" {
errs = append(errs, ValidationError{
"preview.template",
"required when preview.enabled is true",
})
}
if len(p.Ingress) == 0 {
errs = append(errs, ValidationError{
"preview.ingress",
"must have at least one entry when preview.enabled is true",
})
}
for i, ing := range p.Ingress {
base := fmt.Sprintf("preview.ingress[%d]", i)
if ing.Service.Name == "" {
errs = append(errs, ValidationError{base + ".service.name", "required"})
} else if !k8sNameRE.MatchString(ing.Service.Name) {
errs = append(errs, ValidationError{
base + ".service.name",
"must be a valid RFC 1123 DNS label",
})
}
if ing.Service.Port < 1 || ing.Service.Port > 65535 {
errs = append(errs, ValidationError{
base + ".service.port",
fmt.Sprintf("must be in range 1-65535 (got %d)", ing.Service.Port),
})
}
}
if p.ImageTagPattern != "" &&
!strings.Contains(p.ImageTagPattern, "{number}") &&
!strings.Contains(p.ImageTagPattern, "{sha7}") {
errs = append(errs, ValidationError{
"preview.imageTagPattern",
"must contain at least one of {number} or {sha7}",
})
}
for i, s := range p.InheritSecrets {
if !k8sNameRE.MatchString(s) {
errs = append(errs, ValidationError{
fmt.Sprintf("preview.inheritSecrets[%d]", i),
"must be a valid RFC 1123 DNS label",
})
}
}
for i, s := range p.GeneratedSecrets {
if !envVarRE.MatchString(s) {
errs = append(errs, ValidationError{
fmt.Sprintf("preview.generatedSecrets[%d]", i),
fmt.Sprintf("must match %s", envVarRE),
})
}
}
return errs
}