diff --git a/README.md b/README.md index bcaa378..7a8e0da 100644 --- a/README.md +++ b/README.md @@ -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) | ✅ | | diff --git a/compose.go b/compose.go index c55e64e..9729c16 100644 --- a/compose.go +++ b/compose.go @@ -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 diff --git a/nixos-test/docker-compose.nix b/nixos-test/docker-compose.nix index 438f57e..cbc8069 100644 --- a/nixos-test/docker-compose.nix +++ b/nixos-test/docker-compose.nix @@ -21,6 +21,7 @@ ]; log-driver = "journald"; extraOptions = [ + "--cpus=1.0" "--network-alias=sabnzbd" "--network=myproject-default" ]; diff --git a/nixos-test/docker-compose.yml b/nixos-test/docker-compose.yml index 6f320ef..222a00f 100644 --- a/nixos-test/docker-compose.yml +++ b/nixos-test/docker-compose.yml @@ -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" diff --git a/nixos-test/podman-compose.nix b/nixos-test/podman-compose.nix index 7b9be84..2d7874e 100644 --- a/nixos-test/podman-compose.nix +++ b/nixos-test/podman-compose.nix @@ -26,6 +26,7 @@ ]; log-driver = "journald"; extraOptions = [ + "--cpus=1.0" "--network-alias=sabnzbd" "--network=myproject-default" ]; diff --git a/testdata/TestDocker_EnvFilesOnly_out.nix b/testdata/TestDocker_EnvFilesOnly_out.nix index fcb13f4..e290048 100644 --- a/testdata/TestDocker_EnvFilesOnly_out.nix +++ b/testdata/TestDocker_EnvFilesOnly_out.nix @@ -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" ]; }; diff --git a/testdata/TestDocker_NoWriteNixSetup_out.nix b/testdata/TestDocker_NoWriteNixSetup_out.nix index cf2dc00..7f49439 100644 --- a/testdata/TestDocker_NoWriteNixSetup_out.nix +++ b/testdata/TestDocker_NoWriteNixSetup_out.nix @@ -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" ]; }; diff --git a/testdata/TestDocker_OverrideSystemdStopTimeout_out.nix b/testdata/TestDocker_OverrideSystemdStopTimeout_out.nix index 4738e8c..39b127f 100644 --- a/testdata/TestDocker_OverrideSystemdStopTimeout_out.nix +++ b/testdata/TestDocker_OverrideSystemdStopTimeout_out.nix @@ -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" ]; }; diff --git a/testdata/TestDocker_RemoveVolumes_out.nix b/testdata/TestDocker_RemoveVolumes_out.nix index 99c13ce..33e2390 100644 --- a/testdata/TestDocker_RemoveVolumes_out.nix +++ b/testdata/TestDocker_RemoveVolumes_out.nix @@ -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" ]; }; diff --git a/testdata/TestDocker_SystemdMount_out.nix b/testdata/TestDocker_SystemdMount_out.nix index bbddfb8..ea31561 100644 --- a/testdata/TestDocker_SystemdMount_out.nix +++ b/testdata/TestDocker_SystemdMount_out.nix @@ -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" ]; }; diff --git a/testdata/TestDocker_WithProject_out.nix b/testdata/TestDocker_WithProject_out.nix index 4cb9b41..8844356 100644 --- a/testdata/TestDocker_WithProject_out.nix +++ b/testdata/TestDocker_WithProject_out.nix @@ -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" ]; }; diff --git a/testdata/TestDocker_out.nix b/testdata/TestDocker_out.nix index 0ed3ebf..3a86462 100644 --- a/testdata/TestDocker_out.nix +++ b/testdata/TestDocker_out.nix @@ -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" ]; }; diff --git a/testdata/TestPodman_SystemdMount_out.nix b/testdata/TestPodman_SystemdMount_out.nix index ec89664..8e9eb0f 100644 --- a/testdata/TestPodman_SystemdMount_out.nix +++ b/testdata/TestPodman_SystemdMount_out.nix @@ -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" ]; }; diff --git a/testdata/TestPodman_WithProject_out.nix b/testdata/TestPodman_WithProject_out.nix index 1415b8c..ce81f86 100644 --- a/testdata/TestPodman_WithProject_out.nix +++ b/testdata/TestPodman_WithProject_out.nix @@ -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" ]; }; diff --git a/testdata/TestPodman_out.nix b/testdata/TestPodman_out.nix index 9ef01df..fadc404 100644 --- a/testdata/TestPodman_out.nix +++ b/testdata/TestPodman_out.nix @@ -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" ]; }; diff --git a/testdata/docker-compose.yml b/testdata/docker-compose.yml index 85fa110..e6b74ab 100644 --- a/testdata/docker-compose.yml +++ b/testdata/docker-compose.yml @@ -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: