feat: allow changing resource label-domain from default io.podman

Signed-off-by: legobt <6wbvkn0j@anonaddy.me>
This commit is contained in:
legobt 2024-07-31 02:08:52 +00:00 committed by legobeat
parent a3fb4b373a
commit 757d14ebed
2 changed files with 35 additions and 14 deletions

View File

@ -0,0 +1 @@
Added support to change default resource label prefix from io.podman via --label-domain cli arg

View File

@ -401,7 +401,7 @@ async def assert_volume(compose, mount_dict):
log.debug("podman volume inspect %s || podman volume create %s", vol_name, vol_name) log.debug("podman volume inspect %s || podman volume create %s", vol_name, vol_name)
# TODO: might move to using "volume list" # TODO: might move to using "volume list"
# podman volume list --format '{{.Name}}\t{{.MountPoint}}' \ # podman volume list --format '{{.Name}}\t{{.MountPoint}}' \
# -f 'label=io.podman.compose.project=HERE' # -f f"label={compose.label_domain}.compose.project=HERE"
try: try:
_ = (await compose.podman.output([], "volume", ["inspect", vol_name])).decode("utf-8") _ = (await compose.podman.output([], "volume", ["inspect", vol_name])).decode("utf-8")
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
@ -411,7 +411,7 @@ async def assert_volume(compose, mount_dict):
args = [ args = [
"create", "create",
"--label", "--label",
f"io.podman.compose.project={compose.project_name}", f"{compose.label_domain}.compose.project={compose.project_name}",
"--label", "--label",
f"com.docker.compose.project={compose.project_name}", f"com.docker.compose.project={compose.project_name}",
] ]
@ -804,11 +804,11 @@ def norm_ports(ports_in):
return ports_out 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 = [ args = [
"create", "create",
"--label", "--label",
f"io.podman.compose.project={proj_name}", f"{label_domain}.compose.project={proj_name}",
"--label", "--label",
f"com.docker.compose.project={proj_name}", f"com.docker.compose.project={proj_name}",
] ]
@ -871,7 +871,9 @@ async def assert_cnt_nets(compose, cnt):
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if is_ext: if is_ext:
raise RuntimeError(f"External network [{net_name}] does not exists") from e 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", args)
await compose.podman.output([], "network", ["exists", net_name]) await compose.podman.output([], "network", ["exists", net_name])
@ -1461,7 +1463,7 @@ class Podman:
"ls", "ls",
"--noheading", "--noheading",
"--filter", "--filter",
f"label=io.podman.compose.project={self.compose.project_name}", f"label={self.compose.label_domain}.compose.project={self.compose.project_name}",
"--format", "--format",
"{{.Name}}", "{{.Name}}",
], ],
@ -1677,6 +1679,7 @@ class PodmanCompose:
self.commands = {} self.commands = {}
self.global_args = argparse.Namespace() self.global_args = argparse.Namespace()
self.project_name = None self.project_name = None
self.label_domain = None
self.dirname = None self.dirname = None
self.pods = None self.pods = None
self.containers = [] self.containers = []
@ -1794,6 +1797,10 @@ class PodmanCompose:
relative_files = files relative_files = files
filename = files[0] filename = files[0]
project_name = args.project_name 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
# no_ansi = args.no_ansi # no_ansi = args.no_ansi
# no_cleanup = args.no_cleanup # no_cleanup = args.no_cleanup
# dry_run = args.dry_run # dry_run = args.dry_run
@ -1834,7 +1841,9 @@ class PodmanCompose:
env_vars = norm_as_dict(args.env) env_vars = norm_as_dict(args.env)
self.environ.update(env_vars) self.environ.update(env_vars)
compose = {} compose = {
label_domain: label_domain,
}
# Iterate over files primitively to allow appending to files in-loop # Iterate over files primitively to allow appending to files in-loop
files_iter = iter(files) files_iter = iter(files)
@ -1895,6 +1904,7 @@ class PodmanCompose:
raise RuntimeError(f"Project name [{dir_basename}] normalized to empty") raise RuntimeError(f"Project name [{dir_basename}] normalized to empty")
self.project_name = project_name self.project_name = project_name
self.label_domain = label_domain
self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name}) self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name})
services = compose.get("services", None) services = compose.get("services", None)
@ -1941,9 +1951,9 @@ class PodmanCompose:
# volumes: [...] # volumes: [...]
self.vols = compose.get("volumes", {}) self.vols = compose.get("volumes", {})
podman_compose_labels = [ podman_compose_labels = [
"io.podman.compose.config-hash=" + self.yaml_hash, label_domain + ".compose.config-hash=" + self.yaml_hash,
"io.podman.compose.project=" + project_name, label_domain + ".compose.project=" + project_name,
"io.podman.compose.version=" + __version__, label_domain + ".compose.version=" + __version__,
f"PODMAN_SYSTEMD_UNIT=podman-compose@{project_name}.service", f"PODMAN_SYSTEMD_UNIT=podman-compose@{project_name}.service",
"com.docker.compose.project=" + project_name, "com.docker.compose.project=" + project_name,
"com.docker.compose.project.working_dir=" + dirname, "com.docker.compose.project.working_dir=" + dirname,
@ -2119,6 +2129,12 @@ class PodmanCompose:
type=str, type=str,
default=None, 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( parser.add_argument(
"--podman-path", "--podman-path",
help="Specify an alternate path to podman (default: use location in $PATH variable)", help="Specify an alternate path to podman (default: use location in $PATH variable)",
@ -2500,10 +2516,10 @@ async def compose_up(compose: PodmanCompose, args):
"ps", "ps",
[ [
"--filter", "--filter",
f"label=io.podman.compose.project={compose.project_name}", f"label={args.label_domain}.compose.project={compose.project_name}",
"-a", "-a",
"--format", "--format",
'{{ index .Labels "io.podman.compose.config-hash"}}', '{{ index .Labels "' + args.label_domain + '.compose.config-hash"}}',
], ],
) )
) )
@ -2648,7 +2664,7 @@ async def compose_down(compose, args):
"ps", "ps",
[ [
"--filter", "--filter",
f"label=io.podman.compose.project={compose.project_name}", f"label={args.label_domain}.compose.project={compose.project_name}",
"-a", "-a",
"--format", "--format",
"{{ .Names }}", "{{ .Names }}",
@ -2682,7 +2698,11 @@ async def compose_down(compose, args):
@cmd_run(podman_compose, "ps", "show status of containers") @cmd_run(podman_compose, "ps", "show status of containers")
async def compose_ps(compose, args): 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={args.label_domain}.compose.project={compose.project_name}",
]
if args.quiet is True: if args.quiet is True:
ps_args.extend(["--format", "{{.ID}}"]) ps_args.extend(["--format", "{{.ID}}"])
elif args.format: elif args.format: