Skip to content

Commit 4fa6b0e

Browse files
committed
Normalize GKE identifiers in snippets
1 parent 68fcb3f commit 4fa6b0e

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

scripts/dev/contexts/funcs/gke

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Function to normalize identifier for K8s naming requirements
2+
normalize_identifier() {
3+
local str=$1
4+
local max_len=$(($2 - 1))
5+
6+
# Convert to lowercase and replace invalid characters with hyphens
7+
str=$(echo -n "${str}" | tr '[:upper:]_' '[:lower:]-' | sed 's/[^a-z0-9-]/-/g')
8+
# Ensure it ends with alphanumeric
9+
10+
# Truncate to ${max_len} chars by cutting from middle
11+
if [[ ${#str} -gt ${max_len} ]]; then
12+
half_idx=$((max_len / 2 - 1))
13+
local start_part="${str:0:${half_idx}}"
14+
local end_part="${str: -${half_idx}}"
15+
str="${start_part}x${end_part}"
16+
fi
17+
18+
[[ ${str} =~ -$ ]] && str="${str}0"
19+
echo -n "${str}"
20+
}
21+
22+
# for prerelease builds we have:
23+
# version_id=mongodb_kubernetes_1.4.0_68c913f767d52d00076a2698-9041 (len=54)
24+
# k8s cluster name prefix is: k8s-mdb-0- (len=10)
25+
# K8S_CLUSTER_PREFIX must be shorter than 30 to make the final
26+
# gke identifier shorter than 40 characters
27+
create_k8s_cluster_suffix() {
28+
echo -n "$(normalize_identifier "${K8S_CLUSTER_SUFFIX:-"-${version_id}-${RANDOM}"}" 28)"
29+
}
30+

scripts/dev/contexts/prerelease_gke_code_snippets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ script_name=$(readlink -f "${BASH_SOURCE[0]}")
88
script_dir=$(dirname "${script_name}")
99

1010
source "${script_dir}/root-context"
11+
source "${script_dir}/funcs/gke"
12+
K8S_CLUSTER_SUFFIX="$(create_k8s_cluster_suffix)"
13+
export K8S_CLUSTER_SUFFIX
1114

1215
export MDB_GKE_PROJECT="scratch-kubernetes-team"
13-
export K8S_CLUSTER_SUFFIX="${K8S_CLUSTER_SUFFIX:-"-${version_id}-${RANDOM}"}"
16+
1417
export CODE_SNIPPETS_COMMIT_OUTPUT=true
1518

1619
# we reset evg host to use a default ~/.kube/config for GKE instead of the one from evg host

scripts/dev/contexts/private_gke_code_snippets

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ script_dir=$(dirname "${script_name}")
1010
source "${script_dir}/root-context"
1111
source "${script_dir}/variables/om80"
1212

13+
source "${script_dir}/funcs/gke"
14+
K8S_CLUSTER_SUFFIX="$(create_k8s_cluster_suffix)"
15+
export K8S_CLUSTER_SUFFIX
16+
1317
export KUBE_ENVIRONMENT_NAME=multi
1418

1519
export MDB_GKE_PROJECT="scratch-kubernetes-team"
16-
export K8S_CLUSTER_SUFFIX="${K8S_CLUSTER_SUFFIX:-"-${version_id}-${RANDOM}"}"
1720

1821
# we reset evg host to use a default ~/.kube/config for GKE instead of the one from evg host
1922
export EVG_HOST_NAME=""

scripts/dev/contexts/public_gke_code_snippets

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ script_dir=$(dirname "${script_name}")
99

1010
source "${script_dir}/root-context"
1111

12+
source "${script_dir}/funcs/gke"
13+
K8S_CLUSTER_SUFFIX="$(create_k8s_cluster_suffix)"
14+
export K8S_CLUSTER_SUFFIX
15+
1216
export KUBE_ENVIRONMENT_NAME=multi
1317

1418
export MDB_GKE_PROJECT="scratch-kubernetes-team"
15-
# shellcheck disable=SC2154
16-
export K8S_CLUSTER_SUFFIX="${K8S_CLUSTER_SUFFIX:-"-${version_id}-${RANDOM}"}"
1719

1820
# we reset evg host to use a default ~/.kube/config for GKE instead of the one from evg host
1921
export EVG_HOST_NAME=""

0 commit comments

Comments
 (0)