Skip to content

Commit f15e408

Browse files
committed
efactor: improve deploy config handling for CPU and memory limits
- Simplify deploy config check by using single if-let binding - Fix typo: "estart_policy" -> "restart_policy" - Make CPU/memory limits optional in buildService (only add if configured) - Avoid hardcoded defaults that may override container/image defaults
1 parent a0af6f1 commit f15e408

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

Sources/Container-Compose/Commands/ComposeUp.swift

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,22 @@ public struct ComposeUp: AsyncParsableCommand, @unchecked Sendable {
366366
}
367367

368368
// Handle 'deploy' configuration (note that this tool doesn't fully support it)
369-
if service.deploy != nil {
369+
if let deploy = service.deploy {
370370
print("Note: The 'deploy' configuration for service '\(serviceName)' was parsed successfully.")
371-
print(
372-
"However, this 'container-compose' tool does not currently support 'deploy' functionality (e.g., replicas, resources, update strategies) as it is primarily for orchestration platforms like Docker Swarm or Kubernetes, not direct 'container run' commands."
373-
)
374-
print("The service will be run as a single container based on other configurations.")
371+
372+
// Add CPU & Memory from deploy.resources.limits
373+
if let cpuLimit = deploy.resources?.limits?.cpus {
374+
runCommandArgs.append(contentsOf: ["--cpus", "\(cpuLimit)"])
375+
}
376+
377+
if let memoryLimit = deploy.resources?.limits?.memory {
378+
runCommandArgs.append(contentsOf: ["--memory", "\(memoryLimit)"])
379+
}
380+
381+
// Log unsupported deploy features
382+
if deploy.mode != nil || deploy.replicas != nil || deploy.restart_policy != nil {
383+
print("Note: The 'deploy' configuration for service '\(serviceName)' includes features (mode, replicas, restart_policy) that are not supported by this tool.")
384+
}
375385
}
376386

377387
// Add detach flag if specified on the CLI
@@ -527,12 +537,6 @@ public struct ComposeUp: AsyncParsableCommand, @unchecked Sendable {
527537
}
528538
}
529539

530-
// Add CPU & Memory
531-
let cpuCount = Int64(service.deploy?.resources?.limits?.cpus ?? "4") ?? 4
532-
let memoryLimit = service.deploy?.resources?.limits?.memory ?? "1024MB"
533-
runCommandArgs.append(contentsOf: ["--cpus", "\(cpuCount)"])
534-
runCommandArgs.append(contentsOf: ["--memory", memoryLimit])
535-
536540
// Add interactive and TTY flags
537541
if service.stdin_open == true {
538542
runCommandArgs.append("-i") // --interactive
@@ -655,12 +659,14 @@ public struct ComposeUp: AsyncParsableCommand, @unchecked Sendable {
655659

656660
// Add image name
657661
commands.append(contentsOf: ["--tag", imageToRun])
658-
659-
// Add CPU & Memory
660-
let cpuCount = Int64(service.deploy?.resources?.limits?.cpus ?? "2") ?? 2
661-
let memoryLimit = service.deploy?.resources?.limits?.memory ?? "2048MB"
662-
commands.append(contentsOf: ["--cpus", "\(cpuCount)"])
663-
commands.append(contentsOf: ["--memory", memoryLimit])
662+
663+
// Add CPU & Memory from deploy.resources.limits (only if configured)
664+
if let cpuLimit = service.deploy?.resources?.limits?.cpus {
665+
commands.append(contentsOf: ["--cpus", "\(cpuLimit)"])
666+
}
667+
if let memoryLimit = service.deploy?.resources?.limits?.memory {
668+
commands.append(contentsOf: ["--memory", "\(memoryLimit)"])
669+
}
664670

665671
let buildCommand = try Application.BuildCommand.parse(commands)
666672
print("\n----------------------------------------")

0 commit comments

Comments
 (0)