-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitem-limits.go
66 lines (58 loc) · 2.41 KB
/
item-limits.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
package main
import "fmt"
type ItemLimits struct {
name, description, kind string
}
func (il *ItemLimits) Name() string {
return il.name
}
func (il *ItemLimits) Description() string {
return il.description
}
func (il *ItemLimits) Kind() string {
return il.kind
}
func (il *ItemLimits) Lint(config *Config, params LinterParams) (ResultMap, error) {
//deployment config without/with incomplete limits
resultLimits := make(ResultMap)
var problem string
for _, item := range config.Items {
if item.Kind != il.Kind() {
continue
}
//nested template with its own `metadata` and `spec` properties?
if item.Spec != nil && item.Spec.Template != nil {
for _, container := range item.Spec.Template.Spec.Containers {
name := container.Name
if container.Resources == nil {
problem = "no resources"
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
continue
}
limits := container.Resources.Limits
if limits == nil || limits.None() == true {
problem = "no limits"
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
} else if limits.Complete() == false {
problem = "incomplete limits"
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
} else if limits.Valid() == false {
problem = fmt.Sprintf("invalid limits pair %s (CPU) and %s (Memory)", limits.CPU, limits.Memory)
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
}
requests := container.Resources.Requests
if requests == nil || requests.None() == true {
problem = "no requests"
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
} else if requests.Complete() == false {
problem = "incomplete requests"
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
} else if requests.Valid() == false {
problem = fmt.Sprintf("invalid requests pair %s (cpu) and %s (memory)", requests.CPU, requests.Memory)
resultLimits[problem] = append(resultLimits[problem], ContainerSpec{item.Metadata.Namespace, item.Metadata.Name, name})
}
}
}
}
return resultLimits, nil
}