Skip to content

Commit 3510ed2

Browse files
authored
Normalize GKE identifiers in snippets (mongodb#458)
# Summary When executed from a tag build, the version_id is long and contains dots which are unacceptable by GKE. Additionally it reduces verbose output when installing gcloud. ## Proof of Work EVG run with --param override_version_id=mongodb_kubernetes_1.4.0_68c913f767d52d00076a2698 ## Checklist - [ ] Have you linked a jira ticket and/or is the ticket in the title? - [ ] Have you checked whether your jira ticket required DOCSP changes? - [ ] Have you added changelog file? - use `skip-changelog` label if not needed - refer to [Changelog files and Release Notes](https://github.com/mongodb/mongodb-kubernetes/blob/master/CONTRIBUTING.md#changelog-files-and-release-notes) section in CONTRIBUTING.md for more details
1 parent 7b8cc91 commit 3510ed2

File tree

6 files changed

+48
-8
lines changed

6 files changed

+48
-8
lines changed

public/architectures/setup-multi-cluster/ra-09-setup-externaldns/env_variables.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
# ${K8S_CLUSTER_2_CONTEXT_NAME}
1313
# ${MDB_GKE_PROJECT}
1414

15-
# Example K8S_CLUSTER_SUFFIX when running in evg: "-68c1dc3ffdfb5800075ecee0-26080"
1615
suffix_short="${K8S_CLUSTER_SUFFIX}"
17-
# Ensure suffix_short is no longer than 19 characters to make DNS_SA_NAME fit max 30 characters
16+
# Ensure suffix_short is no longer than 19 characters to make GKE identifiers fit under 30 characters
1817
if [[ -n "${suffix_short}" && ${#suffix_short} -gt 19 ]]; then
1918
# Calculate positions for cutting from the middle
2019
prefix_len=$(((19 - 1) / 2)) # -1 for the 'x' replacement
@@ -27,4 +26,4 @@ export DNS_SA_NAME="ext-dns-sa${suffix_short}"
2726
export DNS_SA_EMAIL="${DNS_SA_NAME}@${MDB_GKE_PROJECT}.iam.gserviceaccount.com"
2827

2928
export CUSTOM_DOMAIN="mongodb.custom"
30-
export DNS_ZONE="mongodb"
29+
export DNS_ZONE="mongodb${suffix_short}"

scripts/dev/contexts/funcs/gke

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

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=""

scripts/evergreen/setup_gcloud_cli.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -Eeou pipefail
44
source scripts/dev/set_env_context.sh
55

66
curl -s --retry 3 -LO "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-linux-x86_64.tar.gz"
7-
tar xvf google-cloud-cli-linux-x86_64.tar.gz -C "${workdir}"
7+
tar xf google-cloud-cli-linux-x86_64.tar.gz -C "${workdir}"
88
"${workdir}"/google-cloud-sdk/install.sh --quiet
99
source "${workdir}/google-cloud-sdk/path.bash.inc"
1010

0 commit comments

Comments
 (0)