Skip to content

Commit 88f73bb

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

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

newsfragments/label_domain.feature

+1
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

+34-14
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

@@ -1459,7 +1461,7 @@ async def volume_ls(self):
14591461
"ls",
14601462
"--noheading",
14611463
"--filter",
1462-
f"label=io.podman.compose.project={self.compose.project_name}",
1464+
f"label={self.compose.label_domain}.compose.project={self.compose.project_name}",
14631465
"--format",
14641466
"{{.Name}}",
14651467
],
@@ -1675,6 +1677,7 @@ def __init__(self):
16751677
self.commands = {}
16761678
self.global_args = argparse.Namespace()
16771679
self.project_name = None
1680+
self.label_domain = None
16781681
self.dirname = None
16791682
self.pods = None
16801683
self.containers = []
@@ -1792,6 +1795,10 @@ def _parse_compose_file(self):
17921795
relative_files = files
17931796
filename = files[0]
17941797
project_name = args.project_name
1798+
label_domain = os.environ.get("COMPOSE_LABEL_DOMAIN", "io.podman")
1799+
if label_domain in args:
1800+
label_domain = args.label_domain
1801+
17951802
# no_ansi = args.no_ansi
17961803
# no_cleanup = args.no_cleanup
17971804
# dry_run = args.dry_run
@@ -1832,7 +1839,9 @@ def _parse_compose_file(self):
18321839
env_vars = norm_as_dict(args.env)
18331840
self.environ.update(env_vars)
18341841

1835-
compose = {}
1842+
compose = {
1843+
label_domain: label_domain,
1844+
}
18361845
# Iterate over files primitively to allow appending to files in-loop
18371846
files_iter = iter(files)
18381847

@@ -1893,6 +1902,7 @@ def _parse_compose_file(self):
18931902
raise RuntimeError(f"Project name [{dir_basename}] normalized to empty")
18941903

18951904
self.project_name = project_name
1905+
self.label_domain = label_domain
18961906
self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name})
18971907

18981908
services = compose.get("services", None)
@@ -1939,9 +1949,9 @@ def _parse_compose_file(self):
19391949
# volumes: [...]
19401950
self.vols = compose.get("volumes", {})
19411951
podman_compose_labels = [
1942-
"io.podman.compose.config-hash=" + self.yaml_hash,
1943-
"io.podman.compose.project=" + project_name,
1944-
"io.podman.compose.version=" + __version__,
1952+
label_domain + ".compose.config-hash=" + self.yaml_hash,
1953+
label_domain + ".compose.project=" + project_name,
1954+
label_domain + ".compose.version=" + __version__,
19451955
f"PODMAN_SYSTEMD_UNIT=podman-compose@{project_name}.service",
19461956
"com.docker.compose.project=" + project_name,
19471957
"com.docker.compose.project.working_dir=" + dirname,
@@ -2117,6 +2127,12 @@ def _init_global_parser(parser):
21172127
type=str,
21182128
default=None,
21192129
)
2130+
parser.add_argument(
2131+
"--label-domain",
2132+
help="Specify an alternate root domain for resource labels (default: io.podman)",
2133+
type=str,
2134+
default="io.podman",
2135+
)
21202136
parser.add_argument(
21212137
"--podman-path",
21222138
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)