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 profile option for AWS #730

Merged
merged 2 commits into from
Aug 28, 2024
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
64 changes: 31 additions & 33 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"errors"
"fmt"
"net/http"
"reflect"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
Expand All @@ -31,26 +31,6 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
if err != nil {
return nil, fmt.Errorf("error validating config: %w", err)
}

if cfg.Region == "self" {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}

conf, loadErr := config.LoadDefaultConfig(context.TODO())
if loadErr != nil {
return nil, fmt.Errorf("unable to load default AWS config: %w", loadErr)
}

client := imds.NewFromConfig(conf, func(o *imds.Options) {
o.HTTPClient = httpClient
})

response, regionErr := client.GetRegion(context.TODO(), &imds.GetRegionInput{})
if regionErr != nil {
return nil, fmt.Errorf("unable to retrieve region from ec2metadata: %w", regionErr)
}
cfg.Region = response.Region
}

awsClient.config = cfg

err = awsClient.configure()
Expand Down Expand Up @@ -83,22 +63,40 @@ func (client *AWSClient) GetUpstreams() []Upstream {

// configure configures the AWSClient with necessary parameters.
func (client *AWSClient) configure() error {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
httpClient := http.NewBuildableClient().WithTimeout(connTimeoutInSecs * time.Second)

if client.config.Region == "self" {
conf, loadErr := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigProfile(client.config.Profile),
config.WithHTTPClient(httpClient),
)
if loadErr != nil {
return fmt.Errorf("unable to load default AWS config: %w", loadErr)
}

cfg, err := config.LoadDefaultConfig(context.TODO())
imdClient := imds.NewFromConfig(conf)

response, regionErr := imdClient.GetRegion(context.TODO(), &imds.GetRegionInput{})
if regionErr != nil {
return fmt.Errorf("unable to retrieve region from ec2metadata: %w", regionErr)
}
client.config.Region = response.Region
}

cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigProfile(client.config.Profile),
config.WithRegion(client.config.Region),
config.WithHTTPClient(httpClient),
)
if err != nil {
return fmt.Errorf("unable to load default AWS config: %w", err)
}

client.svcEC2 = ec2.NewFromConfig(cfg, func(o *ec2.Options) {
o.Region = client.config.Region
o.HTTPClient = httpClient
})
client.svcEC2 = ec2.NewFromConfig(cfg)

client.svcAutoscaling = autoscaling.NewFromConfig(cfg, func(o *autoscaling.Options) {
o.Region = client.config.Region
o.HTTPClient = httpClient
})
client.svcAutoscaling = autoscaling.NewFromConfig(cfg)

return nil
}
Expand Down Expand Up @@ -239,10 +237,10 @@ func prepareBatches(maxItems int, items []string) [][]string {
return batches
}

// Configuration for AWS Cloud Provider

// Configuration for AWS Cloud Provider.
type awsConfig struct {
Region string `yaml:"region"`
Profile string `yaml:"profile"`
Upstreams []awsUpstream `yaml:"upstreams"`
}

Expand Down
1 change: 1 addition & 0 deletions cmd/sync/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func getValidAWSConfig() *awsConfig {
cfg := awsConfig{
Region: "us-west-2",
Upstreams: upstreams,
Profile: "default",
}

return &cfg
Expand Down
2 changes: 2 additions & 0 deletions examples/aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ region: us-west-2
api_endpoint: http://127.0.0.1:8080/api
sync_interval: 5s
cloud_provider: AWS
profile: default
upstreams:
- name: backend-one
autoscaling_group: backend-one-group
Expand Down Expand Up @@ -54,6 +55,7 @@ upstreams:
empty if using AWS. Possible values are: `AWS`, `Azure`.
- The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups. Setting `region` to
`self` will use the EC2 Metadata service to retrieve the region of the current instance.
- The optional `profile` key specifies the AWS profile to use.
- The `upstreams` key defines the list of upstream groups. For each upstream group we specify:
- `name` – The name we specified for the upstream block in the NGINX Plus configuration.
- `autoscaling_group` – The name of the corresponding Auto Scaling group. Use of wildcards is supported. For example,
Expand Down
Loading