Przeglądaj źródła

Add ability to configure cloud provider type from env vars (#3236)

Nishanth Reddy 9 miesięcy temu
rodzic
commit
f607fd5532
2 zmienionych plików z 34 dodań i 0 usunięć
  1. 26 0
      pkg/cloud/provider/provider.go
  2. 8 0
      pkg/env/costmodelenv.go

+ 26 - 0
pkg/cloud/provider/provider.go

@@ -175,6 +175,32 @@ func NewProvider(cache clustercache.ClusterCache, apiKey string, config *config.
 	}
 
 	cp := getClusterProperties(nodes[0])
+
+	// If provider is DEFAULT, check for explicitly set cloud provider from environment variable
+	envProvider := env.GetCloudProvider()
+	if cp.provider == "DEFAULT" && envProvider != "" {
+		log.Infof("Using cloud provider from environment variable: %s", envProvider)
+		cp.provider = envProvider
+		switch envProvider {
+		case opencost.AWSProvider:
+			cp.configFileName = "aws.json"
+		case opencost.AzureProvider:
+			cp.configFileName = "azure.json"
+		case opencost.GCPProvider:
+			cp.configFileName = "gcp.json"
+		case opencost.AlibabaProvider:
+			cp.configFileName = "alibaba.json"
+		case opencost.OracleProvider:
+			cp.configFileName = "oracle.json"
+		case opencost.ScalewayProvider:
+			cp.configFileName = "scaleway.json"
+		case opencost.OTCProvider:
+			cp.configFileName = "otc.json"
+		case opencost.CSVProvider:
+			cp.configFileName = "default.json"
+		}
+	}
+
 	providerConfig := NewProviderConfig(config, cp.configFileName)
 	// If ClusterAccount is set apply it to the cluster properties
 	if providerConfig.customPricing != nil && providerConfig.customPricing.ClusterAccountID != "" {

+ 8 - 0
pkg/env/costmodelenv.go

@@ -122,6 +122,9 @@ const (
 	// Deprecated
 	KubecostNamespaceEnvVar    = "KUBECOST_NAMESPACE"
 	KubecostConfigBucketEnvVar = "KUBECOST_CONFIG_BUCKET"
+
+	// Cloud provider override
+	CloudProviderVar = "CLOUD_PROVIDER"
 )
 
 const DefaultConfigMountPath = "/var/configs"
@@ -551,3 +554,8 @@ func GetNodeStatsCertFile() string {
 func GetNodeStatsKeyFile() string {
 	return env.Get(NodeStatsKeyFileEnvVar, "")
 }
+
+// GetCloudProvider returns the explicitly set cloud provider from environment variable
+func GetCloudProvider() string {
+	return env.Get(CloudProviderVar, "")
+}