@@ -15,8 +15,12 @@ package config
1515
1616import (
1717 "errors"
18+ "fmt"
1819 "net/url"
1920
21+ "github.com/aws/aws-sdk-go/aws/session"
22+ "github.com/aws/aws-sdk-go/service/sts"
23+ "github.com/jaypipes/envutil"
2024 flag "github.com/spf13/pflag"
2125 "go.uber.org/zap/zapcore"
2226 ctrlrt "sigs.k8s.io/controller-runtime"
@@ -27,14 +31,14 @@ const (
2731 flagEnableLeaderElection = "enable-leader-election"
2832 flagMetricAddr = "metrics-addr"
2933 flagEnableDevLogging = "enable-development-logging"
30- flagAWSAccountID = "aws-account-id"
3134 flagAWSRegion = "aws-region"
3235 flagAWSEndpointURL = "aws-endpoint-url"
3336 flagLogLevel = "log-level"
3437 flagResourceTags = "resource-tags"
3538 flagWatchNamespace = "watch-namespace"
3639 flagEnableWebhookServer = "enable-webhook-server"
3740 flagWebhookServerAddr = "webhook-server-addr"
41+ envVarAWSRegion = "AWS_REGION"
3842)
3943
4044// Config contains configuration otpions for ACK service controllers
@@ -81,14 +85,9 @@ func (cfg *Config) BindFlags() {
8185 "Configures the logger to use a Zap development config (encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn, no sampling), " +
8286 "otherwise a Zap production config will be used (encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error), sampling)." ,
8387 )
84- flag .StringVar (
85- & cfg .AccountID , flagAWSAccountID ,
86- "" ,
87- "The AWS Account ID in which the service controller will create resources" ,
88- )
8988 flag .StringVar (
9089 & cfg .Region , flagAWSRegion ,
91- "" ,
90+ envutil . WithDefault ( envVarAWSRegion , "" ) ,
9291 "The AWS Region in which the service controller will create its resources" ,
9392 )
9493 flag .StringVar (
@@ -134,13 +133,31 @@ func (cfg *Config) SetupLogger() {
134133 ctrlrt .SetLogger (zap .New (zap .UseFlagOptions (& zapOptions )))
135134}
136135
136+ // SetAWSAccountID uses sts GetCallerIdentity API to find AWS AccountId and set
137+ // in Config
138+ func (cfg * Config ) SetAWSAccountID () error {
139+ // use sts to find AWS AccountId
140+ session , err := session .NewSession ()
141+ if err != nil {
142+ return fmt .Errorf ("unable to create session: %v" , err )
143+ }
144+ client := sts .New (session )
145+ res , err := client .GetCallerIdentity (& sts.GetCallerIdentityInput {})
146+ if err != nil {
147+ return fmt .Errorf ("unable to get caller identity: %v" , err )
148+ }
149+ cfg .AccountID = * res .Account
150+ return nil
151+ }
152+
137153// Validate ensures the options are valid
138154func (cfg * Config ) Validate () error {
139- if cfg .AccountID == "" {
140- return errors .New ("unable to start service controller as account ID is nil. Please pass --aws-account-id flag " )
155+ if err := cfg .SetAWSAccountID (); err != nil {
156+ return errors .New ("unable to determine account ID. Please make sure AWS credentials are setup in controller pod " )
141157 }
158+
142159 if cfg .Region == "" {
143- return errors .New ("unable to start service controller as AWS region is nil . Please pass --aws-region flag" )
160+ return errors .New ("unable to start service controller as AWS region is missing . Please pass --aws-region flag or set AWS_REGION environment variable " )
144161 }
145162
146163 if cfg .EndpointURL != "" {
0 commit comments