-
Notifications
You must be signed in to change notification settings - Fork 1
增加监控导出端口一致性audit #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ghangz
wants to merge
2
commits into
MetaX-MACA:main
Choose a base branch
from
ghangz:mengz/mxexporter-port-alignment-audit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
增加监控导出端口一致性audit #10
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools")) | ||
|
|
||
| from port_alignment import build | ||
|
|
||
|
|
||
| class PortAlignmentTest(unittest.TestCase): | ||
| def test_reports_alignment(self): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| root = Path(tmpdir) | ||
| (root / "mx_exporter").mkdir() | ||
| (root / "deployment" / "mx-exporter").mkdir(parents=True) | ||
| (root / "mx_exporter" / "__init__.py").write_text('parser.add_argument("-p", "--port", default=8000)\n', encoding="utf-8") | ||
| (root / "start_mxexporter.sh").write_text("HOST_PORT=8000\n", encoding="utf-8") | ||
| (root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").write_text( | ||
| "port: 8000\ncontainerPort: 8000\n", encoding="utf-8" | ||
| ) | ||
|
|
||
| report = build(root) | ||
|
|
||
| self.assertTrue(report["aligned"]) | ||
|
|
||
| def test_errors_when_yaml_ports_are_missing(self): | ||
| with tempfile.TemporaryDirectory() as tmpdir: | ||
| root = Path(tmpdir) | ||
| (root / "mx_exporter").mkdir() | ||
| (root / "deployment" / "mx-exporter").mkdir(parents=True) | ||
| (root / "mx_exporter" / "__init__.py").write_text( | ||
| 'parser.add_argument("-p", "--port", default=8000)\n', | ||
| encoding="utf-8", | ||
| ) | ||
| (root / "start_mxexporter.sh").write_text("HOST_PORT=8000\n", encoding="utf-8") | ||
| (root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").write_text( | ||
| "kind: DaemonSet\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| build(root) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #!/usr/bin/env python3 | ||
| """Audit mx-exporter port defaults across code and deployment files.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import json | ||
| import re | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| CLI_RE = re.compile(r'parser\.add_argument\("-p".*default=(\d+)') | ||
| SCRIPT_RE = re.compile(r"HOST_PORT=(\d+)") | ||
| YAML_PORT_RE = re.compile(r"(?:port|containerPort):\s*(\d+)") | ||
|
|
||
|
|
||
| def build(repo_root: Path) -> dict[str, object]: | ||
| cli_text = (repo_root / "mx_exporter" / "__init__.py").read_text(encoding="utf-8") | ||
| script_text = (repo_root / "start_mxexporter.sh").read_text(encoding="utf-8") | ||
| static_text = (repo_root / "deployment" / "mx-exporter" / "mx-exporter-daemonset.yaml").read_text(encoding="utf-8") | ||
|
|
||
| cli_match = CLI_RE.search(cli_text) | ||
| script_match = SCRIPT_RE.search(script_text) | ||
| if not cli_match: | ||
| raise ValueError("Failed to find CLI default port in mx_exporter/__init__.py") | ||
| if not script_match: | ||
| raise ValueError("Failed to find default HOST_PORT in start_mxexporter.sh") | ||
|
|
||
| cli_default = int(cli_match.group(1)) | ||
| script_default = int(script_match.group(1)) | ||
| static_ports = sorted({int(value) for value in YAML_PORT_RE.findall(static_text)}) | ||
| if not static_ports: | ||
| raise ValueError("Failed to find any ports in mx-exporter-daemonset.yaml") | ||
| aligned = script_default == cli_default and all(port == cli_default for port in static_ports) | ||
| return { | ||
| "cli_default_port": cli_default, | ||
| "script_default_port": script_default, | ||
| "static_ports": static_ports, | ||
| "aligned": aligned, | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("--repo-root", type=Path, default=Path(".")) | ||
| parser.add_argument("--output", type=Path) | ||
| args = parser.parse_args() | ||
|
|
||
| text = json.dumps(build(args.repo_root), indent=2, ensure_ascii=False) | ||
| if args.output: | ||
| args.output.write_text(text + "\n", encoding="utf-8") | ||
| else: | ||
| print(text) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果
YAML_PORT_RE.findall(static_text)没有匹配到任何端口(例如 YAML 格式发生变化导致正则失效),static_ports将为空列表,此时aligned仍会返回True。建议增加对
static_ports是否为空的校验,以避免因匹配失败导致的一致性误判。