Skip to content

Commit

Permalink
feat: allow changing resource label-domain from default io.podman
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat committed Jul 31, 2024
1 parent 80b6170 commit bbcc79a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
1 change: 1 addition & 0 deletions newsfragments/label_domain.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support to change default resource label prefix from io.podman via --label-domain cli arg
49 changes: 36 additions & 13 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ async def assert_volume(compose, mount_dict):
log.debug("podman volume inspect %s || podman volume create %s", vol_name, vol_name)
# TODO: might move to using "volume list"
# podman volume list --format '{{.Name}}\t{{.MountPoint}}' \
# -f 'label=io.podman.compose.project=HERE'
# -f f"label={compose.label_domain}.project=HERE"
try:
_ = (await compose.podman.output([], "volume", ["inspect", vol_name])).decode("utf-8")
except subprocess.CalledProcessError as e:
Expand All @@ -412,7 +412,7 @@ async def assert_volume(compose, mount_dict):
args = [
"create",
"--label",
f"io.podman.compose.project={compose.project_name}",
f"{compose.label_domain}.compose.project={compose.project_name}",
"--label",
f"com.docker.compose.project={compose.project_name}",
]
Expand Down Expand Up @@ -799,11 +799,11 @@ def norm_ports(ports_in):
return ports_out


def get_network_create_args(net_desc, proj_name, net_name):
def get_network_create_args(net_desc, proj_name, net_name, label_domain='io.podman'):
args = [
"create",
"--label",
f"io.podman.compose.project={proj_name}",
f"{label_domain}.compose.project={proj_name}",
"--label",
f"com.docker.compose.project={proj_name}",
]
Expand Down Expand Up @@ -866,7 +866,9 @@ async def assert_cnt_nets(compose, cnt):
except subprocess.CalledProcessError as e:
if is_ext:
raise RuntimeError(f"External network [{net_name}] does not exists") from e
args = get_network_create_args(net_desc, compose.project_name, net_name)
args = get_network_create_args(
net_desc, compose.project_name, net_name, compose.label_domain
)
await compose.podman.output([], "network", args)
await compose.podman.output([], "network", ["exists", net_name])

Expand Down Expand Up @@ -1456,7 +1458,7 @@ async def volume_ls(self, proj_name=None):
"ls",
"--noheading",
"--filter",
f"label=io.podman.compose.project={proj_name}",
f"label={self.compose.label_domain}.compose.project={proj_name}",
"--format",
"{{.Name}}",
],
Expand Down Expand Up @@ -1672,6 +1674,7 @@ def __init__(self):
self.commands = {}
self.global_args = None
self.project_name = None
self.label_domain = None
self.dirname = None
self.pods = None
self.containers = None
Expand Down Expand Up @@ -1789,6 +1792,14 @@ def _parse_compose_file(self):
relative_files = files
filename = files[0]
project_name = args.project_name
label_domain = os.environ.get("COMPOSE_LABEL_DOMAIN", "io.podman")
if label_domain in args:
label_domain = args.label_domain

#if args.label_domain:
# label_domain = args.label_domain
#else:
# label_domain = os.eviron.get("COMPOSE_LABEL_DOMAIN", "io.podman")
# no_ansi = args.no_ansi
# no_cleanup = args.no_cleanup
# dry_run = args.dry_run
Expand Down Expand Up @@ -1890,6 +1901,7 @@ def _parse_compose_file(self):
raise RuntimeError(f"Project name [{dir_basename}] normalized to empty")

self.project_name = project_name
self.label_domain = label_domain
self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name})

services = compose.get("services", None)
Expand Down Expand Up @@ -1934,9 +1946,9 @@ def _parse_compose_file(self):
# volumes: [...]
self.vols = compose.get("volumes", {})
podman_compose_labels = [
"io.podman.compose.config-hash=" + self.yaml_hash,
"io.podman.compose.project=" + project_name,
"io.podman.compose.version=" + __version__,
label_domain + ".compose.config-hash=" + self.yaml_hash,
label_domain + ".compose.project=" + project_name,
label_domain + ".compose.version=" + __version__,
f"PODMAN_SYSTEMD_UNIT=podman-compose@{project_name}.service",
"com.docker.compose.project=" + project_name,
"com.docker.compose.project.working_dir=" + dirname,
Expand Down Expand Up @@ -2039,6 +2051,7 @@ def _parse_args(self, argv=None):
for cmd_parser in cmd._parse_args: # pylint: disable=protected-access
cmd_parser(subparser)
self.global_args = parser.parse_args(argv)
print(self.global_args)
if self.global_args.in_pod is not None and self.global_args.in_pod.lower() not in (
'',
'true',
Expand Down Expand Up @@ -2111,6 +2124,12 @@ def _init_global_parser(parser):
type=str,
default=None,
)
parser.add_argument(
"--label-domain",
help="Specify an alternate root domain for resource labels (default: io.podman)",
type=str,
default="io.podman",
)
parser.add_argument(
"--podman-path",
help="Specify an alternate path to podman (default: use location in $PATH variable)",
Expand Down Expand Up @@ -2494,10 +2513,10 @@ async def compose_up(compose: PodmanCompose, args):
"ps",
[
"--filter",
f"label=io.podman.compose.project={compose.project_name}",
f"label={compose.label_domain}.compose.project={compose.project_name}",
"-a",
"--format",
'{{ index .Labels "io.podman.compose.config-hash"}}',
'{{ index .Labels "' + compose.label_domain + '.compose.config-hash"}}',
],
)
)
Expand Down Expand Up @@ -2642,7 +2661,7 @@ async def compose_down(compose, args):
"ps",
[
"--filter",
f"label=io.podman.compose.project={compose.project_name}",
f"label={compose.label_domain}.compose.project={compose.project_name}",
"-a",
"--format",
"{{ .Names }}",
Expand Down Expand Up @@ -2676,7 +2695,11 @@ async def compose_down(compose, args):

@cmd_run(podman_compose, "ps", "show status of containers")
async def compose_ps(compose, args):
ps_args = ["-a", "--filter", f"label=io.podman.compose.project={compose.project_name}"]
ps_args = [
"-a",
"--filter",
f"label={compose.label_domain}.compose.project={compose.project_name}",
]
if args.quiet is True:
ps_args.extend(["--format", "{{.ID}}"])
elif args.format:
Expand Down

0 comments on commit bbcc79a

Please sign in to comment.