Add K8s server version identification in stx-platformclients image
This change enables compatibility with the Kubernetes server version when using the remote CLI by matching the kubectl client version accordingly. Currently, the stx-platformclients image includes only the minimum Kubernetes version supported by StarlingX. This enhancement introduces a new script included in the stx-platformclients image during its build process. The script gets the Kubernetes server version every 30 minutes and dynamically selects the appropriate kubectl binary version to execute commands. The script handles any of Kubernetes server versions: - If the server version is lower than the minimum version available in the image, it uses as default the minimum k8s available version. - If the server version is higher than the maximum version available in the image, it uses as default the maximum k8s available version. - If the server version is within the available range, it uses the exact the k8s server version. Examples: - Using a 1.26.1 Kubernetes server with a stx-platformclients image supporting versions from 1.29.2 to 1.32.2 (use the minimum k8s available version): $ kubectl version [WARN] Detected a mismatch between Kubernetes client and server versions. [WARN] Continuing with client version v1.29.2 and server version v1.26.1. Client Version: v1.29.2 Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 Server Version: v1.26.1 WARNING: version difference between client (1.29) and server (1.26) exceeds the supported minor version skew of +/-1 - Using a 1.31.5 Kubernetes server with a stx-platformclients image supporting versions from 1.24.4 to 1.30.6 (use the maximum k8s available version): $ kubectl version [WARN] Detected a mismatch between Kubernetes client and server versions. [WARN] Continuing with client version v1.30.6 and server version v1.31.5. Client Version: v1.30.6 Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3 Server Version: v1.31.5 - Using a 1.31.5 Kubernetes server with a stx-platformclients image supporting versions from 1.29.2 to 1.32.2 (use the exact k8s server version): $ kubectl version Client Version: v1.31.5 Kustomize Version: v5.4.2 Server Version: v1.31.5 The changes include a new package (vim) to allow using 'kubectl edit' command using remote-cli, which was not possible before because stx-platformclients did not have any text editor. The stable_docker_image file must be updated every time StarlingX updates its supported Kubernetes versions to ensure the user will not see WARN messages when using remote-cli. TEST PLAN: - PASS: Execute kubectl commands - PASS: Check if the script supports to use special characters Notes: - The tests were executed in an environment with the following k8s server versions: 1.24.4, 1.25.4, 1.26.1, 1.27.5, 1.28.4, 1.29.2, 1.30.6 and 1.31.5 - The second test from the test plan (about special characters) was executed using changes in the remote-cli repo to parse the special characters correctly into the Docker image Closes-Bug: 2112129 Change-Id: I2fe1e5b36be0f20862be165c730248d3f22cdecf Signed-off-by: Gustavo Ornaghi Antunes <gustavo.ornaghiantunes@windriver.com>
This commit is contained in:
@@ -3,10 +3,96 @@ LABEL=stx-platformclients
|
||||
PROJECT=infra
|
||||
PROJECT_REPO=nil
|
||||
DIST_REPOS="OS"
|
||||
DIST_PACKAGES="python3-dev libffi-dev libssl-dev libcurl4-openssl-dev libfile-which-perl bash-completion helm kubernetes-1.29.2-client"
|
||||
DIST_PACKAGES="python3-dev libffi-dev libssl-dev libcurl4-openssl-dev libfile-which-perl bash-completion vim helm kubernetes-1.29.2-client kubernetes-1.30.6-client kubernetes-1.31.5-client kubernetes-1.32.2-client kubernetes-1.33.0-client"
|
||||
PIP_PACKAGES="pycryptodomex httplib2 pyopenssl ndg-httpsclient pyasn1 \
|
||||
six prettytable PyYAML python-keystoneclient python-barbicanclient \
|
||||
python-openstackclient cgtsclient fmclient distributedcloud_client \
|
||||
osprofiler beautifulsoup4 oidcauthtools mechanize html5lib webencodings \
|
||||
nfv-client software-client tsconfig"
|
||||
CUSTOMIZATION="ln -s /usr/local/kubernetes/1.29.2/stage1/usr/bin/kubectl /usr/bin/"
|
||||
CUSTOMIZATION="echo '$(base64 -w0 <<'EOL'
|
||||
#!/bin/bash
|
||||
|
||||
#
|
||||
# Copyright (c) 2025 Wind River Systems, Inc.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# All Rights Reserved.
|
||||
#
|
||||
|
||||
# Function to print warnings
|
||||
warn() {
|
||||
echo -e "\e[1;33m[WARN]\e[0m $1" >&2
|
||||
}
|
||||
|
||||
# Function to compare versions
|
||||
version_gt() { [ "$(printf '%s\n' "$1" "$2" | sort -V | head -n 1)" = "$2" ]; }
|
||||
|
||||
# Function to get the expected k8s client version
|
||||
get_client_version() {
|
||||
local VERSION="${MIN_KUBE_VERSION}"
|
||||
local SUPPORTED="false"
|
||||
if [[ -n "${KUBE_SERVER_VERSION}" && ( "${KUBE_AVAILABLE_VERSIONS}" == *"${KUBE_SERVER_VERSION}"* ) ]]; then
|
||||
VERSION="${KUBE_SERVER_VERSION}"
|
||||
local SUPPORTED="true"
|
||||
elif version_gt "${KUBE_SERVER_VERSION}" "${MAX_KUBE_VERSION}"; then
|
||||
VERSION="${MAX_KUBE_VERSION}"
|
||||
fi
|
||||
echo "${VERSION} ${SUPPORTED}"
|
||||
}
|
||||
|
||||
# Function to get the k8s server version
|
||||
get_server_version() {
|
||||
local VERSION=$(echo "${KUBE_SERVER_VERSION}" || echo "")
|
||||
|
||||
# Get the last time that the server version file have been updated.
|
||||
local TIME_DIFF=0
|
||||
if [[ -f "${KUBE_SERVER_VERSION_FILE}" ]]; then
|
||||
local FILE_MOD_TIME=$(stat -c %Y "${KUBE_SERVER_VERSION_FILE}")
|
||||
local CURRENT_TIME=$(date +%s)
|
||||
TIME_DIFF=$((CURRENT_TIME - FILE_MOD_TIME))
|
||||
fi
|
||||
|
||||
# If the k8s server version is empty or
|
||||
# the k8s server version file was updated more than 30min,
|
||||
# get the version from the k8s server version file
|
||||
if [[ -z "${VERSION}" || "${TIME_DIFF}" -gt 1800 ]]; then
|
||||
local KUBE_CLIENT_BIN_PATH=$(find "${KUBE_BASE_PATH}${KUBE_CLIENT_VERSION}"/*/usr/bin/ -name "kubectl")
|
||||
VERSION=$(${KUBE_CLIENT_BIN_PATH} version -o json 2>/dev/null | awk '/serverVersion/ {found=1} found && /gitVersion/ {gsub(/.*"v/, ""); gsub(/".*/, ""); print; exit}')
|
||||
[[ -z "${VERSION}" || ( ! "${VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ) ]] && VERSION="${KUBE_CLIENT_VERSION}"
|
||||
echo "${VERSION}" > "${KUBE_SERVER_VERSION_FILE}"
|
||||
fi
|
||||
|
||||
echo "${VERSION}"
|
||||
}
|
||||
|
||||
BASE_VOLUME="/wd"
|
||||
KUBE_BASE_PATH="/usr/local/kubernetes/"
|
||||
|
||||
KUBE_SERVER_VERSION_FILE="${BASE_VOLUME}"/.kube_server_version
|
||||
|
||||
KUBE_AVAILABLE_VERSIONS=$(ls "${KUBE_BASE_PATH}" | sort -V)
|
||||
|
||||
MIN_KUBE_VERSION=$(echo "${KUBE_AVAILABLE_VERSIONS}" | head -n1)
|
||||
MAX_KUBE_VERSION=$(echo "${KUBE_AVAILABLE_VERSIONS}" | tail -n1)
|
||||
|
||||
KUBE_CLIENT_VERSION="${MIN_KUBE_VERSION}"
|
||||
KUBE_SERVER_VERSION=$(cat "${KUBE_SERVER_VERSION_FILE}" 2> /dev/null)
|
||||
|
||||
if [[ "${KUBE_SERVER_VERSION}" == "${MAX_KUBE_VERSION}" ]]; then
|
||||
KUBE_CLIENT_VERSION="${KUBE_SERVER_VERSION}"
|
||||
else
|
||||
KUBE_SERVER_VERSION=$(get_server_version)
|
||||
read KUBE_CLIENT_VERSION KUBE_SERVER_VERSION_IS_SUPPORTED <<< $(get_client_version)
|
||||
|
||||
if [[ "${KUBE_SERVER_VERSION_IS_SUPPORTED}" == "false" ]]; then
|
||||
warn "Detected a mismatch between Kubernetes client and server versions."
|
||||
warn "Continuing with client version v${KUBE_CLIENT_VERSION} and server version v${KUBE_SERVER_VERSION}."
|
||||
fi
|
||||
fi
|
||||
|
||||
KUBE_BIN_PATH=$(find "${KUBE_BASE_PATH}${KUBE_CLIENT_VERSION}"/*/usr/bin/ -name "kubectl")
|
||||
|
||||
exec "${KUBE_BIN_PATH}" "$@"
|
||||
EOL
|
||||
)' | base64 -d > /usr/local/bin/kubectl && chmod +x /usr/local/bin/kubectl"
|
||||
|
Reference in New Issue
Block a user