Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.

Commit 28dc23b

Browse files
javipolodavidmccormick
authored andcommitted
Add flag to cmds to use AWS profile (#1790)
1 parent 3cd5664 commit 28dc23b

File tree

14 files changed

+54
-15
lines changed

14 files changed

+54
-15
lines changed

awsconn/awsconn.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package awsconn
22

33
import (
44
"fmt"
5+
56
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/aws/credentials"
68
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
79
"github.com/aws/aws-sdk-go/aws/session"
810
"github.com/kubernetes-incubator/kube-aws/pkg/api"
911
)
1012

1113
// NewSessionFromRegion creates an AWS session from AWS region and a debug flag
12-
func NewSessionFromRegion(region api.Region, debug bool) (*session.Session, error) {
14+
func NewSessionFromRegion(region api.Region, debug bool, awsProfile string) (*session.Session, error) {
1315
awsConfig := aws.NewConfig().
1416
WithRegion(region.String()).
1517
WithCredentialsChainVerboseErrors(true)
@@ -18,6 +20,10 @@ func NewSessionFromRegion(region api.Region, debug bool) (*session.Session, erro
1820
awsConfig = awsConfig.WithLogLevel(aws.LogDebug)
1921
}
2022

23+
if awsProfile != "" {
24+
awsConfig = awsConfig.WithCredentials(credentials.NewSharedCredentials("", awsProfile))
25+
}
26+
2127
session, err := newSession(awsConfig)
2228
if err != nil {
2329
return nil, fmt.Errorf("failed to establish aws session: %v", err)

cmd/apply.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
applyOpts = struct {
2525
awsDebug, prettyPrint, skipWait, export bool
2626
force bool
27+
profile string
2728
targets []string
2829
}{}
2930
)
@@ -35,6 +36,7 @@ func init() {
3536
cmdApply.Flags().BoolVar(&applyOpts.prettyPrint, "pretty-print", false, "Pretty print the resulting CloudFormation")
3637
cmdApply.Flags().BoolVar(&applyOpts.skipWait, "skip-wait", false, "Don't wait the resources finish")
3738
cmdApply.Flags().BoolVar(&applyOpts.force, "force", false, "Don't ask for confirmation")
39+
cmdApply.Flags().StringVar(&applyOpts.profile, "profile", "", "The AWS profile to use from credentials file")
3840
cmdApply.Flags().StringSliceVar(&applyOpts.targets, "targets", root.AllOperationTargetsAsStringSlice(), "Update nothing but specified sub-stacks. Specify `all` or any combination of `etcd`, `control-plane`, and node pool names. Defaults to `all`")
3941
}
4042

@@ -44,7 +46,7 @@ func runCmdApply(_ *cobra.Command, _ []string) error {
4446
return nil
4547
}
4648

47-
opts := root.NewOptions(applyOpts.prettyPrint, applyOpts.skipWait)
49+
opts := root.NewOptions(applyOpts.prettyPrint, applyOpts.skipWait, applyOpts.profile)
4850

4951
cluster, err := root.LoadClusterFromFile(configPath, opts, applyOpts.awsDebug)
5052
if err != nil {

cmd/calculator.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ var (
2222
}
2323

2424
calculatorOpts = struct {
25+
profile string
2526
awsDebug bool
2627
}{}
2728
)
2829

2930
func init() {
3031
RootCmd.AddCommand(cmdCalculator)
32+
cmdCalculator.Flags().StringVar(&calculatorOpts.profile, "profile", "", "The AWS profile to use from credentials file")
3133
cmdCalculator.Flags().BoolVar(&calculatorOpts.awsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
3234
}
3335

3436
func runCmdCalculator(_ *cobra.Command, _ []string) error {
3537

36-
opts := root.NewOptions(false, false)
38+
opts := root.NewOptions(false, false, calculatorOpts.profile)
3739

3840
cluster, err := root.LoadClusterFromFile(configPath, opts, calculatorOpts.awsDebug)
3941
if err != nil {

cmd/destroy.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525

2626
func init() {
2727
RootCmd.AddCommand(cmdDestroy)
28+
cmdDestroy.Flags().StringVar(&destroyOpts.Profile, "profile", "", "The AWS profile to use from credentials file")
2829
cmdDestroy.Flags().BoolVar(&destroyOpts.AwsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
2930
cmdDestroy.Flags().BoolVar(&destroyOpts.Force, "force", false, "Don't ask for confirmation")
3031
}

cmd/diff.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package cmd
33
import (
44
"fmt"
55

6+
"strings"
7+
68
"github.com/kubernetes-incubator/kube-aws/core/root"
79
"github.com/kubernetes-incubator/kube-aws/logger"
810
"github.com/spf13/cobra"
9-
"strings"
1011
)
1112

1213
var (
@@ -21,6 +22,7 @@ var (
2122
diffOpts = struct {
2223
awsDebug, prettyPrint, skipWait, export bool
2324
context int
25+
profile string
2426
targets []string
2527
}{}
2628
)
@@ -37,12 +39,13 @@ func (e *ExitError) Error() string {
3739
func init() {
3840
RootCmd.AddCommand(cmdDiff)
3941
cmdDiff.Flags().BoolVar(&diffOpts.awsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
42+
cmdDiff.Flags().StringVar(&diffOpts.profile, "profile", "", "The AWS profile to use from credentials file")
4043
cmdDiff.Flags().StringSliceVar(&diffOpts.targets, "targets", root.AllOperationTargetsAsStringSlice(), "Diff nothing but specified sub-stacks. Specify `all` or any combination of `etcd`, `control-plane`, and node pool names. Defaults to `all`")
4144
cmdDiff.Flags().IntVarP(&diffOpts.context, "context", "C", -1, "output NUM lines of context around changes")
4245
}
4346

4447
func runCmdDiff(c *cobra.Command, _ []string) error {
45-
opts := root.NewOptions(diffOpts.prettyPrint, diffOpts.skipWait)
48+
opts := root.NewOptions(diffOpts.prettyPrint, diffOpts.skipWait, diffOpts.profile)
4649

4750
cluster, err := root.LoadClusterFromFile(configPath, opts, diffOpts.awsDebug)
4851
if err != nil {

cmd/status.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@ var (
1616
RunE: runCmdStatus,
1717
SilenceUsage: true,
1818
}
19+
20+
statusOpts = struct {
21+
profile string
22+
}{}
1923
)
2024

2125
func init() {
2226
RootCmd.AddCommand(cmdStatus)
27+
cmdStatus.Flags().StringVar(&statusOpts.profile, "profile", "", "The AWS profile to use from credentials file")
2328
}
2429

2530
func runCmdStatus(_ *cobra.Command, _ []string) error {
26-
describer, err := root.ClusterDescriberFromFile(configPath)
31+
opts := root.NewOptions(false, false, statusOpts.profile)
32+
describer, err := root.ClusterDescriberFromFile(configPath, opts)
2733
if err != nil {
2834
return fmt.Errorf("failed to read cluster config: %v", err)
2935
}

cmd/up.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919

2020
upOpts = struct {
2121
awsDebug, export, prettyPrint, skipWait bool
22+
profile string
2223
}{}
2324
)
2425

@@ -28,13 +29,14 @@ func init() {
2829
cmdUp.Flags().BoolVar(&upOpts.prettyPrint, "pretty-print", false, "Pretty print the resulting CloudFormation")
2930
cmdUp.Flags().BoolVar(&upOpts.awsDebug, "aws-debug", false, "Log debug information from aws-sdk-go library")
3031
cmdUp.Flags().BoolVar(&upOpts.skipWait, "skip-wait", false, "Don't wait for the cluster components be ready")
32+
cmdUp.Flags().StringVar(&upOpts.profile, "profile", "", "The AWS profile to use from credentials file")
3133
}
3234

3335
func runCmdUp(_ *cobra.Command, _ []string) error {
3436
logger.Warnf("WARNING! kube-aws 'up' command is deprecated and will be removed in future versions")
3537
logger.Warnf("Please use 'apply' to create your cluster")
3638

37-
opts := root.NewOptions(upOpts.prettyPrint, upOpts.skipWait)
39+
opts := root.NewOptions(upOpts.prettyPrint, upOpts.skipWait, upOpts.profile)
3840

3941
cluster, err := root.LoadClusterFromFile(configPath, opts, upOpts.awsDebug)
4042
if err != nil {

cmd/update.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var (
2424
updateOpts = struct {
2525
awsDebug, prettyPrint, skipWait bool
2626
force bool
27+
profile string
2728
targets []string
2829
}{}
2930
)
@@ -34,6 +35,7 @@ func init() {
3435
cmdUpdate.Flags().BoolVar(&updateOpts.prettyPrint, "pretty-print", false, "Pretty print the resulting CloudFormation")
3536
cmdUpdate.Flags().BoolVar(&updateOpts.skipWait, "skip-wait", false, "Don't wait the resources finish")
3637
cmdUpdate.Flags().BoolVar(&updateOpts.force, "force", false, "Don't ask for confirmation")
38+
cmdUpdate.Flags().StringVar(&updateOpts.profile, "profile", "", "The AWS profile to use from credentials file")
3739
cmdUpdate.Flags().StringSliceVar(&updateOpts.targets, "targets", root.AllOperationTargetsAsStringSlice(), "Update nothing but specified sub-stacks. Specify `all` or any combination of `etcd`, `control-plane`, and node pool names. Defaults to `all`")
3840
}
3941

@@ -46,7 +48,7 @@ func runCmdUpdate(_ *cobra.Command, _ []string) error {
4648
return nil
4749
}
4850

49-
opts := root.NewOptions(updateOpts.prettyPrint, updateOpts.skipWait)
51+
opts := root.NewOptions(updateOpts.prettyPrint, updateOpts.skipWait, updateOpts.profile)
5052

5153
cluster, err := root.LoadClusterFromFile(configPath, opts, updateOpts.awsDebug)
5254
if err != nil {

cmd/validate.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"fmt"
5+
56
"github.com/kubernetes-incubator/kube-aws/core/root"
67
"github.com/kubernetes-incubator/kube-aws/logger"
78
"github.com/spf13/cobra"
@@ -18,6 +19,7 @@ var (
1819

1920
validateOpts = struct {
2021
awsDebug, skipWait bool
22+
profile string
2123
targets []string
2224
}{}
2325
)
@@ -30,6 +32,7 @@ func init() {
3032
false,
3133
"Log debug information from aws-sdk-go library",
3234
)
35+
cmdValidate.Flags().StringVar(&validateOpts.profile, "profile", "", "The AWS profile to use from credentials file")
3336
cmdValidate.Flags().StringSliceVar(
3437
&validateOpts.targets,
3538
"targets",
@@ -38,7 +41,7 @@ func init() {
3841
}
3942

4043
func runCmdValidate(_ *cobra.Command, _ []string) error {
41-
opts := root.NewOptions(validateOpts.awsDebug, validateOpts.skipWait)
44+
opts := root.NewOptions(validateOpts.awsDebug, validateOpts.skipWait, validateOpts.profile)
4245

4346
cluster, err := root.LoadClusterFromFile(configPath, opts, validateOpts.awsDebug)
4447
if err != nil {

core/root/cluster.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func CompileClusterFromFile(configPath string, opts options, awsDebug bool) (*Cl
146146
}
147147

148148
func CompileClusterFromConfig(cfg *config.Config, opts options, awsDebug bool) (*Cluster, error) {
149-
session, err := awsconn.NewSessionFromRegion(cfg.Region, awsDebug)
149+
session, err := awsconn.NewSessionFromRegion(cfg.Region, awsDebug, opts.AWSProfile)
150150
if err != nil {
151151
return nil, fmt.Errorf("failed to establish aws session: %v", err)
152152
}

core/root/describer.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package root
22

33
import (
44
"fmt"
5+
56
"github.com/aws/aws-sdk-go/aws"
67
"github.com/aws/aws-sdk-go/aws/session"
78
"github.com/aws/aws-sdk-go/service/cloudformation"
@@ -29,13 +30,13 @@ type clusterDescriberImpl struct {
2930
stackName string
3031
}
3132

32-
func ClusterDescriberFromFile(configPath string) (ClusterDescriber, error) {
33+
func ClusterDescriberFromFile(configPath string, opts options) (ClusterDescriber, error) {
3334
config, err := config.ConfigFromFile(configPath)
3435
if err != nil {
3536
return nil, err
3637
}
3738

38-
session, err := awsconn.NewSessionFromRegion(config.Region, false)
39+
session, err := awsconn.NewSessionFromRegion(config.Region, false, opts.AWSProfile)
3940
if err != nil {
4041
return nil, fmt.Errorf("failed to establish aws session: %v", err)
4142
}

core/root/destroyer.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
type DestroyOptions struct {
12+
Profile string
1213
AwsDebug bool
1314
Force bool
1415
}
@@ -27,7 +28,7 @@ func ClusterDestroyerFromFile(configPath string, opts DestroyOptions) (ClusterDe
2728
return nil, err
2829
}
2930

30-
session, err := awsconn.NewSessionFromRegion(cfg.Region, opts.AwsDebug)
31+
session, err := awsconn.NewSessionFromRegion(cfg.Region, opts.AwsDebug, opts.Profile)
3132
if err != nil {
3233
return nil, fmt.Errorf("failed to establish aws session: %v", err)
3334
}

core/root/options.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ type options struct {
1212
NetworkStackTemplateTmplFile string
1313
EtcdStackTemplateTmplFile string
1414
NodePoolStackTemplateTmplFile string
15+
AWSProfile string
1516
SkipWait bool
1617
PrettyPrint bool
1718
}
1819

19-
func NewOptions(prettyPrint bool, skipWait bool) options {
20+
func NewOptions(prettyPrint bool, skipWait bool, awsProfile ...string) options {
21+
var profile string
22+
if len(awsProfile) > 0 {
23+
profile = awsProfile[0]
24+
}
25+
2026
return options{
2127
AssetsDir: defaults.AssetsDir,
2228
ControllerTmplFile: defaults.ControllerTmplFile,
@@ -27,6 +33,7 @@ func NewOptions(prettyPrint bool, skipWait bool) options {
2733
EtcdStackTemplateTmplFile: defaults.EtcdStackTemplateTmplFile,
2834
NodePoolStackTemplateTmplFile: defaults.NodePoolStackTemplateTmplFile,
2935
RootStackTemplateTmplFile: defaults.RootStackTemplateTmplFile,
36+
AWSProfile: profile,
3037
SkipWait: skipWait,
3138
PrettyPrint: prettyPrint,
3239
}

docs/cli-reference/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Validate cluster assets prior to deployment.
8080
| Flag | Description | Default |
8181
| -- | -- | -- |
8282
| `aws-debug` | Log debug information coming from the AWS SDK library | `false` |
83+
| `profile` | Use AWS profile from credentials file | `empty` |
8384

8485
### `validate` example
8586

@@ -98,6 +99,7 @@ Deploy or Update an existing Kubernetes cluster that was created by kube-aws.
9899
| `export` | Do not create cluster, instead export the CloudFormation stack file | `false` |
99100
| `pretty-print` | Pretty print the resulting CloudFormation | `false` |
100101
| `skip-wait` | Do not wait for the cluster components be ready before the CLI exits | `false` |
102+
| `profile` | Use AWS profile from credentials file | `empty` |
101103

102104
### `apply` example
103105

@@ -112,9 +114,10 @@ Destroy an existing Kubernetes cluster that was created by kube-aws.
112114
| Flag | Description | Default |
113115
| -- | -- | -- |
114116
| `aws-debug` | Log debug information coming from the AWS SDK library | `false` |
117+
| `profile` | Use AWS profile from credentials file | `empty` |
115118

116119
### `destroy` example
117120

118121
```bash
119122
$ kube-aws destory
120-
```
123+
```

0 commit comments

Comments
 (0)