2017-11-06 20:50:41 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-11-06 20:50:41 +00:00
|
|
|
"fmt"
|
2017-11-18 12:51:28 +00:00
|
|
|
"time"
|
2017-11-06 20:50:41 +00:00
|
|
|
|
2017-11-18 12:51:28 +00:00
|
|
|
"github.com/rancher/rke/addons"
|
|
|
|
"github.com/rancher/rke/k8s"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-11-06 20:50:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-11-18 12:51:28 +00:00
|
|
|
KubeDNSAddonResourceName = "rke-kubedns-addon"
|
|
|
|
UserAddonResourceName = "rke-user-addon"
|
2018-02-01 21:28:31 +00:00
|
|
|
IngressAddonResourceName = "rke-ingress-controller"
|
2017-11-06 20:50:41 +00:00
|
|
|
)
|
|
|
|
|
2018-02-01 21:28:31 +00:00
|
|
|
type ingressOptions struct {
|
2018-02-15 00:28:35 +00:00
|
|
|
RBACConfig string
|
|
|
|
Options map[string]string
|
|
|
|
NodeSelector map[string]string
|
|
|
|
AlpineImage string
|
|
|
|
IngressImage string
|
|
|
|
IngressBackend string
|
2018-02-01 21:28:31 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 03:13:06 +00:00
|
|
|
func (c *Cluster) deployK8sAddOns(ctx context.Context) error {
|
2018-02-01 21:28:31 +00:00
|
|
|
if err := c.deployKubeDNS(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.deployIngress(ctx)
|
2017-11-06 20:50:41 +00:00
|
|
|
}
|
|
|
|
|
2018-02-08 03:13:06 +00:00
|
|
|
func (c *Cluster) deployUserAddOns(ctx context.Context) error {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[addons] Setting up user addons..")
|
2017-11-18 12:51:28 +00:00
|
|
|
if c.Addons == "" {
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[addons] No user addons configured..")
|
2017-11-18 12:51:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := c.doAddonDeploy(ctx, c.Addons, UserAddonResourceName); err != nil {
|
2017-11-18 12:51:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[addons] User addon deployed successfully..")
|
2017-11-18 12:51:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (c *Cluster) deployKubeDNS(ctx context.Context) error {
|
|
|
|
log.Infof(ctx, "[addons] Setting up KubeDNS")
|
2017-12-05 01:29:29 +00:00
|
|
|
kubeDNSConfig := map[string]string{
|
|
|
|
addons.KubeDNSServer: c.ClusterDNSServer,
|
|
|
|
addons.KubeDNSClusterDomain: c.ClusterDomain,
|
2018-01-30 12:32:50 +00:00
|
|
|
addons.KubeDNSImage: c.SystemImages.KubeDNS,
|
|
|
|
addons.DNSMasqImage: c.SystemImages.DNSmasq,
|
|
|
|
addons.KubeDNSSidecarImage: c.SystemImages.KubeDNSSidecar,
|
|
|
|
addons.KubeDNSAutoScalerImage: c.SystemImages.KubeDNSAutoscaler,
|
2017-12-05 01:29:29 +00:00
|
|
|
}
|
2017-12-16 03:37:45 +00:00
|
|
|
kubeDNSYaml, err := addons.GetKubeDNSManifest(kubeDNSConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
if err := c.doAddonDeploy(ctx, kubeDNSYaml, KubeDNSAddonResourceName); err != nil {
|
2017-11-18 12:51:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[addons] KubeDNS deployed successfully..")
|
2017-11-18 12:51:28 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
2017-11-06 20:50:41 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (c *Cluster) doAddonDeploy(ctx context.Context, addonYaml, resourceName string) error {
|
2017-11-18 12:51:28 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
err := c.StoreAddonConfigMap(ctx, addonYaml, resourceName)
|
2017-11-18 12:51:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to save addon ConfigMap: %v", err)
|
2017-11-06 20:50:41 +00:00
|
|
|
}
|
2017-11-18 12:51:28 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[addons] Executing deploy job..")
|
2017-11-18 12:51:28 +00:00
|
|
|
|
2017-12-16 03:37:45 +00:00
|
|
|
addonJob, err := addons.GetAddonsExcuteJob(resourceName, c.ControlPlaneHosts[0].HostnameOverride, c.Services.KubeAPI.Image)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to deploy addon execute job: %v", err)
|
|
|
|
}
|
2017-11-18 12:51:28 +00:00
|
|
|
err = c.ApplySystemAddonExcuteJob(addonJob)
|
2017-11-06 20:50:41 +00:00
|
|
|
if err != nil {
|
2017-11-18 12:51:28 +00:00
|
|
|
return fmt.Errorf("Failed to deploy addon execute job: %v", err)
|
2017-11-06 20:50:41 +00:00
|
|
|
}
|
|
|
|
return nil
|
2017-11-18 12:51:28 +00:00
|
|
|
}
|
2017-11-06 20:50:41 +00:00
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
func (c *Cluster) StoreAddonConfigMap(ctx context.Context, addonYaml string, addonName string) error {
|
|
|
|
log.Infof(ctx, "[addons] Saving addon ConfigMap to Kubernetes")
|
2018-02-20 11:51:57 +00:00
|
|
|
kubeClient, err := k8s.NewClient(c.LocalKubeConfigPath, c.K8sWrapTransport)
|
2017-11-18 12:51:28 +00:00
|
|
|
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
|
|
|
|
}
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "[addons] Successfully Saved addon to Kubernetes ConfigMap: %s", addonName)
|
2017-11-18 12:51:28 +00:00
|
|
|
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 {
|
|
|
|
|
2018-02-20 11:51:57 +00:00
|
|
|
if err := k8s.ApplyK8sSystemJob(addonJob, c.LocalKubeConfigPath, c.K8sWrapTransport); err != nil {
|
2017-11-18 12:51:28 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2017-11-06 20:50:41 +00:00
|
|
|
}
|
2018-02-01 21:28:31 +00:00
|
|
|
|
|
|
|
func (c *Cluster) deployIngress(ctx context.Context) error {
|
2018-02-07 00:30:25 +00:00
|
|
|
log.Infof(ctx, "[ingress] Setting up %s ingress controller", c.Ingress.Provider)
|
|
|
|
if c.Ingress.Provider == "none" {
|
2018-02-01 21:28:31 +00:00
|
|
|
log.Infof(ctx, "[ingress] ingress controller is not defined")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ingressConfig := ingressOptions{
|
2018-02-15 00:28:35 +00:00
|
|
|
RBACConfig: c.Authorization.Mode,
|
|
|
|
Options: c.Ingress.Options,
|
|
|
|
NodeSelector: c.Ingress.NodeSelector,
|
|
|
|
AlpineImage: c.SystemImages.Alpine,
|
|
|
|
IngressImage: c.SystemImages.Ingress,
|
|
|
|
IngressBackend: c.SystemImages.IngressBackend,
|
2018-02-01 21:28:31 +00:00
|
|
|
}
|
|
|
|
// 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
|
|
|
|
}
|
2018-02-07 00:30:25 +00:00
|
|
|
log.Infof(ctx, "[ingress] ingress controller %s is successfully deployed", c.Ingress.Provider)
|
2018-02-01 21:28:31 +00:00
|
|
|
return nil
|
|
|
|
}
|