Skip to content

Commit e4ec08a

Browse files
committed
Merge branch 'update-config-init' into dev
2 parents 4e3125b + 842145f commit e4ec08a

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

config/iam/recommended-inline-policy.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"vpc-lattice:*",
88
"ec2:DescribeVpcs",
99
"ec2:DescribeSubnets",
10+
"eks:DescribeCluster",
1011
"ec2:DescribeTags",
1112
"ec2:DescribeSecurityGroups",
1213
"logs:CreateLogDelivery",

docs/guides/environment.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ This variable is required except for EKS cluster. This needs to be specified if
1919

2020
**Type:** *string*
2121

22-
**Default:** *Inferred from IMDS metadata*
22+
**Default:** *Inferred from IMDS metadata or CLUSTER_NAME*
2323

24-
When running AWS Gateway API Controller outside the Kubernetes Cluster, this specifies the VPC of the cluster. This needs to be specified if IMDS is not available.
24+
When running AWS Gateway API Controller outside the Kubernetes Cluster, this specifies the VPC of the cluster.
2525

2626
---
2727

2828
#### `AWS_ACCOUNT_ID`
2929

3030
**Type:** *string*
3131

32-
**Default:** *Inferred from IMDS metadata*
32+
**Default:** *Inferred from IMDS metadata or AWS STS GetCallerIdentity API*
3333

34-
When running AWS Gateway API Controller outside the Kubernetes Cluster, this specifies the AWS account. This needs to be specified if IMDS is not available.
34+
When running AWS Gateway API Controller outside the Kubernetes Cluster, this specifies the AWS account.
3535

3636
---
3737

38-
#### `REGION`
38+
#### `REGION` or `AWS_REGION`
3939

4040
**Type:** *string*
4141

pkg/config/controller_config.go

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
@@ -12,6 +13,8 @@ import (
1213
"github.com/aws/aws-sdk-go/aws/ec2metadata"
1314
"github.com/aws/aws-sdk-go/aws/session"
1415
"github.com/aws/aws-sdk-go/service/ec2"
16+
"github.com/aws/aws-sdk-go/service/eks"
17+
"github.com/aws/aws-sdk-go/service/sts"
1518
)
1619

