Skip to content

Commit

Permalink
set kubernetes version as input from pipeline (#438)
Browse files Browse the repository at this point in the history
enable to set kubernetes version from pipeline input
  • Loading branch information
zqingqing1 authored Dec 17, 2024
1 parent ec80402 commit 96d7346
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
3 changes: 3 additions & 0 deletions modules/terraform/azure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ RUN_ID=$(date +%s)
CLOUD=azure
REGION=eastus2
SKU_TIER=Free
KUBERNETES_VERSION=1.31
NETWORK_POLICY=cilium
NETWORK_DATAPLANE=cilium
TERRAFORM_MODULES_DIR=modules/terraform/$CLOUD
Expand Down Expand Up @@ -62,6 +63,7 @@ Set `INPUT_JSON` variable. This variable is not exhaustive and may vary dependin
--arg run_id $RUN_ID \
--arg region $REGION \
--arg aks_sku_tier "$SKU_TIER" \
--arg aks_kubernetes_version "$KUBERNETES_VERSION" \
--arg aks_network_policy "$NETWORK_POLICY" \
--arg aks_network_dataplane "$NETWORK_DATAPLANE" \
--argjson aks_cli_system_node_pool "$SYSTEM_NODE_POOL" \
Expand All @@ -70,6 +72,7 @@ Set `INPUT_JSON` variable. This variable is not exhaustive and may vary dependin
run_id: $run_id,
region: $region,
aks_sku_tier: $aks_sku_tier,
aks_kubernetes_version: $aks_kubernetes_version,
aks_network_policy: $aks_network_policy,
aks_network_dataplane: $aks_network_dataplane,
aks_cli_system_node_pool: $aks_cli_system_node_pool,
Expand Down
16 changes: 13 additions & 3 deletions modules/terraform/azure/aks-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ configuration, like `--aks-custom-headers" flags. It also supports to use aks-pr
- `role`: Role of the AKS cluster
- `aks_name`: Name of the AKS cluster
- `sku_tier`: The type of pricing tiers
- `kubernetes_version`: The kubernetes version of the AKS cluster
- `aks_custom_headers`: Custom headers for AKS.
- `use_aks_preview_cli_extension`: using aks-preview cli extension
- `default_node_pool`: Configuration for the default node pool
Expand Down Expand Up @@ -59,6 +60,7 @@ module "aks" {
role = "dev"
aks_name = "my-aks-cluster"
sku_tier = "standard"
kubernetes_version = "1.31"
use_aks_preview_cli_extension = true
Expand Down Expand Up @@ -101,18 +103,26 @@ SCENARIO_NAME=apiserver-vn100pod10k
RUN_ID=$(date +%s)
CLOUD=azure
REGION=eastus
KUBERNETES_VERSION=1.31
TERRAFORM_MODULES_DIR=modules/terraform/$CLOUD
TERRAFORM_INPUT_FILE=$(pwd)/scenarios/$SCENARIO_TYPE/$SCENARIO_NAME/terraform-inputs/${CLOUD}.tfvars
SYSTEM_NODE_POOL="{\"name\":\"default\",\"vm_size\":\"Standard_D2s_v3\",\"node_count\":1,\"vm_set_type\":\"VirtualMachineScaleSets\"}"
USER_NODE_POOL="[{\"name\":\"pool1\",\"vm_size\":\"Standard_D2s_v3\",\"node_count\":1,\"vm_set_type\":\"VirtualMachineScaleSets\"},{\"name\":\"pool2\",\"vm_size\":\"Standard_D2s_v3\",\"node_count\":1,\"vm_set_type\":\"VirtualMachineScaleSets\"}]"
SYSTEM_NODE_POOL="{\"name\":\"default\",\"vm_size\":\"Standard_D2_v3\",\"node_count\":1,\"vm_set_type\":\"VirtualMachineScaleSets\"}"
USER_NODE_POOL="[{\"name\":\"pool1\",\"vm_size\":\"Standard_D2_v3\",\"node_count\":1,\"vm_set_type\":\"VirtualMachineScaleSets\"},{\"name\":\"pool2\",\"vm_size\":\"Standard_D2_v3\",\"node_count\":1,\"vm_set_type\":\"VirtualMachineScaleSets\"}]"
```
4. Run following command to set `INPUT_JSON` variable
```bash
INPUT_JSON=$(jq -n \
--arg run_id $RUN_ID \
--arg region $REGION \
--arg aks_kubernetes_version "$KUBERNETES_VERSION" \
--argjson aks_cli_system_node_pool $SYSTEM_NODE_POOL \
--argjson aks_cli_user_node_pool $USER_NODE_POOL \
'{run_id: $run_id, region: $region, aks_cli_system_node_pool: $aks_cli_system_node_pool, aks_cli_user_node_pool: $aks_cli_user_node_pool}'| jq 'with_entries(select(.value != null and .value != ""))')
'{
run_id: $run_id,
region: $region,
aks_kubernetes_version: $aks_kubernetes_version,
aks_cli_system_node_pool: $aks_cli_system_node_pool,
aks_cli_user_node_pool: $aks_cli_user_node_pool
}'| jq 'with_entries(select(.value != null and .value != ""))')
```
5. Run terraform provisioning commands from [here](./../README.md#provision-resources-using-terraform)
10 changes: 10 additions & 0 deletions modules/terraform/azure/aks-cli/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ locals {
pool.name => pool
}

kubernetes_version = (
var.aks_cli_config.kubernetes_version == null ?
"" :
format(
"%s %s",
"--kubernetes-version", var.aks_cli_config.kubernetes_version,
)
)

aks_custom_headers_flags = (
length(var.aks_cli_config.aks_custom_headers) == 0 ?
"" :
Expand Down Expand Up @@ -77,6 +86,7 @@ resource "terraform_data" "aks_cli" {
local.aks_custom_headers_flags,
"--no-ssh-key",
"--enable-managed-identity",
local.kubernetes_version,
"--nodepool-name", var.aks_cli_config.default_node_pool.name,
"--node-count", var.aks_cli_config.default_node_pool.node_count,
"--node-vm-size", var.aks_cli_config.default_node_pool.vm_size,
Expand Down
1 change: 1 addition & 0 deletions modules/terraform/azure/aks-cli/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ variable "aks_cli_config" {
role = string
aks_name = string
sku_tier = string
kubernetes_version = optional(string, null)
aks_custom_headers = optional(list(string), [])
use_aks_preview_cli_extension = optional(bool, true)
default_node_pool = object({
Expand Down
3 changes: 3 additions & 0 deletions modules/terraform/azure/azure_input_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"aks_sku_tier": {
"type": "string"
},
"aks_kubernetes_version": {
"type": "string"
},
"aks_network_policy": {
"type": "string"
},
Expand Down
5 changes: 4 additions & 1 deletion modules/terraform/azure/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ locals {
region = lookup(var.json_input, "region", "East US")
run_id = lookup(var.json_input, "run_id", "123456")
aks_sku_tier = lookup(var.json_input, "aks_sku_tier", null)
aks_kubernetes_version = lookup(var.json_input, "aks_kubernetes_version", null)
aks_network_policy = lookup(var.json_input, "aks_network_policy", null)
aks_network_dataplane = lookup(var.json_input, "aks_network_dataplane", null)
aks_cli_system_node_pool = lookup(var.json_input, "aks_cli_system_node_pool", null)
Expand All @@ -24,7 +25,8 @@ locals {
for aks in var.aks_config_list : merge(
aks,
{
sku_tier = local.aks_sku_tier != null ? local.aks_sku_tier : aks.sku_tier
sku_tier = local.aks_sku_tier != null ? local.aks_sku_tier : aks.sku_tier
kubernetes_version = local.aks_kubernetes_version != null ? local.aks_kubernetes_version : aks.kubernetes_version
network_profile = merge(
aks.network_profile,
{
Expand All @@ -43,6 +45,7 @@ locals {
aks,
{
sku_tier = local.aks_sku_tier != null ? local.aks_sku_tier : aks.sku_tier
kubernetes_version = local.aks_kubernetes_version != null ? local.aks_kubernetes_version : aks.kubernetes_version
aks_custom_headers = length(local.aks_custom_headers) > 0 ? local.aks_custom_headers : aks.aks_custom_headers
default_node_pool = local.aks_cli_system_node_pool != null ? local.aks_cli_system_node_pool : aks.default_node_pool
extra_node_pool = local.aks_cli_user_node_pool != null ? local.aks_cli_user_node_pool : aks.extra_node_pool
Expand Down
14 changes: 8 additions & 6 deletions modules/terraform/azure/variables.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
variable "json_input" {
description = "value of the json input"
type = object({
run_id = string
region = string
aks_sku_tier = optional(string, null)
aks_network_policy = optional(string, null)
aks_network_dataplane = optional(string, null)
aks_custom_headers = optional(list(string), [])
run_id = string
region = string
aks_sku_tier = optional(string, null)
aks_kubernetes_version = optional(string, null)
aks_network_policy = optional(string, null)
aks_network_dataplane = optional(string, null)
aks_custom_headers = optional(list(string), [])
aks_cli_system_node_pool = optional(object({
name = string
node_count = number
Expand Down Expand Up @@ -196,6 +197,7 @@ variable "aks_cli_config_list" {
aks_name = string
sku_tier = string

kubernetes_version = optional(string, null)
aks_custom_headers = optional(list(string), [])
use_aks_preview_cli_extension = optional(bool, true)

Expand Down
2 changes: 2 additions & 0 deletions steps/terraform/set-input-variables-azure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ steps:
--arg run_id $RUN_ID \
--arg region $REGION \
--arg aks_sku_tier "$SKU_TIER" \
--arg aks_kubernetes_version "$KUBERNETES_VERSION" \
--arg aks_network_policy "$NETWORK_POLICY" \
--arg aks_network_dataplane "$NETWORK_DATAPLANE" \
--argjson aks_custom_headers "$AKS_CUSTOM_HEADERS" \
Expand All @@ -43,6 +44,7 @@ steps:
run_id: $run_id,
region: $region,
aks_sku_tier: $aks_sku_tier,
aks_kubernetes_version: $aks_kubernetes_version,
aks_network_policy: $aks_network_policy,
aks_network_dataplane: $aks_network_dataplane,
aks_custom_headers: $aks_custom_headers,
Expand Down

0 comments on commit 96d7346

Please sign in to comment.