Skip to content

Commit 3a3de7b

Browse files
authored
Enable passing config into controller (#264)
* Enable passing input into controller * Enable passing input into controller * Improve input pass to controller * Change flag to env var for input * Remove checking if running inside cluster code * Update deploy docs * Update default setting * Update default setting * Update default setting * Add unit test for env var init
1 parent 13552ca commit 3a3de7b

File tree

7 files changed

+159
-70
lines changed

7 files changed

+159
-70
lines changed

docs/deploy.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Run through them again for a second cluster to use with the extended example sho
1414
```bash
1515
eksctl create cluster --name $CLUSTER_NAME --region $AWS_REGION
1616
```
17-
1. First, configure security group to receive traffic from the VPC Lattice fleet. You must set up security groups so that they allow all Pods communicating with VPC Lattice to allow traffic on all ports from the `169.254.171.0/24` address range.
17+
1. First, configure security group to receive traffic from the VPC Lattice fleet. You must set up security groups so that they allow all Pods communicating with VPC Lattice to allow traffic on all ports from the `169.254.171.0/24` address range.
1818
```bash
1919
PREFIX_LIST_ID=$(aws ec2 describe-managed-prefix-lists --query "PrefixLists[?PrefixListName=="\'com.amazonaws.$AWS_REGION.vpc-lattice\'"].PrefixListId" | jq -r '.[]')
2020
MANAGED_PREFIX=$(aws ec2 get-managed-prefix-list-entries --prefix-list-id $PREFIX_LIST_ID --output json | jq -r '.Entries[0].Cidr')
@@ -79,7 +79,12 @@ Run through them again for a second cluster to use with the extended example sho
7979
helm install gateway-api-controller \
8080
oci://public.ecr.aws/aws-application-networking-k8s/aws-gateway-controller-chart\
8181
--version=v0.0.12 \
82-
--set=aws.region=$AWS_REGION --set=serviceAccount.create=false --namespace aws-application-networking-system
82+
--set=serviceAccount.create=false --namespace aws-application-networking-system \
83+
# Region, clusterVpcId, awsAccountId are required for case where IMDS is NOT AVAILABLE, e.g Fargate
84+
--set=awsRegion= \
85+
--set=clusterVpcId= \
86+
--set=awsAccountId= \
87+
8388
```
8489
1. Create the `amazon-vpc-lattice` GatewayClass:
8590
```bash

helm/templates/configmap.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: env-config
5+
data:
6+
awsRegion: {{ .Values.awsRegion }}
7+
awsAccountId: {{ .Values.awsAccountId }}
8+
clusterVpcId: {{ .Values.clusterVpcId }}

helm/templates/deployment.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ spec:
6666
periodSeconds: 10
6767
securityContext:
6868
allowPrivilegeEscalation: false
69+
env:
70+
- name: REGION
71+
valueFrom:
72+
configMapKeyRef:
73+
name: env-config
74+
key: awsRegion
75+
- name: AWS_ACCOUNT_ID
76+
valueFrom:
77+
configMapKeyRef:
78+
name: env-config
79+
key: awsAccountId
80+
- name: CLUSTER_VPC_ID
81+
valueFrom:
82+
configMapKeyRef:
83+
name: env-config
84+
key: clusterVpcId
85+
6986
terminationGracePeriodSeconds: 10
7087
nodeSelector: {{ toYaml .Values.deployment.nodeSelector | nindent 8 }}
7188
{{ if .Values.deployment.tolerations -}}

helm/values.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,7 @@ serviceAccount:
7070
name: gateway-api-controller
7171
annotations: {}
7272
# eks.amazonaws.com/role-arn: arn:aws:iam::AWS_ACCOUNT_ID:role/IAM_ROLE_NAME
73+
74+
awsRegion:
75+
awsAccountId:
76+
clusterVpcId:

pkg/config/controller_config.go

Lines changed: 62 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@ import (
1212
const (
1313
LatticeGatewayControllerName = "application-networking.k8s.aws/gateway-api-controller"
1414
defaultLogLevel = "Info"
15-
NoDefaultServiceNetwork = ""
16-
NO_DEFAULT_SERVICE_NETWORK = "NO_DEFAULT_SERVICE_NETWORK"
15+
UnknownInput = ""
1716
)
1817

19-
// TODO endpoint, region
20-
var VpcID = "vpc-xxxx"
21-
var AccountID = "yyyyyy"
22-
var Region = "us-west-2"
18+
const (
19+
NO_DEFAULT_SERVICE_NETWORK = "NO_DEFAULT_SERVICE_NETWORK"
20+
REGION = "REGION"
21+
CLUSTER_VPC_ID = "CLUSTER_VPC_ID"
22+
CLUSTER_LOCAL_GATEWAY = "CLUSTER_LOCAL_GATEWAY"
23+
AWS_ACCOUNT_ID = "AWS_ACCOUNT_ID"
24+
TARGET_GROUP_NAME_LEN_MODE = "TARGET_GROUP_NAME_LEN_MODE"
25+
GATEWAY_API_CONTROLLER_LOGLEVEL = "GATEWAY_API_CONTROLLER_LOGLEVEL"
26+
)
27+
28+
var VpcID = UnknownInput
29+
var AccountID = UnknownInput
30+
var Region = UnknownInput
2331
var logLevel = defaultLogLevel
24-
var DefaultServiceNetwork = NoDefaultServiceNetwork
32+
var DefaultServiceNetwork = UnknownInput
2533
var UseLongTGName = false
2634

2735
func GetLogLevel() string {
28-
logLevel = os.Getenv("GATEWAY_API_CONTROLLER_LOGLEVEL")
36+
logLevel = os.Getenv(GATEWAY_API_CONTROLLER_LOGLEVEL)
2937
switch strings.ToLower(logLevel) {
3038
case "debug":
3139
return "10"
@@ -36,86 +44,74 @@ func GetLogLevel() string {
3644
}
3745

3846
func GetClusterLocalGateway() (string, error) {
39-
if DefaultServiceNetwork == NoDefaultServiceNetwork {
40-
return NoDefaultServiceNetwork, errors.New(NO_DEFAULT_SERVICE_NETWORK)
47+
if DefaultServiceNetwork == UnknownInput {
48+
return UnknownInput, errors.New(NO_DEFAULT_SERVICE_NETWORK)
4149
}
4250

4351
return DefaultServiceNetwork, nil
4452
}
4553

4654
func ConfigInit() {
47-
// discover VPC using environment first
48-
VpcID = os.Getenv("CLUSTER_VPC_ID")
49-
glog.V(2).Infoln("CLUSTER_VPC_ID: ", os.Getenv("CLUSTER_VPC_ID"))
50-
51-
// discover Account
52-
AccountID = os.Getenv("AWS_ACCOUNT_ID")
53-
if AccountID == "" {
54-
AccountID = os.Getenv("AWS_ACCOUNT") // Fallback to AWS_ACCOUNT for compatibility
55-
}
56-
glog.V(2).Infoln("AWS_ACCOUNT_ID:", AccountID)
57-
58-
// discover Region
59-
Region = os.Getenv("REGION")
60-
glog.V(2).Infoln("REGION:", os.Getenv("REGION"))
61-
62-
logLevel = os.Getenv("GATEWAY_API_CONTROLLER_LOGLEVEL")
63-
glog.V(2).Infoln("Logging Level:", os.Getenv("GATEWAY_API_CONTROLLER_LOGLEVEL"))
64-
65-
DefaultServiceNetwork = os.Getenv("CLUSTER_LOCAL_GATEWAY")
66-
67-
if DefaultServiceNetwork == NoDefaultServiceNetwork {
68-
glog.V(2).Infoln("No CLUSTER_LOCAL_GATEWAY")
69-
} else {
70-
71-
glog.V(2).Infoln("CLUSTER_LOCAL_GATEWAY", DefaultServiceNetwork)
72-
}
73-
74-
tgNameLengthMode := os.Getenv("TARGET_GROUP_NAME_LEN_MODE")
75-
76-
glog.V(2).Infoln("TARGET_GROUP_NAME_LEN_MODE", tgNameLengthMode)
77-
78-
if tgNameLengthMode == "long" {
79-
UseLongTGName = true
80-
} else {
81-
UseLongTGName = false
82-
}
8355

8456
sess, _ := session.NewSession()
8557
metadata := NewEC2Metadata(sess)
86-
8758
var err error
88-
if ifRunningInCluster() {
59+
60+
// CLUSTER_VPC_ID
61+
VpcID = os.Getenv(CLUSTER_VPC_ID)
62+
if VpcID != UnknownInput {
63+
glog.V(2).Infoln("CLUSTER_VPC_ID passed as input:", VpcID)
64+
} else {
8965
VpcID, err = metadata.VpcID()
66+
glog.V(2).Infoln("CLUSTER_VPC_ID from IMDS config discovery :", VpcID)
9067
if err != nil {
91-
return
68+
glog.V(2).Infoln("IMDS config discovery for CLUSTER_VPC_ID is NOT AVAILABLE :", err)
9269
}
70+
}
71+
72+
// REGION
73+
Region = os.Getenv(REGION)
74+
if Region != UnknownInput {
75+
glog.V(2).Infoln("REGION passed as input:", Region)
76+
} else {
9377
Region, err = metadata.Region()
78+
glog.V(2).Infoln("REGION from IMDS config discovery :", Region)
9479
if err != nil {
95-
return
80+
glog.V(2).Infoln("IMDS config discovery for REGION is NOT AVAILABLE :", err)
9681
}
82+
}
83+
84+
// AWS_ACCOUNT_ID
85+
AccountID = os.Getenv(AWS_ACCOUNT_ID)
86+
if AccountID != UnknownInput {
87+
glog.V(2).Infoln("AWS_ACCOUNT_ID passed as input:", AccountID)
88+
} else {
9789
AccountID, err = metadata.AccountId()
90+
glog.V(2).Infoln("AWS_ACCOUNT_ID from IMDS config discovery :", AccountID)
9891
if err != nil {
99-
return
92+
glog.V(2).Infoln("IMDS config discovery for AWS_ACCOUNT_ID is NOT AVAILABLE :", err)
10093
}
101-
glog.V(2).Infoln("INSIDE CLUSTER CLUSTER_VPC_ID: ", VpcID)
102-
glog.V(2).Infoln("INSIDE CLUSTER REGION: ", Region)
103-
glog.V(2).Infoln("INSIDE CLUSTER ACCOUNT_ID: ", AccountID)
10494
}
105-
}
10695

107-
func ifRunningInCluster() bool {
108-
_, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount")
109-
if err == nil {
110-
glog.V(2).Infoln("Controller is running inside cluster")
111-
return true
112-
}
96+
// GATEWAY_API_CONTROLLER_LOGLEVEL
97+
logLevel = os.Getenv(GATEWAY_API_CONTROLLER_LOGLEVEL)
98+
glog.V(2).Infoln("Logging Level:", os.Getenv(GATEWAY_API_CONTROLLER_LOGLEVEL))
11399

114-
if os.IsNotExist(err) {
115-
glog.V(2).Infoln("Controller is NOT running inside cluster")
116-
return false
100+
// CLUSTER_LOCAL_GATEWAY
101+
DefaultServiceNetwork = os.Getenv(CLUSTER_LOCAL_GATEWAY)
102+
if DefaultServiceNetwork == UnknownInput {
103+
glog.V(2).Infoln("No CLUSTER_LOCAL_GATEWAY")
104+
} else {
105+
glog.V(2).Infoln("CLUSTER_LOCAL_GATEWAY", DefaultServiceNetwork)
117106
}
118107

119-
glog.V(2).Infoln("Controller is NOT running inside cluster")
120-
return false
108+
// TARGET_GROUP_NAME_LEN_MODE
109+
tgNameLengthMode := os.Getenv(TARGET_GROUP_NAME_LEN_MODE)
110+
glog.V(2).Infoln("TARGET_GROUP_NAME_LEN_MODE", tgNameLengthMode)
111+
112+
if tgNameLengthMode == "long" {
113+
UseLongTGName = true
114+
} else {
115+
UseLongTGName = false
116+
}
121117
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package config
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
8+
9+
func Test_config_init_with_partial_env_var(t *testing.T) {
10+
// Test variable
11+
testRegion := "us-west-2"
12+
testClusterVpcId := "vpc-123456"
13+
testClusterLocalGateway := "default"
14+
15+
os.Setenv(REGION, testRegion)
16+
os.Setenv(CLUSTER_VPC_ID, testClusterVpcId)
17+
os.Setenv(CLUSTER_LOCAL_GATEWAY, testClusterLocalGateway)
18+
os.Unsetenv(AWS_ACCOUNT_ID)
19+
os.Unsetenv(TARGET_GROUP_NAME_LEN_MODE)
20+
ConfigInit()
21+
assert.Equal(t, Region, testRegion)
22+
assert.Equal(t, VpcID, testClusterVpcId)
23+
assert.Equal(t, AccountID, UnknownInput)
24+
assert.Equal(t, DefaultServiceNetwork, testClusterLocalGateway)
25+
assert.Equal(t, UseLongTGName, false)
26+
}
27+
28+
func Test_config_init_no_env_var(t *testing.T) {
29+
os.Unsetenv(REGION)
30+
os.Unsetenv(CLUSTER_VPC_ID)
31+
os.Unsetenv(CLUSTER_LOCAL_GATEWAY)
32+
os.Unsetenv(AWS_ACCOUNT_ID)
33+
os.Unsetenv(TARGET_GROUP_NAME_LEN_MODE)
34+
ConfigInit()
35+
assert.Equal(t, Region, UnknownInput)
36+
assert.Equal(t, VpcID, UnknownInput)
37+
assert.Equal(t, AccountID, UnknownInput)
38+
assert.Equal(t, DefaultServiceNetwork, UnknownInput)
39+
assert.Equal(t, UseLongTGName, false)
40+
}
41+
42+
func Test_config_init_with_all_env_var(t *testing.T) {
43+
// Test variable
44+
testRegion := "us-west-2"
45+
testClusterVpcId := "vpc-123456"
46+
testClusterLocalGateway := "default"
47+
testTargetGroupNameLenMode := "long"
48+
testAwsAccountId := "12345678"
49+
50+
os.Setenv(REGION, testRegion)
51+
os.Setenv(CLUSTER_VPC_ID, testClusterVpcId)
52+
os.Setenv(CLUSTER_LOCAL_GATEWAY, testClusterLocalGateway)
53+
os.Setenv(AWS_ACCOUNT_ID, testAwsAccountId)
54+
os.Setenv(TARGET_GROUP_NAME_LEN_MODE, testTargetGroupNameLenMode)
55+
ConfigInit()
56+
assert.Equal(t, Region, testRegion)
57+
assert.Equal(t, VpcID, testClusterVpcId)
58+
assert.Equal(t, AccountID, testAwsAccountId)
59+
assert.Equal(t, DefaultServiceNetwork, testClusterLocalGateway)
60+
assert.Equal(t, UseLongTGName, true)
61+
}

pkg/config/ec2_metadata.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func (c *defaultEC2Metadata) VpcID() (string, error) {
3434
if err != nil {
3535
return "", err
3636
}
37-
fmt.Println("Get VPC ID from ec2 metadata: ", vpcID)
3837
return vpcID, nil
3938
}
4039

@@ -43,7 +42,6 @@ func (c *defaultEC2Metadata) Region() (string, error) {
4342
if err != nil {
4443
return "", err
4544
}
46-
fmt.Println("Get region from ec2 metadata: ", region)
4745
return region, nil
4846
}
4947

0 commit comments

Comments
 (0)