1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-12 13:18:47 +00:00

Refactor cloud provider support

This commit is contained in:
galal-hussein
2018-06-06 19:11:47 +02:00
committed by Alena Prokharchyk
parent 676189a60d
commit 2142661ea7
10 changed files with 323 additions and 55 deletions

View File

@@ -2,13 +2,12 @@ package cluster
import (
"context"
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"github.com/rancher/rke/authz"
"github.com/rancher/rke/cloudprovider"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/k8s"
@@ -59,8 +58,6 @@ const (
LocalNodeHostname = "localhost"
LocalNodeUser = "root"
CloudProvider = "CloudProvider"
AzureCloudProvider = "azure"
AWSCloudProvider = "aws"
ControlPlane = "controlPlane"
WorkerPlane = "workerPlan"
EtcdPlane = "etcd"
@@ -180,11 +177,22 @@ func ParseCluster(
}
c.PrivateRegistriesMap[pr.URL] = pr
}
// parse the cluster config file
c.CloudConfigFile, err = c.parseCloudConfig(ctx)
// Get Cloud Provider
p, err := cloudprovider.InitCloudProvider(c.CloudProvider)
if err != nil {
return nil, fmt.Errorf("Failed to parse cloud config file: %v", err)
return nil, fmt.Errorf("Failed to initialize cloud provider: %v", err)
}
if p != nil {
c.CloudConfigFile, err = p.GenerateCloudConfigFile()
if err != nil {
return nil, fmt.Errorf("Failed to parse cloud config file: %v", err)
}
c.CloudProvider.Name = p.GetName()
if c.CloudProvider.Name == "" {
return nil, fmt.Errorf("Name of the cloud provider is not defined for custom provider")
}
}
// Create k8s wrap transport for bastion host
if len(c.BastionHost.Address) > 0 {
c.K8sWrapTransport = hosts.BastionHostWrapTransport(c.BastionHost)
@@ -371,47 +379,3 @@ func ConfigureCluster(
}
return nil
}
func (c *Cluster) parseCloudConfig(ctx context.Context) (string, error) {
// check for azure cloud provider
if c.CloudProvider.AzureCloudProvider != nil {
c.CloudProvider.Name = AzureCloudProvider
jsonString, err := json.MarshalIndent(c.CloudProvider.AzureCloudProvider, "", "\n")
if err != nil {
return "", err
}
return string(jsonString), nil
}
if c.CloudProvider.AWSCloudProvider != nil {
c.CloudProvider.Name = AWSCloudProvider
return "", nil
}
if len(c.CloudProvider.CloudConfig) == 0 {
return "", nil
}
// handle generic cloud config
tmpMap := make(map[string]interface{})
for key, value := range c.CloudProvider.CloudConfig {
tmpBool, err := strconv.ParseBool(value)
if err == nil {
tmpMap[key] = tmpBool
continue
}
tmpInt, err := strconv.ParseInt(value, 10, 64)
if err == nil {
tmpMap[key] = tmpInt
continue
}
tmpFloat, err := strconv.ParseFloat(value, 64)
if err == nil {
tmpMap[key] = tmpFloat
continue
}
tmpMap[key] = value
}
jsonString, err := json.MarshalIndent(tmpMap, "", "\n")
if err != nil {
return "", err
}
return string(jsonString), nil
}