1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-17 15:40:07 +00:00

Add ClusterName support for kube config

This commit is contained in:
David Lewis
2018-03-13 17:18:07 -07:00
parent 4eb733b2b7
commit 34c41c1b05
6 changed files with 20 additions and 10 deletions

View File

@@ -132,6 +132,10 @@ ignore_docker_version: false
kubernetes_version: v1.8.9-rancher1-1 kubernetes_version: v1.8.9-rancher1-1
# If set, this is the cluster name that will be used in the kube config file
# Default value is "local"
cluster_name: mycluster
# List of registry credentials, if you are using a Docker Hub registry, # List of registry credentials, if you are using a Docker Hub registry,
# you can omit the `url` or set it to `docker.io` # you can omit the `url` or set it to `docker.io`
private_registries: private_registries:

View File

@@ -180,13 +180,13 @@ func rebuildLocalAdminConfig(ctx context.Context, kubeCluster *Cluster) error {
for _, cpHost := range kubeCluster.ControlPlaneHosts { for _, cpHost := range kubeCluster.ControlPlaneHosts {
if (currentKubeConfig == pki.CertificatePKI{}) { if (currentKubeConfig == pki.CertificatePKI{}) {
kubeCluster.Certificates = make(map[string]pki.CertificatePKI) kubeCluster.Certificates = make(map[string]pki.CertificatePKI)
newConfig = getLocalAdminConfigWithNewAddress(kubeCluster.LocalKubeConfigPath, cpHost.Address) newConfig = getLocalAdminConfigWithNewAddress(kubeCluster.LocalKubeConfigPath, cpHost.Address, kubeCluster.ClusterName)
} else { } else {
kubeURL := fmt.Sprintf("https://%s:6443", cpHost.Address) kubeURL := fmt.Sprintf("https://%s:6443", cpHost.Address)
caData := string(cert.EncodeCertPEM(caCrt)) caData := string(cert.EncodeCertPEM(caCrt))
crtData := string(cert.EncodeCertPEM(currentKubeConfig.Certificate)) crtData := string(cert.EncodeCertPEM(currentKubeConfig.Certificate))
keyData := string(cert.EncodePrivateKeyPEM(currentKubeConfig.Key)) keyData := string(cert.EncodePrivateKeyPEM(currentKubeConfig.Key))
newConfig = pki.GetKubeConfigX509WithData(kubeURL, pki.KubeAdminCertName, caData, crtData, keyData) newConfig = pki.GetKubeConfigX509WithData(kubeURL, kubeCluster.ClusterName, pki.KubeAdminCertName, caData, crtData, keyData)
} }
if err := pki.DeployAdminConfig(ctx, newConfig, kubeCluster.LocalKubeConfigPath); err != nil { if err := pki.DeployAdminConfig(ctx, newConfig, kubeCluster.LocalKubeConfigPath); err != nil {
return fmt.Errorf("Failed to redeploy local admin config with new host") return fmt.Errorf("Failed to redeploy local admin config with new host")
@@ -220,7 +220,7 @@ func getLocalConfigAddress(localConfigPath string) (string, error) {
return address[2:], nil return address[2:], nil
} }
func getLocalAdminConfigWithNewAddress(localConfigPath, cpAddress string) string { func getLocalAdminConfigWithNewAddress(localConfigPath, cpAddress string, clusterName string) string {
config, _ := clientcmd.BuildConfigFromFlags("", localConfigPath) config, _ := clientcmd.BuildConfigFromFlags("", localConfigPath)
if config == nil { if config == nil {
return "" return ""
@@ -228,6 +228,7 @@ func getLocalAdminConfigWithNewAddress(localConfigPath, cpAddress string) string
config.Host = fmt.Sprintf("https://%s:6443", cpAddress) config.Host = fmt.Sprintf("https://%s:6443", cpAddress)
return pki.GetKubeConfigX509WithData( return pki.GetKubeConfigX509WithData(
"https://"+cpAddress+":6443", "https://"+cpAddress+":6443",
clusterName,
pki.KubeAdminCertName, pki.KubeAdminCertName,
string(config.CAData), string(config.CAData),
string(config.CertData), string(config.CertData),

View File

@@ -14,6 +14,7 @@ const (
DefaultClusterCIDR = "10.233.64.0/18" DefaultClusterCIDR = "10.233.64.0/18"
DefaultClusterDNSService = "10.233.0.3" DefaultClusterDNSService = "10.233.0.3"
DefaultClusterDomain = "cluster.local" DefaultClusterDomain = "cluster.local"
DefaultClusterName = "local"
DefaultClusterSSHKeyPath = "~/.ssh/id_rsa" DefaultClusterSSHKeyPath = "~/.ssh/id_rsa"
DefaultK8sVersion = v3.K8sV18 DefaultK8sVersion = v3.K8sV18
@@ -76,6 +77,9 @@ func (c *Cluster) setClusterDefaults(ctx context.Context) {
if len(c.Ingress.Provider) == 0 { if len(c.Ingress.Provider) == 0 {
c.Ingress.Provider = DefaultIngressController c.Ingress.Provider = DefaultIngressController
} }
if len(c.ClusterName) == 0 {
c.ClusterName = DefaultClusterName
}
c.setClusterImageDefaults() c.setClusterImageDefaults()
c.setClusterKubernetesImageVersion(ctx) c.setClusterKubernetesImageVersion(ctx)

View File

@@ -2,7 +2,7 @@ package pki
import "encoding/base64" import "encoding/base64"
func getKubeConfigX509(kubernetesURL string, componentName string, caPath string, crtPath string, keyPath string) string { func getKubeConfigX509(kubernetesURL string, clusterName string, componentName string, caPath string, crtPath string, keyPath string) string {
return `apiVersion: v1 return `apiVersion: v1
kind: Config kind: Config
clusters: clusters:
@@ -10,10 +10,10 @@ clusters:
api-version: v1 api-version: v1
certificate-authority: ` + caPath + ` certificate-authority: ` + caPath + `
server: "` + kubernetesURL + `" server: "` + kubernetesURL + `"
name: "local" name: "` + clusterName + `"
contexts: contexts:
- context: - context:
cluster: "local" cluster: "` + clusterName + `"
user: "` + componentName + `" user: "` + componentName + `"
name: "Default" name: "Default"
current-context: "Default" current-context: "Default"
@@ -24,7 +24,7 @@ users:
client-key: ` + keyPath + `` client-key: ` + keyPath + ``
} }
func GetKubeConfigX509WithData(kubernetesURL string, componentName string, cacrt string, crt string, key string) string { func GetKubeConfigX509WithData(kubernetesURL string, clusterName string, componentName string, cacrt string, crt string, key string) string {
return `apiVersion: v1 return `apiVersion: v1
kind: Config kind: Config
clusters: clusters:
@@ -32,10 +32,10 @@ clusters:
api-version: v1 api-version: v1
certificate-authority-data: ` + base64.StdEncoding.EncodeToString([]byte(cacrt)) + ` certificate-authority-data: ` + base64.StdEncoding.EncodeToString([]byte(cacrt)) + `
server: "` + kubernetesURL + `" server: "` + kubernetesURL + `"
name: "local" name: "` + clusterName + `"
contexts: contexts:
- context: - context:
cluster: "local" cluster: "` + clusterName + `"
user: "` + componentName + `" user: "` + componentName + `"
name: "Default" name: "Default"
current-context: "Default" current-context: "Default"

View File

@@ -105,6 +105,7 @@ func GenerateRKECerts(ctx context.Context, rkeConfig v3.RancherKubernetesEngineC
if len(cpHosts) > 0 { if len(cpHosts) > 0 {
kubeAdminConfig := GetKubeConfigX509WithData( kubeAdminConfig := GetKubeConfigX509WithData(
"https://"+cpHosts[0].Address+":6443", "https://"+cpHosts[0].Address+":6443",
rkeConfig.ClusterName,
KubeAdminCertName, KubeAdminCertName,
string(cert.EncodeCertPEM(caCrt)), string(cert.EncodeCertPEM(caCrt)),
string(cert.EncodeCertPEM(kubeAdminCrt)), string(cert.EncodeCertPEM(kubeAdminCrt)),

View File

@@ -188,7 +188,7 @@ func ToCertObject(componentName, commonName, ouName string, cert *x509.Certifica
keyPath := GetKeyPath(componentName) keyPath := GetKeyPath(componentName)
if componentName != CACertName && componentName != KubeAPICertName && !strings.Contains(componentName, EtcdCertName) { if componentName != CACertName && componentName != KubeAPICertName && !strings.Contains(componentName, EtcdCertName) {
config = getKubeConfigX509("https://127.0.0.1:6443", componentName, caCertPath, path, keyPath) config = getKubeConfigX509("https://127.0.0.1:6443", "local", componentName, caCertPath, path, keyPath)
configPath = GetConfigPath(componentName) configPath = GetConfigPath(componentName)
configEnvName = getConfigEnvFromEnv(envName) configEnvName = getConfigEnvFromEnv(envName)
} }