From 882c6f52252000da436e4fed9fd184b263f5a017 Mon Sep 17 00:00:00 2001 From: JuHyung Son Date: Thu, 16 May 2024 16:36:33 +0200 Subject: [PATCH] feat: support AWS_PROFILE (#1114) Signed-off-by: JuHyung-Son --- pkg/integration/aws/aws.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/pkg/integration/aws/aws.go b/pkg/integration/aws/aws.go index 5dd92fd319..f15d72f588 100644 --- a/pkg/integration/aws/aws.go +++ b/pkg/integration/aws/aws.go @@ -1,11 +1,12 @@ package aws import ( + "os" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/k8sgpt-ai/k8sgpt/pkg/common" "github.com/spf13/viper" - "os" ) type AWS struct { @@ -23,16 +24,28 @@ func (a *AWS) UnDeploy(namespace string) error { } func (a *AWS) AddAnalyzer(mergedMap *map[string]common.IAnalyzer) { - // Check for AWS credentials in the environment - // https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html - if os.Getenv("AWS_ACCESS_KEY_ID") == "" || os.Getenv("AWS_SECRET_ACCESS_KEY") == "" { - panic("AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY must be set in the environment") + // Retrieve AWS credentials from the environment + accessKeyID := os.Getenv("AWS_ACCESS_KEY_ID") + secretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY") + awsProfile := os.Getenv("AWS_PROFILE") + + var sess *session.Session + if accessKeyID != "" && secretAccessKey != "" { + // Use access keys if both are provided + sess = session.Must(session.NewSessionWithOptions(session.Options{ + Config: aws.Config{}, + })) + } else { + // Use AWS profile, default to "default" if not set + if awsProfile == "" { + awsProfile = "default" + } + sess = session.Must(session.NewSessionWithOptions(session.Options{ + Profile: awsProfile, + SharedConfigState: session.SharedConfigEnable, + })) } - sess := session.Must(session.NewSessionWithOptions(session.Options{ - SharedConfigState: session.SharedConfigEnable, - Config: aws.Config{}, - })) a.sess = sess (*mergedMap)["EKS"] = &EKSAnalyzer{ session: a.sess,