Skip to content

Commit

Permalink
Fix autoscale desired count
Browse files Browse the repository at this point in the history
  • Loading branch information
nightfury1204 committed Jul 31, 2024
1 parent d9ee34d commit 149acf3
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 44 deletions.
1 change: 1 addition & 0 deletions provider/aws/formation/rack.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@
"Value": { "Ref": "Cluster" }
},
"CustomTopic": {
"Export": { "Name": { "Fn::Sub": "${AWS::StackName}:CustomTopic" } },
"Value": { "Fn::GetAtt": [ "CustomTopic", "Arn" ] }
},
"CustomerManagedKey": {
Expand Down
20 changes: 18 additions & 2 deletions provider/aws/formation/service.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@
}
},
"Resources": {
"MinCount": {
"Type": "Custom::MathMin",
"Properties": {
"ServiceToken": { "Fn::ImportValue": { "Fn::Sub": "${Rack}:CustomTopic" } },
"X": "{{.Scale.Count.Min}}",
"Y": { "Ref": "Count" }
}
},
"MaxCount": {
"Type": "Custom::MathMax",
"Properties": {
"ServiceToken": { "Fn::ImportValue": { "Fn::Sub": "${Rack}:CustomTopic" } },
"X": "{{.Scale.Count.Max}}",
"Y": { "Ref": "Count" }
}
},
"AutoscalingRole": {
"Type": "AWS::IAM::Role",
"Properties": {
Expand Down Expand Up @@ -512,8 +528,8 @@
"AutoscalingTarget": {
"Type": "AWS::ApplicationAutoScaling::ScalableTarget",
"Properties": {
"MaxCapacity": "{{.Scale.Count.Max}}",
"MinCapacity": "{{.Scale.Count.Min}}",
"MaxCapacity": { "Fn::GetAtt": [ "MaxCount", "Value" ] },
"MinCapacity": { "Fn::GetAtt": [ "MinCount", "Value" ] },
"ResourceId": { "Fn::Sub": [ "service/${Cluster}/${Service.Name}", { "Cluster": { "Fn::ImportValue": { "Fn::Sub": "${Rack}:Cluster" } } } ] },
"RoleARN": { "Fn::GetAtt": [ "AutoscalingRole", "Arn" ] },
"ScalableDimension": "ecs:service:DesiredCount",
Expand Down
4 changes: 4 additions & 0 deletions provider/aws/lambda/formation/handler/formation.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func HandleRequest(freq Request) error {
physical, outputs, err = HandleSGIngress(freq)
case "Custom::SNSSubscription":
physical, outputs, err = HandleSNSSubcription(freq)
case "Custom::MathMax":
physical, outputs, err = HandleMathMax(freq)
case "Custom::MathMin":
physical, outputs, err = HandleMathMin(freq)
default:
physical = ""
err = fmt.Errorf("unknown ResourceType: %s", freq.ResourceType)
Expand Down
135 changes: 135 additions & 0 deletions provider/aws/lambda/formation/handler/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package handler

import (
"fmt"
"strconv"
"time"
)

func HandleMathMax(req Request) (string, map[string]string, error) {
defer recoverFailure(req)

switch req.RequestType {
case "Create":
fmt.Println("CREATING Max")
fmt.Printf("req %+v\n", req)
return CreateMathMax(req)
case "Update":
fmt.Println("UPDATING Max")
fmt.Printf("req %+v\n", req)
return UpdateMathMax(req)
case "Delete":
fmt.Println("no need to delete")
fmt.Printf("req %+v\n", req)
return req.PhysicalResourceId, nil, nil
}

return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
}

func HandleMathMin(req Request) (string, map[string]string, error) {
defer recoverFailure(req)

switch req.RequestType {
case "Create":
fmt.Println("CREATING Min")
fmt.Printf("req %+v\n", req)
return CreateMathMin(req)
case "Update":
fmt.Println("UPDATING Min")
fmt.Printf("req %+v\n", req)
return UpdateMathMin(req)
case "Delete":
fmt.Println("no need to delete")
fmt.Printf("req %+v\n", req)
return req.PhysicalResourceId, nil, nil
}

return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
}

func CreateMathMax(req Request) (string, map[string]string, error) {
val, err := mathMax(req)
if err != nil {
return "invalid", nil, err
}

return fmt.Sprintf("mathmax-%d", time.Now().UnixNano()), map[string]string{
"Value": val,
}, nil
}

func UpdateMathMax(req Request) (string, map[string]string, error) {
val, err := mathMax(req)
if err != nil {
return "invalid", nil, err
}

return req.PhysicalResourceId, map[string]string{
"Value": val,
}, nil
}

func CreateMathMin(req Request) (string, map[string]string, error) {
val, err := mathMin(req)
if err != nil {
return "invalid", nil, err
}

return fmt.Sprintf("mathmin-%d", time.Now().UnixNano()), map[string]string{
"Value": val,
}, nil
}

func UpdateMathMin(req Request) (string, map[string]string, error) {
val, err := mathMin(req)
if err != nil {
return "invalid", nil, err
}

return req.PhysicalResourceId, map[string]string{
"Value": val,
}, nil
}

func mathMax(req Request) (string, error) {
x, y, err := parseXY(req)
if err != nil {
return "", err
}

if y > x {
x = y
}

return strconv.Itoa(x), nil
}

func mathMin(req Request) (string, error) {
x, y, err := parseXY(req)
if err != nil {
return "", err
}

if y < x {
x = y
}

return strconv.Itoa(x), nil
}

func parseXY(req Request) (int, int, error) {
xStr := req.ResourceProperties["X"].(string)
yStr := req.ResourceProperties["Y"].(string)

x, err := strconv.Atoi(xStr)
if err != nil {
return 0, 0, err
}

y, err := strconv.Atoi(yStr)
if err != nil {
return 0, 0, err
}
return x, y, nil
}
25 changes: 1 addition & 24 deletions provider/aws/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/eventbridge"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -340,7 +339,7 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
tp[fmt.Sprintf("ResourceTemplate%s", upperName(r.Name))] = ou.Url
}

for i, s := range m.Services {
for _, s := range m.Services {
min := s.Deployment.Minimum
max := s.Deployment.Maximum

Expand Down Expand Up @@ -386,27 +385,6 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
}

tp[fmt.Sprintf("ServiceTemplate%s", upperName(s.Name))] = ou.Url

sarn, err := p.serviceArn(r.App, s.Name)
if err != nil {
return err
}

if sarn != "" {
res, err := p.describeServices(&ecs.DescribeServicesInput{
Cluster: aws.String(p.Cluster),
Services: []*string{aws.String(sarn)},
})
if err != nil {
return err
}

// when autoscale is on set m.Services[i].Scale.Count.Min to desired count
// since this is used to service count param from app
if autoscale && len(res.Services) == 1 && res.Services[0].DesiredCount != nil {
m.Services[i].Scale.Count.Min = int(*res.Services[0].DesiredCount)
}
}
}

for _, t := range m.Timers {
Expand Down Expand Up @@ -440,7 +418,6 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
tp[fmt.Sprintf("TimerTemplate%s", upperName(t.Name))] = ou.Url
}

// m.Services[i].Scale.Count.Min is mutated if service autoscaling is used
data, err := formationTemplate("app", tp)
if err != nil {
return err
Expand Down
18 changes: 0 additions & 18 deletions provider/aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,23 +260,5 @@ func (p *Provider) ServiceUpdate(app, name string, opts structs.ServiceUpdateOpt
return err
}

if opts.Count != nil {
sarn, err := p.serviceArn(app, name)
if err != nil {
return err
}

if sarn != "" {
_, err := p.ecs().UpdateService(&ecs.UpdateServiceInput{
Cluster: aws.String(p.Cluster),
Service: aws.String(sarn),
DesiredCount: aws.Int64(int64(*opts.Count)),
})
if err != nil {
return err
}
}
}

return nil
}

0 comments on commit 149acf3

Please sign in to comment.