2017-10-31 13:55:35 +00:00
|
|
|
package pki
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2017-11-01 21:46:43 +00:00
|
|
|
"io/ioutil"
|
2017-10-31 13:55:35 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"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/hosts"
|
|
|
|
"k8s.io/client-go/util/cert"
|
|
|
|
)
|
|
|
|
|
2017-11-08 17:45:51 +00:00
|
|
|
func ConvertCrtToENV(name string, certificate *x509.Certificate) string {
|
2017-10-31 13:55:35 +00:00
|
|
|
encodedCrt := cert.EncodeCertPEM(certificate)
|
2017-11-02 10:07:10 +00:00
|
|
|
return fmt.Sprintf("%s=%s", name, string(encodedCrt))
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 17:45:51 +00:00
|
|
|
func ConvertKeyToENV(name string, key *rsa.PrivateKey) string {
|
2017-10-31 13:55:35 +00:00
|
|
|
encodedKey := cert.EncodePrivateKeyPEM(key)
|
2017-11-02 10:07:10 +00:00
|
|
|
return fmt.Sprintf("%s=%s", name, string(encodedKey))
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 17:45:51 +00:00
|
|
|
func ConvertConfigToENV(name string, config string) string {
|
2017-11-02 10:07:10 +00:00
|
|
|
return fmt.Sprintf("%s=%s", name, config)
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 10:07:10 +00:00
|
|
|
func DeployCertificatesOnMasters(cpHosts []hosts.Host, crtMap map[string]CertificatePKI) error {
|
2017-10-31 13:55:35 +00:00
|
|
|
env := []string{
|
2017-11-08 17:45:51 +00:00
|
|
|
ConvertCrtToENV(CACertENVName, crtMap[CACertName].Certificate),
|
|
|
|
ConvertKeyToENV(CAKeyENVName, crtMap[CACertName].Key),
|
|
|
|
ConvertCrtToENV(KubeAPICertENVName, crtMap[KubeAPICertName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeAPIKeyENVName, crtMap[KubeAPICertName].Key),
|
|
|
|
ConvertCrtToENV(KubeControllerCertENVName, crtMap[KubeControllerName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeControllerKeyENVName, crtMap[KubeControllerName].Key),
|
|
|
|
ConvertConfigToENV(KubeControllerConfigENVName, crtMap[KubeControllerName].Config),
|
|
|
|
ConvertCrtToENV(KubeSchedulerCertENVName, crtMap[KubeSchedulerName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeSchedulerKeyENVName, crtMap[KubeSchedulerName].Key),
|
|
|
|
ConvertConfigToENV(KubeSchedulerConfigENVName, crtMap[KubeSchedulerName].Config),
|
|
|
|
ConvertCrtToENV(KubeProxyCertENVName, crtMap[KubeProxyName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeProxyKeyENVName, crtMap[KubeProxyName].Key),
|
|
|
|
ConvertConfigToENV(KubeProxyConfigENVName, crtMap[KubeProxyName].Config),
|
|
|
|
ConvertCrtToENV(KubeNodeCertENVName, crtMap[KubeNodeName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeNodeKeyENVName, crtMap[KubeNodeName].Key),
|
|
|
|
ConvertConfigToENV(KubeNodeConfigENVName, crtMap[KubeNodeName].Config),
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
for i := range cpHosts {
|
|
|
|
err := doRunDeployer(&cpHosts[i], env)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-02 10:07:10 +00:00
|
|
|
func DeployCertificatesOnWorkers(workerHosts []hosts.Host, crtMap map[string]CertificatePKI) error {
|
2017-10-31 13:55:35 +00:00
|
|
|
env := []string{
|
2017-11-08 17:45:51 +00:00
|
|
|
ConvertCrtToENV(CACertENVName, crtMap[CACertName].Certificate),
|
|
|
|
ConvertCrtToENV(KubeProxyCertENVName, crtMap[KubeProxyName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeProxyKeyENVName, crtMap[KubeProxyName].Key),
|
|
|
|
ConvertConfigToENV(KubeProxyConfigENVName, crtMap[KubeProxyName].Config),
|
|
|
|
ConvertCrtToENV(KubeNodeCertENVName, crtMap[KubeNodeName].Certificate),
|
|
|
|
ConvertKeyToENV(KubeNodeKeyENVName, crtMap[KubeNodeName].Key),
|
|
|
|
ConvertConfigToENV(KubeNodeConfigENVName, crtMap[KubeNodeName].Config),
|
2017-10-31 13:55:35 +00:00
|
|
|
}
|
2017-11-02 10:07:10 +00:00
|
|
|
for i := range workerHosts {
|
|
|
|
err := doRunDeployer(&workerHosts[i], env)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func doRunDeployer(host *hosts.Host, containerEnv []string) error {
|
|
|
|
logrus.Debugf("[certificates] Pulling Certificate downloader Image on host [%s]", host.Hostname)
|
2017-11-02 10:07:10 +00:00
|
|
|
err := docker.PullImage(host.DClient, host.Hostname, CrtDownloaderImage)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
imageCfg := &container.Config{
|
|
|
|
Image: CrtDownloaderImage,
|
|
|
|
Env: containerEnv,
|
|
|
|
}
|
|
|
|
hostCfg := &container.HostConfig{
|
|
|
|
Binds: []string{
|
|
|
|
"/etc/kubernetes:/etc/kubernetes",
|
|
|
|
},
|
|
|
|
Privileged: true,
|
|
|
|
RestartPolicy: container.RestartPolicy{Name: "never"},
|
|
|
|
}
|
|
|
|
resp, err := host.DClient.ContainerCreate(context.Background(), imageCfg, hostCfg, nil, CrtDownloaderContainer)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create Certificates deployer container on host [%s]: %v", host.Hostname, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := host.DClient.ContainerStart(context.Background(), resp.ID, types.ContainerStartOptions{}); err != nil {
|
|
|
|
return fmt.Errorf("Failed to start Certificates deployer container on host [%s]: %v", host.Hostname, err)
|
|
|
|
}
|
|
|
|
logrus.Debugf("[certificates] Successfully started Certificate deployer container: %s", resp.ID)
|
|
|
|
for {
|
2017-11-02 10:07:10 +00:00
|
|
|
isDeployerRunning, err := docker.IsContainerRunning(host.DClient, host.Hostname, CrtDownloaderContainer)
|
2017-10-31 13:55:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if isDeployerRunning {
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := host.DClient.ContainerRemove(context.Background(), resp.ID, types.ContainerRemoveOptions{}); err != nil {
|
|
|
|
return fmt.Errorf("Failed to delete Certificates deployer container on host[%s]: %v", host.Hostname, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-11-01 21:46:43 +00:00
|
|
|
|
2017-11-02 10:07:10 +00:00
|
|
|
func DeployAdminConfig(kubeConfig string) error {
|
2017-11-01 21:46:43 +00:00
|
|
|
logrus.Debugf("Deploying admin Kubeconfig locally: %s", kubeConfig)
|
2017-11-02 10:07:10 +00:00
|
|
|
err := ioutil.WriteFile(KubeAdminConfigPath, []byte(kubeConfig), 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to create local admin kubeconfig file: %v", err)
|
2017-11-01 21:46:43 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|