forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
191 lines (175 loc) · 5.45 KB
/
validate.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package v20170131
import (
"errors"
"fmt"
"regexp"
)
// Validate implements APIObject
func (o *OrchestratorProfile) Validate() error {
switch o.OrchestratorType {
case DCOS:
case Mesos:
case Swarm:
case SwarmMode:
case Kubernetes:
default:
return fmt.Errorf("OrchestratorProfile has unknown orchestrator: %s", o.OrchestratorType)
}
return nil
}
// Validate implements APIObject
func (m *MasterProfile) Validate() error {
if m.Count != 1 && m.Count != 3 && m.Count != 5 {
return fmt.Errorf("MasterProfile count needs to be 1, 3, or 5")
}
if e := validateName(m.DNSPrefix, "MasterProfile.DNSPrefix"); e != nil {
return e
}
if e := validateDNSName(m.DNSPrefix); e != nil {
return e
}
return nil
}
// Validate implements APIObject
func (a *AgentPoolProfile) Validate(orchestratorType string) error {
if e := validateName(a.Name, "AgentPoolProfile.Name"); e != nil {
return e
}
if e := validatePoolName(a.Name); e != nil {
return e
}
if a.Count < MinAgentCount || a.Count > MaxAgentCount {
return fmt.Errorf("AgentPoolProfile count needs to be in the range [%d,%d]", MinAgentCount, MaxAgentCount)
}
if e := validateName(a.VMSize, "AgentPoolProfile.VMSize"); e != nil {
return e
}
// Kubernetes don't allow agent DNSPrefix
if orchestratorType == Kubernetes {
// The line below need to be removed after June 2017
a.DNSPrefix = ""
if e := validateNameEmpty(a.DNSPrefix, "AgentPoolProfile.DNSPrefix"); e != nil {
return e
}
}
if a.DNSPrefix != "" {
if e := validateDNSName(a.DNSPrefix); e != nil {
return e
}
}
return nil
}
// Validate implements APIObject
func (l *LinuxProfile) Validate() error {
if e := validateName(l.AdminUsername, "LinuxProfile.AdminUsername"); e != nil {
return e
}
if len(l.SSH.PublicKeys) != 1 {
return errors.New("LinuxProfile.PublicKeys requires only 1 SSH Key")
}
if e := validateName(l.SSH.PublicKeys[0].KeyData, "LinuxProfile.PublicKeys.KeyData"); e != nil {
return e
}
return nil
}
// Validate implements APIObject
func (a *Properties) Validate() error {
if a.OrchestratorProfile == nil {
return fmt.Errorf("missing OrchestratorProfile")
}
if a.MasterProfile == nil {
return fmt.Errorf("missing MasterProfile")
}
if a.LinuxProfile == nil {
return fmt.Errorf("missing LinuxProfile")
}
if e := a.MasterProfile.Validate(); e != nil {
return e
}
if e := validateUniqueProfileNames(a.AgentPoolProfiles); e != nil {
return e
}
if a.OrchestratorProfile.OrchestratorType == Kubernetes {
if a.ServicePrincipalProfile == nil {
return fmt.Errorf("ServicePrincipalProfile must be specified with Orchestrator %s", a.OrchestratorProfile.OrchestratorType)
}
if len(a.ServicePrincipalProfile.Secret) == 0 {
return fmt.Errorf("service principal client secret must be specified with Orchestrator %s", a.OrchestratorProfile.OrchestratorType)
}
}
for _, agentPoolProfile := range a.AgentPoolProfiles {
if e := agentPoolProfile.Validate(a.OrchestratorProfile.OrchestratorType); e != nil {
return e
}
if agentPoolProfile.OSType == Windows {
if a.WindowsProfile == nil {
return fmt.Errorf("missing WindowsProfile")
}
switch a.OrchestratorProfile.OrchestratorType {
case Swarm:
case Kubernetes:
default:
return fmt.Errorf("Orchestrator %s does not support Windows", a.OrchestratorProfile.OrchestratorType)
}
if len(a.WindowsProfile.AdminUsername) == 0 {
return fmt.Errorf("WindowsProfile.AdminUsername must not be empty since agent pool '%s' specifies windows", agentPoolProfile.Name)
}
if len(a.WindowsProfile.AdminPassword) == 0 {
return fmt.Errorf("WindowsProfile.AdminPassword must not be empty since agent pool '%s' specifies windows", agentPoolProfile.Name)
}
}
}
if e := a.LinuxProfile.Validate(); e != nil {
return e
}
if e := a.OrchestratorProfile.Validate(); e != nil {
return e
}
return nil
}
func validateNameEmpty(name string, label string) error {
if name != "" {
return fmt.Errorf("%s must be an empty value", label)
}
return nil
}
func validateName(name string, label string) error {
if name == "" {
return fmt.Errorf("%s must be a non-empty value", label)
}
return nil
}
func validatePoolName(poolName string) error {
// we will cap at length of 12 and all lowercase letters since this makes up the VMName
poolNameRegex := `^([a-z][a-z0-9]{0,11})$`
re, err := regexp.Compile(poolNameRegex)
if err != nil {
return err
}
submatches := re.FindStringSubmatch(poolName)
if len(submatches) != 2 {
return fmt.Errorf("pool name '%s' is invalid. A pool name must start with a lowercase letter, have max length of 12, and only have characters a-z0-9", poolName)
}
return nil
}
func validateDNSName(dnsName string) error {
dnsNameRegex := `^([A-Za-z][A-Za-z0-9-]{1,43}[A-Za-z0-9])$`
re, err := regexp.Compile(dnsNameRegex)
if err != nil {
return err
}
if !re.MatchString(dnsName) {
return fmt.Errorf("DNS name '%s' is invalid. The DNS name must contain between 3 and 45 characters. The name can contain only letters, numbers, and hyphens. The name must start with a letter and must end with a letter or a number. (length was %d)", dnsName, len(dnsName))
}
return nil
}
func validateUniqueProfileNames(profiles []*AgentPoolProfile) error {
profileNames := make(map[string]bool)
for _, profile := range profiles {
if _, ok := profileNames[profile.Name]; ok {
return fmt.Errorf("profile name '%s' already exists, profile names must be unique across pools", profile.Name)
}
profileNames[profile.Name] = true
}
return nil
}