|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +""" |
| 19 | +Keep the Go toolchain version in sync across every file that pins it. |
| 20 | +
|
| 21 | +``golang.org/x/net`` (and other ``golang.org/x`` modules) periodically raise |
| 22 | +their minimum Go version. When that happens a dependency bump changes the |
| 23 | +``go`` directive in ``go-sdk/go.mod`` but leaves the *other* places that pin |
| 24 | +the toolchain untouched — and CI then fails deep inside static checks and the |
| 25 | +Go SDK e2e bundle build with ``go.mod requires go >= <new>`` (this is exactly |
| 26 | +what happened in PR #69214). Those pins have no cross-file include, so nothing |
| 27 | +catches the drift until CI does. |
| 28 | +
|
| 29 | +The single source of truth is the ``go`` directive in ``go-sdk/go.mod``. Every |
| 30 | +other site below must agree with it at ``major.minor`` granularity (the minor |
| 31 | +version is what selects the toolchain and the builder image; the go.mod patch |
| 32 | +suffix is not meaningful for those): |
| 33 | +
|
| 34 | +- ``go-sdk/go.mod`` -> ``go <maj.min[.patch]>`` (SOURCE OF TRUTH) |
| 35 | +- ``kubernetes-tests/lang_sdk/go_example/go.mod`` -> ``go <maj.min[.patch]>`` (resolves the SDK via ``replace``) |
| 36 | +- ``.github/workflows/ci-amd.yml`` -> Setup Go ``go-version: <maj.min>`` |
| 37 | +- ``.github/workflows/ci-arm.yml`` -> Setup Go ``go-version: <maj.min>`` |
| 38 | +- ``airflow-e2e-tests/tests/airflow_e2e_tests/constants.py`` -> ``GO_BUILDER_IMAGE`` default ``golang:<maj.min>-alpine`` |
| 39 | +- ``dev/breeze/src/airflow_breeze/commands/kubernetes_commands.py`` -> ``LANG_SDK_GO_BUILDER_IMAGE`` default ``golang:<maj.min>-alpine`` |
| 40 | +- ``go-sdk/.pre-commit-config.yaml`` -> ``default_language_version.golang`` (the toolchain prek uses for the SDK's ``language: golang`` hooks — this is the pin that broke static checks in #69214, not the setup-go one) |
| 41 | +- ``.pre-commit-config.yaml`` -> ``default_language_version.golang`` (top-level default for any ``language: golang`` hook) |
| 42 | +
|
| 43 | +When they disagree, bump the drifting sites to the source-of-truth minor |
| 44 | +version. If you add a new place that pins the Go version, register it here too. |
| 45 | +
|
| 46 | +Run from the repo root: |
| 47 | +
|
| 48 | + uv run --project scripts python scripts/ci/prek/check_go_version_in_sync.py |
| 49 | +
|
| 50 | +Exits 0 if every site agrees with the source of truth, 1 otherwise. |
| 51 | +""" |
| 52 | + |
| 53 | +from __future__ import annotations |
| 54 | + |
| 55 | +import dataclasses |
| 56 | +import pathlib |
| 57 | +import re |
| 58 | +import sys |
| 59 | + |
| 60 | +REPO_ROOT = pathlib.Path(__file__).resolve().parents[3] |
| 61 | + |
| 62 | +# The ``go`` directive: ``go 1.25`` or ``go 1.25.0``. |
| 63 | +GO_MOD_DIRECTIVE = re.compile(r"^go\s+(\d+\.\d+(?:\.\d+)?)\s*$", re.MULTILINE) |
| 64 | +# Setup Go step: ``go-version: 1.25`` (optionally quoted). |
| 65 | +WORKFLOW_GO_VERSION = re.compile(r"^\s*go-version:\s*[\"']?(\d+\.\d+(?:\.\d+)?)[\"']?\s*$", re.MULTILINE) |
| 66 | +# Builder image default: ``golang:1.25-alpine``. |
| 67 | +GOLANG_BUILDER_IMAGE = re.compile(r"golang:(\d+\.\d+(?:\.\d+)?)-alpine") |
| 68 | +# prek ``default_language_version`` entry: `` golang: 1.25.0``. |
| 69 | +PREK_GOLANG_VERSION = re.compile(r"^\s*golang:\s*(\d+\.\d+(?:\.\d+)?)\s*$", re.MULTILINE) |
| 70 | + |
| 71 | + |
| 72 | +@dataclasses.dataclass |
| 73 | +class VersionSite: |
| 74 | + """A single location that pins the Go toolchain version.""" |
| 75 | + |
| 76 | + label: str |
| 77 | + path: pathlib.Path |
| 78 | + pattern: re.Pattern[str] |
| 79 | + is_source_of_truth: bool = False |
| 80 | + |
| 81 | + def extract(self) -> str | None: |
| 82 | + """Return the captured version string, or ``None`` if the pattern is missing.""" |
| 83 | + if not self.path.exists(): |
| 84 | + return None |
| 85 | + if m := self.pattern.search(self.path.read_text()): |
| 86 | + return m.group(1) |
| 87 | + return None |
| 88 | + |
| 89 | + |
| 90 | +def major_minor(version: str) -> str: |
| 91 | + """Reduce ``1.25`` / ``1.25.0`` to its ``major.minor`` (``1.25``).""" |
| 92 | + parts = version.split(".") |
| 93 | + return ".".join(parts[:2]) |
| 94 | + |
| 95 | + |
| 96 | +def build_sites(repo_root: pathlib.Path) -> list[VersionSite]: |
| 97 | + """Build the list of Go-version pin sites, resolved against ``repo_root``.""" |
| 98 | + workflows = repo_root / ".github" / "workflows" |
| 99 | + return [ |
| 100 | + VersionSite( |
| 101 | + label="go-sdk/go.mod (go directive)", |
| 102 | + path=repo_root / "go-sdk" / "go.mod", |
| 103 | + pattern=GO_MOD_DIRECTIVE, |
| 104 | + is_source_of_truth=True, |
| 105 | + ), |
| 106 | + VersionSite( |
| 107 | + label="kubernetes-tests/lang_sdk/go_example/go.mod (go directive)", |
| 108 | + path=repo_root / "kubernetes-tests" / "lang_sdk" / "go_example" / "go.mod", |
| 109 | + pattern=GO_MOD_DIRECTIVE, |
| 110 | + ), |
| 111 | + VersionSite( |
| 112 | + label=".github/workflows/ci-amd.yml (Setup Go go-version)", |
| 113 | + path=workflows / "ci-amd.yml", |
| 114 | + pattern=WORKFLOW_GO_VERSION, |
| 115 | + ), |
| 116 | + VersionSite( |
| 117 | + label=".github/workflows/ci-arm.yml (Setup Go go-version)", |
| 118 | + path=workflows / "ci-arm.yml", |
| 119 | + pattern=WORKFLOW_GO_VERSION, |
| 120 | + ), |
| 121 | + VersionSite( |
| 122 | + label="airflow-e2e-tests/.../constants.py (GO_BUILDER_IMAGE)", |
| 123 | + path=repo_root / "airflow-e2e-tests" / "tests" / "airflow_e2e_tests" / "constants.py", |
| 124 | + pattern=GOLANG_BUILDER_IMAGE, |
| 125 | + ), |
| 126 | + VersionSite( |
| 127 | + label="dev/breeze/.../kubernetes_commands.py (LANG_SDK_GO_BUILDER_IMAGE)", |
| 128 | + path=repo_root |
| 129 | + / "dev" |
| 130 | + / "breeze" |
| 131 | + / "src" |
| 132 | + / "airflow_breeze" |
| 133 | + / "commands" |
| 134 | + / "kubernetes_commands.py", |
| 135 | + pattern=GOLANG_BUILDER_IMAGE, |
| 136 | + ), |
| 137 | + VersionSite( |
| 138 | + label="go-sdk/.pre-commit-config.yaml (default_language_version.golang)", |
| 139 | + path=repo_root / "go-sdk" / ".pre-commit-config.yaml", |
| 140 | + pattern=PREK_GOLANG_VERSION, |
| 141 | + ), |
| 142 | + VersionSite( |
| 143 | + label=".pre-commit-config.yaml (default_language_version.golang)", |
| 144 | + path=repo_root / ".pre-commit-config.yaml", |
| 145 | + pattern=PREK_GOLANG_VERSION, |
| 146 | + ), |
| 147 | + ] |
| 148 | + |
| 149 | + |
| 150 | +def check_sync(sites: list[VersionSite], repo_root: pathlib.Path) -> tuple[int, str]: |
| 151 | + """Compare every site against the source of truth. Returns ``(exit_code, report)``.""" |
| 152 | + results = [(site, site.extract()) for site in sites] |
| 153 | + |
| 154 | + if missing := [site for site, version in results if version is None]: |
| 155 | + lines = [f"ERROR: Go version pin not found in {site.path.relative_to(repo_root)}" for site in missing] |
| 156 | + lines += [f" (expected pattern {site.pattern.pattern!r})" for site in missing] |
| 157 | + return 1, "\n".join(lines) |
| 158 | + |
| 159 | + source = next(site for site, _ in results if site.is_source_of_truth) |
| 160 | + expected = major_minor(source.extract()) # type: ignore[arg-type] |
| 161 | + |
| 162 | + drifted = [(site, version) for site, version in results if major_minor(version) != expected] # type: ignore[arg-type] |
| 163 | + if not drifted: |
| 164 | + return 0, f"OK: Go version is consistently {expected} across all {len(sites)} pin sites." |
| 165 | + |
| 166 | + col = max(len(site.label) for site, _ in results) |
| 167 | + lines = [ |
| 168 | + f"ERROR: Go version drifted from the source of truth ({source.label} = {expected}).", |
| 169 | + "", |
| 170 | + f" {'PIN SITE':<{col}} VERSION", |
| 171 | + ] |
| 172 | + for site, version in results: |
| 173 | + marker = ( |
| 174 | + " <- source of truth" |
| 175 | + if site.is_source_of_truth |
| 176 | + else ( |
| 177 | + " <- DRIFT" if major_minor(version) != expected else "" # type: ignore[arg-type] |
| 178 | + ) |
| 179 | + ) |
| 180 | + lines.append(f" {site.label:<{col}} {version}{marker}") |
| 181 | + lines += [ |
| 182 | + "", |
| 183 | + f"Bump the drifting site(s) to {expected} to match {source.label},", |
| 184 | + "or, if you intentionally changed the source of truth, update the other pins to match.", |
| 185 | + "After changing a go.mod, run its `go mod tidy` so go.sum stays consistent.", |
| 186 | + ] |
| 187 | + return 1, "\n".join(lines) |
| 188 | + |
| 189 | + |
| 190 | +def main() -> int: |
| 191 | + exit_code, report = check_sync(build_sites(REPO_ROOT), REPO_ROOT) |
| 192 | + print(report) |
| 193 | + return exit_code |
| 194 | + |
| 195 | + |
| 196 | +if __name__ == "__main__": |
| 197 | + sys.exit(main()) |
0 commit comments