autoscaling.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ecs
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. applicationautoscaling2 "github.com/aws/aws-sdk-go/service/applicationautoscaling"
  18. "github.com/awslabs/goformation/v4/cloudformation"
  19. "github.com/awslabs/goformation/v4/cloudformation/applicationautoscaling"
  20. "github.com/awslabs/goformation/v4/cloudformation/iam"
  21. "github.com/compose-spec/compose-go/types"
  22. )
  23. type autoscalingConfig struct {
  24. Memory int `json:"memory,omitempty"`
  25. CPU int `json:"cpu,omitempty"`
  26. Min int `json:"min,omitempty"`
  27. Max int `json:"max,omitempty"`
  28. }
  29. func (b *ecsAPIService) createAutoscalingPolicy(project *types.Project, resources awsResources, template *cloudformation.Template, service types.ServiceConfig) error {
  30. if service.Deploy == nil {
  31. return nil
  32. }
  33. v, ok := service.Deploy.Extensions[extensionAutoScaling]
  34. if !ok {
  35. return nil
  36. }
  37. marshalled, err := json.Marshal(v)
  38. if err != nil {
  39. return err
  40. }
  41. var config autoscalingConfig
  42. err = json.Unmarshal(marshalled, &config)
  43. if err != nil {
  44. return err
  45. }
  46. if config.Memory != 0 && config.CPU != 0 {
  47. return fmt.Errorf("%s can't be set with both cpu and memory targets", extensionAutoScaling)
  48. }
  49. if config.Max == 0 {
  50. return fmt.Errorf("%s MUST define max replicas", extensionAutoScaling)
  51. }
  52. role := fmt.Sprintf("%sAutoScalingRole", normalizeResourceName(service.Name))
  53. template.Resources[role] = &iam.Role{
  54. AssumeRolePolicyDocument: ausocalingAssumeRolePolicyDocument,
  55. Path: "/",
  56. Policies: []iam.Role_Policy{
  57. {
  58. PolicyDocument: &PolicyDocument{
  59. Statement: []PolicyStatement{
  60. {
  61. Effect: "Allow",
  62. Action: []string{
  63. actionAutoScaling,
  64. actionDescribeService,
  65. actionUpdateService,
  66. actionGetMetrics,
  67. },
  68. Resource: []string{cloudformation.Ref(serviceResourceName(service.Name))},
  69. },
  70. },
  71. },
  72. PolicyName: "service-autoscaling",
  73. },
  74. },
  75. Tags: serviceTags(project, service),
  76. }
  77. // Why isn't this just the service ARN ?????
  78. resourceID := cloudformation.Join("/", []string{"service", resources.cluster.ID(), cloudformation.GetAtt(serviceResourceName(service.Name), "Name")})
  79. target := fmt.Sprintf("%sScalableTarget", normalizeResourceName(service.Name))
  80. template.Resources[target] = &applicationautoscaling.ScalableTarget{
  81. MaxCapacity: config.Max,
  82. MinCapacity: config.Min,
  83. ResourceId: resourceID,
  84. RoleARN: cloudformation.GetAtt(role, "Arn"),
  85. ScalableDimension: applicationautoscaling2.ScalableDimensionEcsServiceDesiredCount,
  86. ServiceNamespace: applicationautoscaling2.ServiceNamespaceEcs,
  87. AWSCloudFormationDependsOn: []string{serviceResourceName(service.Name)},
  88. }
  89. var (
  90. metric = applicationautoscaling2.MetricTypeEcsserviceAverageCpuutilization
  91. targetPercent = config.CPU
  92. )
  93. if config.Memory != 0 {
  94. metric = applicationautoscaling2.MetricTypeEcsserviceAverageMemoryUtilization
  95. targetPercent = config.Memory
  96. }
  97. policy := fmt.Sprintf("%sScalingPolicy", normalizeResourceName(service.Name))
  98. template.Resources[policy] = &applicationautoscaling.ScalingPolicy{
  99. PolicyType: "TargetTrackingScaling",
  100. PolicyName: policy,
  101. ScalingTargetId: cloudformation.Ref(target),
  102. StepScalingPolicyConfiguration: nil,
  103. TargetTrackingScalingPolicyConfiguration: &applicationautoscaling.ScalingPolicy_TargetTrackingScalingPolicyConfiguration{
  104. PredefinedMetricSpecification: &applicationautoscaling.ScalingPolicy_PredefinedMetricSpecification{
  105. PredefinedMetricType: metric,
  106. },
  107. ScaleOutCooldown: 60,
  108. ScaleInCooldown: 60,
  109. TargetValue: float64(targetPercent),
  110. },
  111. }
  112. return nil
  113. }