Skip to content

Reduced API calls significantly #6

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

Closed
wants to merge 3 commits into from
Closed
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
10 changes: 0 additions & 10 deletions cmd/sync/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 20 additions & 47 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,43 @@ import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)

// AWSClient allows you to get the list of IP addresses of instanes of an Auto Scaling group
type AWSClient struct {
svcEC2 ec2iface.EC2API
svcAutoscaling autoscalingiface.AutoScalingAPI
}

// NewAWSClient creates an AWSClient
func NewAWSClient(svcEC2 ec2iface.EC2API, svcAutoscaling autoscalingiface.AutoScalingAPI) *AWSClient {
return &AWSClient{svcEC2, svcAutoscaling}
}

// CheckIfAutoscalingGroupExists checks if the Auto Scaling group exists
func (client *AWSClient) CheckIfAutoscalingGroupExists(name string) (bool, error) {
_, exists, err := client.getAutoscalingGroup(name)
return exists, err
func NewAWSClient(svcEC2 ec2iface.EC2API) *AWSClient {
return &AWSClient{svcEC2,}
}

// GetPrivateIPsOfInstancesOfAutoscalingGroup returns the list of IP addresses of instanes of the Auto Scaling group
func (client *AWSClient) GetPrivateIPsOfInstancesOfAutoscalingGroup(name string) ([]string, error) {
group, exists, err := client.getAutoscalingGroup(name)
params := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Name: aws.String("tag:aws:autoscaling:groupName"),
Values: []*string{
aws.String(name),
},
},
},
}
resverations, err := client.svcEC2.DescribeInstances(params)

if err != nil {
return nil, err
}
if !exists {
if len(resverations.Reservations) == 0 {
return nil, fmt.Errorf("autoscaling group %v doesn't exists", name)
}

instances, err := client.getInstancesOfAutoscalingGroup(group)
instances, err := client.getInstancesOfReservations(resverations)

if err != nil {
return nil, err
}
Expand All @@ -52,45 +55,15 @@ func (client *AWSClient) GetPrivateIPsOfInstancesOfAutoscalingGroup(name string)
return result, nil
}

func (client *AWSClient) getAutoscalingGroup(name string) (*autoscaling.Group, bool, error) {
params := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(name),
},
}

resp, err := client.svcAutoscaling.DescribeAutoScalingGroups(params)
if err != nil {
return nil, false, err
}

if len(resp.AutoScalingGroups) != 1 {
return nil, false, nil
}

return resp.AutoScalingGroups[0], true, nil
}

func (client *AWSClient) getInstancesOfAutoscalingGroup(group *autoscaling.Group) ([]*ec2.Instance, error) {
func (client *AWSClient) getInstancesOfReservations(group *ec2.DescribeInstancesOutput) ([]*ec2.Instance, error) {
var result []*ec2.Instance

if len(group.Instances) == 0 {
if len(group.Reservations) == 0 {
return result, nil
}

var ids []*string
for _, ins := range group.Instances {
ids = append(ids, ins.InstanceId)
}
params := &ec2.DescribeInstancesInput{
InstanceIds: ids,
}

resp, err := client.svcEC2.DescribeInstances(params)
if err != nil {
return result, err
}
for _, res := range resp.Reservations {
for _, res := range group.Reservations {
for _, ins := range res.Instances {
result = append(result, ins)
}
Expand Down
13 changes: 2 additions & 11 deletions cmd/sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
)

var configFile = flag.String("config_path", "/etc/nginx/aws.yaml", "Path to the config file")
var logFile = flag.String("log_path", "", "Path to the log file. If the file doesn't exist, it will be created")
var version = "0.1-2"
var version = "0.2-0"

const connTimeoutInSecs = 10

Expand Down Expand Up @@ -68,13 +67,6 @@ func main() {
log.Printf("Problem with the NGINX configuration: %v", err)
os.Exit(10)
}
exists, err := awsClient.CheckIfAutoscalingGroupExists(ups.AutoscalingGroup)
if err != nil {
log.Printf("Couldn't check if an Auto Scaling group exists: %v", err)
os.Exit(10)
} else if !exists {
log.Printf("Warning: Auto Scaling group %v doesn't exists", ups.AutoscalingGroup)
}
}

sigterm := make(chan os.Signal, 1)
Expand Down Expand Up @@ -123,7 +115,6 @@ func createAWSClient(region string) *AWSClient {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
cfg := &aws.Config{Region: aws.String(region), HTTPClient: httpClient}
session := session.New(cfg)
svcAutoscaling := autoscaling.New(session)
svcEC2 := ec2.New(session)
return NewAWSClient(svcEC2, svcAutoscaling)
return NewAWSClient(svcEC2)
}