From ca9b57e50c5a6bfccf9e58eaa1319101150c8e14 Mon Sep 17 00:00:00 2001 From: Mateus Caruccio Date: Mon, 18 Mar 2024 18:09:28 +0000 Subject: [PATCH] Minor updates --- Dockerfile | 8 +++++++- root/etc/profile.d/getup.sh | 3 +++ root/usr/local/bin/kubespray-apply | 15 +++++++------- root/usr/local/bin/kubespray-apply-tags | 14 ++++++++----- root/usr/local/bin/kubespray-copy | 25 ++++++++++------------- root/usr/local/bin/kubespray-exec | 8 +++++--- templates/kubespray/main.tf | 21 ++++++++++--------- templates/kubespray/variables-provider.tf | 4 ++-- 8 files changed, 56 insertions(+), 42 deletions(-) diff --git a/Dockerfile b/Dockerfile index d5379c16..fd5636a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -18,7 +18,6 @@ ENV TERM="xterm-256color" \ KREW_REPOS="kvaps@https://github.com/kvaps/krew-index" \ KREW_VERSION="v0.4.2" \ KUBECONFIG="${CLUSTER_DIR}/.kube/config" \ - KUBECTL_VERSIONS="v1.21.14 v1.22.17 v1.23.17 v1.24.14 v1.25.10 v1.26.9 v1.27.6" \ KUBELOGIN_VERSION="v0.0.32" \ OC_VERSION="4.11.0-0.okd-2022-12-02-145640" \ OSH="/etc/oh-my-bash" \ @@ -101,6 +100,13 @@ RUN cd /usr/local/bin && \ curl -skL https://run.linkerd.io/install | INSTALLROOT=/usr/local bash && \ curl -skL https://github.com/openshift/okd/releases/download/${OC_VERSION}/openshift-client-linux-${OC_VERSION}.tar.gz \ | tar xzvf - oc && \ + KUBECTL_VERSIONS=$( \ + curl -s https://api.github.com/repos/kubernetes/kubernetes/releases?per_page=100 \ + | jq -r '.[] | .tag_name' \ + | grep '^v[0-9]\.[0-9][0-9]\?\.[0-9][0-9]\?$' \ + | sort -Vr \ + | awk -F . '!a[$1 FS $2]++' \ + | sort -V) && \ for KUBECTL_VERSION in $KUBECTL_VERSIONS; do \ curl -skL https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl > \ kubectl_${KUBECTL_VERSION}; \ diff --git a/root/etc/profile.d/getup.sh b/root/etc/profile.d/getup.sh index 9a72c290..8433bb4a 100755 --- a/root/etc/profile.d/getup.sh +++ b/root/etc/profile.d/getup.sh @@ -230,6 +230,7 @@ function get_tf_config() { local sh_var_name=$1 local tf_var_name=$2 + local default=$3 if [ -v $sh_var_name ]; then echo ${!sh_var_name} @@ -247,6 +248,8 @@ function get_tf_config() array) hcl2json "$TF_VARS_FILE" | jq -Mrc ".${tf_var_name}|join(\"\n\")" esac + + echo $default } ask() diff --git a/root/usr/local/bin/kubespray-apply b/root/usr/local/bin/kubespray-apply index 7152a466..dd1e9a7c 100755 --- a/root/usr/local/bin/kubespray-apply +++ b/root/usr/local/bin/kubespray-apply @@ -4,18 +4,19 @@ set -eu source /etc/profile.d/getup.sh -if [ $# -gt 0 ]; then - user="$1" -else - user=$(awk '/\s*#?\s*ssh_user\s*=\s".*".*/ { patsplit($3, a, /[^"]+/); print a[1] }' $TF_VARS_FILE | tail -n1) 2>/dev/null +if [[ "$1" =~ (-h|--help) ]] ; then + echo "Usage: $0 [ansible-playbook parameters...]" + exit 1 fi -user=${user:-centos} +user=$(get_tf_config SSH_USER ssh_user centos) +kube_version=$(ansible-inventory -i $INVENTORY_FILE --list | jq -r '._meta.hostvars[._meta.hostvars|keys[0]].kube_version') playbook=$KUBESPRAY_DIR/cluster.yml if [ ${0##*/} == "kubespray-upgrade" ]; then - playbook=$KUBESPRAY_DIR/upgrade-cluster.yml + playbook=$KUBESPRAY_DIR/upgrade-cluster.yml fi -ask_execute_command ansible-playbook -i $INVENTORY_FILE --become --user $user $playbook +echo Kubernetes Version: $kube_version +ask_execute_command ansible-playbook -i $INVENTORY_FILE --become --user $user $@ $playbook diff --git a/root/usr/local/bin/kubespray-apply-tags b/root/usr/local/bin/kubespray-apply-tags index 2e2bfeed..331850a7 100755 --- a/root/usr/local/bin/kubespray-apply-tags +++ b/root/usr/local/bin/kubespray-apply-tags @@ -6,12 +6,15 @@ export ROOT_DIR=$(readlink -nf $(dirname $0)) source /etc/profile.d/getup.sh -if [ $# -gt 0 ]; then - user="$1" -else - user=$(awk '/\s*#?\s*ssh_user\s*=\s".*".*/ { patsplit($3, a, /[^"]+/); print a[1] }' $TF_VARS_FILE | tail -n1) 2>/dev/null +if [[ "$1" =~ (-h|--help) ]] ; then + echo "Usage: $0 [ansible-playbook parameters...]" + exit 1 fi +user=$(get_tf_config SSH_USER ssh_user centos) + +kube_version=$(ansible-inventory -i $INVENTORY_FILE --list | jq -r '._meta.hostvars[._meta.hostvars|keys[0]].kube_version') + out=/tmp/.dialog exec 3>$out eval dialog \ @@ -27,4 +30,5 @@ if [ -z "$tags" ]; then exit 0 fi -ask_execute_command ansible-playbook -i $INVENTORY_FILE --become --user $user $KUBESPRAY_DIR/cluster.yml --tags $tags +echo Kubernetes Version: $kube_version +ask_execute_command ansible-playbook -i $INVENTORY_FILE --become --user $user --tags $tags $@ $KUBESPRAY_DIR/cluster.yml diff --git a/root/usr/local/bin/kubespray-copy b/root/usr/local/bin/kubespray-copy index b4b10823..16fe14a3 100755 --- a/root/usr/local/bin/kubespray-copy +++ b/root/usr/local/bin/kubespray-copy @@ -4,15 +4,21 @@ set -eu source /etc/profile.d/getup.sh -user=$(get_tf_config SSH_USER ssh_user) - -if [ -z "$user" ]; then - user=centos -fi +user=$(get_tf_config SSH_USER ssh_user centos) hosts=all remote_src=false +if [ $# -lt 2 ] || [[ "$1" =~ (-h|--help) ]]; then + echo "Usage:" + echo + echo " Upload: $0 local-file hosts:remote-file [copy module extra parameters...]" + echo " Download: $0 host:remote-file local-file [copy module extra parameters...]" + echo + echo "See available flags in https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html" + exit 1 +fi + src=$1 dest=$2 shift 2 @@ -22,13 +28,6 @@ dest_host=${dest%%:*} src=${src#*:} dest=${dest#*:} -if [ -n "$src_host" -a -n "$dest_host" ] || [ -z "$src_host$dest_host" ]; then - echo "Usage:" - echo " Upload: $0 local-file hosts:remote-file" - echo " Download: $0 host:remote-file local-file" - exit 1 -fi - if [ -n "$src_host" ]; then # downloading hosts=$src_host @@ -38,6 +37,4 @@ elif [ -n "$dest_host" ]; then hosts=$dest_host fi -echo "See available flags in https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html" >&2 - execute_command ansible $hosts -i $INVENTORY_FILE --become --user $user -m copy -a "src=$src dest=$dest remote_src=$remote_src $*" diff --git a/root/usr/local/bin/kubespray-exec b/root/usr/local/bin/kubespray-exec index d0cc551e..81ea0938 100755 --- a/root/usr/local/bin/kubespray-exec +++ b/root/usr/local/bin/kubespray-exec @@ -4,11 +4,13 @@ set -eu source /etc/profile.d/getup.sh -user=$(grep 'ssh_user\s*=\s*.*' $CLUSTER_DIR/terraform.tfvars | awk '{print $NF}' | tr -d '"') -if [ -z "$user" ]; then - user=centos +if [[ "$1" =~ (-h|--help|^$) ]] ; then + echo "Usage: $0 command [parameters...]" + exit 1 fi +user=$(get_tf_config SSH_USER ssh_user centos) + set -x ansible all -i $INVENTORY_FILE --become --user $user -m shell -a "$*" diff --git a/templates/kubespray/main.tf b/templates/kubespray/main.tf index 424da278..f72b149f 100644 --- a/templates/kubespray/main.tf +++ b/templates/kubespray/main.tf @@ -1,16 +1,17 @@ module "cluster" { - source = "github.com/getupcloud/terraform-cluster-kubespray?ref=v3.4.0" + source = "github.com/getupcloud/terraform-cluster-kubespray?ref=v4.0.0-beta1" # cluster basics - customer_name = var.customer_name - cluster_name = var.cluster_name - cluster_sla = var.cluster_sla - cluster_provider = var.cluster_provider - use_kubeconfig = var.use_kubeconfig - pre_create = var.pre_create - post_create = var.post_create - modules = local.modules_result - dump_debug = var.dump_debug + customer_name = var.customer_name + cluster_name = var.cluster_name + cluster_sla = var.cluster_sla + cluster_provider = var.cluster_provider + kubernetes_version = var.kubernetes_version + use_kubeconfig = var.use_kubeconfig + pre_create = var.pre_create + post_create = var.post_create + modules = local.modules_result + dump_debug = var.dump_debug # monitoring and operations cronitor_enabled = var.cronitor_enabled diff --git a/templates/kubespray/variables-provider.tf b/templates/kubespray/variables-provider.tf index ae78b6a1..b9ff1682 100644 --- a/templates/kubespray/variables-provider.tf +++ b/templates/kubespray/variables-provider.tf @@ -10,7 +10,7 @@ variable "api_endpoint" { variable "kubernetes_version" { description = "Kubernetes rersion" type = string - default = "1.24" + default = "v1.25.6" } variable "region" { @@ -28,7 +28,7 @@ variable "deploy_components" { variable "kubespray_git_ref" { description = "Kubespray ref name" type = string - default = "remotes/origin/release-2.17" + default = "remotes/origin/release-2.22" } variable "kubespray_dir" {