faas-gitops 0.4.1__tar.gz → 0.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {faas_gitops-0.4.1/src/faas_gitops.egg-info → faas_gitops-0.6.1}/PKG-INFO +1 -1
  2. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/pyproject.toml +1 -1
  3. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/adapters/bizy_function_provider.py +6 -0
  4. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/cli/app.py +44 -2
  5. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/cli/parser.py +20 -0
  6. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/protocols/faas_function.py +12 -0
  7. faas_gitops-0.6.1/src/faas_gitops/usecases/admit_check.py +225 -0
  8. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/deploy_functions.py +10 -4
  9. faas_gitops-0.6.1/src/faas_gitops/usecases/format.py +63 -0
  10. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/plan.py +37 -5
  11. {faas_gitops-0.4.1 → faas_gitops-0.6.1/src/faas_gitops.egg-info}/PKG-INFO +1 -1
  12. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/SOURCES.txt +6 -1
  13. faas_gitops-0.6.1/tests/test_admit_check.py +383 -0
  14. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_deploy_functions.py +18 -0
  15. faas_gitops-0.6.1/tests/test_format.py +158 -0
  16. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_plan.py +41 -2
  17. faas_gitops-0.6.1/tests/test_version.py +16 -0
  18. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/MANIFEST.in +0 -0
  19. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/setup.cfg +0 -0
  20. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/__init__.py +0 -0
  21. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/adapters/__init__.py +0 -0
  22. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/adapters/bizy_template_provider.py +0 -0
  23. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/adapters/git_cli_gateway.py +0 -0
  24. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/cli/__init__.py +0 -0
  25. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/config.py +0 -0
  26. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/filtering.py +0 -0
  27. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/infra/__init__.py +0 -0
  28. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/infra/yaml_utils.py +0 -0
  29. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/layout.py +0 -0
  30. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/models.py +0 -0
  31. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/protocols/__init__.py +0 -0
  32. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/protocols/faas_template.py +0 -0
  33. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/protocols/git_gateway.py +0 -0
  34. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/result.py +0 -0
  35. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/__init__.py +0 -0
  36. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/apply_function_changes.py +0 -0
  37. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/apply_plan.py +0 -0
  38. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/apply_template_changes.py +0 -0
  39. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/detect_drift.py +0 -0
  40. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/detect_function_drift.py +0 -0
  41. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/detect_template_drift.py +0 -0
  42. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/render_definitions.py +0 -0
  43. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/snapshot.py +0 -0
  44. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/sync_functions.py +0 -0
  45. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops/usecases/sync_templates.py +0 -0
  46. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/dependency_links.txt +0 -0
  47. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/entry_points.txt +0 -0
  48. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/requires.txt +0 -0
  49. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/src/faas_gitops.egg-info/top_level.txt +0 -0
  50. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_apply_function_changes.py +0 -0
  51. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_apply_plan.py +0 -0
  52. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_apply_template_changes.py +0 -0
  53. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_detect_drift.py +0 -0
  54. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_detect_function_drift.py +0 -0
  55. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_git_cli_gateway.py +0 -0
  56. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_render_definitions.py +0 -0
  57. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_result_contract.py +0 -0
  58. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_scope_args.py +0 -0
  59. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_snapshot.py +0 -0
  60. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_sync_functions.py +0 -0
  61. {faas_gitops-0.4.1 → faas_gitops-0.6.1}/tests/test_sync_templates.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: faas-gitops
3
- Version: 0.4.1
3
+ Version: 0.6.1
4
4
  Summary: Declarative GitOps for FaaS
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: aiofiles>=23.0
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "faas-gitops"
7
- version = "0.4.1"
7
+ version = "0.6.1"
8
8
  description = "Declarative GitOps for FaaS"
9
9
  requires-python = ">=3.12"
