Skip to content

Commit

Permalink
Fix the selection of latest node image available
Browse files Browse the repository at this point in the history
  • Loading branch information
robo-cap authored and hyder committed Feb 3, 2025
1 parent ffd9b1d commit d2ab5bf
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
19 changes: 10 additions & 9 deletions data-images.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ locals {
node_pool_images = try(one(data.oci_containerengine_node_pool_option.oke[*].sources), [])

# Parse platform/operating system information from node pool image names
parsed_images = try({
indexed_images = try({
for k, v in local.node_pool_images : v.image_id => merge(
try(element(regexall("OKE-(?P<k8s_version>[0-9\\.]+)-(?P<build>[0-9]+)", v.source_name), 0), { k8s_version = "none" }),
{
Expand All @@ -25,25 +25,26 @@ locals {
is_gpu = length(regexall("GPU", v.source_name)) > 0
os = trimspace(replace(element(regexall("^[a-zA-Z-]+", v.source_name), 0), "-", " "))
os_version = element(regexall("[0-9\\.]+", v.source_name), 0)
sort_key = replace(try(join(".", regex("-([0-9]{4}\\.[01][0-9].[0-9]{1,2}).*?-([0-9]+)$", v.source_name)), v.source_name), ".", "")
source_name = v.source_name
},
)
}, {})

# Create non-exclusive groupings of image IDs for intersection when selecting based on config and instance shape
image_ids = try(merge({
x86_64 = [for k, v in local.parsed_images : k if v.arch == "x86_64"]
aarch64 = [for k, v in local.parsed_images : k if v.arch == "aarch64"]
oke = [for k, v in local.parsed_images : k if v.image_type == "oke" && contains(local.k8s_versions_only, v.k8s_version)]
platform = [for k, v in local.parsed_images : k if v.image_type == "platform"]
gpu = [for k, v in local.parsed_images : k if v.is_gpu]
nongpu = [for k, v in local.parsed_images : k if !v.is_gpu]
x86_64 = [for k, v in local.indexed_images : k if v.arch == "x86_64"]
aarch64 = [for k, v in local.indexed_images : k if v.arch == "aarch64"]
oke = [for k, v in local.indexed_images : k if v.image_type == "oke" && contains(local.k8s_versions_only, v.k8s_version)]
platform = [for k, v in local.indexed_images : k if v.image_type == "platform"]
gpu = [for k, v in local.indexed_images : k if v.is_gpu]
nongpu = [for k, v in local.indexed_images : k if !v.is_gpu]
}, {
# Include groups for OS name and major version
# https://developer.hashicorp.com/terraform/language/expressions/for#grouping-results
for k, v in local.parsed_images : format("%v %v", v.os, split(".", v.os_version)[0]) => k...
for k, v in local.indexed_images : format("%v %v", v.os, split(".", v.os_version)[0]) => k...
}, {
# Include groups for referenced Kubernetes versions
for k, v in local.parsed_images : format("%v", v.k8s_version) => k... if contains(local.k8s_versions_only, v.k8s_version)
for k, v in local.indexed_images : format("%v", v.k8s_version) => k... if contains(local.k8s_versions_only, v.k8s_version)
}), {})
}
1 change: 1 addition & 0 deletions module-workers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module "workers" {
image_os = var.worker_image_os
image_os_version = var.worker_image_os_version
image_type = var.worker_image_type
indexed_images = local.indexed_images
kubeproxy_mode = var.kubeproxy_mode
max_pods_per_node = var.max_pods_per_node
node_labels = alltrue([var.cluster_type == "basic", var.cilium_install == true]) ? merge(var.worker_node_labels, {"oci.oraclecloud.com/custom-k8s-networking" = true}) : var.worker_node_labels
Expand Down
28 changes: 16 additions & 12 deletions modules/workers/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,22 @@ locals {
])

# Use provided image_id for 'custom' type, or first match for all shape + OS criteria
image_id = (pool.image_type == "custom" ? pool.image_id : element(tolist(setintersection([
pool.image_type == "oke" ?
setintersection(
lookup(var.image_ids, "oke", null),
lookup(var.image_ids, trimprefix(lower(pool.kubernetes_version), "v"), null)
) :
lookup(var.image_ids, "platform", null),
lookup(var.image_ids, pool.image_type, null),
length(regexall("GPU", pool.shape)) > 0 ? var.image_ids.gpu : var.image_ids.nongpu,
length(regexall("A1\\.", pool.shape)) > 0 ? var.image_ids.aarch64 : var.image_ids.x86_64,
lookup(var.image_ids, format("%v %v", pool.os, split(".", pool.os_version)[0]), null),
]...)), 0))
image_id = (
pool.image_type == "custom" ?
pool.image_id :
element(split("###", element(reverse(sort([for entry in tolist(setintersection([
pool.image_type == "oke" ?
setintersection(
lookup(var.image_ids, "oke", null),
lookup(var.image_ids, trimprefix(lower(pool.kubernetes_version), "v"), null)
) :
lookup(var.image_ids, "platform", null),
lookup(var.image_ids, pool.image_type, null),
length(regexall("GPU", pool.shape)) > 0 ? var.image_ids.gpu : var.image_ids.nongpu,
length(regexall("A1\\.", pool.shape)) > 0 ? var.image_ids.aarch64 : var.image_ids.x86_64,
lookup(var.image_ids, format("%v %v", pool.os, split(".", pool.os_version)[0]), null),
]...)): "${var.indexed_images[entry].sort_key}###${entry}"])), 0)), 1)
)

# Standard tags as defined if enabled for use
# User-provided freeform tags are merged and take precedence
Expand Down
6 changes: 6 additions & 0 deletions modules/workers/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ variable "image_ids" {
type = any
}

variable "indexed_images" {
default = {}
description = "Map of images."
type = any
}

variable "ssh_public_key" {
default = null
description = "The contents of the SSH public key file. Used to allow login for workers/bastion/operator with corresponding private key."
Expand Down

0 comments on commit d2ab5bf

Please sign in to comment.