Răsfoiți Sursa

Read secret and save values to env variable

Sean Holcomb 5 ani în urmă
părinte
comite
13f4fea4f7
3 a modificat fișierele cu 89 adăugiri și 0 ștergeri
  1. 72 0
      pkg/cloud/azureprovider.go
  2. 1 0
      pkg/cloud/provider.go
  3. 16 0
      pkg/env/costmodelenv.go

+ 72 - 0
pkg/cloud/azureprovider.go

@@ -65,6 +65,8 @@ var (
 
 var loadedAzureSecret bool = false
 var azureSecret *AzureServiceKey = nil
+var loadedAzureStorageConfigSecret bool = false
+var azureStorageConfig *AzureStorageConfig= nil
 
 type regionParts []string
 
@@ -211,6 +213,13 @@ func (k *azureKey) ID() string {
 	return ""
 }
 
+// Represents an azure storage config
+type AzureStorageConfig struct {
+	AccountName string `json:"azureStorageAccount"`
+	AccessKey string `json:"azureStorageAccessKey"`
+	ContainerName string `json:"azureStorageContainer"`
+}
+
 // Represents an azure app key
 type AzureAppKey struct {
 	AppID       string `json:"appId"`
@@ -226,6 +235,7 @@ type AzureServiceKey struct {
 	ServiceKey     *AzureAppKey `json:"serviceKey"`
 }
 
+
 // Validity check on service key
 func (ask *AzureServiceKey) IsValid() bool {
 	return ask.SubscriptionID != "" &&
@@ -260,6 +270,39 @@ func (az *Azure) getAzureAuth(forceReload bool, cp *CustomPricing) (subscription
 	return "", "", "", ""
 }
 
+func (az *Azure) ConfigureAzureStorage() error {
+	accessKey, accountName, containerName := az.getAzureStorageConfig(false)
+	if accessKey != "" && accountName != "" || containerName != "" {
+		err := env.Set(env.AzureStorageAccessKeyEnvVar, accessKey)
+		if err != nil {
+			return err
+		}
+		err = env.Set(env.AzureStorageAccountNameEnvVar, accountName)
+		if err != nil {
+			return err
+		}
+		err = env.Set(env.AzureStorageContainerNameEnvVar, containerName)
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
+func (az *Azure) getAzureStorageConfig(forceReload bool) (accessKey, accountName, containerName string) {
+
+	// 1. Check for secret
+	s, _ := az.loadAzureStorageConfig(forceReload)
+	if s != nil && s.AccessKey != "" && s.AccountName != ""  && s.ContainerName != ""{
+		accessKey = s.AccessKey
+		accountName = s.AccountName
+		containerName = s.ContainerName
+		return
+	}
+
+	// 3. Fall back to env vars
+	return env.GetAzureStorageAccessKey(), env.GetAzureStorageAccountName(), env.GetAzureStorageContainerName()
+}
+
 // Load once and cache the result (even on failure). This is an install time secret, so
 // we don't expect the secret to change. If it does, however, we can force reload using
 // the input parameter.
@@ -289,6 +332,35 @@ func (az *Azure) loadAzureAuthSecret(force bool) (*AzureServiceKey, error) {
 	return azureSecret, nil
 }
 
+// Load once and cache the result (even on failure). This is an install time secret, so
+// we don't expect the secret to change. If it does, however, we can force reload using
+// the input parameter.
+func (az *Azure) loadAzureStorageConfig(force bool) (*AzureStorageConfig, error) {
+	if !force && loadedAzureStorageConfigSecret {
+		return azureStorageConfig, nil
+	}
+	loadedAzureSecret = true
+
+	exists, err := util.FileExists(storageConfigSecretPath)
+	if !exists || err != nil {
+		return nil, fmt.Errorf("Failed to locate azure storage config file: %s", storageConfigSecretPath)
+	}
+
+	result, err := ioutil.ReadFile(storageConfigSecretPath)
+	if err != nil {
+		return nil, err
+	}
+
+	var ask AzureStorageConfig
+	err = json.Unmarshal(result, &ask)
+	if err != nil {
+		return nil, err
+	}
+
+	azureStorageConfig = &ask
+	return azureStorageConfig, nil
+}
+
 func (az *Azure) GetKey(labels map[string]string, n *v1.Node) Key {
 	cfg, err := az.GetConfig()
 	if err != nil {

+ 1 - 0
pkg/cloud/provider.go

@@ -18,6 +18,7 @@ import (
 )
 
 const authSecretPath = "/var/secrets/service-key.json"
+const storageConfigSecretPath = "/var/secrets/azure-storage-config.json"
 
 var createTableStatements = []string{
 	`CREATE TABLE IF NOT EXISTS names (

+ 16 - 0
pkg/env/costmodelenv.go

@@ -15,6 +15,10 @@ const (
 	AWSAccessKeySecretEnvVar = "AWS_SECRET_ACCESS_KEY"
 	AWSClusterIDEnvVar       = "AWS_CLUSTER_ID"
 
+	AzureStorageAccessKeyEnvVar = "AZURE_STORAGE_ACCESS_KEY"
+	AzureStorageAccountNameEnvVar = "AZURE_STORAGE_ACCOUNT"
+	AzureStorageContainerNameEnvVar = "AZURE_STORAGE_CONTAINER"
+
 	KubecostNamespaceEnvVar        = "KUBECOST_NAMESPACE"
 	ClusterIDEnvVar                = "CLUSTER_ID"
 	ClusterProfileEnvVar           = "CLUSTER_PROFILE"
@@ -97,6 +101,18 @@ func GetAWSClusterID() string {
 	return Get(AWSClusterIDEnvVar, "")
 }
 
+func GetAzureStorageAccessKey() string {
+	return Get(AzureStorageAccessKeyEnvVar, "")
+}
+
+func GetAzureStorageAccountName() string {
+	return Get(AzureStorageAccountNameEnvVar, "")
+}
+
+func GetAzureStorageContainerName() string {
+	return Get(AzureStorageContainerNameEnvVar, "")
+}
+
 // GetKubecostNamespace returns the environment variable value for KubecostNamespaceEnvVar which
 // represents the namespace the cost model exists in.
 func GetKubecostNamespace() string {