11#! /usr/bin/env bash
2- # Copyright (c) 2022, 2023 Oracle Corporation and/or its affiliates.
2+ # Copyright (c) 2022, 2025 Oracle Corporation and/or its affiliates.
33# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
44# shellcheck disable=SC1091 # Ignore unresolved file path present on base images
55set -o pipefail
66
7+ function get_imds_base_url() {
8+ imds_base_url=$( cat /tmp/imds_base_url || echo " " )
9+
10+ if [[ -z $imds_base_url ]]; then
11+ for url in " http://169.254.169.254/" " http://[fd00:c1::a9fe:a9fe]/" ; do
12+ if curl -sSf -m 5 --retry 5 --retry-delay 1 -H ' Authorization: Bearer Oracle' -L0 " ${url} opc/v2/instance/state" > /dev/null; then
13+ imds_base_url=" $url "
14+ echo " $imds_base_url " > /tmp/imds_base_url
15+ break
16+ fi
17+ done
18+ fi
19+
20+ if [ -z " ${imds_base_url} " ]; then
21+ echo " Unable to determine imds base url" >&2
22+ exit 1
23+ fi
24+
25+ echo " ${imds_base_url} "
26+ }
27+
28+ function curl_instance_metadata() {
29+ local imds_base=" $( get_imds_base_url) "
30+ local url=" ${imds_base} $1 "
31+ local retries=10
32+ local output
33+
34+ while (( retries-- > 0 )) ; do
35+ if output=$( curl -sSf -m 5 -H ' Authorization: Bearer Oracle' -L0 " $url " ) ; then
36+ echo " $output "
37+ return 0
38+ fi
39+ sleep 1
40+ done
41+
42+ echo " Failed to fetch metadata from $url " >&2
43+ return 1
44+ }
45+
46+ function get_imds_instance() {
47+ find " ${INSTANCE_FILE} " -mmin -1 -not -empty > /dev/null 2>&1 || (curl_instance_metadata ' opc/v2/instance' | jq -rcM ' .' > " ${INSTANCE_FILE} " )
48+ INSTANCE=" $( cat " ${INSTANCE_FILE} " || echo -n ' ' ) "
49+
50+ export INSTANCE
51+ echo " ${INSTANCE} "
52+ }
53+
54+ function get_imds_metadata() {
55+ get_imds_instance | jq -rcM ' .metadata // {}'
56+ }
57+
758function run_oke_init() { # Initialize OKE worker node
859 if [[ -f /etc/systemd/system/oke-init.service ]]; then
960 systemctl --no-block enable --now oke-init.service
10- elif [[ -f /etc/oke/oke-functions.sh ]] && [[ -f /etc/oke/oke-install.sh ]]; then
11- source /etc/oke/oke-functions.sh
12- local apiserver_host; apiserver_host=$( get_apiserver_host)
13- if [[ -z " ${apiserver_host} " ]]; then
61+ return
62+ fi
63+
64+ if [[ -f /etc/oke/oke-install.sh ]]; then
65+ local apiserver_host cluster_ca
66+
67+ if [[ -f " /etc/oke/oke-apiserver" ]]; then
68+ apiserver_host=$( < /etc/oke/oke-apiserver)
69+ else
1470 apiserver_host=$( get_imds_metadata | jq -rcM ' .apiserver_host' )
1571 fi
1672
17- cluster_ca=$( get_kubelet_client_ca)
18- if [[ -z " ${cluster_ca} " ]]; then
73+ if [[ -f " /etc/kubernetes/ca.crt" ]]; then
74+ cluster_ca=$( base64 -w0 /etc/kubernetes/ca.crt)
75+ else
1976 cluster_ca=$( get_imds_metadata | jq -rcM ' .cluster_ca_cert' )
2077 fi
2178
2279 bash /etc/oke/oke-install.sh \
2380 --apiserver-endpoint " ${apiserver_host} " \
2481 --kubelet-ca-cert " ${cluster_ca} "
25- else # Retrieve base64-encoded script content from http, e.g. instance metadata
26- local oke_init_url=' http://169.254.169.254/opc/v2/instance/metadata/oke_init_script'
27- curl --fail -H " Authorization: Bearer Oracle" -L0 " ${oke_init_url} " \
28- | base64 --decode > /var/run/oke-init.sh && bash /var/run/oke-init.sh
82+ return
2983 fi
84+
85+ local retries=5
86+ local delay=2
87+ local oke_init_relative_path=" opc/v2/instance/metadata/oke_init_script"
88+ local script_path=" /var/run/oke-init.sh"
89+
90+ for (( i= 0 ; i< retries; i++ )) ; do
91+ for url in " http://169.254.169.254/" " http://[fd00:c1::a9fe:a9fe]/" ; do
92+ echo " Attempting to fetch OKE init script from ${base_url}${oke_init_relative_path} "
93+ if curl -sSf -H ' Authorization: Bearer Oracle' -L0 " ${url}${oke_init_relative_path} " | base64 --decode > " ${script_path} " ; then
94+ bash " ${script_path} "
95+ exit 0
96+ fi
97+ done
98+ echo " Retry $(( i+ 1 )) failed, retrying in $delay seconds..."
99+ done
30100}
31101
32- time run_oke_init || { echo " Error in OKE startup" 1>&2 ; exit 1; }
102+ INSTANCE_FILE=" /etc/oke/imds_instance.json"
103+ time run_oke_init || { echo " Error in OKE startup" >&2 ; exit 1; }
0 commit comments