|
| 1 | +# We use wrapper library to interact with openshift cluster kinds. |
| 2 | +# This script looks for calls bypassing wrapper library: https://github.com/RedHatQE/openshift-python-wrapper/ |
| 3 | +# created with help from claude |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +PROHIBITED_PATTERNS = [ |
| 10 | + r"\.get\((.*)api_version=(.*),\)", |
| 11 | + r"\.resources\.get\((.*)kind=(.*),\)", |
| 12 | + r"client\.resources\.get(.*)kind=(.*)", |
| 13 | +] |
| 14 | +KIND_PATTERN = r'kind="(.*)"' |
| 15 | + |
| 16 | + |
| 17 | +def find_all_python_files(root_dir: Path) -> list[str]: |
| 18 | + skip_folders = {".tox", "venv", ".pytest_cache", "site-packages", ".git", ".local"} |
| 19 | + |
| 20 | + py_files = [ |
| 21 | + file_name |
| 22 | + for file_name in Path(os.path.abspath(root_dir)).rglob("*.py") |
| 23 | + if not any(any(folder_name in part for folder_name in skip_folders) for part in file_name.parts) |
| 24 | + ] |
| 25 | + return [str(file_name) for file_name in py_files] |
| 26 | + |
| 27 | + |
| 28 | +def check_file_for_violations(filepath: str) -> dict[str, set[str]]: |
| 29 | + with open(filepath, "r") as f: |
| 30 | + content = f.read() |
| 31 | + violations = set() |
| 32 | + kinds = set() |
| 33 | + for line_num, line in enumerate(content.split("\n"), 1): |
| 34 | + line = line.strip() |
| 35 | + for pattern in PROHIBITED_PATTERNS: |
| 36 | + if re.search(pattern, line): |
| 37 | + kind_match = re.search(KIND_PATTERN, line) |
| 38 | + if kind_match: |
| 39 | + kinds.add(kind_match.group(1)) |
| 40 | + violation_str = f"{filepath}:{line_num} - {line}" |
| 41 | + violations.add(violation_str) |
| 42 | + |
| 43 | + return {"violations": violations, "kind": kinds} |
| 44 | + |
| 45 | + |
| 46 | +if __name__ == "__main__": |
| 47 | + all_violations = set() |
| 48 | + all_kinds = set() |
| 49 | + all_files = find_all_python_files(root_dir=Path(__file__).parent.parent) |
| 50 | + for filepath in all_files: |
| 51 | + result = check_file_for_violations(filepath=filepath) |
| 52 | + if result["violations"]: |
| 53 | + all_violations.update(result["violations"]) |
| 54 | + if result["kind"]: |
| 55 | + all_kinds.update(result["kind"]) |
| 56 | + if all_violations: |
| 57 | + print("Prohibited patterns found:") |
| 58 | + for violation in all_violations: |
| 59 | + print(f" {violation}") |
| 60 | + if all_kinds: |
| 61 | + print( |
| 62 | + "\n\nPlease check if the following kinds exists in " |
| 63 | + "https://github.com/RedHatQE/openshift-python-wrapper/tree/main/ocp_resources:" |
| 64 | + ) |
| 65 | + print( |
| 66 | + "For details about why we need such resources in openshift-python-wrapper, please check: " |
| 67 | + "https://github.com/opendatahub-io/opendatahub-tests/blob/main/docs/DEVELOPER_GUIDE.md#" |
| 68 | + "interacting-with-kubernetesopenshift-apis" |
| 69 | + ) |
| 70 | + for kind in all_kinds: |
| 71 | + print(f" {kind}") |
| 72 | + if all_kinds or all_violations: |
| 73 | + sys.exit(1) |
| 74 | + sys.exit(0) |
0 commit comments