Skip to content

Commit 757d14e

Browse files
committed
feat: allow changing resource label-domain from default io.podman
Signed-off-by: legobt <[email protected]>
1 parent a3fb4b3 commit 757d14e

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

newsfragments/label_domain.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support to change default resource label prefix from io.podman via --label-domain cli arg

podman_compose.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ async def assert_volume(compose, mount_dict):
401401
log.debug("podman volume inspect %s || podman volume create %s", vol_name, vol_name)
402402
# TODO: might move to using "volume list"
403403
# podman volume list --format '{{.Name}}\t{{.MountPoint}}' \
404-
# -f 'label=io.podman.compose.project=HERE'
404+
# -f f"label={compose.label_domain}.compose.project=HERE"
405405
try:
406406
_ = (await compose.podman.output([], "volume", ["inspect", vol_name])).decode("utf-8")
407407
except subprocess.CalledProcessError as e:
@@ -411,7 +411,7 @@ async def assert_volume(compose, mount_dict):
411411
args = [
412412
"create",
413413
"--label",
414-
f"io.podman.compose.project={compose.project_name}",
414+
f"{compose.label_domain}.compose.project={compose.project_name}",
415415
"--label",
416416
f"com.docker.compose.project={compose.project_name}",
417417
]
@@ -804,11 +804,11 @@ def norm_ports(ports_in):
804804
return ports_out
805805

806806

807-
def get_network_create_args(net_desc, proj_name, net_name):
807+
def get_network_create_args(net_desc, proj_name, net_name, label_domain='io.podman'):
808808
args = [
809809
"create",
810810
"--label",
811-
f"io.podman.compose.project={proj_name}",
811+
f"{label_domain}.compose.project={proj_name}",
812812
"--label",
813813
f"com.docker.compose.project={proj_name}",
814814
]
@@ -871,7 +871,9 @@ async def assert_cnt_nets(compose, cnt):
871871
except subprocess.CalledProcessError as e:
872872
if is_ext:
873873
raise RuntimeError(f"External network [{net_name}] does not exists") from e
874-
args = get_network_create_args(net_desc, compose.project_name, net_name)
874+
args = get_network_create_args(
875+
net_desc, compose.project_name, net_name, compose.label_domain
876+
)
875877
await compose.podman.output([], "network", args)
876878
await compose.podman.output([], "network", ["exists", net_name])
877879

@@ -1461,7 +1463,7 @@ async def volume_ls(self):
14611463
"ls",
14621464
"--noheading",
14631465
"--filter",
1464-
f"label=io.podman.compose.project={self.compose.project_name}",
1466+
f"label={self.compose.label_domain}.compose.project={self.compose.project_name}",
14651467
"--format",
14661468
"{{.Name}}",
14671469
],
@@ -1677,6 +1679,7 @@ def __init__(self):
16771679
self.commands = {}
16781680
self.global_args = argparse.Namespace()
16791681
self.project_name = None
1682+
self.label_domain = None
16801683
self.dirname = None
16811684
self.pods = None
16821685
self.containers = []
@@ -1794,6 +1797,10 @@ def _parse_compose_file(self):
17941797
relative_files = files
17951798
filename = files[0]
17961799
project_name = args.project_name
1800+
label_domain = os.environ.get("COMPOSE_LABEL_DOMAIN", "io.podman")
1801+
if label_domain in args:
1802+
label_domain = args.label_domain
1803+
17971804
# no_ansi = args.no_ansi
17981805
# no_cleanup = args.no_cleanup
17991806
# dry_run = args.dry_run
@@ -1834,7 +1841,9 @@ def _parse_compose_file(self):
18341841
env_vars = norm_as_dict(args.env)
18351842
self.environ.update(env_vars)
18361843

1837-
compose = {}
1844+
compose = {
1845+
label_domain: label_domain,
1846+
}
18381847
# Iterate over files primitively to allow appending to files in-loop
18391848
files_iter = iter(files)
18401849

@@ -1895,6 +1904,7 @@ def _parse_compose_file(self):
18951904
raise RuntimeError(f"Project name [{dir_basename}] normalized to empty")
18961905

18971906
self.project_name = project_name
1907+
self.label_domain = label_domain
18981908
self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name})
18991909

19001910
services = compose.get("services", None)
@@ -1941,9 +1951,9 @@ def _parse_compose_file(self):
19411951
# volumes: [...]
19421952
self.vols = compose.get("volumes", {})
19431953
podman_compose_labels = [
1944-
"io.podman.compose.config-hash=" + self.yaml_hash,
1945-
"io.podman.compose.project=" + project_name,
1946-
"io.podman.compose.version=" + __version__,
1954+
label_domain + ".compose.config-hash=" + self.yaml_hash,
1955+
label_domain + ".compose.project=" + project_name,
1956+
label_domain + ".compose.version=" + __version__,
19471957
f"PODMAN_SYSTEMD_UNIT=podman-compose@{project_name}.service",
19481958
"com.docker.compose.project=" + project_name,
19491959
"com.docker.compose.project.working_dir=" + dirname,
@@ -2119,6 +2129,12 @@ def _init_global_parser(parser):
21192129
type=str,
21202130
default=None,
21212131
)
2132+
parser.add_argument(
2133+
"--label-domain",
2134+
help="Specify an alternate root domain for resource labels (default: io.podman)",
2135+
type=str,
2136+
default="io.podman",
2137+
)
21222138
parser.add_argument(
21232139
"--podman-path",
21242140
help="Specify an alternate path to podman (default: use location in $PATH variable)",
@@ -2500,10 +2516,10 @@ async def compose_up(compose: PodmanCompose, args):
25002516
"ps",
25012517
[
25022518
"--filter",
2503-
f"label=io.podman.compose.project={compose.project_name}",
2519+
f"label={args.label_domain}.compose.project={compose.project_name}",
25042520
"-a",
25052521
"--format",
2506-
'{{ index .Labels "io.podman.compose.config-hash"}}',
2522+
'{{ index .Labels "' + args.label_domain + '.compose.config-hash"}}',
25072523
],
25082524
)
25092525
)
@@ -2648,7 +2664,7 @@ async def compose_down(compose, args):
26482664
"ps",
26492665
[
26502666
"--filter",
2651-
f"label=io.podman.compose.project={compose.project_name}",
2667+
f"label={args.label_domain}.compose.project={compose.project_name}",
26522668
"-a",
26532669
"--format",
26542670
"{{ .Names }}",
@@ -2682,7 +2698,11 @@ async def compose_down(compose, args):
26822698

26832699
@cmd_run(podman_compose, "ps", "show status of containers")
26842700
async def compose_ps(compose, args):
2685-
ps_args = ["-a", "--filter", f"label=io.podman.compose.project={compose.project_name}"]
2701+
ps_args = [
2702+
"-a",
2703+
"--filter",
2704+
f"label={args.label_domain}.compose.project={compose.project_name}",
2705+
]
26862706
if args.quiet is True:
26872707
ps_args.extend(["--format", "{{.ID}}"])
26882708
elif args.format:

0 commit comments

Comments
 (0)