/
daemonset.go
400 lines (358 loc) · 15.3 KB
/
daemonset.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package beku
import (
"encoding/json"
"errors"
"fmt"
"github.com/ghodss/yaml"
"k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// DaemonSet include Kubernets resource object DaemonSet and error
type DaemonSet struct {
ds *v1.DaemonSet
err error
}
// NewDS create DaemonSet(ds) and chain function call begin with this function.
func NewDS() *DaemonSet { return &DaemonSet{ds: &v1.DaemonSet{}} }
// Finish Chain function call end with this function
// return real DaemonSet(really DaemonSet is kubernetes resource object DaemonSet and error
// In the function, it will check necessary parameters、input the default field
func (obj *DaemonSet) Finish() (*v1.DaemonSet, error) {
obj.verify()
return obj.ds, obj.err
}
// JSONNew use json data create DaemonSet
func (obj *DaemonSet) JSONNew(jsonbyts []byte) *DaemonSet {
obj.error(json.Unmarshal(jsonbyts, obj.ds))
return obj
}
// YAMLNew use yaml data create DaemonSet
func (obj *DaemonSet) YAMLNew(yamlbyts []byte) *DaemonSet {
obj.error(yaml.Unmarshal(yamlbyts, obj.ds))
return obj
}
// Replace replace ds by Kubernetes resource object
func (obj *DaemonSet) Replace(ds *v1.DaemonSet) *DaemonSet {
if ds != nil {
obj.ds = ds
}
return obj
}
// SetName set DaemonSet(ds) name
func (obj *DaemonSet) SetName(name string) *DaemonSet {
obj.ds.SetName(name)
return obj
}
// SetNamespace set DaemonSet(ds) namespace, default namespace value is 'default'
func (obj *DaemonSet) SetNamespace(namespace string) *DaemonSet {
obj.ds.SetNamespace(namespace)
return obj
}
// SetNamespaceAndName set DaemonSet namespace and DaemonSet name and Pod Namespace
func (obj *DaemonSet) SetNamespaceAndName(namespace, name string) *DaemonSet {
obj.ds.SetName(name)
obj.ds.SetNamespace(namespace)
obj.ds.Spec.Template.SetNamespace(namespace)
return obj
}
// SetLabels set DaemonSet(ds) Labels,set Pod Labels.
func (obj *DaemonSet) SetLabels(labels map[string]string) *DaemonSet {
obj.ds.SetLabels(labels)
return obj
}
// SetSelector set DaemonSet(ds) Selector and Set Pod Label
// The Pod that matches the seletor will be selected, DaemonSet will controller the Pod.
func (obj *DaemonSet) SetSelector(selector map[string]string) *DaemonSet {
obj.ds.Spec.Template.SetLabels(selector)
if obj.ds.Spec.Selector == nil {
obj.ds.Spec.Selector = &metav1.LabelSelector{
MatchLabels: selector,
}
return obj
}
obj.ds.Spec.Selector.MatchLabels = selector
return obj
}
// SetPodLabels set Pod Label and set DaemonSet Selector
func (obj *DaemonSet) SetPodLabels(labels map[string]string) *DaemonSet {
obj.SetSelector(labels)
return obj
}
// SetContainer set DaemonSet container
// name Not required when only one Container,you can input "".
// when many container this Field is necessary and cann't repeat
// image is necessary, image very important
// containerPort container port,this is necessary
func (obj *DaemonSet) SetContainer(name, image string, containerPort int32) *DaemonSet {
obj.error(setContainer(&obj.ds.Spec.Template, name, image, containerPort))
return obj
}
// SetAnnotations set DaemonSet annotations
func (obj *DaemonSet) SetAnnotations(annotations map[string]string) *DaemonSet {
if len(obj.ds.Annotations) <= 0 {
obj.ds.Annotations = annotations
return obj
}
for key, value := range annotations {
obj.ds.Annotations[key] = value
}
return obj
}
// SetPodQos set pod quality of service
// qosClass: is quality of service,the value only 'Guaranteed','Burstable' and 'BestEffort'
// autoSet: If your previous settings do not meet the requirements of PodQoS, we will automatically set
func (obj *DaemonSet) SetPodQos(qosClass string, autoSet ...bool) *DaemonSet {
obj.SetAnnotations(setQosMap(obj.ds.Annotations, qosClass, autoSet...))
return obj
}
// SetHTTPLiveness set container liveness of http style
// port: required
// path: http request URL,eg: /api/v1/posts/1
// initDelaySec: how long time after the first start of the program the probe is executed for the first time.(sec)
// timeoutSec: http request timeout seconds,defaults to 1 second. Minimum value is 1.
// periodSec: how often does the probe??defaults to 1 second. Minimum value is 1,Except for the first time?
// headers: headers[0] is HTTP Header, do not fill if you do not need to set
// on the other hand, only **first container** will be set livenessProbe
func (obj *DaemonSet) SetHTTPLiveness(port int, path string, initDelaySec, timeoutSec, periodSec int32, headers ...map[string]string) *DaemonSet {
setLiveness(&obj.ds.Spec.Template, httpProbe(port, path, initDelaySec, timeoutSec, periodSec, headers...))
return obj
}
// SetCMDLiveness set container liveness of cmd style
// cmd: execute liveness probe as commond line
// timeoutSec: http request timeout seconds,defaults to 1 second. Minimum value is 1.
// periodSec: how often does the probe??defaults to 1 second. Minimum value is 1,Except for the first time?
// headers: headers[0] is HTTP Header, do not fill if you do not need to set
// on the other hand, only **first container** will be set livenessProbe
func (obj *DaemonSet) SetCMDLiveness(cmd []string, initDelaySec, timeoutSec, periodSec int32) *DaemonSet {
setLiveness(&obj.ds.Spec.Template, cmdProbe(cmd, initDelaySec, timeoutSec, periodSec))
return obj
}
// SetTCPLiveness set container liveness of tcp style
// host: default is ""
// port: required
// timeoutSec: http request timeout seconds,defaults to 1 second. Minimum value is 1.
// periodSec: how often does the probe??defaults to 1 second. Minimum value is 1,Except for the first time?
// headers: headers[0] is HTTP Header, do not fill if you do not need to set
// on the other hand, only **first container** will be set livenessProbe
func (obj *DaemonSet) SetTCPLiveness(host string, port int, initDelaySec, timeoutSec, periodSec int32) *DaemonSet {
setLiveness(&obj.ds.Spec.Template, tcpProbe(host, port, initDelaySec, timeoutSec, periodSec))
return obj
}
// SetHTTPReadness set container readness
// initDelaySec: how long time after the first start of the program the probe is executed for the first time.(sec)
// timeoutSec: http request timeout seconds,defaults to 1 second. Minimum value is 1.
// periodSec: how often does the probe??defaults to 1 second. Minimum value is 1,Except for the first time?
// on the other hand, only **first container** will be set livenessProbe
func (obj *DaemonSet) SetHTTPReadness(port int, path string, initDelaySec, timeoutSec, periodSec int32, headers ...map[string]string) *DaemonSet {
setReadness(&obj.ds.Spec.Template, httpProbe(port, path, initDelaySec, timeoutSec, periodSec, headers...))
return obj
}
// SetCMDReadness set container readness of cmd style
// cmd: execute readness probe as commond line
// timeoutSec: http request timeout seconds,defaults to 1 second. Minimum value is 1.
// periodSec: how often does the probe? defaults to 1 second. Minimum value is 1,Except for the first time?
// headers: headers[0] is HTTP Header, do not fill if you do not need to set
// on the other hand, only **first container** will be set livenessProbe
func (obj *DaemonSet) SetCMDReadness(cmd []string, initDelaySec, timeoutSec, periodSec int32) *DaemonSet {
setReadness(&obj.ds.Spec.Template, cmdProbe(cmd, initDelaySec, timeoutSec, periodSec))
return obj
}
// SetTCPReadness set container readness of tcp style
// host: default is ""
// port: required
// timeoutSec: http request timeout seconds,defaults to 1 second. Minimum value is 1.
// periodSec: how often does the probe? defaults to 1 second. Minimum value is 1,Except for the first time?
// headers: headers[0] is HTTP Header, do not fill if you do not need to set
// on the other hand, only **first container** will be set livenessProbe
func (obj *DaemonSet) SetTCPReadness(host string, port int, initDelaySec, timeoutSec, periodSec int32) *DaemonSet {
setReadness(&obj.ds.Spec.Template, tcpProbe(host, port, initDelaySec, timeoutSec, periodSec))
return obj
}
// SetPVClaim set DaemonSet PersistentVolumeClaimVolumeSource
// params:
// volumeName: this is Custom field,you can define VolumeSource name,will be used of the container MountPath,
// claimName: this is PersistentVolumeClaim(PVC) name,the PVC and DaemonSet must on same namespace and exist.
func (obj *DaemonSet) SetPVClaim(volumeName, claimName string) *DaemonSet {
obj.error(setPVClaim(&obj.ds.Spec.Template, volumeName, claimName))
return obj
}
//SetPVCMounts mount PersistentVolumeClaim on container
// params:
// volumeName:the param is SetPVClaim() function volumeName,and when you call SetPVCMounts function you must call SetPVClaim function,and no order.
// on the other hand SetPVCMounts() function only mount first Container,and On the Container you can volumeMount many PersistentVolumeClaim.
// mountPath: runtime container dir eg:/var/lib/mysql
func (obj *DaemonSet) SetPVCMounts(volumeName, mountPath string) *DaemonSet {
obj.error(setPVCMounts(&obj.ds.Spec.Template, volumeName, mountPath))
return obj
}
// SetPreStopExec set StatefulSet PreStop command
// PreStop is called immediately before a container is terminated.
// The container is terminated after the handler completes.
// The reason for termination is passed to the handler.
// Regardless of the outcome of the handler, the container is eventually terminated.
// Other management of the container blocks until the hook completes.
// More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
func (obj *DaemonSet) SetPreStopExec(command []string) *DaemonSet {
setPreStopExec(&obj.ds.Spec.Template, command)
return obj
}
// SetPostStartExec set PostStart shell command style
// PostStart is called immediately after a container is created. If the handler fails,
// the container is terminated and restarted according to its restart policy.
// Other management of the container blocks until the hook completes.
// More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
func (obj *DaemonSet) SetPostStartExec(command []string) *DaemonSet {
setPostStartExec(&obj.ds.Spec.Template, command)
return obj
}
// SetPreStopHTTP set preStop http style
// PreStop is called immediately before a container is terminated.
// The container is terminated after the handler completes.
// The reason for termination is passed to the handler.
// Regardless of the outcome of the handler, the container is eventually terminated.
// Other management of the container blocks until the hook completes.
// More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
func (obj *DaemonSet) SetPreStopHTTP(scheme URIScheme, host string, port int, path string, headers ...map[string]string) *DaemonSet {
setPreStopHTTP(&obj.ds.Spec.Template, scheme, host, port, path, headers...)
return obj
}
// SetPostStartHTTP set PostStart http style
// PostStart is called immediately after a container is created. If the handler fails,
// the container is terminated and restarted according to its restart policy.
// Other management of the container blocks until the hook completes.
// More info: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
func (obj *DaemonSet) SetPostStartHTTP(scheme URIScheme, host string, port int, path string, headers ...map[string]string) *DaemonSet {
setPostStartHTTP(&obj.ds.Spec.Template, scheme, host, port, path, headers...)
return obj
}
// SetPodPriorityClass set DaemonSet Pod Priority
// priorityClassName is Kubernetes resource object PriorityClass name
// priorityClassName must already exists in kubernetes cluster
func (obj *DaemonSet) SetPodPriorityClass(priorityClassName string) *DaemonSet {
obj.error(setPodPriorityClass(&obj.ds.Spec.Template, priorityClassName))
return obj
}
// SetEnvs set Pod Environmental variable
func (obj *DaemonSet) SetEnvs(envMap map[string]string) *DaemonSet {
obj.error(setEnvs(&obj.ds.Spec.Template, envMap))
return obj
}
// SetMinReadySeconds set DaemonSet minreadyseconds default 600
func (obj *DaemonSet) SetMinReadySeconds(sec int32) *DaemonSet {
if sec < 0 {
sec = 0
}
obj.ds.Spec.MinReadySeconds = sec
return obj
}
// SetHistoryLimit set DaemonSet history version numbers, limit default 10
// the field is used to Rollback
func (obj *DaemonSet) SetHistoryLimit(limit int32) *DaemonSet {
if limit <= 0 {
limit = 10
}
obj.ds.Spec.RevisionHistoryLimit = &limit
return obj
}
// SetImagePullSecrets set pod pull secret
func (obj *DaemonSet) SetImagePullSecrets(secretName string) *DaemonSet {
setImagePullSecrets(&obj.ds.Spec.Template, secretName)
return obj
}
// ImagePullPolicy DaemonSet pull image policy:Always,Never,IfNotPresent
func (obj *DaemonSet) ImagePullPolicy(pullPolicy PullPolicy) *DaemonSet {
if len(obj.ds.Annotations) <= 0 {
obj.ds.Annotations = make(map[string]string, 0)
}
obj.ds.Annotations[ImagePullPolicyKey] = string(pullPolicy)
return obj
}
// Release release DaemonSet on Kubernetes
func (obj *DaemonSet) Release() (*v1.DaemonSet, error) {
ds, err := obj.Finish()
if err != nil {
return nil, err
}
client, err := GetKubeClient()
if err != nil {
return nil, err
}
return client.AppsV1().DaemonSets(ds.GetNamespace()).Create(ds)
}
// GetPodLabel get pod labels
func (obj *DaemonSet) GetPodLabel() map[string]string {
return obj.ds.Spec.Template.GetLabels()
}
// Apply it will be updated when this resource object exists in K8s,
// it will be created when it does not exist.
func (obj *DaemonSet) Apply() (*v1.DaemonSet, error) {
ds, err := obj.Finish()
if err != nil {
return nil, err
}
client, err := GetKubeClient()
if err != nil {
return nil, err
}
_, err = client.AppsV1().DaemonSets(ds.GetNamespace()).Get(ds.GetName(), metav1.GetOptions{})
if err != nil {
return client.AppsV1().DaemonSets(ds.GetNamespace()).Create(ds)
}
return client.AppsV1().DaemonSets(ds.GetNamespace()).Update(ds)
}
func (obj *DaemonSet) error(err error) {
if obj.err != nil {
return
}
obj.err = err
}
// verify check service necessary value, input the default field and input related data.
func (obj *DaemonSet) verify() {
if obj.err != nil {
return
}
if !verifyString(obj.ds.Name) {
obj.err = errors.New("DaemonSet.Name is not allowed to be empty")
return
}
if err := containerRepeated(obj.ds.Spec.Template.Spec.Containers); err != nil {
obj.err = fmt.Errorf("DaemonSet.Spec.Template.Spec.Containers err:%s", err.Error())
return
}
if len(obj.GetPodLabel()) < 1 {
obj.err = errors.New("Pod Labels is not allowed to be empty,you can call SetPodLabels input")
return
}
//check qos set,if err!=nil, check need auto set qos
presentQos, err := qosCheck(obj.ds.Annotations[qosKey], obj.ds.Spec.Template.Spec)
if err != nil {
if obj.ds.Annotations[autoQosKey] == "true" {
err := obj.autoSetQos(presentQos)
if err != nil {
obj.err = err
return
}
} else {
obj.err = err
return
}
}
obj.ds.Kind = "DaemonSet"
obj.ds.APIVersion = "app/v1"
if obj.ds.Annotations[ImagePullPolicyKey] == "" {
for index := range obj.ds.Spec.Template.Spec.Containers {
obj.ds.Spec.Template.Spec.Containers[index].ImagePullPolicy = corev1.PullIfNotPresent
}
return
}
policy := PullPolicy(obj.ds.Annotations[ImagePullPolicyKey]).ToK8s()
for index := range obj.ds.Spec.Template.Spec.Containers {
obj.ds.Spec.Template.Spec.Containers[index].ImagePullPolicy = policy
}
delete(obj.ds.Annotations, ImagePullPolicyKey)
}
// autoSetQos auto set Pod of Deployment QOS
func (obj *DaemonSet) autoSetQos(presentQos string) error {
return autoSetQos(obj.ds.Annotations[qosKey], presentQos, &obj.ds.Spec.Template.Spec)
}