Skip to content

Commit 055ef27

Browse files
committed
Use DescribeInstance to retrieve filtered AWS instances
initial implementation for issue #28
1 parent f118bb0 commit 055ef27

File tree

5 files changed

+45
-385
lines changed

5 files changed

+45
-385
lines changed

Gopkg.lock

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/sync/aws.go

Lines changed: 37 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,15 @@ import (
77

88
"github.com/aws/aws-sdk-go/aws"
99
"github.com/aws/aws-sdk-go/aws/session"
10-
"github.com/aws/aws-sdk-go/service/autoscaling"
11-
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
1210
"github.com/aws/aws-sdk-go/service/ec2"
1311
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
1412
yaml "gopkg.in/yaml.v2"
1513
)
1614

1715
// AWSClient allows you to get the list of IP addresses of instanes of an Auto Scaling group. It implements the CloudProvider interface
1816
type AWSClient struct {
19-
svcEC2 ec2iface.EC2API
20-
svcAutoscaling autoscalingiface.AutoScalingAPI
21-
config *awsConfig
17+
svcEC2 ec2iface.EC2API
18+
config *awsConfig
2219
}
2320

2421
// NewAWSClient creates and configures an AWSClient
@@ -64,10 +61,8 @@ func (client *AWSClient) configure() error {
6461
return err
6562
}
6663

67-
svcAutoscaling := autoscaling.New(session)
6864
svcEC2 := ec2.New(session)
6965
client.svcEC2 = svcEC2
70-
client.svcAutoscaling = svcAutoscaling
7166
return nil
7267
}
7368

@@ -89,79 +84,58 @@ func parseAWSConfig(data []byte) (*awsConfig, error) {
8984

9085
// CheckIfScalingGroupExists checks if the Auto Scaling group exists
9186
func (client *AWSClient) CheckIfScalingGroupExists(name string) (bool, error) {
92-
_, exists, err := client.getAutoscalingGroup(name)
93-
if err != nil {
94-
return exists, fmt.Errorf("couldn't check if an AutoScaling group exists: %v", err)
95-
}
96-
return exists, nil
97-
}
98-
99-
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Auto Scaling group
100-
func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
101-
group, exists, err := client.getAutoscalingGroup(name)
102-
if err != nil {
103-
return nil, err
104-
}
105-
106-
if !exists {
107-
return nil, fmt.Errorf("autoscaling group %v doesn't exist", name)
87+
params := &ec2.DescribeInstancesInput{
88+
Filters: []*ec2.Filter{
89+
&ec2.Filter{
90+
Name: aws.String("tag:aws:autoscaling:groupName"),
91+
Values: []*string{
92+
aws.String(name),
93+
},
94+
},
95+
},
10896
}
10997

110-
instances, err := client.getInstancesOfAutoscalingGroup(group)
98+
response, err := client.svcEC2.DescribeInstances(params)
11199
if err != nil {
112-
return nil, err
100+
return false, fmt.Errorf("couldn't check if an AutoScaling group exists: %v", err)
113101
}
114102

115-
var result []string
116-
for _, ins := range instances {
117-
if len(ins.NetworkInterfaces) > 0 && ins.NetworkInterfaces[0].PrivateIpAddress != nil {
118-
result = append(result, *ins.NetworkInterfaces[0].PrivateIpAddress)
119-
}
103+
if len(response.Reservations) == 0 {
104+
return false, nil
120105
}
121106

122-
return result, nil
107+
return true, nil
123108
}
124109

125-
func (client *AWSClient) getAutoscalingGroup(name string) (*autoscaling.Group, bool, error) {
126-
params := &autoscaling.DescribeAutoScalingGroupsInput{
127-
AutoScalingGroupNames: []*string{
128-
aws.String(name),
110+
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Auto Scaling group
111+
func (client *AWSClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
112+
params := &ec2.DescribeInstancesInput{
113+
Filters: []*ec2.Filter{
114+
&ec2.Filter{
115+
Name: aws.String("tag:aws:autoscaling:groupName"),
116+
Values: []*string{
117+
aws.String(name),
118+
},
119+
},
129120
},
130121
}
131122

132-
resp, err := client.svcAutoscaling.DescribeAutoScalingGroups(params)
123+
response, err := client.svcEC2.DescribeInstances(params)
133124
if err != nil {
134-
return nil, false, err
135-
}
136-
137-
if len(resp.AutoScalingGroups) != 1 {
138-
return nil, false, nil
139-
}
140-
141-
return resp.AutoScalingGroups[0], true, nil
142-
}
143-
144-
func (client *AWSClient) getInstancesOfAutoscalingGroup(group *autoscaling.Group) ([]*ec2.Instance, error) {
145-
var result []*ec2.Instance
146-
147-
if len(group.Instances) == 0 {
148-
return result, nil
125+
return nil, err
149126
}
150127

151-
var ids []*string
152-
for _, ins := range group.Instances {
153-
ids = append(ids, ins.InstanceId)
154-
}
155-
params := &ec2.DescribeInstancesInput{
156-
InstanceIds: ids,
128+
if len(response.Reservations) == 0 {
129+
return nil, fmt.Errorf("autoscaling group %v doesn't exist", name)
157130
}
158131

159-
resp, err := client.svcEC2.DescribeInstances(params)
160-
if err != nil {
161-
return result, err
162-
}
163-
for _, res := range resp.Reservations {
164-
result = append(result, res.Instances...)
132+
var result []string
133+
for _, res := range response.Reservations {
134+
for _, ins := range res.Instances {
135+
if len(ins.NetworkInterfaces) > 0 && ins.NetworkInterfaces[0].PrivateIpAddress != nil {
136+
result = append(result, *ins.NetworkInterfaces[0].PrivateIpAddress)
137+
}
138+
}
165139
}
166140

167141
return result, nil

examples/aws.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ upstreams:
2626
autoscaling_group: backend-two-group
2727
port: 80
2828
kind: http
29+
- name: backend
30+
autoscaling_group: backend-*
31+
port: 80
32+
kind: http
2933
```
3034
3135
* The `api_endpoint` key defines the NGINX Plus API endpoint.
@@ -34,6 +38,6 @@ upstreams:
3438
* The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups.
3539
* The `upstreams` key defines the list of upstream groups. For each upstream group we specify:
3640
* `name` – The name we specified for the upstream block in the NGINX Plus configuration.
37-
* `autoscaling_group` – The name of the corresponding Auto Scaling group.
41+
* `autoscaling_group` – The name of the corresponding Auto Scaling group. Use of wildcards is supported.
3842
* `port` – The port on which our backend applications are exposed.
39-
* `kind` – The protocol of the traffic NGINX Plus load balances to the backend application, here `http`. If the application uses TCP/UDP, specify `stream` instead.
43+
* `kind` – The protocol of the traffic NGINX Plus load balances to the backend application, here `http`. If the application uses TCP/UDP, specify `stream` instead.

vendor/github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute/virtualmachinescalesetvms.go

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)