Skip to content

Commit e040c6f

Browse files
committed
Query node group min and max size from AWS ASG API (#576)
(cherry picked from commit c7f430f)
1 parent e3053cb commit e040c6f

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

pkg/lib/aws/autoscaling.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2019 Cortex Labs, Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package aws
18+
19+
import (
20+
"github.com/aws/aws-sdk-go/service/autoscaling"
21+
"github.com/cortexlabs/cortex/pkg/lib/errors"
22+
)
23+
24+
// if specified, all tags must be present
25+
func (c *Client) AutoscalingGroups(tags map[string]string) ([]*autoscaling.Group, error) {
26+
var asgs []*autoscaling.Group
27+
28+
err := c.autoscaling.DescribeAutoScalingGroupsPages(nil,
29+
func(page *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
30+
for _, asg := range page.AutoScalingGroups {
31+
asgTags := make(map[string]string, len(asg.Tags))
32+
for _, asgTag := range asg.Tags {
33+
if asgTag.Key != nil && asgTag.Value != nil {
34+
asgTags[*asgTag.Key] = *asgTag.Value
35+
}
36+
}
37+
38+
missingTag := false
39+
for key, value := range tags {
40+
if asgTags[key] != value {
41+
missingTag = true
42+
break
43+
}
44+
}
45+
46+
if missingTag {
47+
continue
48+
}
49+
50+
asgs = append(asgs, asg)
51+
}
52+
53+
return true
54+
})
55+
56+
if err != nil {
57+
return nil, errors.WithStack(err)
58+
}
59+
60+
return asgs, nil
61+
}

pkg/lib/aws/aws.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package aws
1919
import (
2020
"github.com/aws/aws-sdk-go/aws"
2121
"github.com/aws/aws-sdk-go/aws/session"
22+
"github.com/aws/aws-sdk-go/service/autoscaling"
2223
"github.com/aws/aws-sdk-go/service/cloudwatch"
2324
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
2425
"github.com/aws/aws-sdk-go/service/s3"
@@ -33,6 +34,7 @@ type Client struct {
3334
Bucket string
3435
S3 *s3.S3
3536
stsClient *sts.STS
37+
autoscaling *autoscaling.AutoScaling
3638
CloudWatchLogsClient *cloudwatchlogs.CloudWatchLogs
3739
CloudWatchMetrics *cloudwatch.CloudWatch
3840
AccountID string
@@ -50,6 +52,7 @@ func New(region string, bucket string, withAccountID bool) (*Client, error) {
5052
Region: region,
5153
S3: s3.New(sess),
5254
stsClient: sts.New(sess),
55+
autoscaling: autoscaling.New(sess),
5356
CloudWatchMetrics: cloudwatch.New(sess),
5457
CloudWatchLogsClient: cloudwatchlogs.New(sess),
5558
}

pkg/operator/endpoints/info.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,30 @@ limitations under the License.
1717
package endpoints
1818

1919
import (
20+
"fmt"
2021
"net/http"
2122

23+
"github.com/cortexlabs/cortex/pkg/lib/errors"
2224
"github.com/cortexlabs/cortex/pkg/operator/api/schema"
2325
"github.com/cortexlabs/cortex/pkg/operator/config"
2426
)
2527

2628
func Info(w http.ResponseWriter, r *http.Request) {
29+
asgs, err := config.AWS.AutoscalingGroups(map[string]string{"alpha.eksctl.io/nodegroup-name": "ng-1"})
30+
if err != nil {
31+
RespondError(w, errors.WithStack(err))
32+
return
33+
}
34+
35+
if len(asgs) != 1 {
36+
message := fmt.Sprintf("found %d matching autoscaling groups, expected only one", len(asgs))
37+
RespondError(w, errors.New(message)) // unexpected
38+
return
39+
}
40+
41+
config.Cluster.MinInstances = asgs[0].MinSize
42+
config.Cluster.MaxInstances = asgs[0].MaxSize
43+
2744
response := schema.InfoResponse{ClusterConfig: config.Cluster}
2845
Respond(w, response)
2946
}

0 commit comments

Comments
 (0)