From 2edd4f376c898106adaab21c1cb8ce51a0b2b924 Mon Sep 17 00:00:00 2001 From: Yashodhan Pise Date: Fri, 7 Mar 2025 15:59:58 +0530 Subject: [PATCH] Updates handling of scale/replicas through CLI & compose file Signed-off-by: Yashodhan Pise --- podman_compose.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index df1f9ee..b669939 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -2095,7 +2095,23 @@ class PodmanCompose: container_names_by_service = {} self.services = services for service_name, service_desc in services.items(): - replicas = try_int(service_desc.get("deploy", {}).get("replicas"), fallback=1) + replicas = 1 + if "scale" in args and args.scale is not None: + # Check `--scale` args from CLI command + scale_args = args.scale.split('=') + if service_name == scale_args[0]: + replicas = try_int(scale_args[1], fallback=1) + elif "scale" in service_desc: + # Check `scale` value from compose yaml file + replicas = try_int(service_desc.get("scale"), fallback=1) + elif ( + "deploy" in service_desc + and "replicas" in service_desc.get("deploy", {}) + and "replicated" == service_desc.get("deploy", {}).get("mode", '') + ): + # Check `deploy: replicas:` value from compose yaml file + # Note: All conditions are necessary to handle case + replicas = try_int(service_desc.get("deploy", {}).get("replicas"), fallback=1) container_names_by_service[service_name] = [] for num in range(1, replicas + 1): @@ -3383,12 +3399,13 @@ def compose_up_parse(parser): action="store_true", help="Remove containers for services not defined in the Compose file.", ) + # `--scale` argument needs to store as single value and not append, + # as multiple scale values could be confusing. parser.add_argument( "--scale", metavar="SERVICE=NUM", - action="append", - help="Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if " - "present.", + help="Scale SERVICE to NUM instances. " + "Overrides the `scale` setting in the Compose file if present.", ) parser.add_argument( "--exit-code-from",