Skip to content

Commit 4a3d4c7

Browse files
authored
Merge pull request #54 from vijtrip2/env-var-fallback
use sts GetCallerIdentity to find AWS AccountID
2 parents 8e4dc5e + 6080101 commit 4a3d4c7

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ any 'help wanted' issues is a great place to start.
6464

6565
[See the documentation][dev-docs] for detailed development information.
6666

67-
[dev-docs]: https://aws.github.io/aws-controllers-k8s/dev-docs/overview/
67+
[dev-docs]: https://aws-controllers-k8s.github.io/community/docs/contributor-docs/overview/
6868

6969
## Code of Conduct
7070

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/aws/aws-sdk-go v1.37.10
77
github.com/go-logr/logr v0.1.0
88
github.com/google/go-cmp v0.3.1
9+
github.com/jaypipes/envutil v1.0.0
910
github.com/pkg/errors v0.9.1
1011
github.com/prometheus/client_golang v1.1.0
1112
github.com/spf13/pflag v1.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
173173
github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28=
174174
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
175175
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
176+
github.com/jaypipes/envutil v1.0.0 h1:u6Vwy9HwruFihoZrL0bxDLCa/YNadGVwKyPElNmZWow=
177+
github.com/jaypipes/envutil v1.0.0/go.mod h1:vgIRDly+xgBq0eeZRcflOHMMobMwgC6MkMbxo/Nw65M=
176178
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
177179
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
178180
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=

pkg/config/config.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ package config
1515

1616
import (
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
138154
func (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

Comments
 (0)