Skip to content
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

Merged
merged 2 commits into from
Oct 10, 2024

Conversation

addyess
Copy link
Member

@addyess addyess commented Oct 9, 2024

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.

  • the sync-snaps job will now confirm the correct go-version in the snapcraft yaml is used.

Comment on lines +72 to +78
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()]


Copy link
Member Author

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

Comment on lines 22 to 55
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}")

Copy link
Member Author

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()
Copy link
Member Author

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.

Comment on lines +44 to +73
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))
Copy link
Member Author

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

Comment on lines +75 to +84
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"
Copy link
Member Author

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

Comment on lines +185 to +199
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
Copy link
Member Author

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.

Copy link
Contributor

@bschimke95 bschimke95 left a 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

if "git.launchpad.net" in parsed.netloc:
"""
Launchpad supports viewing files directly from the web interface
for example)
Copy link
Contributor

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 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

Copy link
Contributor

@bschimke95 bschimke95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if "git.launchpad.net" in parsed.netloc:
"""
Launchpad supports viewing files directly from the web interface
for example)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolved

@bschimke95 bschimke95 merged commit f7addf0 into main Oct 10, 2024
6 checks passed
@bschimke95 bschimke95 deleted the akd/k8s-snap-builder/update-go-version branch October 10, 2024 17:20
@addyess
Copy link
Member Author

addyess commented Oct 10, 2024

@bschimke95 thanks so much dude

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants