Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
fseldow committed Dec 9, 2024
1 parent 896a173 commit 267f3ec
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 22 deletions.
25 changes: 25 additions & 0 deletions parts/linux/cloud-init/artifacts/cse_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,29 @@ removeKubeletNodeLabel() {
fi
}

updateKubeBinaryRegistryURL() {
# if rp already passes registry url, then directly use the registry url that rp passes
# this path should have not catch for now, but keep it for future
if [[ -n "${KUBE_BINARY_URL}" ]] && isRegistryUrl "${KUBE_BINARY_URL}"; then
echo "KUBE_BINARY_URL is a registry url, will use it to pull the kube binary"
KUBE_BINARY_REGISTRY_URL="${KUBE_BINARY_URL}"
else
# however, the kubelet and kubectl binary version may different with kubernetes version due to hotfix or beta
# so that we still need to extract the binary version from kube_binary_url
url_regex='https://[^/]+/kubernetes/v[0-9]+\.[0-9]+\..+/binaries/.+'
if [[ -n ${KUBE_BINARY_URL} ]]; then
binary_version="v${KUBERNETES_VERSION}" # by default use Kubernetes versions
if [[ ${KUBE_BINARY_URL} =~ $url_regex ]]; then
version_with_prefix="${KUBE_BINARY_URL#*kubernetes/}"
# Extract the version part
binary_version="${version_with_prefix%%/*}"
echo "Extracted version: $binary_version from KUBE_BINARY_URL: ${KUBE_BINARY_URL}"
else
echo "KUBE_BINARY_URL is formatted unexpectedly, will use the kubernetes version as binary version: $binary_version"
fi
fi
KUBE_BINARY_REGISTRY_URL="${BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER}/${K8S_REGISTRY_REPO}/kubernetes-node:${binary_version}-linux-${CPU_ARCH}"
fi
}

