1
1
mirror of https://github.com/containers/udica synced 2024-09-24 10:20:44 +02:00

Use constants for engines

Replace copy/paste strings referencing container engines with
constants for reuse. These constants live under parse.

- ENGINE_PODMAN: Constant for the podman engine
- ENGINE_CRIO: Constant for the cri-o engine
- ENGINE_DOCKER: Constant for the docker engine
- ENGINE_ALL: All supported engines

Signed-off-by: Steve Milner <smilner@redhat.com>
This commit is contained in:
Steve Milner 2020-05-15 11:05:48 -04:00 committed by Lukas Vrabec
parent e90a5ed957
commit d56d6cf957
2 changed files with 29 additions and 14 deletions

View File

@ -18,6 +18,7 @@ import argparse
# import udica
from udica.parse import parse_inspect, parse_avc_file
from udica.parse import ENGINE_ALL, ENGINE_PODMAN, ENGINE_DOCKER
from udica import parse
from udica.policy import create_policy, load_policy, generate_playbook
@ -117,7 +118,9 @@ def get_args():
"-e",
"--container-engine",
type=str,
help="Specify which container engine is used for the inspected container (supports: CRI-O, docker, podman)",
help="Specify which container engine is used for the inspected container (supports: {})".format(
", ".join(ENGINE_ALL)
),
dest="ContainerEngine",
required=False,
default="-",
@ -132,7 +135,7 @@ def main():
if opts["ContainerID"]:
container_inspect_raw = None
for backend in ["podman", "docker"]:
for backend in [ENGINE_PODMAN, ENGINE_DOCKER]:
try:
run_inspect = subprocess.Popen(
[backend, "inspect", opts["ContainerID"]],

View File

@ -15,6 +15,18 @@
import json
#: Constant for the podman engine
ENGINE_PODMAN = "podman"
#: Constant for the cri-o engine
ENGINE_CRIO = "CRI-O"
#: Constant for the docker engine
ENGINE_DOCKER = "docker"
#: All supported engines
ENGINE_ALL = [ENGINE_PODMAN, ENGINE_CRIO, ENGINE_DOCKER]
def json_is_podman_or_docker_format(json_rep):
"""Check if the inspected file is in a format from docker or podman.
@ -69,7 +81,7 @@ def parse_inspect(data, ContainerEngine):
if not json_is_podman_format(json_rep):
adjust_json_from_docker(json_rep)
if engine == "docker":
if engine == ENGINE_DOCKER:
adjust_json_from_docker(json_rep)
return json_rep
@ -81,25 +93,25 @@ def get_inspect_format(data, ContainerEngine):
json_rep = json.loads(data)
if json_is_podman_or_docker_format(json_rep):
if json_is_podman_format(json_rep):
return "podman"
return "docker"
return "CRI-O"
return ENGINE_PODMAN
return ENGINE_DOCKER
return ENGINE_CRIO
else:
return engine
def get_mounts(data, inspect_format):
if inspect_format in ["podman", "docker"]:
if inspect_format in [ENGINE_PODMAN, ENGINE_DOCKER]:
return data[0]["Mounts"]
if inspect_format == "CRI-O" and not json_is_podman_or_docker_format(data):
if inspect_format == ENGINE_CRIO and not json_is_podman_or_docker_format(data):
return data["status"]["mounts"]
raise Exception("Error getting mounts from unknown format %s" % inspect_format)
def get_ports(data, inspect_format):
if inspect_format in ["podman", "docker"]:
if inspect_format in [ENGINE_PODMAN, ENGINE_DOCKER]:
return data[0]["NetworkSettings"]["Ports"]
if inspect_format == "CRI-O":
if inspect_format == ENGINE_CRIO:
# Not applicable in the CRI-O case, since this is handled by the
# kube-proxy/CNI.
return []
@ -112,7 +124,7 @@ def get_caps(data, opts, inspect_format):
return []
return opts["Caps"].split(",")
if inspect_format == "podman":
if inspect_format == ENGINE_PODMAN:
return data[0]["EffectiveCaps"]
return []
@ -167,10 +179,10 @@ def parse_avc_file(data):
def validate_container_engine(ContainerEngine):
supported_engines = ["docker", "podman", "CRI-O", "CRIO", "-"]
if ContainerEngine in supported_engines:
if ContainerEngine in ENGINE_ALL + ["CRIO", "-"]:
# Fix CRIO reference to use ENGINE_CRIO
if ContainerEngine == "CRIO":
return "CRI-O"
return ENGINE_CRIO
return ContainerEngine
else:
raise Exception("Container Engine %s is not supported." % ContainerEngine)