Skip to content

Commit 315963b

Browse files
authored
Infer aws region from eks cluster (#82)
1 parent 1345250 commit 315963b

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ See the demo from AWS Container Day x KubeCon!
2626

2727
First, install the controller with latest release on at least 2 AWS EKS clusters. Nodes must have sufficient IAM permissions to perform CloudMap operations.
2828

29-
> **_NOTE:_** AWS region environment variable should be set like `export AWS_REGION=us-west-2`
29+
> **_NOTE:_** AWS region environment variable can be _optionaly_ set like `export AWS_REGION=us-west-2` Otherwise controller will infer region in the order `AWS_REGION` environment variable, ~/.aws/config file, then EC2 metadata (for EKS environment)
3030
3131
```sh
3232
kubectl apply -k "github.com/aws/aws-cloud-map-mcs-controller-for-k8s/config/controller_install_release"
@@ -74,7 +74,7 @@ kubectl get ServiceImport -A
7474

7575
AWS Cloud Map MCS Controller for K8s adheres to the [SemVer](https://semver.org/) specification. Each release updates the major version tag (eg. `vX`), a major/minor version tag (eg. `vX.Y`) and a major/minor/patch version tag (eg. `vX.Y.Z`). To see a full list of all releases, refer to our [Github releases page](https://github.com/aws/aws-cloud-map-mcs-controller-for-k8s/releases).
7676

77-
> **_NOTE:_** AWS region environment variable should be set like `export AWS_REGION=us-west-2`
77+
> **_NOTE:_** AWS region environment variable can be _optionally_ set like `export AWS_REGION=us-west-2` Otherwise controller will infer region in the order `AWS_REGION` environment variable, ~/.aws/config file, then EC2 metadata (for EKS environment)
7878

7979
To install from a release run
8080
```sh

main.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ package main
1919
import (
2020
"context"
2121
"flag"
22+
"os"
23+
2224
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/cloudmap"
2325
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/version"
2426
"github.com/aws/aws-sdk-go-v2/config"
25-
"os"
2627

2728
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2829
// to ensure that exec-entrypoint and run can make use of them.
@@ -84,18 +85,17 @@ func main() {
8485
setupLog.Error(err, "unable to start manager")
8586
os.Exit(1)
8687
}
88+
setupLog.Info("configuring AWS session")
89+
// GO sdk will look for region in order 1) AWS_REGION env var, 2) ~/.aws/config file, 3) EC2 IMDS
90+
awsCfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEC2IMDSRegion())
8791

88-
// TODO: configure session
89-
awsRegion := os.Getenv("AWS_REGION")
90-
setupLog.Info("configuring AWS session", "AWS_REGION", awsRegion)
91-
awsCfg, err := config.LoadDefaultConfig(context.TODO(),
92-
config.WithRegion(awsRegion),
93-
)
94-
if err != nil {
95-
setupLog.Error(err, "unable to configure AWS session", "AWS_REGION", awsRegion)
92+
if err != nil || awsCfg.Region == "" {
93+
setupLog.Error(err, "unable to configure AWS session", "AWS_REGION", awsCfg.Region)
9694
os.Exit(1)
9795
}
9896

97+
setupLog.Info("Running with AWS region", "AWS_REGION", awsCfg.Region)
98+
9999
serviceDiscoveryClient := cloudmap.NewDefaultServiceDiscoveryClient(&awsCfg)
100100
if err = (&controllers.ServiceExportReconciler{
101101
Client: mgr.GetClient(),

0 commit comments

Comments
 (0)