Skip to content

Commit

Permalink
add support for deploy.resources
Browse files Browse the repository at this point in the history
  • Loading branch information
aksiksi committed Mar 16, 2024
1 parent e02a4eb commit 4df31ab
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ If a feature is missing, please feel free to [create an issue](https://github.co
| [`depends_on`](https://docs.docker.com/compose/compose-file/05-services/#depends_on) | ⚠️ | Only short syntax is supported. |
| [`restart`](https://docs.docker.com/compose/compose-file/05-services/#restart) | ✅ | |
| [`deploy.restart_policy`](https://docs.docker.com/compose/compose-file/deploy/#restart_policy) | ✅ | |
| [`deploy.resources`](https://docs.docker.com/compose/compose-file/deploy/#resources) | ✅ | |
| [`devices`](https://docs.docker.com/compose/compose-file/05-services/#devices) | ✅ | |
| [`networks.aliases`](https://docs.docker.com/compose/compose-file/05-services/#aliases) | ✅ | |
| [`network_mode`](https://docs.docker.com/compose/compose-file/05-services/#network_mode) | ✅ | |
Expand Down
23 changes: 23 additions & 0 deletions compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,29 @@ func (g *Generator) buildNixContainer(service types.ServiceConfig) (*NixContaine
}
}

// Deploy resources configuration.
// https://docs.docker.com/compose/compose-file/deploy/#resources
if deploy := service.Deploy; deploy != nil {
if limits := deploy.Resources.Limits; limits != nil {
if limits.MemoryBytes != 0 {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--memory=%db", limits.MemoryBytes))
}
// Name is misleading - this actually is the exact number passed in with "cpus".
if limits.NanoCPUs != "" {
c.ExtraOptions = append(c.ExtraOptions, "--cpu-quota="+limits.NanoCPUs)
}
}
if reservations := deploy.Resources.Reservations; reservations != nil {
if reservations.MemoryBytes != 0 {
c.ExtraOptions = append(c.ExtraOptions, fmt.Sprintf("--memory-reservation=%db", reservations.MemoryBytes))
}
// Name is misleading - this actually is the exact number passed in with "cpus".
if reservations.NanoCPUs != "" {
c.ExtraOptions = append(c.ExtraOptions, "--cpus="+reservations.NanoCPUs)
}
}
}

// Restart policy.
if err := c.SystemdConfig.ParseRestartPolicy(&service, g.Runtime); err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions nixos-test/docker-compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
];
log-driver = "journald";
extraOptions = [
"--cpus=1.0"
"--network-alias=sabnzbd"
"--network=myproject-default"
];
Expand Down
4 changes: 4 additions & 0 deletions nixos-test/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ services:
volumes:
- /var/volumes/sabnzbd:/config
- storage:/storage
deploy:
resources:
reservations:
cpus: "1.0"
labels:
- 'compose2nix.systemd.service.Restart="no"'
- "compose2nix.systemd.service.RuntimeMaxSec=360"
Expand Down
1 change: 1 addition & 0 deletions nixos-test/podman-compose.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
];
log-driver = "journald";
extraOptions = [
"--cpus=1.0"
"--network-alias=sabnzbd"
"--network=myproject-default"
];
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_EnvFilesOnly_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_NoWriteNixSetup_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_OverrideSystemdStopTimeout_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_RemoveVolumes_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_SystemdMount_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestDocker_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
];
log-driver = "journald";
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestPodman_SystemdMount_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestPodman_WithProject_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
log-driver = "journald";
autoStart = false;
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
4 changes: 4 additions & 0 deletions testdata/TestPodman_out.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@
];
log-driver = "journald";
extraOptions = [
"--cpu-quota=1.5"
"--cpus=1.0"
"--dns=1.1.1.1"
"--health-cmd='curl -f http://localhost/\${POTATO}'"
"--log-opt=compress=true"
"--log-opt=max-file=3"
"--log-opt=max-size=10m"
"--memory-reservation=524288000b"
"--memory=1048576000b"
"--network=container:myproject-sabnzbd"
];
};
Expand Down
7 changes: 7 additions & 0 deletions testdata/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ services:
delay: 5s
max_attempts: 3
window: 120s
resources:
limits:
cpus: "1.5"
memory: 1000M
reservations:
cpus: "1.0"
memory: 500M
logging:
driver: "json-file"
options:
Expand Down

0 comments on commit 4df31ab

Please sign in to comment.