|
| 1 | +/* |
| 2 | + Copyright 2020 Docker, Inc. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ecs |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "strconv" |
| 22 | + |
| 23 | + "github.com/compose-spec/compose-go/types" |
| 24 | + "github.com/docker/go-units" |
| 25 | +) |
| 26 | + |
| 27 | +type machine struct { |
| 28 | + id string |
| 29 | + cpus float64 |
| 30 | + memory types.UnitBytes |
| 31 | + gpus int64 |
| 32 | +} |
| 33 | + |
| 34 | +type family []machine |
| 35 | + |
| 36 | +var p3family = family{ |
| 37 | + { |
| 38 | + id: "p3.2xlarge", |
| 39 | + cpus: 8, |
| 40 | + memory: 64 * units.GiB, |
| 41 | + gpus: 2, |
| 42 | + }, |
| 43 | + { |
| 44 | + id: "p3.8xlarge", |
| 45 | + cpus: 32, |
| 46 | + memory: 244 * units.GiB, |
| 47 | + gpus: 4, |
| 48 | + }, |
| 49 | + { |
| 50 | + id: "p3.16xlarge", |
| 51 | + cpus: 64, |
| 52 | + memory: 488 * units.GiB, |
| 53 | + gpus: 8, |
| 54 | + }, |
| 55 | +} |
| 56 | + |
| 57 | +type filterFn func(machine) bool |
| 58 | + |
| 59 | +func (f family) filter(fn filterFn) family { |
| 60 | + var filtered family |
| 61 | + for _, machine := range f { |
| 62 | + if fn(machine) { |
| 63 | + filtered = append(filtered, machine) |
| 64 | + } |
| 65 | + } |
| 66 | + return filtered |
| 67 | +} |
| 68 | + |
| 69 | +func (f family) firstOrError(msg string, args ...interface{}) (machine, error) { |
| 70 | + if len(f) == 0 { |
| 71 | + return machine{}, fmt.Errorf(msg, args...) |
| 72 | + } |
| 73 | + return f[0], nil |
| 74 | +} |
| 75 | + |
| 76 | +func guessMachineType(project *types.Project) (string, error) { |
| 77 | + // we select a machine type to match all gpu-bound services requirements |
| 78 | + // once https://github.com/aws/containers-roadmap/issues/631 is implemented we can define dedicated CapacityProviders per service. |
| 79 | + minMemory, minCPU, minGPU, err := getResourceRequirements(project) |
| 80 | + if err != nil { |
| 81 | + return "", err |
| 82 | + } |
| 83 | + |
| 84 | + instanceType, err := p3family. |
| 85 | + filter(func(m machine) bool { |
| 86 | + return m.memory >= minMemory |
| 87 | + }). |
| 88 | + filter(func(m machine) bool { |
| 89 | + return m.cpus >= minCPU |
| 90 | + }). |
| 91 | + filter(func(m machine) bool { |
| 92 | + return m.gpus >= minGPU |
| 93 | + }). |
| 94 | + firstOrError("none of the AWS p3 machines match requirement for memory:%d cpu:%f gpu:%d", minMemory, minCPU, minGPU) |
| 95 | + if err != nil { |
| 96 | + return "", err |
| 97 | + } |
| 98 | + return instanceType.id, nil |
| 99 | +} |
| 100 | + |
| 101 | +func getResourceRequirements(project *types.Project) (types.UnitBytes, float64, int64, error) { |
| 102 | + var minMemory types.UnitBytes |
| 103 | + var minCPU float64 |
| 104 | + var minGPU int64 |
| 105 | + for _, service := range project.Services { |
| 106 | + if service.Deploy == nil { |
| 107 | + continue |
| 108 | + } |
| 109 | + reservations := service.Deploy.Resources.Reservations |
| 110 | + if reservations == nil { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + var requiredGPUs int64 |
| 115 | + for _, r := range reservations.GenericResources { |
| 116 | + if r.DiscreteResourceSpec.Kind == "gpu" { |
| 117 | + requiredGPUs = r.DiscreteResourceSpec.Value |
| 118 | + break |
| 119 | + } |
| 120 | + } |
| 121 | + if requiredGPUs == 0 { |
| 122 | + continue |
| 123 | + } |
| 124 | + if requiredGPUs > minGPU { |
| 125 | + minGPU = requiredGPUs |
| 126 | + } |
| 127 | + |
| 128 | + if reservations.MemoryBytes > minMemory { |
| 129 | + minMemory = reservations.MemoryBytes |
| 130 | + } |
| 131 | + if reservations.NanoCPUs != "" { |
| 132 | + nanocpu, err := strconv.ParseFloat(reservations.NanoCPUs, 64) |
| 133 | + if err != nil { |
| 134 | + return 0, 0, 0, err |
| 135 | + } |
| 136 | + if nanocpu > minCPU { |
| 137 | + minCPU = nanocpu |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return minMemory, minCPU, minGPU, nil |
| 142 | +} |
0 commit comments