Skip to content

Commit

Permalink
Fix scale when service auto scaling is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
nightfury1204 committed Jul 4, 2024
1 parent 3e055fa commit 36a2921
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
12 changes: 2 additions & 10 deletions provider/aws/formation/service.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@
}
},
{{ end }}
{{ if .Autoscale }}
{{ if $.Autoscale }}
"AutoscalingTarget": {
"Type": "AWS::ApplicationAutoScaling::ScalableTarget",
"Properties": {
Expand Down Expand Up @@ -568,15 +568,7 @@
{{ if .Agent.Enabled }}
"SchedulingStrategy": "DAEMON",
{{ else }}
{{ if .Autoscale }}
{{ with $.CurrentDesiredCount }}
"DesiredCount": "{{.}}",
{{ else }}
"DesiredCount": "{{$.Service.Scale.Count.Min}}",
{{ end }}
{{ else }}
"DesiredCount": { "Ref": "Count" },
{{ end }}
"DesiredCount": { "Ref": "Count" },
"SchedulingStrategy": "REPLICA",
"PlacementStrategies": { "Fn::If": [ "FargateEither",
{ "Ref": "AWS::NoValue" },
Expand Down
44 changes: 29 additions & 15 deletions provider/aws/releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
tp[fmt.Sprintf("ResourceTemplate%s", upperName(r.Name))] = ou.Url
}

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

Expand All @@ -352,8 +352,19 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
max = *opts.Max
}

stackAutoscale, err := p.stackParameter(p.Rack, "Autoscale")
if err != nil {
return err
}

autoscale := false
if stackAutoscale == "Yes" {
autoscale = true
}

stp := map[string]interface{}{
"App": r.App,
"Autoscale": autoscale,
"Build": tp["Build"],
"DeploymentMin": min,
"DeploymentMax": max,
Expand All @@ -364,6 +375,18 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
"WildcardDomain": tp["WildcardDomain"],
}

data, err := formationTemplate("service", stp)
if err != nil {
return err
}

ou, err := p.ObjectStore(app, "", bytes.NewReader(data), structs.ObjectStoreOptions{Presign: options.Bool(true)})
if err != nil {
return err
}

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

sarn, err := p.serviceArn(r.App, s.Name)
if err != nil {
return err
Expand All @@ -378,22 +401,12 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
return err
}

if len(res.Services) == 1 && res.Services[0].DesiredCount != nil {
stp["CurrentDesiredCount"] = *res.Services[0].DesiredCount
// 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)
}
}

data, err := formationTemplate("service", stp)
if err != nil {
return err
}

ou, err := p.ObjectStore(app, "", bytes.NewReader(data), structs.ObjectStoreOptions{Presign: options.Bool(true)})
if err != nil {
return err
}

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

for _, t := range m.Timers {
Expand Down Expand Up @@ -427,6 +440,7 @@ 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: 18 additions & 0 deletions provider/aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,23 @@ 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 36a2921

Please sign in to comment.