diff --git a/cluster-autoscaler/cloudprovider/aws/aws_manager.go b/cluster-autoscaler/cloudprovider/aws/aws_manager.go index f10d476379..d91662644e 100644 --- a/cluster-autoscaler/cloudprovider/aws/aws_manager.go +++ b/cluster-autoscaler/cloudprovider/aws/aws_manager.go @@ -22,11 +22,13 @@ import ( "fmt" "io" "math/rand" + "os" "regexp" "strings" "time" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/autoscaling" "github.com/aws/aws-sdk-go/service/ec2" @@ -64,6 +66,18 @@ type asgTemplate struct { Tags []*autoscaling.TagDescription } +// getRegion deduces the current AWS Region. +func getRegion(cfg ...*aws.Config) string { + region, present := os.LookupEnv("AWS_REGION") + if !present { + svc := ec2metadata.New(session.New(), cfg...) + if r, err := svc.Region(); err == nil { + region = r + } + } + return region +} + // createAwsManagerInternal allows for a customer autoScalingWrapper to be passed in by tests func createAWSManagerInternal( configReader io.Reader, @@ -80,7 +94,7 @@ func createAWSManagerInternal( } if autoScalingService == nil || ec2Service == nil { - sess := session.New() + sess := session.New(aws.NewConfig().WithRegion(getRegion())) if autoScalingService == nil { autoScalingService = &autoScalingWrapper{autoscaling.New(sess)} diff --git a/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go b/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go index 8eddd7eb53..fe3af85f78 100644 --- a/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go +++ b/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go @@ -18,6 +18,9 @@ package aws import ( "fmt" + "net/http" + "net/http/httptest" + "os" "testing" "github.com/aws/aws-sdk-go/aws" @@ -30,6 +33,32 @@ import ( kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis" ) +// TestGetRegion ensures correct source supplies AWS Region. +func TestGetRegion(t *testing.T) { + key := "AWS_REGION" + originalRegion, originalPresent := os.LookupEnv(key) + defer func(region string, present bool) { + os.Unsetenv(key) + if present { + os.Setenv(key, region) + } + }(originalRegion, originalPresent) + // Ensure environment variable retains precedence. + expected1 := "the-shire-1" + os.Setenv(key, expected1) + assert.Equal(t, expected1, getRegion()) + // Ensure without environment variable, EC2 Metadata used... and it merely + // chops the last character off the Availability Zone. + expected2 := "mordor-2" + expected2a := expected2 + "a" + os.Unsetenv(key) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(expected2a)) + })) + cfg := aws.NewConfig().WithEndpoint(server.URL) + assert.Equal(t, expected2, getRegion(cfg)) +} + func TestBuildGenericLabels(t *testing.T) { labels := buildGenericLabels(&asgTemplate{ InstanceType: &instanceType{ diff --git a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml index 14dc8e6ded..f7a8290aeb 100644 --- a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml +++ b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-autodiscover.yaml @@ -138,9 +138,6 @@ spec: - --skip-nodes-with-local-storage=false - --expander=least-waste - --node-group-auto-discovery=asg:tag=k8s.io/cluster-autoscaler/enabled,k8s.io/cluster-autoscaler/ - env: - - name: AWS_REGION - value: us-east-1 volumeMounts: - name: ssl-certs mountPath: /etc/ssl/certs/ca-certificates.crt diff --git a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-multi-asg.yaml b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-multi-asg.yaml index a1cf1e9861..e047f32b43 100644 --- a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-multi-asg.yaml +++ b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-multi-asg.yaml @@ -139,9 +139,6 @@ spec: - --expander=least-waste - --nodes=1:10:k8s-worker-asg-1 - --nodes=1:3:k8s-worker-asg-2 - env: - - name: AWS_REGION - value: us-east-1 volumeMounts: - name: ssl-certs mountPath: /etc/ssl/certs/ca-certificates.crt diff --git a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-one-asg.yaml b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-one-asg.yaml index f48251f267..7c9f387eec 100644 --- a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-one-asg.yaml +++ b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-one-asg.yaml @@ -137,9 +137,6 @@ spec: - --cloud-provider=aws - --skip-nodes-with-local-storage=false - --nodes=1:10:k8s-worker-asg-1 - env: - - name: AWS_REGION - value: us-east-1 volumeMounts: - name: ssl-certs mountPath: /etc/ssl/certs/ca-certificates.crt diff --git a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-run-on-master.yaml b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-run-on-master.yaml index e9cf765c0b..b4f5c65fcb 100644 --- a/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-run-on-master.yaml +++ b/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-run-on-master.yaml @@ -144,9 +144,6 @@ spec: - --cloud-provider=aws - --skip-nodes-with-local-storage=false - --nodes={{ node_asg_min }}:{{ node_asg_max }}:{{ name }} - env: - - name: AWS_REGION - value: {{ region }} volumeMounts: - name: ssl-certs mountPath: /etc/ssl/certs/ca-certificates.crt