1720
const (
@@ -21,6 +24,7 @@ const (
2124

2225
const (
2326
REGION = "REGION"
27+
AWS_REGION = "AWS_REGION"
2428
CLUSTER_VPC_ID = "CLUSTER_VPC_ID"
2529
CLUSTER_NAME = "CLUSTER_NAME"
2630
DEFAULT_SERVICE_NETWORK = "DEFAULT_SERVICE_NETWORK"
@@ -53,33 +57,44 @@ func ConfigInit() error {
5357
func configInit(sess *session.Session, metadata EC2Metadata) error {
5458
var err error
5559

56-
DevMode = os.Getenv(DEV_MODE)
57-
WebhookEnabled = os.Getenv(WEBHOOK_ENABLED)
60+
var metadataErr error
61+
if Region = os.Getenv(REGION); Region == "" {
62+
if Region, metadataErr = metadata.Region(); metadataErr != nil {
63+
if Region = os.Getenv(AWS_REGION); Region == "" {
64+
return fmt.Errorf("region is not specified")
65+
}
66+
}
67+
}
5868

59-
VpcID = os.Getenv(CLUSTER_VPC_ID)
60-
if VpcID == "" {
61-
VpcID, err = metadata.VpcID()
62-
if err != nil {
63-
return fmt.Errorf("vpcId is not specified: %s", err)
69+
if ClusterName = os.Getenv(CLUSTER_NAME); ClusterName == "" {
70+
if ClusterName, err = getClusterName(sess, Region); err != nil {
71+
return fmt.Errorf("cannot get cluster name: %s", err)
6472
}
6573
}
6674

67-
Region = os.Getenv(REGION)
68-
if Region == "" {
69-
Region, err = metadata.Region()
70-
if err != nil {
71-
return fmt.Errorf("region is not specified: %s", err)
75+
if VpcID = os.Getenv(CLUSTER_VPC_ID); VpcID == "" {
76+
if metadataErr != nil {
77+
if VpcID, err = fromClusterNameToVPCId(sess, ClusterName); err != nil {
78+
return fmt.Errorf("vpcId is not specified: %s", err)
79+
}
80+
} else if VpcID, err = metadata.VpcID(); err != nil {
81+
return fmt.Errorf("vpcId is not specified: %s", err)
7282
}
7383
}
7484

75-
AccountID = os.Getenv(AWS_ACCOUNT_ID)
76-
if AccountID == "" {
77-
AccountID, err = metadata.AccountId()
78-
if err != nil {
85+
if AccountID = os.Getenv(AWS_ACCOUNT_ID); AccountID == "" {
86+
if metadataErr != nil {
87+
if AccountID, err = fromIdentityToAccountId(sess); err != nil {
88+
return fmt.Errorf("account is not specified: %s", err)
89+
}
90+
} else if AccountID, err = metadata.AccountId(); err != nil {
7991
return fmt.Errorf("account is not specified: %s", err)
8092
}
8193
}
8294

95+
DevMode = os.Getenv(DEV_MODE)
96+
WebhookEnabled = os.Getenv(WEBHOOK_ENABLED)
97+
8398
DefaultServiceNetwork = os.Getenv(DEFAULT_SERVICE_NETWORK)
8499

85100
overrideFlag := os.Getenv(ENABLE_SERVICE_NETWORK_OVERRIDE)
@@ -93,11 +108,6 @@ func configInit(sess *session.Session, metadata EC2Metadata) error {
93108
DisableTaggingServiceAPI = true
94109
}
95110

96-
ClusterName, err = getClusterName(sess)
97-
if err != nil {
98-
return fmt.Errorf("cannot get cluster name: %s", err)
99-
}
100-
101111
routeMaxConcurrentReconciles := os.Getenv(ROUTE_MAX_CONCURRENT_RECONCILES)
102112
if routeMaxConcurrentReconciles != "" {
103113
routeMaxConcurrentReconcilesInt, err := strconv.Atoi(routeMaxConcurrentReconciles)
@@ -111,22 +121,13 @@ func configInit(sess *session.Session, metadata EC2Metadata) error {
111121
}
112122

113123
// try to find cluster name, search in env then in ec2 instance tags
114-
func getClusterName(sess *session.Session) (string, error) {
115-
cn := os.Getenv(CLUSTER_NAME)
116-
if cn != "" {
117-
return cn, nil
118-
}
119-
// fallback to ec2 instance tags
124+
func getClusterName(sess *session.Session, region string) (string, error) {
120125
meta := ec2metadata.New(sess)
121126
doc, err := meta.GetInstanceIdentityDocument()
122127
if err != nil {
123128
return "", err
124129
}
125130
instanceId := doc.InstanceID
126-
region, err := meta.Region()
127-
if err != nil {
128-
return "", err
129-
}
130131
ec2Client := ec2.New(sess, &aws.Config{Region: aws.String(region)})
131132
tagReq := &ec2.DescribeTagsInput{Filters: []*ec2.Filter{{
132133
Name: aws.String("resource-id"),
@@ -143,3 +144,27 @@ func getClusterName(sess *session.Session) (string, error) {
143144
}
144145
return "", errors.New("not found in env and metadata")
145146
}
147+
148+
func fromClusterNameToVPCId(sess *session.Session, clusterName string) (string, error) {
149+
eksClient := eks.New(sess)
150+
clusterConf, err := eksClient.DescribeClusterWithContext(context.Background(), &eks.DescribeClusterInput{Name: aws.String(clusterName)})
151+
if err != nil {
152+
return "", err
153+
}
154+
if clusterConf.Cluster.ResourcesVpcConfig == nil {
155+
return "", fmt.Errorf("VPC ID is not found in cluster %s", clusterName)
156+
}
157+
return *clusterConf.Cluster.ResourcesVpcConfig.VpcId, nil
158+
}
159+
160+
func fromIdentityToAccountId(sess *session.Session) (string, error) {
161+
stsClient := sts.New(sess)
162+
identity, err := stsClient.GetCallerIdentityWithContext(context.Background(), &sts.GetCallerIdentityInput{})
163+
if err != nil {
164+
return "", err
165+
}
166+
if identity.Account == nil {
167+
return "", fmt.Errorf("account id is not found")
168+
}
169+
return *identity.Account, nil
170+
}

0 commit comments

Comments
 (0)