Skip to content

Commit 1eb1e4e

Browse files
committed
Add the capability to the latest GCE images from GCE projects
Add a function to get the latest GCE image based on the project name, the family and the architecture. Signed-off-by: Arnaud Meukam <[email protected]>
1 parent fe86e64 commit 1eb1e4e

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

config/jobs/kubernetes/kops/build_jobs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
skip_jobs,
4444
image,
4545
networking_options,
46+
gce_distro_options,
4647
distro_options,
4748
k8s_versions,
4849
kops_versions,
@@ -485,7 +486,7 @@ def generate_grid():
485486
# TODO(justinsb): merge into above block when we can
486487
# pylint: disable=too-many-nested-blocks
487488
for networking in ['kubenet', 'calico', 'cilium', 'gce']: # TODO: all networking_options:
488-
for distro in ['u2204']: # TODO: all distro_options:
489+
for distro in gce_distro_options: # TODO: all distro_options:
489490
for k8s_version in k8s_versions:
490491
for kops_version in [None]: # TODO: all kops_versions:
491492
results.append(

config/jobs/kubernetes/kops/build_vars.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
"kopeio",
3030
]
3131

32+
# GCE distributions
33+
gce_distro_options = [
34+
"u2204",
35+
]
36+
3237
# AWS distributions
3338
distro_options = [
3439
"al2023",

config/jobs/kubernetes/kops/helpers.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,51 @@ def latest_aws_image(owner, name, arch='x86_64'):
174174
set_pinned(pin, image)
175175
return image
176176

177+
# latest_gce_image returns the latest GCE image for the given project, family, and arch
178+
# If the image is pinned, it returns the pinned image
179+
# Otherwise, it fetches the latest image from the specified family
180+
def latest_gce_image(project, family, arch="X86_64"):
181+
pin = f"gce://images/{project}/{family}:{arch}"
182+
image = get_pinned(pin)
183+
if image:
184+
return image
185+
186+
import googleapiclient.discovery # pylint: disable=import-error, import-outside-toplevel
187+
188+
compute = googleapiclient.discovery.build("compute", "v1", cache_discovery=False)
189+
try:
190+
# Get the latest image from the specified family
191+
# pylint: disable=no-member
192+
image_response = (
193+
compute.images().getFromFamily(project=project, family=family).execute()
194+
)
195+
# pylint: enable=no-member
196+
except Exception as e:
197+
raise RuntimeError(
198+
f"Failed to get image from family {family} in project {project}: {str(e)}"
199+
)
200+
201+
# Verify architecture matches
202+
if "architecture" not in image_response:
203+
raise RuntimeError(
204+
f"Image {image_response['name']} has no architecture specified"
205+
)
206+
207+
if arch not in image_response["architecture"]:
208+
raise RuntimeError(
209+
f"Image family {family} has architecture {image_response['architecture']} "
210+
f"which doesn't match requested {arch}"
211+
)
212+
213+
image_name = image_response["name"]
214+
set_pinned(pin, image_name)
215+
return f"{project}/{image_name}"
216+
217+
# Get latest images from some public images families
218+
gce_distro_images = {
219+
"u2204": latest_gce_image("ubuntu-os-cloud", "ubuntu-2204-lts"),
220+
}
221+
177222
distro_images = {
178223
'al2023': latest_aws_image('137112412989', 'al2023-ami-2*-kernel-6.1-x86_64'),
179224
'amzn2': latest_aws_image('137112412989', 'amzn2-ami-kernel-5.10-hvm-*-x86_64-gp2'),

config/jobs/kubernetes/kops/pinned.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
gce://images/ubuntu-os-cloud/ubuntu-2204-lts:X86_64=ubuntu-2204-jammy-v20250826
12
aws://images/137112412989/al2023-ami-2*-kernel-6.1-x86_64:x86_64=137112412989/al2023-ami-2023.8.20250818.0-kernel-6.1-x86_64
23
aws://images/137112412989/amzn2-ami-kernel-5.10-hvm-*-x86_64-gp2:x86_64=137112412989/amzn2-ami-kernel-5.10-hvm-2.0.20250818.2-x86_64-gp2
34
aws://images/136693071363/debian-11-amd64-*:x86_64=136693071363/debian-11-amd64-20250801-2191

0 commit comments

Comments
 (0)