forked from rancher/rke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addons.go
266 lines (235 loc) · 7.33 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
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
package cluster
import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
"time"
"io/ioutil"
"net/http"
"strings"
"github.com/rancher/rke/addons"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
const (
KubeDNSAddonResourceName = "rke-kubedns-addon"
UserAddonResourceName = "rke-user-addon"
IngressAddonResourceName = "rke-ingress-controller"
UserAddonsIncludeResourceName = "rke-user-includes-addons"
)
type ingressOptions struct {
RBACConfig string
Options map[string]string
NodeSelector map[string]string
AlpineImage string
IngressImage string
IngressBackend string
}
func (c *Cluster) deployK8sAddOns(ctx context.Context) error {
if err := c.deployKubeDNS(ctx); err != nil {
return err
}
return c.deployIngress(ctx)
}
func (c *Cluster) deployUserAddOns(ctx context.Context) error {
log.Infof(ctx, "[addons] Setting up user addons")
if c.Addons != "" {
if err := c.doAddonDeploy(ctx, c.Addons, UserAddonResourceName); err != nil {
return err
}
}
if len(c.AddonsInclude) > 0 {
if err := c.deployAddonsInclude(ctx); err != nil {
return err
}
}
if c.Addons == "" && len(c.AddonsInclude) == 0 {
log.Infof(ctx, "[addons] no user addons defined")
} else {
log.Infof(ctx, "[addons] User addons deployed successfully")
}
return nil
}
func (c *Cluster) deployAddonsInclude(ctx context.Context) error {
var manifests []byte
log.Infof(ctx, "[addons] Checking for included user addons")
if len(c.AddonsInclude) == 0 {
log.Infof(ctx, "[addons] No included addon paths or urls..")
return nil
}
for _, addon := range c.AddonsInclude {
if strings.HasPrefix(addon, "http") {
addonYAML, err := getAddonFromURL(addon)
if err != nil {
return err
}
log.Infof(ctx, "[addons] Adding addon from url %s", addon)
logrus.Debugf("URL Yaml: %s", addonYAML)
if err := validateUserAddonYAML(addonYAML); err != nil {
return err
}
manifests = append(manifests, addonYAML...)
} else if isFilePath(addon) {
addonYAML, err := ioutil.ReadFile(addon)
if err != nil {
return err
}
log.Infof(ctx, "[addons] Adding addon from %s", addon)
logrus.Debugf("FilePath Yaml: %s", string(addonYAML))
// make sure we properly separated manifests
addonYAMLStr := string(addonYAML)
if !strings.HasPrefix(addonYAMLStr, "---") {
addonYAML = []byte(fmt.Sprintf("%s\n%s", "---", addonYAMLStr))
}
if err := validateUserAddonYAML(addonYAML); err != nil {
return err
}
manifests = append(manifests, addonYAML...)
} else {
log.Warnf(ctx, "[addons] Unable to determine if %s is a file path or url, skipping", addon)
}
}
log.Infof(ctx, "[addons] Deploying %s", UserAddonsIncludeResourceName)
logrus.Debugf("[addons] Compiled addons yaml: %s", string(manifests))
return c.doAddonDeploy(ctx, string(manifests), UserAddonsIncludeResourceName)
}
func validateUserAddonYAML(addon []byte) error {
yamlContents := make(map[string]interface{})
return yaml.Unmarshal(addon, &yamlContents)
}
func isFilePath(addonPath string) bool {
if _, err := os.Stat(addonPath); os.IsNotExist(err) {
return false
}
return true
}
func getAddonFromURL(yamlURL string) ([]byte, error) {
resp, err := http.Get(yamlURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
addonYaml, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return addonYaml, nil
}
func (c *Cluster) deployKubeDNS(ctx context.Context) error {
log.Infof(ctx, "[addons] Setting up KubeDNS")
kubeDNSConfig := map[string]string{
addons.KubeDNSServer: c.ClusterDNSServer,
addons.KubeDNSClusterDomain: c.ClusterDomain,
addons.KubeDNSImage: c.SystemImages.KubeDNS,
addons.DNSMasqImage: c.SystemImages.DNSmasq,
addons.KubeDNSSidecarImage: c.SystemImages.KubeDNSSidecar,
addons.KubeDNSAutoScalerImage: c.SystemImages.KubeDNSAutoscaler,
}
kubeDNSYaml, err := addons.GetKubeDNSManifest(kubeDNSConfig)
if err != nil {
return err
}
if err := c.doAddonDeploy(ctx, kubeDNSYaml, KubeDNSAddonResourceName); err != nil {
return err
}
log.Infof(ctx, "[addons] KubeDNS deployed successfully..")
return nil
}
func (c *Cluster) deployWithKubectl(ctx context.Context, addonYaml string) error {
buf := bytes.NewBufferString(addonYaml)
cmd := exec.Command("kubectl", "--kubeconfig", c.LocalKubeConfigPath, "apply", "-f", "-")
cmd.Stdin = buf
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (c *Cluster) doAddonDeploy(ctx context.Context, addonYaml, resourceName string) error {
if c.UseKubectlDeploy {
return c.deployWithKubectl(ctx, addonYaml)
}
err := c.StoreAddonConfigMap(ctx, addonYaml, resourceName)
if err != nil {
return fmt.Errorf("Failed to save addon ConfigMap: %v", err)
}
log.Infof(ctx, "[addons] Executing deploy job..")
k8sClient, err := k8s.NewClient(c.LocalKubeConfigPath, c.K8sWrapTransport)
if err != nil {
return err
}
node, err := k8s.GetNode(k8sClient, c.ControlPlaneHosts[0].HostnameOverride)
if err != nil {
return fmt.Errorf("Failed to get Node [%s]: %v", c.ControlPlaneHosts[0].HostnameOverride, err)
}
addonJob, err := addons.GetAddonsExcuteJob(resourceName, node.Name, c.Services.KubeAPI.Image)
if err != nil {
return fmt.Errorf("Failed to deploy addon execute job: %v", err)
}
err = c.ApplySystemAddonExcuteJob(addonJob)
if err != nil {
return fmt.Errorf("Failed to deploy addon execute job: %v", err)
}
return nil
}
func (c *Cluster) StoreAddonConfigMap(ctx context.Context, addonYaml string, addonName string) error {
log.Infof(ctx, "[addons] Saving addon ConfigMap to Kubernetes")
kubeClient, err := k8s.NewClient(c.LocalKubeConfigPath, c.K8sWrapTransport)
if err != nil {
return err
}
timeout := make(chan bool, 1)
go func() {
for {
err := k8s.UpdateConfigMap(kubeClient, []byte(addonYaml), addonName)
if err != nil {
time.Sleep(time.Second * 5)
fmt.Println(err)
continue
}
log.Infof(ctx, "[addons] Successfully Saved addon to Kubernetes ConfigMap: %s", addonName)
timeout <- true
break
}
}()
select {
case <-timeout:
return nil
case <-time.After(time.Second * UpdateStateTimeout):
return fmt.Errorf("[addons] Timeout waiting for kubernetes to be ready")
}
}
func (c *Cluster) ApplySystemAddonExcuteJob(addonJob string) error {
if err := k8s.ApplyK8sSystemJob(addonJob, c.LocalKubeConfigPath, c.K8sWrapTransport); err != nil {
logrus.Error(err)
return err
}
return nil
}
func (c *Cluster) deployIngress(ctx context.Context) error {
if c.Ingress.Provider == "none" {
log.Infof(ctx, "[ingress] ingress controller is not defined, skipping ingress controller")
return nil
}
log.Infof(ctx, "[ingress] Setting up %s ingress controller", c.Ingress.Provider)
ingressConfig := ingressOptions{
RBACConfig: c.Authorization.Mode,
Options: c.Ingress.Options,
NodeSelector: c.Ingress.NodeSelector,
AlpineImage: c.SystemImages.Alpine,
IngressImage: c.SystemImages.Ingress,
IngressBackend: c.SystemImages.IngressBackend,
}
// Currently only deploying nginx ingress controller
ingressYaml, err := addons.GetNginxIngressManifest(ingressConfig)
if err != nil {
return err
}
if err := c.doAddonDeploy(ctx, ingressYaml, IngressAddonResourceName); err != nil {
return err
}
log.Infof(ctx, "[ingress] ingress controller %s is successfully deployed", c.Ingress.Provider)
return nil
}