-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--------- Co-authored-by: Adam Dyess <[email protected]>
- Loading branch information
1 parent
02f369b
commit 6ce90fa
Showing
7 changed files
with
188 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,93 @@ | ||
# | ||
# Copyright 2024 Canonical, Ltd. | ||
# | ||
import json | ||
import logging | ||
import re | ||
import urllib.error | ||
import urllib.request | ||
from typing import List | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
SNAP_NAME = "k8s" | ||
|
||
# For Snap Store API request | ||
SNAPSTORE_INFO_API = "https://api.snapcraft.io/v2/snaps/info/" | ||
SNAPSTORE_HEADERS = { | ||
"Snap-Device-Series": "16", | ||
"User-Agent": "Mozilla/5.0", | ||
} | ||
RISK_LEVELS = ["stable", "candidate", "beta", "edge"] | ||
|
||
|
||
def get_snap_info(snap_name=SNAP_NAME): | ||
"""Get the snap info from the Snap Store API.""" | ||
req = urllib.request.Request( | ||
SNAPSTORE_INFO_API + snap_name, headers=SNAPSTORE_HEADERS | ||
) | ||
try: | ||
with urllib.request.urlopen(req) as response: # nosec | ||
return json.loads(response.read().decode()) | ||
except urllib.error.HTTPError as e: | ||
LOG.exception("HTTPError ({%s}): {%s} {%s}", req.full_url, e.code, e.reason) | ||
raise | ||
except urllib.error.URLError as e: | ||
LOG.exception("URLError ({%s}): {%s}", req.full_url, e.reason) | ||
raise | ||
|
||
|
||
def get_latest_channels( | ||
num_of_channels: int, flavor: str, arch: str, include_latest=True | ||
) -> List[str]: | ||
"""Get an ascending list of latest channels based on the number of channels and flavour. | ||
e.g. get_latest_release_channels(3, "classic") -> ['1.31-classic/candidate', '1.30-classic/candidate'] | ||
if there are less than num_of_channels available, return all available channels. | ||
Only the most stable risk level is returned for each major.minor version. | ||
By default, the `latest/edge/<flavor>` channel is included in the list. | ||
""" | ||
snap_info = get_snap_info() | ||
|
||
# Extract channel information | ||
channels = snap_info.get("channel-map", []) | ||
available_channels = [ | ||
ch["channel"]["name"] | ||
for ch in channels | ||
if ch["channel"]["architecture"] == arch | ||
] | ||
|
||
# Define regex pattern to match channels in the format 'major.minor-flavour' | ||
if flavor == "strict": | ||
pattern = re.compile(r"(\d+)\.(\d+)\/(" + "|".join(RISK_LEVELS) + ")") | ||
else: | ||
pattern = re.compile( | ||
r"(\d+)\.(\d+)-" + re.escape(flavor) + r"\/(" + "|".join(RISK_LEVELS) + ")" | ||
) | ||
|
||
# Dictionary to store the highest risk level for each major.minor | ||
channel_map = {} | ||
|
||
for channel in available_channels: | ||
match = pattern.match(channel) | ||
if match: | ||
major, minor, risk = match.groups() | ||
major_minor = (int(major), int(minor)) | ||
|
||
# Store only the highest risk level channel for each major.minor | ||
if major_minor not in channel_map or RISK_LEVELS.index( | ||
risk | ||
) < RISK_LEVELS.index(channel_map[major_minor][1]): | ||
channel_map[major_minor] = (channel, risk) | ||
|
||
# Sort channels by major and minor version in descending order | ||
sorted_channels = sorted(channel_map.keys(), reverse=False) | ||
|
||
# Prepare final channel list | ||
final_channels = [channel_map[mm][0] for mm in sorted_channels[:num_of_channels]] | ||
|
||
if include_latest: | ||
latest_channel = f"latest/edge/{flavor}" | ||
final_channels.append(latest_channel) | ||
|
||
return final_channels |
This file contains 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,54 @@ | ||
# | ||
# Copyright 2024 Canonical, Ltd. | ||
# | ||
import logging | ||
from typing import List | ||
|
||
import pytest | ||
from test_util import config, harness, snap, util | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
@pytest.mark.node_count(1) | ||
@pytest.mark.no_setup() | ||
@pytest.mark.xfail("cilium failures are blocking this from working") | ||
@pytest.mark.skipif( | ||
not config.VERSION_UPGRADE_CHANNELS, reason="No upgrade channels configured" | ||
) | ||
def test_version_upgrades(instances: List[harness.Instance]): | ||
channels = config.VERSION_UPGRADE_CHANNELS | ||
cp = instances[0] | ||
|
||
if channels[0].lower() == "recent": | ||
if len(channels) != 3: | ||
pytest.fail( | ||
"'recent' requires the number of releases as second argument and the flavour as third argument" | ||
) | ||
_, num_channels, flavour = channels | ||
arch = cp.exec( | ||
["dpkg", "--print-architecture"], text=True, capture_output=True | ||
).stdout.strip() | ||
channels = snap.get_latest_channels(int(num_channels), flavour, arch) | ||
|
||
LOG.info( | ||
f"Bootstrap node on {channels[0]} and upgrade through channels: {channels[1:]}" | ||
) | ||
|
||
# Setup the k8s snap from the bootstrap channel and setup basic configuration. | ||
cp.exec(["snap", "install", "k8s", "--channel", channels[0], "--classic"]) | ||
cp.exec(["k8s", "bootstrap"]) | ||
|
||
util.stubbornly(retries=30, delay_s=20).until(util.ready_nodes(cp) == 1) | ||
|
||
current_channel = channels[0] | ||
for channel in channels[1:]: | ||
LOG.info(f"Upgrading {cp.id} from {current_channel} to channel {channel}") | ||
# Log the current snap version on the node. | ||
cp.exec(["snap", "info", "k8s"]) | ||
|
||
# note: the `--classic` flag will be ignored by snapd for strict snaps. | ||
cp.exec(["snap", "refresh", "k8s", "--channel", channel, "--classic"]) | ||
|
||
util.stubbornly(retries=30, delay_s=20).until(util.ready_nodes(cp) == 1) | ||
LOG.info(f"Upgraded {cp.id} to channel {channel}") |