-
Notifications
You must be signed in to change notification settings - Fork 24
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
Update snapcraft go version based on changes to upstream's .go-version
#1580
Conversation
def diff(**subprocess_kwargs): | ||
"""Diff git repo""" | ||
cmd = ["git", "diff", "--name-only"] | ||
output = run(cmd, capture_output=True, text=True, **subprocess_kwargs) | ||
return [line for line in output.stdout.splitlines()] | ||
|
||
|
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.
Determines which files have changed in a given repo
cilib/models/repos/__init__.py
Outdated
def cat(self, branch, path): | ||
"""Cat file from a git repo""" | ||
parsed = urlparse(self.repo) | ||
if "git.launchpad.net" in parsed.netloc: | ||
""" | ||
Launchpad supports viewing files directly from the web interface | ||
for example) | ||
repo: git+ssh://k8s-team-ci@git.launchpad.net/snap-kube-apiserver | ||
branch: v1.28.13 | ||
file: /snapcraft.yaml | ||
|
||
becomes | ||
https://k8s-team-ci@git.launchpad.net/snap-kubectl/plain/snapcraft.yaml?h=v1.28.13 | ||
""" | ||
full_path = Path("/") / path | ||
url = f"https://{parsed.netloc}{parsed.path}/plain{full_path}?h={branch}" | ||
elif "github.com" in parsed.netloc: | ||
""" | ||
Github supports viewing files directly from the web interface | ||
https://raw.githubusercontent.com/kubernetes/kubernetes/refs/tags/v1.31.0/.go-version | ||
""" | ||
full_path = Path("/") / path | ||
url = f"https://raw.githubusercontent.com{parsed.path}/refs/tags/{branch}{full_path}" | ||
else: | ||
raise NotImplementedError("Only launchpad.net and github.com supported") | ||
|
||
response = requests.get(url) | ||
if response.status_code == 200: | ||
return response.text | ||
elif response.status_code == 404: | ||
raise FileNotFoundError(f"File not found: {url}") | ||
else: | ||
raise Exception(f"Failed to fetch {url}: {response.status_code}") | ||
|
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.
Implements a way to get the contents of a file hosted on git without cloning the whole repo. Fortunately public launchpad and github projects support raw file content download like this
@@ -14,6 +14,7 @@ def __init__(self): | |||
self.name = "k8s-internal-mirror" | |||
self.git_user = "k8s-team-ci" | |||
self.repo = f"git+ssh://{self.git_user}@git.launchpad.net/{self.name}" | |||
self.source = UpstreamKubernetesRepoModel() |
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.
Since the k8s-internal-mirror on launchpad is private, it's not possible to cat
its files. We'll instead need to use the Upstream K8s model from github.
def template_snapcraft_yml( | ||
self, | ||
src_path: Path, | ||
branch: str, | ||
k8s_version: semver.Version, | ||
commit_msg: str = "Creating branch", | ||
): | ||
snapcraft_fn = src_path / "snapcraft.yaml" | ||
snapcraft_fn_tpl = src_path / "snapcraft.yaml.in" | ||
snapcraft_yml_context = { | ||
"snap_version": branch.lstrip("v"), | ||
"patches": [], | ||
"go_version": self.go_version(k8s_version), | ||
} | ||
|
||
if k8s_version.compare("1.27.0-alpha.0") >= 0: | ||
snapcraft_yml_context["base"] = "core20" | ||
elif k8s_version.compare("1.19.0") >= 0: | ||
snapcraft_yml_context["base"] = "core18" | ||
|
||
self.log(f"Writing template vars {snapcraft_yml_context}") | ||
snapcraft_yml = snapcraft_fn_tpl.read_text() | ||
snapcraft_yml = self.render(snapcraft_fn_tpl, snapcraft_yml_context) | ||
snapcraft_fn.write_text(snapcraft_yml) | ||
|
||
if self.snap_model.base.diff(cwd=str(src_path)): | ||
self.log(f"Committing {branch}") | ||
self.snap_model.base.add([str(snapcraft_fn)], cwd=str(src_path)) | ||
self.snap_model.base.commit(f"{commit_msg} {branch}", cwd=str(src_path)) | ||
self.snap_model.base.push(ref=branch, cwd=str(src_path)) |
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.
Use the snap template in the repo, to craft a new snapcraft.yaml for the branch
def go_version(self, k8s_version: semver.Version): | ||
"""Determines the go version to use for a given k8s version""" | ||
k8s_major_minor = f"{k8s_version.major}.{k8s_version.minor}" | ||
default = enums.K8S_GO_MAP[k8s_major_minor] | ||
try: | ||
go_ver = self.upstream_model.source.cat(f"v{k8s_version}", "/.go-version") | ||
except FileNotFoundError: | ||
return default | ||
go_parsed = semver.VersionInfo.parse(go_ver) | ||
return f"go/{go_parsed.major}.{go_parsed.minor}/stable" |
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.
Try to use the upstream kubernetes repo's version of .go-version
file to determine which go snap to use while building the snap
if go_version not in content: | ||
self.log( | ||
f"Go version mismatch for {branch}, updating snapcraft.yaml" | ||
) | ||
with tempfile.TemporaryDirectory() as tmpdir: | ||
branch = f"v{latest_branch_version}" | ||
src_path = Path(tmpdir) / self.snap_model.src | ||
self.snap_model.base.clone(cwd=tmpdir) | ||
self.snap_model.base.checkout(branch, cwd=str(src_path)) | ||
self.template_snapcraft_yml( | ||
src_path, branch, branch_ver, commit_msg="Update Snapcraft" | ||
) | ||
self.log("Go version changed to {go_version}, building new snap") | ||
self._create_recipe(_version, branch) | ||
continue |
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.
If the go version changes, push the change to launchpad and run the recipe to rebuild the snap.
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.
Looks good to me, minor typo
cilib/models/repos/__init__.py
Outdated
if "git.launchpad.net" in parsed.netloc: | ||
""" | ||
Launchpad supports viewing files directly from the web interface | ||
for example) |
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.
nit: should be :
instead of )
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.
resolved
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.
LGTM
cilib/models/repos/__init__.py
Outdated
if "git.launchpad.net" in parsed.netloc: | ||
""" | ||
Launchpad supports viewing files directly from the web interface | ||
for example) |
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.
resolved
@bschimke95 thanks so much dude |
Overview
Build snaps according to a rolling upstream go-version
Details
Upstream https://github.com/kubernetes/kubernetes began to embed the required go-version for a specific branch or tag using the
.go-version
file. It's recommended to use this version of go to build the snaps.Charmed Kubernetes has been keeping an internal map which can get stale -- rather than change it -- we can continue to use it if the supported branch doesn't contain the .go-version file.