Просмотр исходного кода

Merge pull request #353 from kubecost/AjayTripathy-fix-auth

Ajay tripathy fix auth
Ajay Tripathy 6 лет назад
Родитель
Сommit
dad74b2c33
2 измененных файлов с 16 добавлено и 31 удалено
  1. 1 1
      deploying-as-a-pod.md
  2. 15 30
      pkg/cloud/awsprovider.go

+ 1 - 1
deploying-as-a-pod.md

@@ -2,7 +2,7 @@
 
 See this page for all [Kubecost install options](http://docs.kubecost.com/install).
 
-If you would like to deploy the cost model (w/o dashboards) directly a pod on your cluster, complete the listed below. 
+If you would like to deploy the cost model (w/o dashboards) directly a pod on your cluster, complete the steps listed below. 
 
 1. Set [this environment variable](https://github.com/kubecost/cost-model/blob/master/kubernetes/deployment.yaml#L30) to the address of your prometheus server
 2. `kubectl create namespace cost-model`

+ 15 - 30
pkg/cloud/awsprovider.go

@@ -928,33 +928,18 @@ func (*AWS) AddServiceKey(formValues url.Values) error {
 	return ioutil.WriteFile("/var/configs/key.json", result, 0644)
 }
 
-func configureAWSAuth(keyFile string) error {
-	jsonFile, err := os.Open(keyFile)
-	if err != nil {
-		if os.IsNotExist(err) {
-			klog.V(2).Infof("Using Default Credentials")
-			return nil
+func (aws *AWS) configureAWSAuth() error {
+	accessKeyID := aws.ServiceKeyName
+	accessKeySecret := aws.ServiceKeySecret
+	if accessKeyID != "" && accessKeySecret != "" { // credentials may exist on the actual AWS node-- if so, use those. If not, override with the service key
+		err := os.Setenv(awsAccessKeyIDEnvVar, accessKeyID)
+		if err != nil {
+			return err
+		}
+		err = os.Setenv(awsAccessKeySecretEnvVar, accessKeySecret)
+		if err != nil {
+			return err
 		}
-
-		return err
-	}
-	defer jsonFile.Close()
-
-	byteValue, _ := ioutil.ReadAll(jsonFile)
-	var result map[string]string
-	err = json.Unmarshal([]byte(byteValue), &result)
-	if err != nil {
-		return err
-	}
-
-	err = os.Setenv(awsAccessKeyIDEnvVar, result["awsServiceKeyName"])
-	if err != nil {
-		return err
-	}
-
-	err = os.Setenv(awsAccessKeySecretEnvVar, result["awsServiceKeySecret"])
-	if err != nil {
-		return err
 	}
 	return nil
 }
@@ -979,8 +964,8 @@ func getClusterConfig(ccFile string) (map[string]string, error) {
 }
 
 // GetDisks returns the AWS disks backing PVs. Useful because sometimes k8s will not clean up PVs correctly. Requires a json config in /var/configs with key region.
-func (*AWS) GetDisks() ([]byte, error) {
-	err := configureAWSAuth("/var/configs/key.json")
+func (a *AWS) GetDisks() ([]byte, error) {
+	err := a.configureAWSAuth()
 	if err != nil {
 		return nil, err
 	}
@@ -1701,9 +1686,9 @@ func getRegionReservedInstances(region string) ([]*AWSReservedInstance, error) {
 }
 
 func (a *AWS) getReservedInstances() ([]*AWSReservedInstance, error) {
-	err := configureAWSAuth("/var/configs/aws.json")
+	err := a.configureAWSAuth()
 	if err != nil {
-		return nil, err
+		return nil, fmt.Errorf("Error Configuring aws auth: %s", err.Error())
 	}
 
 	var reservedInstances []*AWSReservedInstance