diff --git a/ci/jenkins/install-kubectl.sh b/ci/jenkins/install-kubectl.sh new file mode 100755 index 00000000000..edc380c9597 --- /dev/null +++ b/ci/jenkins/install-kubectl.sh @@ -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 diff --git a/ci/jenkins/k8s-version b/ci/jenkins/k8s-version new file mode 100644 index 00000000000..9b277b94aca --- /dev/null +++ b/ci/jenkins/k8s-version @@ -0,0 +1 @@ +v1.29.0 diff --git a/ci/jenkins/test-mc.sh b/ci/jenkins/test-mc.sh index 83283ae8394..41d3e94b847 100755 --- a/ci/jenkins/test-mc.sh +++ b/ci/jenkins/test-mc.sh @@ -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 @@ -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 diff --git a/ci/jenkins/test-rancher.sh b/ci/jenkins/test-rancher.sh index d0148faecec..cc088eb6c7b 100644 --- a/ci/jenkins/test-rancher.sh +++ b/ci/jenkins/test-rancher.sh @@ -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 diff --git a/ci/jenkins/test-vm.sh b/ci/jenkins/test-vm.sh index 556999e9200..0efac9653ba 100755 --- a/ci/jenkins/test-vm.sh +++ b/ci/jenkins/test-vm.sh @@ -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" } @@ -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 diff --git a/ci/jenkins/test-vmc.sh b/ci/jenkins/test-vmc.sh index e736360faee..d193bd6765b 100755 --- a/ci/jenkins/test-vmc.sh +++ b/ci/jenkins/test-vmc.sh @@ -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 @@ -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 diff --git a/ci/jenkins/test.sh b/ci/jenkins/test.sh index 1456db960fc..b436dbf887d 100755 --- a/ci/jenkins/test.sh +++ b/ci/jenkins/test.sh @@ -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