10
10
  dependencies = [
@@ -1,4 +1,6 @@
1
1
  from __future__ import annotations
2
+ from pathlib import Path
3
+
2
4
  from bizy_deploy import AsyncFaaSActionProvider
3
5
 
4
6
 
@@ -20,6 +22,10 @@ class BizyFunctionProvider:
20
22
  result = await self._p.get_latest_function_definition(function_id=func_id)
21
23
  return result.to_dict() if result else None
22
24
 
25
+ async def render_definition(self, func_id: str, version: str) -> str | None:
26
+ path = await self._p.get_definition_render(function_id=func_id, version=version)
27
+ return Path(path).read_text() if path else None
28
+
23
29
  async def push_definition(self, func_id: str, data: dict, comment: str = "") -> str:
24
30
  return await self._p.create_function_definition(merged_def=data, function_id=func_id)
25
31
 
@@ -18,11 +18,13 @@ from faas_gitops.config import load_settings, warn_deprecated_env
18
18
  from faas_gitops.models import ApplyResult
19
19
  from faas_gitops.usecases.plan import Plan, PlanBuild
20
20
  from faas_gitops.usecases.apply_function_changes import ApplyFunctionChanges
21
+ from faas_gitops.usecases.admit_check import AdmitCheck, admit_report
21
22
  from faas_gitops.usecases.apply_plan import ApplyPlan, StalePlanError
22
23
  from faas_gitops.usecases.apply_template_changes import ApplyTemplateChanges
23
24
  from faas_gitops.usecases.deploy_functions import DeployFunctions
24
25
  from faas_gitops.usecases.detect_function_drift import DetectFunctionDrift
25
26
  from faas_gitops.usecases.detect_template_drift import DetectTemplateDrift
27
+ from faas_gitops.usecases.format import format_branch
26
28
  from faas_gitops.usecases.render_definitions import render_definitions
27
29
  from faas_gitops.usecases.snapshot import snapshot
28
30
  from faas_gitops.usecases.sync_functions import SyncFunctions
@@ -87,9 +89,10 @@ async def async_main() -> int:
87
89
  return 0
88
90
 
89
91
  if args.command == "plan":
92
+ faas = await _make_faas(stack)
90
93
  git = _make_git()
91
94
  repo_path = await git.clone_repo(repo_slug)
92
- plan = await PlanBuild(git, settings).run(
95
+ plan = await PlanBuild(git, settings, BizyFunctionProvider(faas)).run(
93
96
  repo_path, base=args.to, ref=args.from_ref, repo_slug=repo_slug,
94
97
  )
95
98
  plan.log()
@@ -100,6 +103,26 @@ async def async_main() -> int:
100
103
  logger.info(f"plan written to {out.resolve()}")
101
104
  return 0 if plan.kind == "none" else 2
102
105
 
106
+ if args.command == "admit":
107
+ faas = await _make_faas(stack)
108
+ git = _make_git()
109
+ repo_path = await git.clone_repo(repo_slug)
110
+ plan = Plan.from_dict(json.loads(Path(args.plan).read_text()))
111
+ try:
112
+ admitted = await AdmitCheck(BizyFunctionProvider(faas), BizyTemplateProvider(faas), git).run(repo_path, plan)
113
+ except StalePlanError as e:
114
+ logger.error(str(e))
115
+ return 1
116
+ out = Path(args.output)
117
+ out.parent.mkdir(parents=True, exist_ok=True)
118
+ out.write_text(json.dumps(admitted.payload(), ensure_ascii=False, indent=2, sort_keys=True) + "\n")
119
+ logger.info(f"admitted plan written to {out.resolve()}")
120
+ rc = admit_report(admitted.payload()).emit()
121
+ if args.strict and admitted.skipped:
122
+ logger.error(f"--strict: {len(admitted.skipped)} entr(y/ies) refused → exit 1")
123
+ return 1
124
+ return rc
125
+
103
126
  if args.command == "apply-plan":
104
127
  faas = await _make_faas(stack)
105
128
  git = _make_git()
@@ -121,9 +144,21 @@ async def async_main() -> int:
121
144
  print_apply_report(tpl_result, dry_run, _out("templates"), domain="templates"),
122
145
  print_apply_report(fn_result, dry_run, _out("functions"), domain="functions"),
123
146
  )
147
+ for f in plan.functions: # per-deployment visibility (fid/depid, admit marks)
148
+ for d in f.deployments:
149
+ if isinstance(d, dict):
150
+ refused = d.get("refused")
151
+ logger.info(
152
+ f" ◆ {f.id}/{d['id']} {d.get('status', '?')} v{d.get('definition_version', '?')}"
153
+ + (f" ⏸ {refused}" if refused else "")
154
+ )
124
155
  if not dry_run and args.auto_deploy and fn_result.applied_versions:
156
+ refused_deployments = {
157
+ (f.id, d["id"]) for f in plan.functions for d in f.deployments
158
+ if isinstance(d, dict) and "refused" in d
159
+ }
125
160
  deploy_result = await DeployFunctions(BizyFunctionProvider(faas), settings).run(
126
- fn_result.applied_versions, repo_path
161
+ fn_result.applied_versions, repo_path, refused_deployments=refused_deployments
127
162
  )
128
163
  rc = max(rc, print_deploy_report(deploy_result, _out("deploy")))
129
164
  return rc
@@ -137,6 +172,13 @@ async def async_main() -> int:
137
172
  logger.info(f"render-definitions: {n} file(s) written")
138
173
  return 0
139
174
 
175
+ if args.command == "format":
176
+ git = _make_git()
177
+ repo_path = await git.clone_repo(repo_slug)
178
+ n = await format_branch(git, repo_path, branch=args.branch, settings=settings)
179
+ logger.info(f"format: {n} file(s) formatted")
180
+ return 0
181
+
140
182
  if args.command == "detect-template-changes":
141
183
  git = _make_git()
142
184
  repo_path = await git.clone_repo(repo_slug)
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import argparse
4
+ import importlib.metadata
4
5
 
5
6
  from faas_gitops.config import Settings
6
7
  from faas_gitops.filtering import match_scope
@@ -49,6 +50,9 @@ def in_template_scope(item_id: str, tags: list[str], settings: Settings) -> bool
49
50
 
50
51
  def build_parser(settings: Settings) -> argparse.ArgumentParser:
51
52
  parser = argparse.ArgumentParser(prog="faas-gitops")
53
+ parser.add_argument(
54
+ "--version", action="version", version=importlib.metadata.version("faas-gitops"),
55
+ )
52
56
  sub = parser.add_subparsers(dest="command")
53
57
 
54
58
  def _add_repo(p: argparse.ArgumentParser) -> None:
@@ -86,11 +90,27 @@ def build_parser(settings: Settings) -> argparse.ArgumentParser:
86
90
  add_scope_args(p)
87
91
  _add_repo(p)
88
92
 
93
+ p = sub.add_parser("admit")
94
+ p.add_argument("--plan", required=True, help="path to plan.json produced by `faas-gitops plan`")
95
+ p.add_argument("-o", "--output", required=True,
96
+ help="write the narrowed plan here (drop-in --plan input for apply-plan; "
97
+ "refused deployments marked in place, all-refused functions under `skipped: [{id, error}]`)")
98
+ p.add_argument("--strict", action="store_true",
99
+ help="exit 1 when anything was refused (default exit 0 — refusal is a skip)")
100
+ add_scope_args(p)
101
+ p.add_argument("--repo", default=settings.default_repo)
102
+
89
103
  p = sub.add_parser("render-definitions")
90
104
  p.add_argument("--branch", default=None)
91
105
  add_scope_args(p)
92
106
  _add_repo(p)
93
107
 
108
+ p = sub.add_parser("format")
109
+ p.add_argument("--branch", required=True,
110
+ help="branch whose template + function YAML files get re-formatted in place")
111
+ add_scope_args(p)
112
+ _add_repo(p)
113
+
94
114
  p = sub.add_parser("detect-template-changes")
95
115
  add_scope_args(p)
96
116
  _add_repo(p)
@@ -31,6 +31,18 @@ class FaaSFunctionProvider(Protocol):
31
31
  """
32
32
  ...
33
33
 
34
+ async def render_definition(self, func_id: str, version: str) -> str | None:
35
+ """Render one definition version into deployable YAML (current template on FaaS).
36
+
37
+ Args:
38
+ func_id: Function identifier.
39
+ version: Definition version number (e.g. a deployment's definition_version).
40
+
41
+ Returns:
42
+ Rendered YAML string (same shape as rendered.yaml files), or None if unavailable.
43
+ """
44
+ ...
45
+
34
46
  async def push_definition(self, func_id: str, data: dict, comment: str = "") -> str:
35
47
  """Create a new function definition version.
36
48
 
@@ -0,0 +1,225 @@
1
+ """F24 admit: read-only plan filter — verify the plan's baseline (``from_ref``
2
+ snapshot) still holds before anything is updated:
3
+
4
+ * templates: FaaS template content vs baseline template (diff surfaced on refusal);
5
+ a diverged template is dropped from the plan;
6
+ * functions: per active deployment, the platform's rendering of the deployed
7
+ ``definition_version`` vs baseline ``rendered.yaml`` — a diverged (or
8
+ un-checkable) deployment is MARKED ``"refused": <reason>`` and rides along
9
+ in the plan; the function's definition update is skipped only when EVERY
10
+ deployment is refused (baseline ``rendered.yaml`` required — missing → the
11
+ function is refused).
12
+
13
+ The narrowed result is an ``AdmitPlan`` — a ``Plan`` subclass carrying the
14
+ refusals as ``skipped: [Refusal]`` dataclasses. ``AdmitPlan.payload()`` reuses
15
+ ``Plan.payload()`` and appends the skipped bucket, so it stays a drop-in
16
+ ``--plan`` input for apply-plan (``Plan.from_dict`` ignores the extra key).
17
+ One refused entry never refuses the run (refusal → exit 0); refused entries
18
+ resurface on the next converge run until the drift is resolved by hand.
19
+
20
+ Self-contained by design (#105): plan.py / apply_plan.py / result.py are NOT
21
+ touched — StalePlanError is imported from its current home (apply_plan) and the
22
+ freshness guard mirrors ApplyPlan._assert_fresh locally.
23
+ """
24
+ from __future__ import annotations
25
+
26
+ import difflib
27
+ import json
28
+ from dataclasses import asdict, dataclass, field, replace
29
+ from pathlib import Path
30
+
31
+ from loguru import logger
32
+
33
+ from faas_gitops.infra.yaml_utils import load_yaml
34
+ from faas_gitops.layout import FUNCTION_DIR, FUNCTION_RENDERED, TEMPLATE_DIR
35
+ from faas_gitops.protocols.faas_function import FaaSFunctionProvider
36
+ from faas_gitops.protocols.faas_template import FaaSTemplateProvider
37
+ from faas_gitops.protocols.git_gateway import GitWriter
38
+ from faas_gitops.result import Bucket, Buckets, Report
39
+ from faas_gitops.usecases.apply_plan import StalePlanError
40
+ from faas_gitops.usecases.plan import FunctionChange, Plan, PlanContext
41
+ from faas_gitops.usecases.sync_functions import read_function_from_dir
42
+ from faas_gitops.usecases.sync_templates import read_template_from_dir
43
+
44
+
45
+ def _canon(doc) -> str:
46
+ """Content identity of a YAML document — comment/key-order/str-vs-dict agnostic."""
47
+ return json.dumps(load_yaml(doc) if isinstance(doc, str) else doc,
48
+ sort_keys=True, ensure_ascii=False, default=str)
49
+
50
+
51
+ def _diff(ref: str, cur: str, limit: int = 8) -> str:
52
+ """First differing lines of a baseline→remote unified diff (capped, one line)."""
53
+ lines = [l for l in difflib.unified_diff(ref.splitlines(), cur.splitlines(), lineterm="")
54
+ if not l.startswith(("---", "+++", "@@"))]
55
+ return " | ".join(lines[:limit]) + (" …" if len(lines) > limit else "")
56
+
57
+
58
+ def _kind(n_templates: int, n_functions: int) -> str:
59
+ if n_templates and n_functions:
60
+ return "mixed"
61
+ if n_templates:
62
+ return "templates"
63
+ if n_functions:
64
+ return "functions"
65
+ return "none"
66
+
67
+
68
+ @dataclass(frozen=True)
69
+ class Refusal:
70
+ id: str
71
+ error: str
72
+
73
+
74
+ @dataclass(frozen=True)
75
+ class AdmitPlan(Plan):
76
+ """A plan narrowed by admit — refused templates/functions are dropped from
77
+ their lists and ride along as ``skipped`` refusals; refused deployments
78
+ stay in their function's ``deployments`` records, marked ``refused``."""
79
+
80
+ skipped: list[Refusal] = field(default_factory=list)
81
+
82
+ @classmethod
83
+ def narrowed(cls, plan: Plan, templates: list[str],
84
+ functions: list[FunctionChange], skipped: list[Refusal]) -> "AdmitPlan":
85
+ return cls(
86
+ kind=_kind(len(templates), len(functions)),
87
+ templates=templates,
88
+ functions=functions,
89
+ context=plan.context,
90
+ skipped=sorted(skipped, key=lambda s: s.id),
91
+ )
92
+
93
+ def payload(self) -> dict:
94
+ """Plan payload + skipped bucket — drop-in for apply-plan (extra key ignored)."""
95
+ return super().payload() | {"skipped": [asdict(s) for s in self.skipped]}
96
+
97
+
98
+ def admit_report(payload: dict) -> Report:
99
+ """F01 contract built from existing buckets — refusal is a skip (exit 0), not a failure."""
100
+ r = Report(headline="admit report")
101
+ r.add(Bucket("admitted", "✓"), [f["id"] for f in payload["functions"]])
102
+ r.add(Buckets.SKIPPED, payload["skipped"])
103
+ r.add(Buckets.FAILED, [])
104
+ return r
105
+
106
+
107
+ class AdmitCheck:
108
+ """Per-entry isolation — a diverged (or un-checkable) entry never refuses
109
+ the run: templates and all-deployments-refused functions are dropped;
110
+ single refused deployments are marked in place (fail-safe, fail-local)."""
111
+
112
+ def __init__(
113
+ self,
114
+ func_provider: FaaSFunctionProvider,
115
+ tpl_provider: FaaSTemplateProvider,
116
+ git: GitWriter,
117
+ ) -> None:
118
+ self._provider = func_provider
119
+ self._tpl = tpl_provider
120
+ self._git = git
121
+
122
+ async def run(self, repo_path: Path, plan: Plan) -> AdmitPlan:
123
+ """Return the narrowed AdmitPlan (context preserved, kind recomputed,
124
+ function refusals under skipped: [Refusal], per-deployment refusals
125
+ marked in ``deployments`` records)."""
126
+ await self._assert_fresh(repo_path, plan.context)
127
+ from_ref = plan.context.from_ref
128
+ await self._git.checkout_origin(repo_path, from_ref)
129
+
130
+ templates: list[str] = []
131
+ functions: list[FunctionChange] = []
132
+ skipped: list[Refusal] = []
133
+ for t in plan.templates:
134
+ error = await self._guard(self._tpl_check, repo_path, from_ref, t)
135
+ if error is None:
136
+ templates.append(t)
137
+ else:
138
+ self._refuse(skipped, t, error)
139
+ for f in plan.functions:
140
+ try:
141
+ refusal, deployments = await self._fn_check(repo_path, from_ref, f)
142
+ except Exception as e: # un-enumerable → refuse that function only (fail-safe)
143
+ refusal, deployments = f"admit-check error: {e}", f.deployments
144
+ if refusal is None:
145
+ functions.append(replace(f, deployments=deployments))
146
+ else:
147
+ self._refuse(skipped, f.id, refusal)
148
+ return AdmitPlan.narrowed(plan, templates, functions, skipped)
149
+
150
+ async def _guard(self, check, repo_path: Path, from_ref: str, entry: str) -> str | None:
151
+ try:
152
+ return await check(repo_path, from_ref, entry)
153
+ except Exception as e: # un-checkable → refuse that entry only (fail-safe)
154
+ return f"admit-check error: {e}"
155
+
156
+ @staticmethod
157
+ def _refuse(skipped: list[Refusal], entry: str, error: str) -> None:
158
+ logger.warning(f" ⏸ {entry}: {error} — 本轮跳过,欠账下轮 plan 自动现形")
159
+ skipped.append(Refusal(entry, error))
160
+
161
+ async def _assert_fresh(self, repo_path: Path, c: PlanContext) -> None:
162
+ """Mirrors ApplyPlan._assert_fresh (kept local: apply_plan.py untouched)."""
163
+ for ref, sha in ((c.from_ref, c.from_sha), (c.to_ref, c.to_sha)):
164
+ try:
165
+ current = await self._git.short_sha(repo_path, ref)
166
+ except RuntimeError:
167
+ raise StalePlanError(f"plan is stale: {ref} no longer exists (expected {sha})") from None
168
+ if current != sha:
169
+ raise StalePlanError(f"plan is stale: {ref} moved {sha} → {current}; re-run `faas-gitops plan`")
170
+
171
+ async def _tpl_check(self, repo_path: Path, from_ref: str, tpl_id: str) -> str | None:
172
+ """Rule 1 — FaaS template vs baseline template. None = admit; error = refuse."""
173
+ org, _, name = tpl_id.partition("/")
174
+ tpl_dir = repo_path / TEMPLATE_DIR / org / name
175
+ if not tpl_dir.exists():
176
+ return f"template-baseline-missing: 基线 {from_ref} 无 templates/{tpl_id}"
177
+ ref = (await read_template_from_dir(tpl_dir)).to_faas_yaml()
178
+ remote = await self._tpl.get_template_content(org, name)
179
+ if _canon(remote) != _canon(ref):
180
+ return f"template-diverged: FaaS 模板 ≠ 基线 {from_ref}: {_diff(ref, remote)}"
181
+ return None
182
+
183
+ async def _fn_check(self, repo_path: Path, from_ref: str,
184
+ f: FunctionChange) -> tuple[str | None, list[dict]]:
185
+ """Rule 2 — per deployment: the platform must still render the deployed
186
+ version to the baseline rendered.yaml. A diverged (or un-checkable)
187
+ deployment is marked ``refused`` in its record; the function's
188
+ definition update is skipped only when EVERY deployment is refused.
189
+ Returns (function refusal | None, annotated deployments)."""
190
+ func_dir = repo_path / FUNCTION_DIR / f.id
191
+ parts = await read_function_from_dir(func_dir)
192
+ if parts.definition is None:
193
+ return None, f.deployments # no baseline definition to protect → admit
194
+ rendered_path = func_dir / FUNCTION_RENDERED
195
+ if not rendered_path.exists():
196
+ logger.warning(f" ! {f.id}: 基线 {from_ref} 无 {FUNCTION_RENDERED},无法核验部署渲染(先 render-definitions)")
197
+ return f"rendered-missing: 基线 {from_ref} 无 {FUNCTION_RENDERED}", f.deployments
198
+ deployments = f.deployments or await self._load_deployments(f.id) # old/pure-git plan → fresh listing
199
+ if not deployments:
200
+ return None, deployments # nothing deployed → admit
201
+ want = _canon(rendered_path.read_text())
202
+ annotated: list[dict] = []
203
+ for d in deployments:
204
+ entry = dict(d)
205
+ dep, version = d["id"], str(d.get("definition_version", ""))
206
+ try:
207
+ rendered = await self._provider.render_definition(f.id, version)
208
+ if _canon(rendered or "") == want:
209
+ annotated.append(entry)
210
+ continue
211
+ reason = f"render-diverged: v{version} 渲染结果 ≠ 基线 rendered.yaml @{from_ref}"
212
+ except Exception as e: # per-deployment failure blocks that deployment only
213
+ reason = f"admit-check error: {e}"
214
+ logger.warning(f" ⏸ {f.id}/{dep}: {reason} — 标记该部署不可更新")
215
+ annotated.append(entry | {"refused": reason})
216
+ if all("refused" in e for e in annotated):
217
+ ids = ", ".join(e["id"] for e in annotated)
218
+ return f"all-deployments-refused: {ids} 全部部署不可更新(基线 {from_ref})", annotated
219
+ return None, annotated
220
+
221
+ async def _load_deployments(self, fid: str) -> list[dict]:
222
+ """Non-terminated deployment records — mirrors plan-time recording."""
223
+ ds = await self._provider.list_deployments(fid)
224
+ return [{k: d.get(k) for k in ("id", "status", "definition_version")}
225
+ for d in ds if d.get("status") != "terminated"]
@@ -10,19 +10,21 @@ from faas_gitops.protocols.faas_function import FaaSFunctionProvider
10
10
 
11
11
 
12
12
  class DeployFunctions:
13
- """Deploy applied functions to all their active deployments.
13
+ """Deploy applied functions to their active deployments.
14
14
 
15
15
  For each applied function, deploys the pushed version to every
16
16
  non-terminated deployment. Skips deployments already at the target
17
- version (idempotency guard). Fire-and-forget: triggers deploy without
18
- waiting for readiness.
17
+ version (idempotency guard) and (fid, deployment_id) pairs rejected
18
+ by admit. Fire-and-forget: triggers deploy without waiting for
19
+ readiness.
19
20
  """
20
21
 
21
22
  def __init__(self, provider: FaaSFunctionProvider, settings: Settings) -> None:
22
23
  self._provider = provider
23
24
  self._settings = settings
24
25
 
25
- async def run(self, applied_versions: dict[str, str], repo_path: Path) -> DeployResult:
26
+ async def run(self, applied_versions: dict[str, str], repo_path: Path,
27
+ refused_deployments: set[tuple[str, str]] | None = None) -> DeployResult:
26
28
  result = DeployResult()
27
29
  for fid, version in applied_versions.items():
28
30
  try:
@@ -34,6 +36,10 @@ class DeployFunctions:
34
36
  continue
35
37
  for dep in active:
36
38
  deploy_id = dep["id"]
39
+ if (fid, deploy_id) in (refused_deployments or set()):
40
+ logger.warning(f" ⏸ {fid}/{deploy_id}: refused by admit — skipped")
41
+ result.skipped.append(f"{fid}/{deploy_id}")
42
+ continue
37
43
  current = str(dep.get("definition_version", ""))
38
44
  if current == str(version):
39
45
  logger.info(f" = {fid}/{deploy_id}: already at version {version}")
@@ -0,0 +1,63 @@
1
+ """Re-format template and function YAML files on a branch to the canonical FaaS yaml format."""
2
+ from __future__ import annotations
3
+
4
+ from pathlib import Path
5
+
6
+ import aiofiles
7
+ from loguru import logger
8
+ from tqdm.asyncio import tqdm_asyncio
9
+
10
+ from faas_gitops.config import Settings
11
+ from faas_gitops.infra.yaml_utils import dump_faas_yaml, load_yaml
12
+ from faas_gitops.layout import FUNCTION_DIR, FUNCTION_META, FUNCTION_BASE, FUNCTION_DEFINITION, TEMPLATE_DIR, TEMPLATE_PRIMARY
13
+ from faas_gitops.protocols.git_gateway import GitWriter
14
+
15
+ _TEMPLATE_YAML = (TEMPLATE_PRIMARY, "schema.yaml")
16
+ _FUNCTION_YAML = (FUNCTION_META, FUNCTION_BASE, FUNCTION_DEFINITION)
17
+
18
+
19
+ def _collect(root: Path, names: tuple[str, ...]) -> list[Path]:
20
+ if not root.is_dir():
21
+ return []
22
+ return sorted(p for p in root.rglob("*.yaml") if p.name in names)
23
+
24
+
25
+ async def _format_file(path: Path) -> bool:
26
+ try:
27
+ async with aiofiles.open(path) as f:
28
+ original = await f.read()
29
+ data = load_yaml(original)
30
+ if not data:
31
+ return False
32
+ formatted = dump_faas_yaml(data)
33
+ if formatted == original:
34
+ return False
35
+ async with aiofiles.open(path, "w") as f:
36
+ await f.write(formatted)
37
+ return True
38
+ except Exception as e:
39
+ logger.warning(f"format failed for {path}: {e}")
40
+ return False
41
+
42
+
43
+ def _in_scope(path: Path, settings: Settings) -> bool:
44
+ if path.parts[0] == TEMPLATE_DIR:
45
+ return not settings.scoped_templates or "/".join(path.parts[1:3]) in settings.scoped_templates
46
+ return not settings.scoped_functions or path.parts[1] in settings.scoped_functions
47
+
48
+
49
+ async def format_branch(git: GitWriter, repo_path: Path, *, branch: str, settings: Settings) -> int:
50
+ await git.prepare_branch(repo_path=repo_path, branch=branch, force=False)
51
+ logger.info(f"format: branch={branch}")
52
+
53
+ paths = [
54
+ p for p in _collect(repo_path / TEMPLATE_DIR, _TEMPLATE_YAML) + _collect(repo_path / FUNCTION_DIR, _FUNCTION_YAML)
55
+ if _in_scope(p.relative_to(repo_path), settings)
56
+ ]
57
+ changed = sum(await tqdm_asyncio.gather(
58
+ *[_format_file(p) for p in paths], desc=f"format [{branch}]", unit="file",
59
+ ))
60
+ await git.commit_and_push(
61
+ repo_path, ".", f"format[{changed}]", branch=branch, force=False, create_pr=False,
62
+ )
63
+ return changed
@@ -1,13 +1,15 @@
1
1
  """F03 plan: pure git, read-only diff between two refs → plan.json."""
2
2
  from __future__ import annotations
3
3
 
4
- from dataclasses import asdict, dataclass
4
+ import asyncio
5
+ from dataclasses import asdict, dataclass, field
5
6
  from datetime import datetime, timezone
6
7
  from pathlib import Path
7
8
 
8
9
  from loguru import logger
9
10
 
10
11
  from faas_gitops.config import Settings
12
+ from faas_gitops.protocols.faas_function import FaaSFunctionProvider
11
13
  from faas_gitops.protocols.git_gateway import GitWriter
12
14
  from faas_gitops.usecases.detect_function_drift import DetectFunctionDrift
13
15
  from faas_gitops.usecases.detect_template_drift import DetectTemplateDrift
@@ -18,6 +20,14 @@ class FunctionChange:
18
20
  id: str
19
21
  channel: str
20
22
  reason: str
23
+ deployments: list[dict] = field(default_factory=list) # non-terminated: {id, status, definition_version}
24
+
25
+ @classmethod
26
+ def from_dict(cls, data: dict) -> "FunctionChange":
27
+ return cls(
28
+ id=data["id"], channel=data["channel"], reason=data["reason"],
29
+ deployments=list(data.get("deployments", [])),
30
+ )
21
31
 
22
32
 
23
33
  @dataclass(frozen=True)
@@ -45,7 +55,7 @@ class Plan:
45
55
  return cls(
46
56
  kind=data["kind"],
47
57
  templates=list(data.get("templates", [])),
48
- functions=[FunctionChange(**f) for f in data.get("functions", [])],
58
+ functions=[FunctionChange.from_dict(f) for f in data.get("functions", [])],
49
59
  context=PlanContext(**data["context"]),
50
60
  )
51
61
 
@@ -72,9 +82,13 @@ class Plan:
72
82
  class PlanBuild:
73
83
  """Read-only plan of merge-able changes between two refs."""
74
84
 
75
- def __init__(self, git: GitWriter, settings: Settings) -> None:
85
+ def __init__(self, git: GitWriter, settings: Settings,
86
+ func_provider: FaaSFunctionProvider | None = None) -> None:
87
+ """``func_provider`` (optional) records each function's active deployment
88
+ ids in the plan; without it the plan stays pure-git."""
76
89
  self._git = git
77
90
  self._settings = settings
91
+ self._func = func_provider
78
92
 
79
93
  async def run(
80
94
  self, repo_path: Path, base: str = "main", ref: str = "sync/faas-functions", repo_slug: str = "",
@@ -96,7 +110,9 @@ class PlanBuild:
96
110
 
97
111
  # rendered-only drift ⇒ meta-only edits are not drift; every planned
98
112
  # function is definition-channel by construction
99
- functions = [FunctionChange(fid, "definition", "modified") for fid in fn.drifted]
113
+ active = await self._active_deployments(fn.drifted)
114
+ functions = [FunctionChange(fid, "definition", "modified", active.get(fid, []))
115
+ for fid in fn.drifted]
100
116
  templates = tpl.drifted
101
117
  if templates and functions:
102
118
  kind = "mixed"
@@ -112,4 +128,20 @@ class PlanBuild:
112
128
  to_sha=await self._git.short_sha(repo_path, base),
113
129
  captured_at=f"{datetime.now(timezone.utc):%Y-%m-%dT%H:%M:%SZ}",
114
130
  )
115
- return Plan(kind, templates, functions, context)
131
+ return Plan(kind, templates, functions, context)
132
+
133
+ async def _active_deployments(self, fids: list[str]) -> dict[str, list[dict]]:
134
+ """Non-terminated deployment records ({id, status, definition_version}) per
135
+ drifted function (recorded in the plan so downstream steps know which
136
+ deployments a change touches and what they run)."""
137
+ if self._func is None or not fids:
138
+ return {}
139
+
140
+ async def _one(fid: str) -> tuple[str, list[dict]]:
141
+ ds = await self._func.list_deployments(fid)
142
+ return fid, [
143
+ {k: d.get(k) for k in ("id", "status", "definition_version")}
144
+ for d in ds if d.get("status") != "terminated"
145
+ ]
146
+
147
+ return dict(await asyncio.gather(*[_one(f) for f in fids]))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: faas-gitops
3
- Version: 0.4.1
3
+ Version: 0.6.1
4
4
  Summary: Declarative GitOps for FaaS
5
5
  Requires-Python: >=3.12
6
6
  Requires-Dist: aiofiles>=23.0
@@ -26,6 +26,7 @@ src/faas_gitops/protocols/faas_function.py
26
26
  src/faas_gitops/protocols/faas_template.py
27
27
  src/faas_gitops/protocols/git_gateway.py
28
28
  src/faas_gitops/usecases/__init__.py
29
+ src/faas_gitops/usecases/admit_check.py
29
30
  src/faas_gitops/usecases/apply_function_changes.py
30
31
  src/faas_gitops/usecases/apply_plan.py
31
32
  src/faas_gitops/usecases/apply_template_changes.py
@@ -33,17 +34,20 @@ src/faas_gitops/usecases/deploy_functions.py
33
34
  src/faas_gitops/usecases/detect_drift.py
34
35
  src/faas_gitops/usecases/detect_function_drift.py
35
36
  src/faas_gitops/usecases/detect_template_drift.py
37
+ src/faas_gitops/usecases/format.py
36
38
  src/faas_gitops/usecases/plan.py
37
39
  src/faas_gitops/usecases/render_definitions.py
38
40
  src/faas_gitops/usecases/snapshot.py
39
41
  src/faas_gitops/usecases/sync_functions.py
40
42
  src/faas_gitops/usecases/sync_templates.py
43
+ tests/test_admit_check.py
41
44
  tests/test_apply_function_changes.py
42
45
  tests/test_apply_plan.py
43
46
  tests/test_apply_template_changes.py
44
47
  tests/test_deploy_functions.py
45
48
  tests/test_detect_drift.py
46
49
  tests/test_detect_function_drift.py
50
+ tests/test_format.py
47
51
  tests/test_git_cli_gateway.py
48
52
  tests/test_plan.py
49
53
  tests/test_render_definitions.py
@@ -51,4 +55,5 @@ tests/test_result_contract.py
51
55
  tests/test_scope_args.py
52
56
  tests/test_snapshot.py
53
57
  tests/test_sync_functions.py
54
- tests/test_sync_templates.py
58
+ tests/test_sync_templates.py
59
+ tests/test_version.py