Skip to content

Commit 995a344

Browse files
committed
fix for empty cpus reservations
1 parent 02235fe commit 995a344

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/pkg/cli/composeStart.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/bufbuild/connect-go"
12+
"github.com/compose-spec/compose-go/v2/types"
1213
"github.com/defang-io/defang/src/pkg"
1314
pb "github.com/defang-io/defang/src/protos/io/defang/v1"
1415
"github.com/defang-io/defang/src/protos/io/defang/v1/defangv1connect"
@@ -204,6 +205,16 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
204205
if svccfg.Deploy.EndpointMode != "" {
205206
return nil, &ComposeError{fmt.Errorf("unsupported compose directive: deploy endpoint_mode")}
206207
}
208+
if svccfg.Deploy.Resources.Limits != nil && svccfg.Deploy.Resources.Reservations == nil {
209+
logrus.Warn("no reservations specified; using limits as reservations")
210+
}
211+
reservations := getResourceReservations(svccfg.Deploy.Resources)
212+
if reservations != nil && reservations.NanoCPUs != "" {
213+
cpus, err := strconv.ParseFloat(reservations.NanoCPUs, 32)
214+
if err != nil || cpus < 0 { // "0" just means "as small as possible"
215+
return nil, &ComposeError{fmt.Errorf("invalid value for cpus: %q", reservations.NanoCPUs)}
216+
}
217+
}
207218
}
208219
}
209220

@@ -249,15 +260,14 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
249260
deploy.Replicas = uint32(*svccfg.Deploy.Replicas)
250261
}
251262

252-
reservations := svccfg.Deploy.Resources.Reservations
253-
if reservations == nil {
254-
reservations = svccfg.Deploy.Resources.Limits
255-
}
263+
reservations := getResourceReservations(svccfg.Deploy.Resources)
256264
if reservations != nil {
257-
cpus, err := strconv.ParseFloat(reservations.NanoCPUs, 32)
258-
if err != nil {
259-
// TODO: move this validation up so we don't upload the build context if it's invalid
260-
return nil, &ComposeError{fmt.Errorf("invalid reservations cpus: %v", err)}
265+
cpus := 0.0
266+
if reservations.NanoCPUs != "" {
267+
cpus, err = strconv.ParseFloat(reservations.NanoCPUs, 32)
268+
if err != nil {
269+
panic(err) // was already validated above
270+
}
261271
}
262272
var devices []*pb.Device
263273
for _, d := range reservations.Devices {
@@ -380,3 +390,11 @@ func ComposeStart(ctx context.Context, client defangv1connect.FabricControllerCl
380390

381391
return serviceInfos, nil
382392
}
393+
394+
func getResourceReservations(r types.Resources) *types.Resource {
395+
if r.Reservations == nil {
396+
// TODO: we might not want to default to all the limits, maybe only memory?
397+
return r.Limits
398+
}
399+
return r.Reservations
400+
}

src/pkg/cli/compose_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func TestLoadDockerCompose(t *testing.T) {
4747
DoVerbose = true
4848

4949
t.Run("no project name", func(t *testing.T) {
50-
_, err := loadDockerCompose("../../tests/docker-compose.yml", "")
50+
_, err := loadDockerCompose("../../tests/compose.yaml", "")
5151
if err != nil {
5252
t.Fatalf("loadDockerCompose() failed: %v", err)
5353
}
5454
})
5555

5656
t.Run("override project name", func(t *testing.T) {
57-
p, err := loadDockerCompose("../../tests/docker-compose.yml", "blah")
57+
p, err := loadDockerCompose("../../tests/compose.yaml", "blah")
5858
if err != nil {
5959
t.Fatalf("loadDockerCompose() failed: %v", err)
6060
}
@@ -64,7 +64,7 @@ func TestLoadDockerCompose(t *testing.T) {
6464
})
6565

6666
t.Run("fancy project name", func(t *testing.T) {
67-
p, err := loadDockerCompose("../../tests/docker-compose.yml", "Valid-Username")
67+
p, err := loadDockerCompose("../../tests/compose.yaml", "Valid-Username")
6868
if err != nil {
6969
t.Fatalf("loadDockerCompose() failed: %v", err)
7070
}

src/tests/docker-compose.yml renamed to src/tests/compose.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ services:
77
DNS: "${COMPOSE_PROJECT_NAME}-dfnx.prod1.defang.dev"
88
env_file:
99
- fileName.env
10-
# deploy:
11-
# resources:
12-
# limits:
13-
# cpus: '0.50'
14-
# memory: 512M
15-
# reservations:
16-
# cpus: '0.25'
17-
# memory: 256M
10+
deploy:
11+
resources:
12+
limits:
13+
cpus: '0.50'
14+
memory: 512M
15+
reservations:
16+
cpus: '0.25'
17+
memory: 256M
1818
ports:
1919
- target: 80
2020
mode: ingress

0 commit comments

Comments
 (0)