mirror of
https://github.com/docker/compose.git
synced 2025-07-23 13:45:00 +02:00
Allow user to set ami/machine by Deploy.Placement.Constraint
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
50a2ae1100
commit
10b8fabaae
@ -88,23 +88,23 @@ type ecsAPIService struct {
|
|||||||
aws API
|
aws API
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ecsAPIService) ContainerService() containers.Service {
|
func (b *ecsAPIService) ContainerService() containers.Service {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ecsAPIService) ComposeService() compose.Service {
|
func (b *ecsAPIService) ComposeService() compose.Service {
|
||||||
return a
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ecsAPIService) SecretsService() secrets.Service {
|
func (b *ecsAPIService) SecretsService() secrets.Service {
|
||||||
return a
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ecsAPIService) VolumeService() volumes.Service {
|
func (b *ecsAPIService) VolumeService() volumes.Service {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ecsAPIService) ResourceService() resources.Service {
|
func (b *ecsAPIService) ResourceService() resources.Service {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,21 +23,19 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/efs"
|
|
||||||
|
|
||||||
"github.com/golang/mock/gomock"
|
|
||||||
|
|
||||||
"github.com/docker/compose-cli/api/compose"
|
"github.com/docker/compose-cli/api/compose"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/elbv2"
|
"github.com/aws/aws-sdk-go/service/elbv2"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation"
|
"github.com/awslabs/goformation/v4/cloudformation"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/ec2"
|
"github.com/awslabs/goformation/v4/cloudformation/ec2"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/ecs"
|
"github.com/awslabs/goformation/v4/cloudformation/ecs"
|
||||||
|
"github.com/awslabs/goformation/v4/cloudformation/efs"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/elasticloadbalancingv2"
|
"github.com/awslabs/goformation/v4/cloudformation/elasticloadbalancingv2"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/iam"
|
"github.com/awslabs/goformation/v4/cloudformation/iam"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/logs"
|
"github.com/awslabs/goformation/v4/cloudformation/logs"
|
||||||
"github.com/compose-spec/compose-go/loader"
|
"github.com/compose-spec/compose-go/loader"
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
|
"github.com/golang/mock/gomock"
|
||||||
"gotest.tools/v3/assert"
|
"gotest.tools/v3/assert"
|
||||||
"gotest.tools/v3/golden"
|
"gotest.tools/v3/golden"
|
||||||
)
|
)
|
||||||
|
@ -54,6 +54,8 @@ var compatibleComposeAttributes = []string{
|
|||||||
"services.cap_drop",
|
"services.cap_drop",
|
||||||
"services.depends_on",
|
"services.depends_on",
|
||||||
"services.deploy",
|
"services.deploy",
|
||||||
|
"services.deploy.placement",
|
||||||
|
"services.deploy.placement.constraints",
|
||||||
"services.deploy.replicas",
|
"services.deploy.replicas",
|
||||||
"services.deploy.resources.limits",
|
"services.deploy.resources.limits",
|
||||||
"services.deploy.resources.limits.cpus",
|
"services.deploy.resources.limits.cpus",
|
||||||
|
42
ecs/ec2.go
42
ecs/ec2.go
@ -20,6 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/awslabs/goformation/v4/cloudformation"
|
"github.com/awslabs/goformation/v4/cloudformation"
|
||||||
"github.com/awslabs/goformation/v4/cloudformation/autoscaling"
|
"github.com/awslabs/goformation/v4/cloudformation/autoscaling"
|
||||||
@ -28,11 +29,22 @@ import (
|
|||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
placementConstraintAMI = "node.ami == "
|
||||||
|
placementConstraintMachine = "node.machine == "
|
||||||
|
)
|
||||||
|
|
||||||
func (b *ecsAPIService) createCapacityProvider(ctx context.Context, project *types.Project, template *cloudformation.Template, resources awsResources) error {
|
func (b *ecsAPIService) createCapacityProvider(ctx context.Context, project *types.Project, template *cloudformation.Template, resources awsResources) error {
|
||||||
var ec2 bool
|
var (
|
||||||
for _, s := range project.Services {
|
ec2 bool
|
||||||
if requireEC2(s) {
|
ami string
|
||||||
|
machineType string
|
||||||
|
)
|
||||||
|
for _, service := range project.Services {
|
||||||
|
if requireEC2(service) {
|
||||||
ec2 = true
|
ec2 = true
|
||||||
|
// TODO once we can assign a service to a CapacityProvider, we could run this _per service_
|
||||||
|
ami, machineType = getUserDefinedMachine(service)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -41,15 +53,21 @@ func (b *ecsAPIService) createCapacityProvider(ctx context.Context, project *typ
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ami, err := b.aws.GetParameter(ctx, "/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended")
|
if ami == "" {
|
||||||
|
recommended, err := b.aws.GetParameter(ctx, "/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
ami = recommended
|
||||||
|
}
|
||||||
|
|
||||||
machineType, err := guessMachineType(project)
|
if machineType == "" {
|
||||||
|
t, err := guessMachineType(project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
machineType = t
|
||||||
|
}
|
||||||
|
|
||||||
template.Resources["CapacityProvider"] = &ecs.CapacityProvider{
|
template.Resources["CapacityProvider"] = &ecs.CapacityProvider{
|
||||||
AutoScalingGroupProvider: &ecs.CapacityProvider_AutoScalingGroupProvider{
|
AutoScalingGroupProvider: &ecs.CapacityProvider_AutoScalingGroupProvider{
|
||||||
@ -98,3 +116,17 @@ func (b *ecsAPIService) createCapacityProvider(ctx context.Context, project *typ
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getUserDefinedMachine(s types.ServiceConfig) (ami string, machineType string) {
|
||||||
|
if s.Deploy != nil {
|
||||||
|
for _, s := range s.Deploy.Placement.Constraints {
|
||||||
|
if strings.HasPrefix(s, placementConstraintAMI) {
|
||||||
|
ami = s[len(placementConstraintAMI):]
|
||||||
|
}
|
||||||
|
if strings.HasPrefix(s, placementConstraintMachine) {
|
||||||
|
machineType = s[len(placementConstraintMachine):]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ami, machineType
|
||||||
|
}
|
||||||
|
49
ecs/ec2_test.go
Normal file
49
ecs/ec2_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
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 (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/awslabs/goformation/v4/cloudformation/autoscaling"
|
||||||
|
"gotest.tools/v3/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUserDefinedAMI(t *testing.T) {
|
||||||
|
template := convertYaml(t, `
|
||||||
|
services:
|
||||||
|
test:
|
||||||
|
image: "image"
|
||||||
|
deploy:
|
||||||
|
placement:
|
||||||
|
constraints:
|
||||||
|
- "node.ami == ami123456789"
|
||||||
|
- "node.machine == t0.femto"
|
||||||
|
resources:
|
||||||
|
# devices:
|
||||||
|
# - capabilities: ["gpu"]
|
||||||
|
reservations:
|
||||||
|
memory: 8Gb
|
||||||
|
generic_resources:
|
||||||
|
- discrete_resource_spec:
|
||||||
|
kind: gpus
|
||||||
|
value: 1
|
||||||
|
`, useDefaultVPC)
|
||||||
|
lc := template.Resources["LaunchConfiguration"].(*autoscaling.LaunchConfiguration)
|
||||||
|
assert.Check(t, lc.ImageId == "ami123456789")
|
||||||
|
assert.Check(t, lc.InstanceType == "t0.femto")
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user