-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
182 lines (150 loc) · 4.46 KB
/
types.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
package main
import (
"encoding/json"
"regexp"
"strconv"
)
type LinterItem interface {
Lint(*Config, LinterParams) (ResultMap, error)
Name() string
Description() string
Kind() string
}
type Config struct {
Kind string `json:"kind"`
Items []*Item `json:"items"`
Objects []*Item `json:"objects"`
CustomNamespacePattern string `json:"customNamespacePattern"`
CustomNamePattern string `json:"customNamePattern"`
CustomContainerPattern string `json:"customContainerPattern"`
CustomEnvPattern string `json:"customEnvPattern"`
}
type Item struct {
Kind string `json:"kind"`
Metadata *Metadata `json:"metadata"`
Spec *Spec `json:"spec"`
Status map[string]interface{} `json:"status"`
}
type Metadata struct {
Labels map[string]string `json:"labels"`
Name string `json:"name"`
Namespace string `json:"namespace"`
}
type Spec struct {
//DeploymentConfig
Volumes []interface{} `json:"volumes"`
Containers []*Container `json:"containers"`
Template *Item `json:"template"` //nested item with its own Metadata and Spec sections
//Route
Host string `json:"host"`
Path string `json:"path"`
Port *Port `json:"port"`
}
type Container struct {
Image string `json:"image"`
Name string `json:"name"`
ImagePullPolicy string `json:"imagePullPolicy"`
Env []*EnvItem `json:"env"`
Resources *Resources `json:"resources"`
LivenessProbe *Probe `json:"livenessProbe"`
ReadinessProbe *Probe `json:"readinessProbe"`
SecurityContext *SecurityContext `json:"securityContext"`
}
type Probe struct {
TimeoutSeconds int `json:"timeoutSeconds"`
PeriodSeconds int `json:"periodSeconds"`
SuccessThreshold int `json:"successThreshold"`
InitialDelaySeconds int `json:"initialDelaySeconds"`
FailureThreshold int `json:"failureThreshold"`
}
type SecurityContext struct {
Privileged bool `json:"privileged"`
RunAsNonRoot bool `json:"runAsNonRoot"` //currently not verifiable: property not found in OSE3.1, so can't enforce `true`
RunAsUser int64 `json:"runAsUser"` //currently not verifiable: property not found in OSE3.1, so can't enforce non-root (i.e. >0)
}
type EnvItem struct {
Name string `json:"name"`
Value string `json:"value"`
}
type ContainerSet []ContainerSpec
type ContainerSpec struct {
Namespace string
Name string
Container string
}
type Resources struct {
Limits *ResourceConstraint `json:"limits"`
Requests *ResourceConstraint `json:"requests"`
}
type ResourceConstraint struct {
CPU string `json:"cpu"`
Memory string `json:"memory"`
}
type Port struct {
TargetPort CoerceString `json:"targetPort"`
}
func (r *ResourceConstraint) Complete() bool {
return len(r.CPU) > 0 && len(r.Memory) > 0
}
func (r *ResourceConstraint) Valid() bool {
//see Kubernetes pkg/api/validation/validation.go ll. 1300f.
reCpu := regexp.MustCompile(`^[0-9]+m?$`)
reMemory := regexp.MustCompile(`^[0-9]+(k|M|G|T|P|E|Ki|Mi|Gi|Ti|Pi|Ei)?$`)
if len(r.CPU) > 0 && reCpu.FindStringIndex(r.CPU) == nil {
return false
}
if len(r.Memory) > 0 && reMemory.FindStringIndex(r.Memory) == nil {
return false
}
return true
}
func (r *ResourceConstraint) None() bool {
return len(r.CPU) == 0 && len(r.Memory) == 0
}
type ResultList []ResultItem
type ResultMap map[string]ContainerSet
type ResultItem struct {
Kind string `json:"kind"`
Namespace string `json:"namespace"`
Name string `json:"name"`
Container string `json:"container"`
}
type CombinedResultMap map[string]ResultMap
type LinterParams struct {
NamespacePattern string
NamePattern string
ContainerPattern string
EnvPattern string
SkipContainerPattern string
WhitelistRegistriesPattern string
CheckPattern string
Output string
}
type MinimalObject struct {
Kind string
}
type Table struct {
Row []string
}
type CoerceString struct {
s string
}
func (cs *CoerceString) String() string {
return cs.s
}
//see also: kubernetes/api/util.go for fuzzy alternative
func (cs *CoerceString) UnmarshalJSON(value []byte) error {
//string
if value[0] == '"' {
return json.Unmarshal(value, &cs.s)
}
//int
//TODO: validate (^[0-9]+$)
var i int
err := json.Unmarshal(value, &i)
if err == nil {
cs.s = strconv.Itoa(i)
return nil
}
return err
}