/
addons.go
149 lines (137 loc) · 4.43 KB
/
addons.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
package core
import (
"context"
"fmt"
"strconv"
"time"
"github.com/zdnscloud/zke/pkg/k8s"
"github.com/zdnscloud/zke/pkg/log"
"github.com/zdnscloud/zke/pkg/templates"
)
const (
NginxIngressAddonAppName = "ingress-nginx"
CoreDNSAddonAppName = "coredns"
)
type AddonError struct {
err string
IsCritical bool
}
func (e *AddonError) Error() string {
return e.err
}
func (c *Cluster) DoAddonDeploy(ctx context.Context, addonYaml, resourceName string, IsCritical bool) error {
addonUpdated, err := c.StoreAddonConfigMap(ctx, addonYaml, resourceName)
if err != nil {
return &AddonError{fmt.Sprintf("Failed to save addon ConfigMap: %v", err), IsCritical}
}
log.Infof(ctx, "[addons] Executing deploy job %s", resourceName)
node, err := k8s.GetNode(c.KubeClient, c.ControlPlaneHosts[0].NodeName)
if err != nil {
return &AddonError{fmt.Sprintf("Failed to get Node [%s]: %v", c.ControlPlaneHosts[0].NodeName, err), IsCritical}
}
addonJob, err := GetAddonsExecuteJob(resourceName, node.Name, c.Core.KubeAPI.Image)
if err != nil {
return &AddonError{fmt.Sprintf("Failed to generate addon execute job: %v", err), IsCritical}
}
if err = c.ApplySystemAddonExecuteJob(ctx, addonJob, addonUpdated); err != nil {
return &AddonError{fmt.Sprintf("%v", err), IsCritical}
}
return nil
}
func (c *Cluster) StoreAddonConfigMap(ctx context.Context, addonYaml string, addonName string) (bool, error) {
log.Infof(ctx, "[addons] Saving ConfigMap for addon %s to Kubernetes", addonName)
var err error
updated := false
timeout := make(chan bool, 1)
go func() {
for {
updated, err = k8s.UpdateConfigMap(c.KubeClient, []byte(addonYaml), addonName)
if err != nil {
time.Sleep(time.Second * 5)
continue
}
log.Infof(ctx, "[addons] Successfully saved ConfigMap for addon %s to Kubernetes", addonName)
timeout <- true
break
}
}()
select {
case <-timeout:
return updated, nil
case <-time.After(time.Second * UpdateStateTimeout):
return updated, fmt.Errorf("[addons] Timeout waiting for kubernetes to be ready")
}
}
func (c *Cluster) ApplySystemAddonExecuteJob(ctx context.Context, addonJob string, addonUpdated bool) error {
if err := k8s.ApplyK8sSystemJob(ctx, addonJob, c.KubeClient, k8s.DefaultTimeout, addonUpdated); err != nil {
return err
}
return nil
}
func GetAddonsExecuteJob(addonName, nodeName, image string) (string, error) {
getAddonJob := func(addonName, nodeName, image string, isDelete bool) (string, error) {
jobConfig := map[string]string{
"AddonName": addonName,
"NodeName": nodeName,
"Image": image,
"DeleteJob": strconv.FormatBool(isDelete),
}
return templates.CompileTemplateFromMap(addonJobTemplate, jobConfig)
}
return getAddonJob(addonName, nodeName, image, false)
}
const addonJobTemplate = `
{{- $addonName := .AddonName }}
{{- $nodeName := .NodeName }}
{{- $image := .Image }}
apiVersion: batch/v1
kind: Job
metadata:
{{- if eq .DeleteJob "true" }}
name: {{$addonName}}-delete-job
{{- else }}
name: {{$addonName}}-deploy-job
{{- end }}
namespace: kube-system
spec:
backoffLimit: 10
template:
metadata:
name: zke-deploy
spec:
tolerations:
- key: node-role.kubernetes.io/controlplane
operator: Exists
effect: NoSchedule
- key: node-role.kubernetes.io/etcd
operator: Exists
effect: NoExecute
hostNetwork: true
serviceAccountName: zke-job-deployer
nodeName: {{$nodeName}}
containers:
{{- if eq .DeleteJob "true" }}
- name: {{$addonName}}-delete-pod
{{- else }}
- name: {{$addonName}}-pod
{{- end }}
image: {{$image}}
{{- if eq .DeleteJob "true" }}
command: ["/bin/sh"]
args: ["-c" ,"kubectl get --ignore-not-found=true -f /etc/config/{{$addonName}}.yaml -o name | xargs kubectl delete --ignore-not-found=true"]
{{- else }}
command: [ "kubectl", "apply", "-f" , "/etc/config/{{$addonName}}.yaml"]
{{- end }}
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: {{$addonName}}
items:
- key: {{$addonName}}
path: {{$addonName}}.yaml
restartPolicy: Never`