Skip to content

Commit

Permalink
Install kubectl tool to a specific version
Browse files Browse the repository at this point in the history
Check and make sure the kubectl tool is up to date.

Signed-off-by: Lan Luo <luola@vmware.com>
  • Loading branch information
luolanzone committed Apr 10, 2024
1 parent 8619bb2 commit fe6b0b8
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 6 deletions.
69 changes: 69 additions & 0 deletions ci/jenkins/install-kubectl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash

# Copyright 2024 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eo pipefail

expected_kubectl_version=""
if [[ -n $1 ]];then
expected_kubectl_version=$1
elif ! which kubectl > /dev/null; then
expected_kubectl_version=$(head -n1 ./k8s-version)
else
# Check both Client and Server versions. If the Server version is not found, make sure the
# version of kubectl matches the version in the file k8s-version. If Client and Server versions
# are not matched, reinstall kubectl to the same version as K8s server.
installed_kubectl_version=$(kubectl version | grep "Client Version" | awk '{print $3}')
cmd_return_code=0
k8s_server_version=$(kubectl version | grep "Server Version" | awk '{print $3}') || cmd_return_code=$?
if [[ $cmd_return_code -ne 0 ]]; then
expected_kubectl_version=$(head -n1 ./k8s-version)
if [[ "$installed_kubectl_version" == "$expected_kubectl_version" ]]; then
echo "=== Existing kubectl version $installed_kubectl_version is up to date ==="
exit 0
fi
elif [[ "$installed_kubectl_version" != "$k8s_server_version" ]]; then
expected_kubectl_version=$k8s_server_version
else
echo "=== Existing kubectl version $installed_kubectl_version is up to date ==="
exit 0
fi
fi

# Linux only
function install_kubectl {
arch_name="$(uname -m)"
arch=""
case "$arch_name" in
"x86_64")
arch="amd64"
;;
"aarch64")
arch="arm64"
;;
*)
echoerr "Unsupported platform $arch_name"
exit 1
;;
esac

curl -LO "https://dl.k8s.io/release/$expected_kubectl_version/bin/linux/$arch/kubectl"
chmod +x ./kubectl
sudo mv kubectl /usr/local/bin
}

echo "=== Install kubectl to the expected version $expected_kubectl_version ==="
install_kubectl
exit 0
1 change: 1 addition & 0 deletions ci/jenkins/k8s-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v1.29.0
7 changes: 6 additions & 1 deletion ci/jenkins/test-mc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ function run_multicluster_e2e {
done
else
for kubeconfig in "${membercluster_kubeconfigs[@]}"; do
kubectl get nodes -o wide --no-headers=true "${kubeconfig}"| awk '{print $6}' | while read IP; do
kubectl get nodes -o wide --no-headers=true "${kubeconfig}" | awk '{print $6}' | while read IP; do
rsync -avr --progress --inplace -e "ssh -o StrictHostKeyChecking=no" "${WORKDIR}"/nginx.tar jenkins@["${IP}"]:"${WORKDIR}"/nginx.tar
rsync -avr --progress --inplace -e "ssh -o StrictHostKeyChecking=no" "${WORKDIR}"/agnhost.tar jenkins@["${IP}"]:"${WORKDIR}"/agnhost.tar
if ${IS_CONTAINERD};then
Expand Down Expand Up @@ -493,6 +493,11 @@ if [[ ${KIND} == "true" ]]; then
done
fi

# All clusters in one testing ClusterSet should be the same K8s version, so
# set KUBECONFIG with the leader config file to check and install kubectl.
export KUBECONFIG=${LEADER_CLUSTER_CONFIG}
bash $(dirname "$0")/install-kubectl.sh

# We assume all clusters in one testing ClusterSet are using the same runtime,
# so check leader cluster only to set IS_CONTAINERD.
set +e
Expand Down
3 changes: 3 additions & 0 deletions ci/jenkins/test-rancher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ function deliver_antrea {
fetch_kubeconfig $KUBECONFIG_PATH
export KUBECONFIG=$KUBECONFIG_PATH

echo "====== Check and install kubectl with a specific version ======"
bash $(dirname "$0")/install-kubectl.sh

echo "====== Building Antrea for the Following Commit ======"
export GO111MODULE=on
export GOPATH=${WORKDIR}/go
Expand Down
8 changes: 5 additions & 3 deletions ci/jenkins/test-vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ declare -a WIN_HOSTNAMES=("vmbmtest0-win-0")
declare -A LINUX_HOSTS_TO_USERNAME=(["vmbmtest0-1"]="ubuntu" ["vmbmtest0-redhat-0"]="root")
declare -A WINDOWS_HOSTS_TO_USERNAME=(["vmbmtest0-win-0"]="Administrator")

# To run kubectl cmds
export KUBECONFIG=${KUBECONFIG_PATH}

function print_usage {
echoerr "$_usage"
}
Expand Down Expand Up @@ -96,6 +93,11 @@ if [[ "$WORKDIR" != "$DEFAULT_WORKDIR" && "$KUBECONFIG_PATH" == "$DEFAULT_KUBECO
KUBECONFIG_PATH=${WORKDIR}/.kube/config
fi

# To run kubectl cmds.
export KUBECONFIG=${KUBECONFIG_PATH}
# Check and install kubectl with a specific version.
bash $(dirname "$0")/install-kubectl.sh

function export_govc_env_var {
# This should be coming from jenkins configuration
export GOVC_URL=$GOVC_URL
Expand Down
6 changes: 5 additions & 1 deletion ci/jenkins/test-vmc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ if [[ "$WORKDIR" != "$DEFAULT_WORKDIR" && "$KUBECONFIG_PATH" == "$DEFAULT_KUBECO
KUBECONFIG_PATH=$WORKDIR/.kube/config
fi

# Check and install kubectl to a specific version.
export KUBECONFIG=$KUBECONFIG_PATH
bash $(dirname "$0")/install-kubectl.sh

# If DOCKER_REGISTRY is non null, we ensure that "make" commands never pull from docker.io.
NO_PULL=
if [[ ${DOCKER_REGISTRY} != "" ]]; then
Expand Down Expand Up @@ -189,7 +193,7 @@ function release_static_ip() {
function setup_cluster() {
export KUBECONFIG=$KUBECONFIG_PATH
if [ -z $K8S_VERSION ]; then
export K8S_VERSION=v1.28.0
export K8S_VERSION=$(head -n1 ./k8s-version)
fi
if [ -z $TEST_OS ]; then
export TEST_OS=ubuntu-2004
Expand Down
5 changes: 4 additions & 1 deletion ci/jenkins/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,10 @@ EOF
done
}

export KUBECONFIG=${KUBECONFIG_PATH}
# Check and install kubectl to a specific version.
export KUBECONFIG=$KUBECONFIG_PATH
bash $(dirname "$0")/install-kubectl.sh

if [[ $TESTBED_TYPE == "flexible-ipam" ]]; then
./hack/generate-manifest.sh --flexible-ipam --multicast --verbose-log > build/yamls/antrea.yml
fi
Expand Down

0 comments on commit fe6b0b8

Please sign in to comment.