Skip to content

Commit 83f3b42

Browse files
Merge branch 'master' into fix/app-params-import
2 parents a27d899 + 1a027ce commit 83f3b42

File tree

6 files changed

+160
-45
lines changed

6 files changed

+160
-45
lines changed

provider/aws/formation/rack.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
"Value": { "Ref": "Cluster" }
297297
},
298298
"CustomTopic": {
299+
"Export": { "Name": { "Fn::Sub": "${AWS::StackName}:CustomTopic" } },
299300
"Value": { "Fn::GetAtt": [ "CustomTopic", "Arn" ] }
300301
},
301302
"CustomerManagedKey": {

provider/aws/formation/service.json.tmpl

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,22 @@
212212
}
213213
},
214214
"Resources": {
215+
"MinCount": {
216+
"Type": "Custom::MathMin",
217+
"Properties": {
218+
"ServiceToken": { "Fn::ImportValue": { "Fn::Sub": "${Rack}:CustomTopic" } },
219+
"X": "{{.Scale.Count.Min}}",
220+
"Y": { "Ref": "Count" }
221+
}
222+
},
223+
"MaxCount": {
224+
"Type": "Custom::MathMax",
225+
"Properties": {
226+
"ServiceToken": { "Fn::ImportValue": { "Fn::Sub": "${Rack}:CustomTopic" } },
227+
"X": "{{.Scale.Count.Max}}",
228+
"Y": { "Ref": "Count" }
229+
}
230+
},
215231
"AutoscalingRole": {
216232
"Type": "AWS::IAM::Role",
217233
"Properties": {
@@ -508,12 +524,12 @@
508524
}
509525
},
510526
{{ end }}
511-
{{ if $.Autoscale }}
527+
{{ if and ($.Autoscale) (not .Agent.Enabled) }}
512528
"AutoscalingTarget": {
513529
"Type": "AWS::ApplicationAutoScaling::ScalableTarget",
514530
"Properties": {
515-
"MaxCapacity": "{{.Scale.Count.Max}}",
516-
"MinCapacity": "{{.Scale.Count.Min}}",
531+
"MaxCapacity": { "Fn::GetAtt": [ "MaxCount", "Value" ] },
532+
"MinCapacity": { "Fn::GetAtt": [ "MinCount", "Value" ] },
517533
"ResourceId": { "Fn::Sub": [ "service/${Cluster}/${Service.Name}", { "Cluster": { "Fn::ImportValue": { "Fn::Sub": "${Rack}:Cluster" } } } ] },
518534
"RoleARN": { "Fn::GetAtt": [ "AutoscalingRole", "Arn" ] },
519535
"ScalableDimension": "ecs:service:DesiredCount",

provider/aws/lambda/formation/handler/formation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ func HandleRequest(freq Request) error {
182182
physical, outputs, err = HandleSGIngress(freq)
183183
case "Custom::SNSSubscription":
184184
physical, outputs, err = HandleSNSSubcription(freq)
185+
case "Custom::MathMax":
186+
physical, outputs, err = HandleMathMax(freq)
187+
case "Custom::MathMin":
188+
physical, outputs, err = HandleMathMin(freq)
185189
default:
186190
physical = ""
187191
err = fmt.Errorf("unknown ResourceType: %s", freq.ResourceType)
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"time"
7+
)
8+
9+
func HandleMathMax(req Request) (string, map[string]string, error) {
10+
defer recoverFailure(req)
11+
12+
switch req.RequestType {
13+
case "Create":
14+
fmt.Println("CREATING Max")
15+
fmt.Printf("req %+v\n", req)
16+
return CreateMathMax(req)
17+
case "Update":
18+
fmt.Println("UPDATING Max")
19+
fmt.Printf("req %+v\n", req)
20+
return UpdateMathMax(req)
21+
case "Delete":
22+
fmt.Println("no need to delete")
23+
fmt.Printf("req %+v\n", req)
24+
return req.PhysicalResourceId, nil, nil
25+
}
26+
27+
return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
28+
}
29+
30+
func HandleMathMin(req Request) (string, map[string]string, error) {
31+
defer recoverFailure(req)
32+
33+
switch req.RequestType {
34+
case "Create":
35+
fmt.Println("CREATING Min")
36+
fmt.Printf("req %+v\n", req)
37+
return CreateMathMin(req)
38+
case "Update":
39+
fmt.Println("UPDATING Min")
40+
fmt.Printf("req %+v\n", req)
41+
return UpdateMathMin(req)
42+
case "Delete":
43+
fmt.Println("no need to delete")
44+
fmt.Printf("req %+v\n", req)
45+
return req.PhysicalResourceId, nil, nil
46+
}
47+
48+
return "", nil, fmt.Errorf("unknown RequestType: %s", req.RequestType)
49+
}
50+
51+
func CreateMathMax(req Request) (string, map[string]string, error) {
52+
val, err := mathMax(req)
53+
if err != nil {
54+
return "invalid", nil, err
55+
}
56+
57+
return fmt.Sprintf("mathmax-%d", time.Now().UnixNano()), map[string]string{
58+
"Value": val,
59+
}, nil
60+
}
61+
62+
func UpdateMathMax(req Request) (string, map[string]string, error) {
63+
val, err := mathMax(req)
64+
if err != nil {
65+
return "invalid", nil, err
66+
}
67+
68+
return req.PhysicalResourceId, map[string]string{
69+
"Value": val,
70+
}, nil
71+
}
72+
73+
func CreateMathMin(req Request) (string, map[string]string, error) {
74+
val, err := mathMin(req)
75+
if err != nil {
76+
return "invalid", nil, err
77+
}
78+
79+
return fmt.Sprintf("mathmin-%d", time.Now().UnixNano()), map[string]string{
80+
"Value": val,
81+
}, nil
82+
}
83+
84+
func UpdateMathMin(req Request) (string, map[string]string, error) {
85+
val, err := mathMin(req)
86+
if err != nil {
87+
return "invalid", nil, err
88+
}
89+
90+
return req.PhysicalResourceId, map[string]string{
91+
"Value": val,
92+
}, nil
93+
}
94+
95+
func mathMax(req Request) (string, error) {
96+
x, y, err := parseXY(req)
97+
if err != nil {
98+
return "", err
99+
}
100+
101+
if y > x {
102+
x = y
103+
}
104+
105+
return strconv.Itoa(x), nil
106+
}
107+
108+
func mathMin(req Request) (string, error) {
109+
x, y, err := parseXY(req)
110+
if err != nil {
111+
return "", err
112+
}
113+
114+
if y < x {
115+
x = y
116+
}
117+
118+
return strconv.Itoa(x), nil
119+
}
120+
121+
func parseXY(req Request) (int, int, error) {
122+
xStr := req.ResourceProperties["X"].(string)
123+
yStr := req.ResourceProperties["Y"].(string)
124+
125+
x, err := strconv.Atoi(xStr)
126+
if err != nil {
127+
return 0, 0, err
128+
}
129+
130+
y, err := strconv.Atoi(yStr)
131+
if err != nil {
132+
return 0, 0, err
133+
}
134+
return x, y, nil
135+
}

provider/aws/releases.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/aws/aws-sdk-go/aws"
1515
"github.com/aws/aws-sdk-go/service/cloudformation"
1616
"github.com/aws/aws-sdk-go/service/dynamodb"
17-
"github.com/aws/aws-sdk-go/service/ecs"
1817
"github.com/aws/aws-sdk-go/service/eventbridge"
1918
"github.com/aws/aws-sdk-go/service/iam"
2019
"github.com/aws/aws-sdk-go/service/s3"
@@ -340,7 +339,7 @@ func (p *Provider) ReleasePromote(app, id string, opts structs.ReleasePromoteOpt
340339
tp[fmt.Sprintf("ResourceTemplate%s", upperName(r.Name))] = ou.Url
341340
}
342341

343-
for i, s := range m.Services {
342+
for _, s := range m.Services {
344343
min := s.Deployment.Minimum
345344
max := s.Deployment.Maximum
346345

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

388387
tp[fmt.Sprintf("ServiceTemplate%s", upperName(s.Name))] = ou.Url
389-
390-
sarn, err := p.serviceArn(r.App, s.Name)
391-
if err != nil {
392-
return err
393-
}
394-
395-
if sarn != "" {
396-
res, err := p.describeServices(&ecs.DescribeServicesInput{
397-
Cluster: aws.String(p.Cluster),
398-
Services: []*string{aws.String(sarn)},
399-
})
400-
if err != nil {
401-
return err
402-
}
403-
404-
// when autoscale is on set m.Services[i].Scale.Count.Min to desired count
405-
// since this is used to service count param from app
406-
if autoscale && len(res.Services) == 1 && res.Services[0].DesiredCount != nil {
407-
m.Services[i].Scale.Count.Min = int(*res.Services[0].DesiredCount)
408-
}
409-
}
410388
}
411389

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

443-
// m.Services[i].Scale.Count.Min is mutated if service autoscaling is used
444421
data, err := formationTemplate("app", tp)
445422
if err != nil {
446423
return err

provider/aws/service.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,5 @@ func (p *Provider) ServiceUpdate(app, name string, opts structs.ServiceUpdateOpt
260260
return err
261261
}
262262

263-
if opts.Count != nil {
264-
sarn, err := p.serviceArn(app, name)
265-
if err != nil {
266-
return err
267-
}
268-
269-
if sarn != "" {
270-
_, err := p.ecs().UpdateService(&ecs.UpdateServiceInput{
271-
Cluster: aws.String(p.Cluster),
272-
Service: aws.String(sarn),
273-
DesiredCount: aws.Int64(int64(*opts.Count)),
274-
})
275-
if err != nil {
276-
return err
277-
}
278-
}
279-
}
280-
281263
return nil
282264
}

0 commit comments

Comments
 (0)