2020-09-30 14:35:07 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Docker Compose CLI authors
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package ecs
|
|
|
|
|
|
|
|
import (
|
2020-10-20 10:27:36 +02:00
|
|
|
"encoding/json"
|
2020-09-30 14:35:07 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
applicationautoscaling2 "github.com/aws/aws-sdk-go/service/applicationautoscaling"
|
|
|
|
"github.com/awslabs/goformation/v4/cloudformation"
|
|
|
|
"github.com/awslabs/goformation/v4/cloudformation/applicationautoscaling"
|
|
|
|
"github.com/awslabs/goformation/v4/cloudformation/iam"
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
|
)
|
|
|
|
|
2020-10-20 10:27:36 +02:00
|
|
|
type autoscalingConfig struct {
|
|
|
|
Memory int `json:"memory,omitempty"`
|
|
|
|
CPU int `json:"cpu,omitempty"`
|
|
|
|
Min int `json:"min,omitempty"`
|
|
|
|
Max int `json:"max,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resources awsResources, template *cloudformation.Template, service types.ServiceConfig) error {
|
2020-09-30 14:35:07 +02:00
|
|
|
if service.Deploy == nil {
|
2020-10-20 10:27:36 +02:00
|
|
|
return nil
|
2020-09-30 14:35:07 +02:00
|
|
|
}
|
|
|
|
v, ok := service.Deploy.Extensions[extensionAutoScaling]
|
|
|
|
if !ok {
|
2020-10-20 10:27:36 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
marshalled, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var config autoscalingConfig
|
|
|
|
err = json.Unmarshal(marshalled, &config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Memory != 0 && config.CPU != 0 {
|
|
|
|
return fmt.Errorf("%s can't be set with both cpu and memory targets", extensionAutoScaling)
|
|
|
|
}
|
|
|
|
if config.Max == 0 {
|
|
|
|
return fmt.Errorf("%s MUST define max replicas", extensionAutoScaling)
|
2020-09-30 14:35:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
role := fmt.Sprintf("%sAutoScalingRole", normalizeResourceName(service.Name))
|
|
|
|
template.Resources[role] = &iam.Role{
|
|
|
|
AssumeRolePolicyDocument: ausocalingAssumeRolePolicyDocument,
|
|
|
|
Path: "/",
|
|
|
|
Policies: []iam.Role_Policy{
|
|
|
|
{
|
|
|
|
PolicyDocument: &PolicyDocument{
|
|
|
|
Statement: []PolicyStatement{
|
|
|
|
{
|
|
|
|
Effect: "Allow",
|
|
|
|
Action: []string{
|
|
|
|
actionAutoScaling,
|
|
|
|
actionDescribeService,
|
|
|
|
actionUpdateService,
|
|
|
|
actionGetMetrics,
|
|
|
|
},
|
|
|
|
Resource: []string{cloudformation.Ref(serviceResourceName(service.Name))},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PolicyName: "service-autoscaling",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Tags: serviceTags(project, service),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Why isn't this just the service ARN ?????
|
2020-10-19 09:48:23 +02:00
|
|
|
resourceID := cloudformation.Join("/", []string{"service", resources.cluster.ID(), cloudformation.GetAtt(serviceResourceName(service.Name), "Name")})
|
2020-09-30 14:35:07 +02:00
|
|
|
|
|
|
|
target := fmt.Sprintf("%sScalableTarget", normalizeResourceName(service.Name))
|
|
|
|
template.Resources[target] = &applicationautoscaling.ScalableTarget{
|
2020-10-20 10:27:36 +02:00
|
|
|
MaxCapacity: config.Max,
|
|
|
|
MinCapacity: config.Min,
|
2020-09-30 14:35:07 +02:00
|
|
|
ResourceId: resourceID,
|
|
|
|
RoleARN: cloudformation.GetAtt(role, "Arn"),
|
|
|
|
ScalableDimension: applicationautoscaling2.ScalableDimensionEcsServiceDesiredCount,
|
|
|
|
ServiceNamespace: applicationautoscaling2.ServiceNamespaceEcs,
|
|
|
|
AWSCloudFormationDependsOn: []string{serviceResourceName(service.Name)},
|
|
|
|
}
|
|
|
|
|
2020-10-20 10:27:36 +02:00
|
|
|
var (
|
|
|
|
metric = applicationautoscaling2.MetricTypeEcsserviceAverageCpuutilization
|
|
|
|
targetPercent = config.CPU
|
|
|
|
)
|
|
|
|
if config.Memory != 0 {
|
|
|
|
metric = applicationautoscaling2.MetricTypeEcsserviceAverageMemoryUtilization
|
|
|
|
targetPercent = config.Memory
|
|
|
|
}
|
|
|
|
|
2020-09-30 14:35:07 +02:00
|
|
|
policy := fmt.Sprintf("%sScalingPolicy", normalizeResourceName(service.Name))
|
|
|
|
template.Resources[policy] = &applicationautoscaling.ScalingPolicy{
|
|
|
|
PolicyType: "TargetTrackingScaling",
|
|
|
|
PolicyName: policy,
|
|
|
|
ScalingTargetId: cloudformation.Ref(target),
|
|
|
|
StepScalingPolicyConfiguration: nil,
|
|
|
|
TargetTrackingScalingPolicyConfiguration: &applicationautoscaling.ScalingPolicy_TargetTrackingScalingPolicyConfiguration{
|
|
|
|
PredefinedMetricSpecification: &applicationautoscaling.ScalingPolicy_PredefinedMetricSpecification{
|
2020-10-20 10:27:36 +02:00
|
|
|
PredefinedMetricType: metric,
|
2020-09-30 14:35:07 +02:00
|
|
|
},
|
|
|
|
ScaleOutCooldown: 60,
|
|
|
|
ScaleInCooldown: 60,
|
2020-10-20 10:27:36 +02:00
|
|
|
TargetValue: float64(targetPercent),
|
2020-09-30 14:35:07 +02:00
|
|
|
},
|
|
|
|
}
|
2020-10-20 10:27:36 +02:00
|
|
|
return nil
|
2020-09-30 14:35:07 +02:00
|
|
|
}
|