Skip to content

Commit 6460989

Browse files
authored
fix: cloud-init changes to allow user to pass custom script per nodepool(#502)
* fix: cloudinit changes to allow user to pass custom script * added variable type in submodule * feat: Custom Cloud-init * doc changes for cloudinit * doc changes for cloudinit
1 parent c4f70d5 commit 6460989

File tree

9 files changed

+85
-17
lines changed

9 files changed

+85
-17
lines changed

docs/cloudinit.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
= cloud-init
2+
:idprefix:
3+
:idseparator: -
4+
:sectlinks:
5+
:sectnums:
6+
:toc: auto
7+
8+
:uri-cloudinit: https://docs.oracle.com/en-us/iaas/Content/ContEng/Tasks/contengusingcustomcloudinitscripts.htm
9+
10+
== Instructions:
11+
12+
* {uri-cloudinit}[Using cloud-init in OKE]
13+
* The default cloud-init script used by this module is as below:
14+
----
15+
#!/bin/bash
16+
17+
# DO NOT MODIFY
18+
curl --fail -H "Authorization: Bearer Oracle" -L0 http://169.254.169.254/opc/v2/instance/metadata/oke_init_script | base64 --decode >/var/run/oke-init.sh
19+
20+
## run oke provisioning script
21+
bash -x /var/run/oke-init.sh
22+
23+
### adjust block volume size
24+
/usr/libexec/oci-growfs -y
25+
26+
timedatectl set-timezone ${worker_timezone}
27+
----
28+
29+
* To customize this you can modify the above script and pass the script as input variable to `cloudinit_nodepool_common`. This script will be used across all the nodepools.
30+
31+
* To use specific cloud-init script for a nodepool pass the script as input variable to `cloudinit_nodepool` as a map.This will take precedence over `cloudinit_nodepool_common` for that nodepool.
32+
Ex: cloudinit_nodepool = {
33+
#np1 = "/tmp/np1cloudinit.sh"
34+
#np3 = "/tmp/np3cloudinit.sh"
35+
#}
36+
37+
38+

docs/terraformoptions.adoc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,18 @@ node_pools = {
698698
|
699699
|7.9
700700

701+
|cloudinit_nodepool_common
702+
|cloud-init common for all nodepools when no specific script mentioned for nodepool in cloudinit_nodepool.
703+
|e.g.: `"/tmp/commoncloudinit.sh"`
704+
|""
705+
706+
|cloudinit_nodepool
707+
|cloud-init specific to nodepool to override cloudinit_nodepool_common.
708+
|e.g.: `cloudinit_nodepool = {
709+
np2 = "/tmp/np2cloudinit.sh"
710+
}`
711+
|{}
712+
701713
|node_pool_timezone
702714
|The preferred timezone for the worker nodes. {uri-timezones}[List of timezones].
703715
|

main.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module "vcn" {
1414
create_nat_gateway = var.worker_type == "private" || var.create_operator == true || (var.load_balancers == "internal" || var.load_balancers == "both") ? true : false
1515
create_service_gateway = true
1616
nat_gateway_public_ip_id = var.nat_gateway_public_ip_id
17-
create_drg = var.create_drg
17+
create_drg = var.create_drg
1818

1919
# lpgs
2020
local_peering_gateways = var.local_peering_gateways
@@ -242,6 +242,8 @@ module "oke" {
242242
enable_pv_encryption_in_transit = var.enable_pv_encryption_in_transit
243243
use_node_pool_volume_encryption = var.use_node_pool_volume_encryption
244244
node_pool_volume_kms_key_id = var.node_pool_volume_kms_key_id
245+
cloudinit_nodepool = var.cloudinit_nodepool
246+
cloudinit_nodepool_common = var.cloudinit_nodepool_common
245247

246248
# oke load balancer parameters
247249
preferred_load_balancer = var.preferred_load_balancer
@@ -330,7 +332,7 @@ module "extensions" {
330332
use_cluster_encryption = var.use_cluster_encryption
331333
cluster_kms_key_id = var.cluster_kms_key_id
332334
cluster_kms_dynamic_group_id = module.oke.cluster_kms_dynamic_group_id
333-
create_policies = var.create_policies
335+
create_policies = var.create_policies
334336

335337
# ocir parameters
336338
email_address = var.email_address

modules/oke/cloudinit.tf

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ locals {
44
worker_timezone = var.node_pool_timezone
55
}
66
)
7-
# example for adding more script
8-
# second_script_template = templatefile("${path.module}/cloudinit/second.template.sh",{})
7+
98
}
109

1110
# cloud-init for workers
@@ -18,12 +17,6 @@ data "cloudinit_config" "worker" {
1817
content_type = "text/x-shellscript"
1918
content = local.worker_script_template
2019
}
21-
22-
# example for adding more script
23-
# part {
24-
# filename = "second.sh"
25-
# content_type = "text/x-shellscript"
26-
# content = local.second_script_template
27-
# }
20+
2821
}
2922

modules/oke/cloudinit/second.template.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

modules/oke/nodepools.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ resource "oci_containerengine_node_pool" "nodepools" {
3636
}
3737

3838
node_metadata = {
39-
user_data = data.cloudinit_config.worker.rendered
39+
user_data = var.cloudinit_nodepool_common == "" && lookup(var.cloudinit_nodepool, each.key, null) == null ? data.cloudinit_config.worker.rendered : lookup(var.cloudinit_nodepool, each.key, null) != null ? filebase64(lookup(var.cloudinit_nodepool, each.key, null)) : filebase64(var.cloudinit_nodepool_common)
4040
}
41-
41+
4242
node_source_details {
4343
boot_volume_size_in_gbs = lookup(each.value, "boot_volume_size", 50)
4444
# check is done for GPU,A1 and other shapes.In future if some other shapes or images added we need to modify

modules/oke/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ variable "use_node_pool_volume_encryption" {
5353

5454
variable "node_pool_volume_kms_key_id" {}
5555

56+
variable "cloudinit_nodepool" {
57+
type = map(any)
58+
}
59+
60+
variable "cloudinit_nodepool_common" {
61+
type = string
62+
}
63+
5664
variable "enable_pv_encryption_in_transit" {
5765
type = bool
5866
}

terraform.tfvars.example

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,16 @@ node_pool_image_id = "none"
172172
node_pool_name_prefix = "np"
173173
node_pool_os = "Oracle Linux"
174174
node_pool_os_version = "7.9"
175-
node_pool_timezone = "Etc/UTC"
176175
worker_nsgs = []
177176
worker_type = "private"
178177

178+
#cloudinit_nodepool_common = "/tmp/commoncloudinit.sh"
179+
#cloudinit_nodepool = {
180+
#np1 = "/tmp/np1cloudinit.sh"
181+
#np3 = "/tmp/np3cloudinit.sh"
182+
#}
183+
node_pool_timezone = "Etc/UTC"
184+
179185
# upgrade of existing node pools
180186
upgrade_nodepool = false
181187
node_pools_to_drain = ["np1", "np2"]

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,18 @@ variable "enable_pv_encryption_in_transit" {
539539
default = false
540540
}
541541

542+
variable "cloudinit_nodepool" {
543+
description = "Cloudinit script specific to nodepool"
544+
type = map(any)
545+
default = {}
546+
}
547+
548+
variable "cloudinit_nodepool_common" {
549+
description = "Cloudinit script common to all nodepool when cloudinit_nodepool is not provided"
550+
type = string
551+
default = ""
552+
}
553+
542554
variable "node_pools" {
543555
default = {
544556
np1 = { shape = "VM.Standard.E4.Flex", ocpus = 1, memory = 16, node_pool_size = 1, boot_volume_size = 150, label = { app = "frontend", pool = "np1" } }

0 commit comments

Comments
 (0)