From 8be8d6e3f5143eca78d7cebd7a2fdc118336dbdf Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Wed, 26 Feb 2020 22:04:06 +0200 Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9F=91=8C=20IMPROVE]=20test=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/all-in-one-test | 15 ++++-- scripts/kind-example-config.yaml | 15 +++--- scripts/wait-for-line-in-file | 82 ++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 13 deletions(-) create mode 100755 scripts/wait-for-line-in-file diff --git a/scripts/all-in-one-test b/scripts/all-in-one-test index 24cd4d2..35b78fe 100755 --- a/scripts/all-in-one-test +++ b/scripts/all-in-one-test @@ -5,9 +5,14 @@ set -euo pipefail # Get script path. script_path=$(dirname "$(readlink -f "${0}")") +# Cleanup +for t in HUP INT QUIT KILL TERM EXIT; do + trap "rm -f '${script_path}/a.log' '${script_path}/a.yaml' '${script_path}/kube-config.yaml'" "${t}" +done + # Create cluster using YAML provided in quick start guide of kind. kind create cluster --config "${script_path}/kind-example-config.yaml" || true -export KUBECONFIG=$(kind get kubeconfig-path --name='kind') +kind get kubeconfig > "${script_path}/kube-config.yaml" # For all the nodes, export it's configuration to YAML, add gke-preemptible # annotation, delete any previous killer state, then replace the configuration. @@ -18,12 +23,12 @@ for n in '' {2..3}; do kubectl replace -f "${script_path}/a.yaml" done -# Cleanup -rm -f "${script_path}/a.yaml" - # Run estafette-gke-preemptible-killer with all the arguments provided to this # script. -timeout 5s "${script_path}/../estafette-gke-preemptible-killer" "${@}" || true +"${script_path}/../estafette-gke-preemptible-killer" "${@}" --kubeconfig "${script_path}/kube-config.yaml" | tee "${script_path}/a.log" & +pid=${!} +"${script_path}/wait-for-line-in-file" 'Sleeping for' "${script_path}/a.log" +kill ${pid} # For all the nodes report their CreationTimestamp and their # gke-preemptible-killer-state. diff --git a/scripts/kind-example-config.yaml b/scripts/kind-example-config.yaml index 8a1ff35..1fd4927 100644 --- a/scripts/kind-example-config.yaml +++ b/scripts/kind-example-config.yaml @@ -1,17 +1,16 @@ # this config file contains all config fields with comments +# NOTE: this is not a particularly useful config file kind: Cluster -apiVersion: kind.sigs.k8s.io/v1alpha3 +apiVersion: kind.x-k8s.io/v1alpha4 # patch the generated kubeadm config with some extra settings kubeadmConfigPatches: - | - apiVersion: kubeadm.k8s.io/v1beta2 - kind: ClusterConfiguration - metadata: - name: config - networking: - serviceSubnet: 10.0.0.0/16 + apiVersion: kubelet.config.k8s.io/v1beta1 + kind: KubeletConfiguration + evictionHard: + nodefs.available: "0%" # patch it further using a JSON 6902 patch -kubeadmConfigPatchesJson6902: +kubeadmConfigPatchesJSON6902: - group: kubeadm.k8s.io version: v1beta2 kind: ClusterConfiguration diff --git a/scripts/wait-for-line-in-file b/scripts/wait-for-line-in-file new file mode 100755 index 0000000..fbc0787 --- /dev/null +++ b/scripts/wait-for-line-in-file @@ -0,0 +1,82 @@ +#!/bin/bash + +# Copyright (C) 2019 Andrei Pavel, andrei.pavel@cti.pub.ro +# Licensed under the MIT License + +# Header +[[ -z "${ARGUMENTS+x}" ]] && ARGUMENTS="\ + \$line + \$file + [\$timeout] +" +script_path="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")" + +#------------------------------------------------------------------------------# + +function read-offset() { + local file + file="$(readlink -f "${1}")" + file="${file//-/_}" + file="${file//./_}" + file="${file//\//_}" + offset="OFFSET_${file}" + printf '%s' "${!offset-1}" +} + +function update-offset() { + local file + file="$(readlink -f "${1}")" + file="${file//-/_}" + file="${file//./_}" + file="${file//\//_}" + read -r "OFFSET_${file}" <<< "${2}" +} + +function update-line-count-for-file() { + file="$(readlink -f "${1}")" + line_count_in_file="$(wc -l "${file}" | cut -d ' ' -f 1)" + update-offset "${file}" "$(( line_count_in_file + 1 ))" +} + +line="$(printf '%q' "${1}")" +line="${line//\//\\\/}" +file="$(readlink -f "${2}")" +timeout="${3-30 seconds}" +finish="$(date +%s -d "${timeout}")" + +# Wait for file to be created. +while [[ ! -f ${file} ]]; do + sleep 1s +done + +while true; do + # Check if timeout has passed. + if (( "${finish}" < "$(date +%s)" )); then + printf " +ERROR: Timed out after ${timeout} of waiting for line in file: ++- line: ${line} ++- file: ${file} + +%s\n" "$(tail "${file}")" >&2 + exit 1 + fi + + offset="$(read-offset "${file}")" + # Check if we have reached end of file. + line_count_in_file="$(wc -l "${file}" | cut -d ' ' -f 1)" + if (( offset > line_count_in_file )); then + sleep 1s + continue + fi + + line_number_found="$(sed "${offset},${line_count_in_file}!d;/${line}/!d;=" "${file}" | head -n 1)" || true + + if [[ -z "${line_number_found}" ]]; then + # not found + update-offset "${file}" "$(( line_count_in_file + 1 ))" + else + # found + update-offset "${file}" "$(( line_number_found + 1 ))" + break + fi +done From 82cd6b9dc8de2f2bb012becded6963aaf7ca914d Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Wed, 26 Feb 2020 22:29:42 +0200 Subject: [PATCH 2/3] =?UTF-8?q?[=F0=9F=90=9B=20FIX]=20test=20script:=20get?= =?UTF-8?q?=20correct=20PID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/all-in-one-test | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scripts/all-in-one-test b/scripts/all-in-one-test index 35b78fe..dc20085 100755 --- a/scripts/all-in-one-test +++ b/scripts/all-in-one-test @@ -7,7 +7,7 @@ script_path=$(dirname "$(readlink -f "${0}")") # Cleanup for t in HUP INT QUIT KILL TERM EXIT; do - trap "rm -f '${script_path}/a.log' '${script_path}/a.yaml' '${script_path}/kube-config.yaml'" "${t}" + trap "rm -f '${script_path}/a.log' '${script_path}/a.yaml' '${script_path}/kube-config.yaml' '${script_path}/pid'" "${t}" done # Create cluster using YAML provided in quick start guide of kind. @@ -25,10 +25,9 @@ done # Run estafette-gke-preemptible-killer with all the arguments provided to this # script. -"${script_path}/../estafette-gke-preemptible-killer" "${@}" --kubeconfig "${script_path}/kube-config.yaml" | tee "${script_path}/a.log" & -pid=${!} +( "${script_path}/../estafette-gke-preemptible-killer" "${@}" --kubeconfig "${script_path}/kube-config.yaml" & echo ${!} >&3 ) 3>"${script_path}/pid" | tee "${script_path}/a.log" & "${script_path}/wait-for-line-in-file" 'Sleeping for' "${script_path}/a.log" -kill ${pid} +kill $(<"${script_path}/pid") # For all the nodes report their CreationTimestamp and their # gke-preemptible-killer-state. From 74a265a9542eca48c64caf07a11083352acd8ba6 Mon Sep 17 00:00:00 2001 From: Andrei Pavel Date: Mon, 2 Mar 2020 21:41:56 +0200 Subject: [PATCH 3/3] =?UTF-8?q?[=F0=9F=90=9B=20FIX]=20test=20script:=20che?= =?UTF-8?q?ck=20if=20binary=20exists?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/all-in-one-test | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/all-in-one-test b/scripts/all-in-one-test index dc20085..068fa36 100755 --- a/scripts/all-in-one-test +++ b/scripts/all-in-one-test @@ -23,9 +23,15 @@ for n in '' {2..3}; do kubectl replace -f "${script_path}/a.yaml" done +killer="${script_path}/../estafette-gke-preemptible-killer" +if ! test -f "${killer}"; then + printf "%s doesn't exist. Build first.\\n" "${killer}" + exit 1 +fi + # Run estafette-gke-preemptible-killer with all the arguments provided to this # script. -( "${script_path}/../estafette-gke-preemptible-killer" "${@}" --kubeconfig "${script_path}/kube-config.yaml" & echo ${!} >&3 ) 3>"${script_path}/pid" | tee "${script_path}/a.log" & +( "${killer}" "${@}" --kubeconfig "${script_path}/kube-config.yaml" & echo ${!} >&3 ) 3>"${script_path}/pid" | tee "${script_path}/a.log" & "${script_path}/wait-for-line-in-file" 'Sleeping for' "${script_path}/a.log" kill $(<"${script_path}/pid")