forked from nais/naisd
/
manifest.go
356 lines (303 loc) · 9.92 KB
/
manifest.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package api
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"github.com/golang/glog"
"github.com/hashicorp/go-multierror"
"github.com/imdario/mergo"
"github.com/nais/naisd/api/naisrequest"
"gopkg.in/yaml.v2"
k8sapi "k8s.io/apimachinery/pkg/api/resource"
)
type Probe struct {
Path string
InitialDelay int `yaml:"initialDelay"`
PeriodSeconds int `yaml:"periodSeconds"`
FailureThreshold int `yaml:"failureThreshold"`
Timeout int `yaml:"timeout"`
}
type Healthcheck struct {
Liveness Probe
Readiness Probe
}
type ResourceList struct {
Cpu string
Memory string
}
type ResourceRequirements struct {
Limits ResourceList
Requests ResourceList
}
type PrometheusConfig struct {
Enabled bool
Port string
Path string
}
type IstioConfig struct {
Enabled bool
}
type NaisManifest struct {
Team string
Image string
Port int
Healthcheck Healthcheck
PreStopHookPath string `yaml:"preStopHookPath"`
Prometheus PrometheusConfig
Istio IstioConfig
Replicas Replicas
Ingress Ingress
Resources ResourceRequirements
FasitResources FasitResources `yaml:"fasitResources"`
LeaderElection bool `yaml:"leaderElection"`
Redis bool `yaml:"redis"`
WebProxy bool
Alerts []PrometheusAlertRule
Logformat string
Logtransform string
}
type Ingress struct {
Disabled bool
}
type Replicas struct {
Min int
Max int
CpuThresholdPercentage int `yaml:"cpuThresholdPercentage"`
}
type FasitResources struct {
Used []UsedResource
Exposed []ExposedResource
}
type UsedResource struct {
Alias string
ResourceType string `yaml:"resourceType"`
PropertyMap map[string]string `yaml:"propertyMap"`
}
type ExposedResource struct {
Alias string
ResourceType string `yaml:"resourceType"`
Path string
Description string
WsdlGroupId string `yaml:"wsdlGroupId"`
WsdlArtifactId string `yaml:"wsdlArtifactId"`
WsdlVersion string `yaml:"wsdlVersion"`
SecurityToken string `yaml:"securityToken"`
AllZones bool `yaml:"allZones"`
}
type ValidationErrors struct {
Errors []ValidationError
}
type ValidationError struct {
ErrorMessage string
Fields map[string]string
}
type Field struct {
Name string
Value string
}
func GenerateManifest(deploymentRequest naisrequest.Deploy) (naisManifest NaisManifest, err error) {
manifest, err := downloadManifest(deploymentRequest)
if err != nil {
glog.Errorf("could not download manifest", err)
return NaisManifest{}, err
}
if err := AddDefaultManifestValues(&manifest, deploymentRequest.Application); err != nil {
glog.Errorf("Could not merge manifest %s", err)
return NaisManifest{}, err
}
validationErrors := ValidateManifest(manifest)
if len(validationErrors.Errors) != 0 {
glog.Error("Invalid manifest: ", validationErrors.Error())
return NaisManifest{}, validationErrors
}
return manifest, nil
}
func downloadManifest(deploymentRequest naisrequest.Deploy) (naisManifest NaisManifest, err error) {
var urls []string
var errors error
if len(deploymentRequest.ManifestUrl) > 0 {
urls = []string{deploymentRequest.ManifestUrl}
} else {
glog.Info("No manifest url specified. Using defaults")
urls = createManifestUrl(deploymentRequest.Application, deploymentRequest.Version)
}
for _, url := range urls {
if manifest, err := fetchManifest(url); err != nil {
errors = multierror.Append(errors, err)
} else {
return manifest, nil
}
}
return NaisManifest{}, errors
}
func createManifestUrl(application, version string) []string {
return []string{
fmt.Sprintf("https://repo.adeo.no/repository/raw/nais/%s/%s/nais.yaml", application, version),
fmt.Sprintf("http://nexus.adeo.no/nexus/service/local/repositories/m2internal/content/nais/%s/%s/nais.yaml", application, version),
fmt.Sprintf("http://nexus.adeo.no/nexus/service/local/repositories/m2internal/content/nais/%s/%s/%s.yaml", application, version, application+"-"+version),
}
}
func AddDefaultManifestValues(manifest *NaisManifest, application string) error {
return mergo.Merge(manifest, GetDefaultManifest(application))
}
func fetchManifest(url string) (NaisManifest, error) {
glog.Infof("Fetching manifest from URL %s\n", url)
response, err := http.Get(url)
if err != nil {
glog.Errorf("Could not fetch %s", err)
return NaisManifest{}, fmt.Errorf("HTTP GET failed for url: %s. %s", url, err.Error())
}
defer response.Body.Close()
if response.StatusCode > 299 {
glog.Errorf("got HTTP status code %d fetching manifest from URL: %s", response.StatusCode, url)
return NaisManifest{}, fmt.Errorf("got HTTP status code %d fetching manifest from URL: %s", response.StatusCode, url)
}
if body, err := ioutil.ReadAll(response.Body); err != nil {
return NaisManifest{}, err
} else {
var manifest NaisManifest
if err := yaml.Unmarshal(body, &manifest); err != nil {
glog.Errorf("Could not unmarshal yaml %s from URL: %s", err, url)
return NaisManifest{}, fmt.Errorf("unable to unmarshal %s from URL: %s", err.Error(), url)
}
glog.Infof("Got manifest %s", manifest)
return manifest, err
}
}
func ValidateManifest(manifest NaisManifest) ValidationErrors {
validations := []func(NaisManifest) *ValidationError{
validateImage,
validateReplicasMax,
validateReplicasMin,
validateMinIsSmallerThanMax,
validateCpuThreshold,
validateRequestMemoryQuantity,
validateLimitsMemoryQuantity,
validateRequestCpuQuantity,
validateLimitsCpuQuantity,
validateResources,
validateAlertRules,
}
var validationErrors ValidationErrors
for _, valfunc := range validations {
if valError := valfunc(manifest); valError != nil {
validationErrors.Errors = append(validationErrors.Errors, *valError)
}
}
return validationErrors
}
func validateResources(manifest NaisManifest) *ValidationError {
for _, resource := range manifest.FasitResources.Exposed {
if resource.ResourceType == "" || resource.Alias == "" {
return &ValidationError{
"Alias and ResourceType must be specified",
map[string]string{"Alias": resource.Alias},
}
}
}
for _, resource := range manifest.FasitResources.Used {
if resource.ResourceType == "" || resource.Alias == "" {
return &ValidationError{
"Alias and ResourceType must be specified",
map[string]string{"Alias": resource.Alias},
}
}
}
return nil
}
func validateImage(manifest NaisManifest) *ValidationError {
if strings.LastIndex(manifest.Image, ":") > strings.LastIndex(manifest.Image, "/") {
return &ValidationError{
"Image cannot contain tag",
map[string]string{"Image": manifest.Image},
}
}
return nil
}
func createQuanitityValidationError(key string, value string, err error) *ValidationError {
validationError := new(ValidationError)
validationError.ErrorMessage = "not a valid quantity. " + err.Error()
validationError.Fields = make(map[string]string)
validationError.Fields[key] = value
return validationError
}
func validateRequestMemoryQuantity(manifest NaisManifest) *ValidationError {
_, err := k8sapi.ParseQuantity(manifest.Resources.Requests.Memory)
if err != nil {
return createQuanitityValidationError("Resources.Requests.Memory", manifest.Resources.Requests.Memory, err)
}
return nil
}
func validateLimitsMemoryQuantity(manifest NaisManifest) *ValidationError {
_, err := k8sapi.ParseQuantity(manifest.Resources.Limits.Memory)
if err != nil {
return createQuanitityValidationError("Resources.Limits.Memory", manifest.Resources.Limits.Memory, err)
}
return nil
}
func validateRequestCpuQuantity(manifest NaisManifest) *ValidationError {
_, err := k8sapi.ParseQuantity(manifest.Resources.Requests.Cpu)
if err != nil {
return createQuanitityValidationError("Resources.Request.Cpu", manifest.Resources.Requests.Cpu, err)
}
return nil
}
func validateLimitsCpuQuantity(manifest NaisManifest) *ValidationError {
_, err := k8sapi.ParseQuantity(manifest.Resources.Limits.Cpu)
if err != nil {
return createQuanitityValidationError("Resources.Limits.Cpu", manifest.Resources.Limits.Cpu, err)
}
return nil
}
func validateCpuThreshold(manifest NaisManifest) *ValidationError {
if manifest.Replicas.CpuThresholdPercentage < 10 || manifest.Replicas.CpuThresholdPercentage > 90 {
err := new(ValidationError)
err.ErrorMessage = "CpuThreshold must be between 10 and 90."
err.Fields = make(map[string]string)
err.Fields["Replicas.CpuThreshold"] = strconv.Itoa(manifest.Replicas.CpuThresholdPercentage)
return err
}
return nil
}
func validateMinIsSmallerThanMax(manifest NaisManifest) *ValidationError {
if manifest.Replicas.Min > manifest.Replicas.Max {
validationError := new(ValidationError)
validationError.ErrorMessage = "Replicas.Min is larger than Replicas.Max."
validationError.Fields = make(map[string]string)
validationError.Fields["Replicas.Max"] = strconv.Itoa(manifest.Replicas.Max)
validationError.Fields["Replicas.Min"] = strconv.Itoa(manifest.Replicas.Min)
return validationError
}
return nil
}
func validateReplicasMin(manifest NaisManifest) *ValidationError {
if manifest.Replicas.Min == 0 {
validationError := new(ValidationError)
validationError.ErrorMessage = "Replicas.Min is not set"
validationError.Fields = make(map[string]string)
validationError.Fields["Replicas.Min"] = strconv.Itoa(manifest.Replicas.Min)
return validationError
}
return nil
}
func validateReplicasMax(manifest NaisManifest) *ValidationError {
if manifest.Replicas.Max == 0 {
validationError := new(ValidationError)
validationError.ErrorMessage = "Replicas.Max is not set"
validationError.Fields = make(map[string]string)
validationError.Fields["Replicas.Max"] = strconv.Itoa(manifest.Replicas.Max)
return validationError
}
return nil
}
func (errors ValidationErrors) Error() (s string) {
for _, validationError := range errors.Errors {
s += validationError.ErrorMessage + "\n"
for k, v := range validationError.Fields {
s += " - " + k + ": " + v + ".\n"
}
}
return s
}