Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add AWS_EC2_ENDPOINT variable for custom endpoint (#2317) #2326

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,14 @@ Default: empty
Specify a comma-separated list of IPv4 CIDRs that *must* be routed via main routing table. This is required for secondary ENIs to reach endpoints outside of VPC that are backed by a service.
For every item in the list, an `ip rule` will be created with a priority greater than the `ip rule` capturing egress traffic from the container. If an item is not a valid IPv4 CIDR, it will be skipped.

#### `AWS_EC2_ENDPOINT` (v1.13.0+)

Type: String

Default: empty

Specify the EC2 endpoint to use. This is useful if you are using a custom endpoint for EC2. For example, if you are using a proxy for EC2, you can set this to the proxy endpoint. Any kind of URL or IP address is valid such as `https://localhost:8080` or `http://ec2.us-west-2.customaws.com`. If this is not set, the default EC2 endpoint will be used.

### VPC CNI Feature Matrix

IP Mode | Secondary IP Mode | Prefix Delegation | Security Groups Per Pod | WARM & MIN IP/Prefix Targets | External SNAT
Expand Down
15 changes: 15 additions & 0 deletions pkg/awsutils/awssession/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)

// Http client timeout env for sessions
Expand Down Expand Up @@ -65,6 +66,20 @@ func New() *session.Session {
},
STSRegionalEndpoint: endpoints.RegionalSTSEndpoint,
}

endpoint := os.Getenv("AWS_EC2_ENDPOINT")
if endpoint != "" {
customResolver := func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
if service == ec2.EndpointsID {
return endpoints.ResolvedEndpoint{
URL: endpoint,
}, nil
}
return endpoints.DefaultResolver().EndpointFor(service, region, optFns...)
}
awsCfg.EndpointResolver = endpoints.ResolverFunc(customResolver)
}

sess := session.Must(session.NewSession(&awsCfg))
//injecting session handler info
injectUserAgent(&sess.Handlers)
Expand Down
14 changes: 14 additions & 0 deletions pkg/awsutils/awssession/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/service/ec2"
"github.com/stretchr/testify/assert"
)

Expand All @@ -21,3 +22,16 @@ func TestHttpTimeoutWithValueAbove10(t *testing.T) {
expectedHTTPTimeOut := time.Duration(12) * time.Second
assert.Equal(t, expectedHTTPTimeOut, getHTTPTimeout())
}

func TestAwsEc2EndpointResolver(t *testing.T) {
customEndpoint := "https://ec2.us-west-2.customaws.com"

os.Setenv("AWS_EC2_ENDPOINT", customEndpoint)
defer os.Unsetenv("AWS_EC2_ENDPOINT")

sess := New()

resolvedEndpoint, err := sess.Config.EndpointResolver.EndpointFor(ec2.EndpointsID, "")
assert.NoError(t, err)
assert.Equal(t, customEndpoint, resolvedEndpoint.URL)
}