#HELPERSEOF
48 changes: 26 additions & 22 deletions parts/linux/cloud-init/artifacts/cse_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -613,30 +613,11 @@ installKubeletKubectlAndKubeProxy() {
if [[ ! -z ${BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER} ]]; then
# network isolated cluster
echo "Detect Bootstrap profile artifact is Cache, will use oras to pull artifact binary"
binary_version="v${KUBERNETES_VERSION}" # by default use Kubernetes versions
# if rp already passes registry url, then directly use the registry url that rp passes
# this path should have not catch for now, but keep it for future
if [[ -n "${KUBE_BINARY_URL}" ]] && isRegistryUrl "${KUBE_BINARY_URL}"; then
echo "KUBE_BINARY_URL is a registry url, will use it to pull the kube binary"
registry_url="${KUBE_BINARY_URL}"
else
# however, the kubelet and kubectl binary version may different with kubernetes version due to hotfix or beta
# so that we still need to extract the binary version from kube_binary_url
url_regex='https://[^/]+/kubernetes/v[0-9]+\.[0-9]+\..+/binaries/.+'
if [[ -n ${KUBE_BINARY_URL} ]]; then
if [[ ${KUBE_BINARY_URL} =~ $url_regex ]]; then
version_with_prefix="${KUBE_BINARY_URL#*kubernetes/}"
# Extract the version part
binary_version="${version_with_prefix%%/*}"
echo "Extracted version: $binary_version from KUBE_BINARY_URL: ${KUBE_BINARY_URL}"
fi
fi
registry_url="${BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER}/${K8S_REGISTRY_REPO}/kubernetes-node:${binary_version}-linux-${CPU_ARCH}"
fi

KUBE_BINARY_REGISTRY_URL=""
updateKubeBinaryRegistryURL

K8S_DOWNLOADS_TEMP_DIR_FROM_REGISTRY="/tmp/kubernetes/downloads" # /opt folder will return permission error
logs_to_events "AKS.CSE.installKubeletKubectlAndKubeProxy.extractKubeBinaries" extractKubeBinaries ${KUBERNETES_VERSION} $registry_url false ${K8S_DOWNLOADS_TEMP_DIR_FROM_REGISTRY}
logs_to_events "AKS.CSE.installKubeletKubectlAndKubeProxy.extractKubeBinaries" extractKubeBinaries ${KUBERNETES_VERSION} $KUBE_BINARY_REGISTRY_URL false ${K8S_DOWNLOADS_TEMP_DIR_FROM_REGISTRY}
# no egress traffic, default install will fail
# will exit if the download fails

Expand All @@ -653,6 +634,29 @@ installKubeletKubectlAndKubeProxy() {
rm -rf /usr/local/bin/kubelet-* /usr/local/bin/kubectl-* /home/hyperkube-downloads &
}

generateKubeBinaryRegistryURL() {
# if rp already passes registry url, then directly use the registry url that rp passes
# this path should have not catch for now, but keep it for future
if [[ -n "${KUBE_BINARY_URL}" ]] && isRegistryUrl "${KUBE_BINARY_URL}"; then
echo "KUBE_BINARY_URL is a registry url, will use it to pull the kube binary"
registry_url="${KUBE_BINARY_URL}"
else
# however, the kubelet and kubectl binary version may different with kubernetes version due to hotfix or beta
# so that we still need to extract the binary version from kube_binary_url
url_regex='https://[^/]+/kubernetes/v[0-9]+\.[0-9]+\..+/binaries/.+'
if [[ -n ${KUBE_BINARY_URL} ]]; then
binary_version="v${KUBERNETES_VERSION}" # by default use Kubernetes versions
if [[ ${KUBE_BINARY_URL} =~ $url_regex ]]; then
version_with_prefix="${KUBE_BINARY_URL#*kubernetes/}"
# Extract the version part
binary_version="${version_with_prefix%%/*}"
echo "Extracted version: $binary_version from KUBE_BINARY_URL: ${KUBE_BINARY_URL}"
fi
fi
registry_url="${BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER}/${K8S_REGISTRY_REPO}/kubernetes-node:${binary_version}-linux-${CPU_ARCH}"
fi
}

pullContainerImage() {
CLI_TOOL=$1
CONTAINER_IMAGE_URL=$2
Expand Down
44 changes: 44 additions & 0 deletions spec/parts/linux/cloud-init/artifacts/cse_install_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,48 @@ Describe 'cse_install.sh'
The output line 3 should equal "mock installContainerdWithManifestJson calling"
End
End

Describe 'updateKubeBinaryRegistryURL'
logs_to_events() {
echo "mock logs to events calling with $1"
}
It 'returns KUBE_BINARY_URL if it is already registry url'
KUBE_BINARY_URL="mcr.microsoft.com/oss/binaries/kubernetes/kubernetes-node:v1.30.0-linux-amd64"
KUBERNETES_VERSION="1.30.0"
updateKubeBinaryRegistryURL
When call updateKubeBinaryRegistryURL
The variable KUBE_BINARY_REGISTRY_URL should equal "$KUBE_BINARY_URL"
The output line 1 should equal "KUBE_BINARY_URL is a registry url, will use it to pull the kube binary"
End
It 'returns expected output from KUBE_BINARY_URL'
KUBE_BINARY_URL="https://acs-mirror.azureedge.net/kubernetes/v1.30.0-hotfix20241209/binaries/kubernetes-nodes-linux-amd64.tar.gz"
BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER="mcr.microsoft.com"
KUBERNETES_VERSION="1.30.0"
CPU_ARCH="amd64"
updateKubeBinaryRegistryURL
When call updateKubeBinaryRegistryURL
The variable KUBE_BINARY_REGISTRY_URL should equal "$BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER/oss/binaries/kubernetes/kubernetes-node:v1.30.0-hotfix20241209-linux-amd64"
The output line 1 should equal "Extracted version: v1.30.0-hotfix20241209 from KUBE_BINARY_URL: $KUBE_BINARY_URL"
End
It 'returns expected output for moonckae acs-mirror'
KUBE_BINARY_URL="https://acs-mirror.azureedge.cn/kubernetes/v1.30.0-alpha/binaries/kubernetes-nodes-linux-amd64.tar.gz"
BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER="mcr.microsoft.com"
KUBERNETES_VERSION="1.30.0"
CPU_ARCH="amd64"
updateKubeBinaryRegistryURL
When call updateKubeBinaryRegistryURL
The variable KUBE_BINARY_REGISTRY_URL should equal "$BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER/oss/binaries/kubernetes/kubernetes-node:v1.30.0-alpha-linux-amd64"
The output line 1 should equal "Extracted version: v1.30.0-alpha from KUBE_BINARY_URL: $KUBE_BINARY_URL"
End
It 'uses KUBENETES_VERSION if KUBE_BINARY_URL is invalid'
KUBE_BINARY_URL="https://invalidpath/v1.30.0-lts100/binaries/kubernetes-nodes-linux-amd64.tar.gz"
BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER="mcr.microsoft.com"
KUBERNETES_VERSION="1.30.0"
CPU_ARCH="amd64"
updateKubeBinaryRegistryURL
When call updateKubeBinaryRegistryURL
The variable KUBE_BINARY_REGISTRY_URL should equal "$BOOTSTRAP_PROFILE_CONTAINER_REGISTRY_SERVER/oss/binaries/kubernetes/kubernetes-node:v1.30.0-linux-amd64"
The output line 1 should equal "KUBE_BINARY_URL is formatted unexpectedly, will use the kubernetes version as binary version: v$KUBERNETES_VERSION"
End
End
End

0 comments on commit 267f3ec

Please sign in to comment.