1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-01 23:16:22 +00:00

Refactor kubectl

Export pki utlitity functions
This commit is contained in:
moelsayed
2017-11-08 19:45:51 +02:00
parent 7172f6ec46
commit d58fc84de9
5 changed files with 112 additions and 100 deletions

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/pki"
)
const (
@@ -20,17 +18,12 @@ func (c *Cluster) DeployK8sAddOns() error {
func (c *Cluster) deployKubeDNS() error {
logrus.Infof("[plugins] Setting up KubeDNS")
deployerHost := c.ControlPlaneHosts[0]
kubectlCmd := []string{"apply -f /addons/kubedns*.yaml"}
env := []string{
fmt.Sprintf("%s=%s", pki.KubeAdminConfigENVName, c.Certificates[pki.KubeAdminCommonName].Config),
fmt.Sprintf("%s=%s", ClusterDNSServerIPEnvName, c.ClusterDNSServer),
fmt.Sprintf("%s=%s", ClusterDomainEnvName, c.ClusterDomain),
kubectlCmd := &KubectlCommand{
Cmd: []string{"apply -f /addons/kubedns*.yaml"},
}
logrus.Infof("[plugins] Executing the deploy command..")
err := k8s.RunKubectlCmd(deployerHost.DClient, deployerHost.Hostname, kubectlCmd, env)
err := c.RunKubectlCmd(kubectlCmd)
if err != nil {
return fmt.Errorf("Failed to run kubectl command: %v", err)
}

79
cluster/kubectl.go Normal file
View File

@@ -0,0 +1,79 @@
package cluster
import (
"context"
"fmt"
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/pki"
)
const (
KubectlImage = "melsayed/kubectl:latest"
KubctlContainer = "kubectl"
)
type KubectlCommand struct {
Cmd []string
Env []string
}
func (c *Cluster) buildClusterConfigEnv() []string {
// This needs to be updated when add more configuration
return []string{
pki.ConvertConfigToENV(pki.KubeAdminConfigENVName, c.Certificates[pki.KubeAdminCommonName].Config),
pki.ConvertConfigToENV(ClusterCIDREnvName, c.ClusterCIDR),
pki.ConvertConfigToENV(ClusterDNSServerIPEnvName, c.ClusterDNSServer),
pki.ConvertConfigToENV(ClusterDomainEnvName, c.ClusterDomain),
}
}
func (c *Cluster) RunKubectlCmd(kubectlCmd *KubectlCommand) error {
h := c.ControlPlaneHosts[0]
logrus.Debugf("[kubectl] Using host [%s] for deployment", h.Hostname)
logrus.Debugf("[kubectl] Pulling kubectl image..")
if err := docker.PullImage(h.DClient, h.Hostname, KubectlImage); err != nil {
return err
}
clusterConfigEnv := c.buildClusterConfigEnv()
if kubectlCmd.Env != nil {
clusterConfigEnv = append(clusterConfigEnv, kubectlCmd.Env...)
}
imageCfg := &container.Config{
Image: KubectlImage,
Env: clusterConfigEnv,
Cmd: kubectlCmd.Cmd,
}
logrus.Debugf("[kubectl] Creating kubectl container..")
resp, err := h.DClient.ContainerCreate(context.Background(), imageCfg, nil, nil, KubctlContainer)
if err != nil {
return fmt.Errorf("Failed to create kubectl container on host [%s]: %v", h.Hostname, err)
}
logrus.Debugf("[kubectl] Container %s created..", resp.ID)
if err := h.DClient.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
return fmt.Errorf("Failed to start kubectl container on host [%s]: %v", h.Hostname, err)
}
logrus.Debugf("[kubectl] running command: %s", kubectlCmd.Cmd)
statusCh, errCh := h.DClient.ContainerWait(context.Background(), resp.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
return fmt.Errorf("Failed to execute kubectl container on host [%s]: %v", h.Hostname, err)
}
case status := <-statusCh:
if status.StatusCode != 0 {
return fmt.Errorf("kubectl command failed on host [%s]: exit status %v", h.Hostname, status.StatusCode)
}
}
if err := h.DClient.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{}); err != nil {
return fmt.Errorf("Failed to remove kubectl container on host[%s]: %v", h.Hostname, err)
}
return nil
}

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"github.com/Sirupsen/logrus"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/pki"
)
const (
@@ -14,14 +12,12 @@ const (
func (c *Cluster) DeployNetworkPlugin() error {
logrus.Infof("[network] Setting up network plugin: %s", c.NetworkPlugin)
deployerHost := c.ControlPlaneHosts[0]
kubectlCmd := []string{"apply -f /network/" + c.NetworkPlugin + ".yaml"}
env := []string{
fmt.Sprintf("%s=%s", pki.KubeAdminConfigENVName, c.Certificates[pki.KubeAdminCommonName].Config),
fmt.Sprintf("%s=%s", ClusterCIDREnvName, c.ClusterCIDR),
kubectlCmd := &KubectlCommand{
Cmd: []string{"apply -f /network/" + c.NetworkPlugin + ".yaml"},
}
logrus.Infof("[network] Executing the deploy command..")
err := k8s.RunKubectlCmd(deployerHost.DClient, deployerHost.Hostname, kubectlCmd, env)
err := c.RunKubectlCmd(kubectlCmd)
if err != nil {
return fmt.Errorf("Failed to run kubectl command: %v", err)
}