2023-08-12 14:25:01 +00:00
|
|
|
from collections.abc import Iterator
|
2023-08-07 09:17:56 +00:00
|
|
|
|
|
|
|
from semver import Version
|
|
|
|
|
|
|
|
from .. import Bundle
|
|
|
|
from ..utils import lookup_dict
|
2023-08-12 13:49:35 +00:00
|
|
|
from . import CheckResult, Fail, Warn
|
2023-08-07 09:17:56 +00:00
|
|
|
|
|
|
|
|
2023-08-12 13:49:35 +00:00
|
|
|
def check_operator_name(bundle: Bundle) -> Iterator[CheckResult]:
|
2023-08-08 08:06:11 +00:00
|
|
|
"""Check if the operator names used in CSV, metadata and filesystem are consistent"""
|
2023-08-07 09:17:56 +00:00
|
|
|
name = bundle.annotations.get("operators.operatorframework.io.bundle.package.v1")
|
|
|
|
if name is None:
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail("Bundle does not define the operator name in annotations.yaml")
|
2023-08-07 09:17:56 +00:00
|
|
|
return
|
|
|
|
if name != bundle.csv_operator_name:
|
2023-08-12 18:06:10 +00:00
|
|
|
msg = (
|
|
|
|
f"Operator name from annotations.yaml ({name})"
|
|
|
|
f" does not match the name defined in the CSV ({bundle.csv_operator_name})"
|
2023-08-12 13:49:35 +00:00
|
|
|
)
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail(msg)
|
2023-08-07 09:17:56 +00:00
|
|
|
if name != bundle.operator_name:
|
2023-08-12 18:06:10 +00:00
|
|
|
msg = (
|
|
|
|
f"Operator name from annotations.yaml ({name})"
|
|
|
|
f" does not match the operator's directory name ({bundle.operator_name})"
|
2023-08-12 13:49:35 +00:00
|
|
|
)
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail(msg)
|
2023-08-07 09:17:56 +00:00
|
|
|
|
|
|
|
|
2023-08-12 13:49:35 +00:00
|
|
|
def check_image(bundle: Bundle) -> Iterator[CheckResult]:
|
2023-08-08 08:06:11 +00:00
|
|
|
"""Check if containerImage is properly defined and used in a deployment"""
|
2023-08-07 09:17:56 +00:00
|
|
|
try:
|
|
|
|
container_image = lookup_dict(bundle.csv, "metadata.annotations.containerImage")
|
|
|
|
if container_image is None:
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail("CSV doesn't define .metadata.annotations.containerImage")
|
2023-08-07 09:17:56 +00:00
|
|
|
return
|
|
|
|
deployments = lookup_dict(bundle.csv, "spec.install.spec.deployments")
|
|
|
|
if deployments is None:
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail("CSV doesn't define .spec.install.spec.deployments")
|
2023-08-07 09:17:56 +00:00
|
|
|
return
|
|
|
|
for deployment in deployments:
|
|
|
|
containers = lookup_dict(deployment, "spec.template.spec.containers", [])
|
|
|
|
if any(container_image == x.get("image") for x in containers):
|
|
|
|
return
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail(f"container image {container_image} not used by any deployment")
|
2023-08-18 16:33:07 +00:00
|
|
|
except Exception as exc: # pylint: disable=broad-exception-caught
|
2023-08-15 07:02:11 +00:00
|
|
|
yield Fail(str(exc))
|
2023-08-07 09:17:56 +00:00
|
|
|
|
|
|
|
|
2023-08-12 13:49:35 +00:00
|
|
|
def check_semver(bundle: Bundle) -> Iterator[CheckResult]:
|
2023-08-08 08:06:11 +00:00
|
|
|
"""Check that the bundle version is semver compliant"""
|
2023-08-07 09:17:56 +00:00
|
|
|
try:
|
|
|
|
_ = Version.parse(bundle.operator_version)
|
|
|
|
except ValueError:
|
2023-08-12 13:49:35 +00:00
|
|
|
yield Warn(
|
2023-08-15 07:02:11 +00:00
|
|
|
f"Version from filesystem ({bundle.operator_version}) is not valid semver"
|
2023-08-12 13:49:35 +00:00
|
|
|
)
|
2023-08-07 09:17:56 +00:00
|
|
|
try:
|
|
|
|
_ = Version.parse(bundle.csv_operator_version)
|
|
|
|
except ValueError:
|
2023-08-12 13:49:35 +00:00
|
|
|
yield Warn(
|
2023-08-15 07:02:11 +00:00
|
|
|
f"Version from CSV ({bundle.csv_operator_version}) is not valid semver"
|
2023-08-12 13:49:35 +00:00
|
|
|
)
|