Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 49 additions & 17 deletions bash/aliases
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,73 @@ alias klu='kubectl config get-users| grep -v NAME'
#shellcheck disable=SC2142
alias kln='k get ns -o name |awk -F/ "{print \$2}"'
alias kg='k get'
alias kl='k logs'
alias ke='k exec'

function ks {
local pod=$1
command=(kubectl exec -it "$pod")
local -a cmd=(kubectl exec -it "$pod")
if [ -n "$2" ]; then
local container=$1
shift
command+=(-c "$container")
local container=$2
cmd+=(-c "$container")
fi
command+=(-- bash)
"${command[@]}"
cmd+=(-- bash)
"${cmd[@]}"
}

function kss {
local pod=$1
command=(kubectl exec -it "$pod")
local -a cmd=(kubectl exec -it "$pod")
if [ -n "$2" ]; then
local container=$1
shift
command+=(-c "$container")
local container=$2
cmd+=(-c "$container")
fi
command+=(-- sh)
"${command[@]}"
cmd+=(-- sh)
"${cmd[@]}"
}

function update_eksctl {
local arch
case "$(uname -m)" in
x86_64) arch=amd64 ;;
aarch64 | arm64) arch=arm64 ;;
*)
echo "update_eksctl: unsupported architecture '$(uname -m)'" >&2
return 1
;;
esac
curl --silent --location \
"https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" |
"https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_${arch}.tar.gz" |
tar xz -C /tmp
mkdir -p "$HOME/.local/bin"
mv /tmp/eksctl "$HOME/.local/bin"
}

function update_helm {
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
# Pinned version: bump manually after reviewing release notes.
# Avoids piping a network-fetched master-branch script straight to bash.
local helm_version=v3.16.4
local arch
case "$(uname -m)" in
x86_64) arch=amd64 ;;
aarch64 | arm64) arch=arm64 ;;
*)
echo "update_helm: unsupported architecture '$(uname -m)'" >&2
return 1
;;
esac
local os
os=$(uname -s | tr '[:upper:]' '[:lower:]')
local tmpdir
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' RETURN
if ! curl -fsSL -o "$tmpdir/helm.tgz" \
"https://get.helm.sh/helm-${helm_version}-${os}-${arch}.tar.gz"; then
echo "update_helm: download failed" >&2
return 1
fi
tar xz -C "$tmpdir" -f "$tmpdir/helm.tgz"
mkdir -p "$HOME/.local/bin"
mv "$tmpdir/${os}-${arch}/helm" "$HOME/.local/bin/helm"
}

function update_pip3_packages {
Expand Down Expand Up @@ -107,9 +137,11 @@ function ua {
function get_latest_btp_branch {
for d in *; do
echo "Check $d"
if [ -d "$d/.git" ];then
if [ -d "$d/.git" ]; then
cd "$d" || continue
for b in $(git branch -la|grep origin/btp-releases|sort|tail); do
# tail -n 1 to pick only the latest sorted branch; bare 'tail' defaults
# to -n 10 and would check out 10 branches sequentially.
for b in $(git branch -la | grep origin/btp-releases | sort | tail -n 1); do
branch=${b##remotes/origin/}
echo "Set $d to branch $branch"
git checkout "$branch" "$@"
Expand Down
11 changes: 6 additions & 5 deletions bash/fetch-all-pod-logs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ source "$(dirname "${BASH_SOURCE[0]}")/includer.sh"
options::standard
options::description "Fetch the logs for the containers into local files"
options::add -o l -d "label of pod selector" -a -m -e selector
options::add -o c -d "container whose logs to dump" -a -m -e container
options::add -o c -d "container whose logs to dump (defaults to all)" -a -e container
options::parse "$@"
shift $((OPTIND - 1))

# shellcheck disable=SC2154 # selector/container exported by options::add -e
get_pods_cmd=(k8s::ctl get pods -l "$selector" -o json)
log::info "Fetching logs of pods for selector $selector container: ${container:?}"
log::info "Fetching logs of pods for selector $selector container: ${container:-all}"
for pod in $("${get_pods_cmd[@]}" |
$(commands::use jq) -r '.items[].metadata.name'); do
log_cmd=(k8s::ctl logs "$pod" "--tail=10000" --timestamps)
suffix=${container:-all}
if [ -n "$container" ]; then
log::info "Fetching $container logs of pod $pod to $pod.$container.out"
log::info "Fetching $container logs of pod $pod to $pod.$suffix.out"
log_cmd+=(-c "$container")
else
log::info "Fetching all logs of pod $pod to $pod.$container.out"
log::info "Fetching all logs of pod $pod to $pod.$suffix.out"
log_cmd+=(--all-containers "--max-log-requests=100")
fi
"${log_cmd[@]}" | sed 's/\x1b\[[0-9;]*m//g' >"$pod.$container.out"
"${log_cmd[@]}" | sed 's/\x1b\[[0-9;]*m//g' >"$pod.$suffix.out"
done
26 changes: 20 additions & 6 deletions bash/kind-test-environment
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ options::add -o c -d "command: create, delete" -a -m -e COMMAND
options::add -o t -d "namespaces to add" -a -f ::add_ns
options::add -o d -d "address of a remote docker daemon" -a -e DOCKER_HOST
options::add -o D -d "Use DinD(Docker in Docker)" -x USE_DIND
options::add -o P -d "Use the specified port fo::add_ingress_providerr the DinD daemon" -a -e DIND_PORT
options::add -o P -d "Use the specified port for the DinD daemon" -a -e DIND_PORT
options::parse "$@"

CLUSTER_NAME=${CLUSTER_NAME:-test}
Expand All @@ -71,20 +71,34 @@ function ::is_dind_running {
[ "$is_running" = "true" ]
}

function ::wait_for_dind {
# Poll the daemon up to DIND_READY_TIMEOUT seconds (default 60).
local timeout=${DIND_READY_TIMEOUT:-60}
local waited=0
while [ "$waited" -lt "$timeout" ]; do
if docker -H "tcp://localhost:$DOCKER_PORT" info >/dev/null 2>&1; then
return 0
fi
sleep 2
waited=$((waited + 2))
done
log::error "Timed out after ${timeout}s waiting for DinD on tcp://localhost:$DOCKER_PORT"
return 1
}

function ::use_dind {
local name="$CLUSTER_NAME-dind"
if [ "$USE_DIND" = "true" ]; then
if ::is_dind_running "$name"; then
log::info "Using pre-existing DinD container $name"
export DOCKER_HOST=tcp://localhost:2375
export DOCKER_HOST=tcp://localhost:$DOCKER_PORT
else
log::info "Starting DinD container $name"
export DOCKER_PORT=2375
docker run -d --name "$name" --privileged -p $DOCKER_PORT:2375 -p 6443:6443 -p 8080:80 -p 8443:443 \
log::info "Starting DinD container $name on port $DOCKER_PORT"
docker run -d --name "$name" --privileged -p "$DOCKER_PORT:2375" -p 6443:6443 -p 8080:80 -p 8443:443 \
-e DOCKER_TLS_CERTDIR="" \
docker:dind --tls=false >/dev/null
export DOCKER_HOST=tcp://localhost:$DOCKER_PORT
sleep 30
::wait_for_dind || exit 1
fi
fi
}
Expand Down
52 changes: 26 additions & 26 deletions bash/plantuml-from-k8s
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ source "$(dirname "${BASH_SOURCE[0]}")/includer.sh"
@include k8s

function start() {
local cluster=$1
echo @startuml "$ns"
local cluster=${1:?}
echo @startuml "$cluster"
cat <<EOF
' Azure
!define AzurePuml https://raw.githubusercontent.com/RicardoNiepel/Azure-PlantUML/release/2-1/dist
Expand Down Expand Up @@ -88,7 +88,7 @@ function getChildren() {
local pumlTypeParent=$5
local pumlTypeChild=$6
local extra=$7
select=$(kubectl describe -n "$ns" \
select=$(k8s::ctl describe -n "$ns" \
"$parentType" "$res" | grep "^Selector:" | awk '{print $NF}')
echo \'"$ns" "$parentType" "$res" Selector "$childType" "$select"
if [ "$select" = '<none>' ]; then
Expand All @@ -112,7 +112,7 @@ EOF
}

function getNamespaces() {
kubectl get ns -o name | awk -F/ '{print $2}' | grep -v "^kube-\|openebs"
k8s::ctl get ns -o name | awk -F/ '{print $2}' | grep -v "^kube-\|openebs"
}

function namespace() {
Expand All @@ -121,38 +121,38 @@ function namespace() {
cat <<EOF
Namespace_Boundary(ns_${clean_ns},"$ns") {
EOF
for item in $(getItems "$ns" ingress); do
item Ing "$ns" "$item"
for entry in $(getItems "$ns" ingress); do
item Ing "$ns" "$entry"
done
for item in $(getItems "$ns" svc); do
item Svc "$ns" "$item"
for entry in $(getItems "$ns" svc); do
item Svc "$ns" "$entry"
done
for item in $(getItems "$ns" ds); do
item Ds "$ns" "$item"
for entry in $(getItems "$ns" ds); do
item Ds "$ns" "$entry"
done
for item in $(getItems "$ns" sts); do
item Sts "$ns" "$item"
for entry in $(getItems "$ns" sts); do
item Sts "$ns" "$entry"
done
for item in $(getItems "$ns" deployment); do
item Deploy "$ns" "$item"
for entry in $(getItems "$ns" deployment); do
item Deploy "$ns" "$entry"
done
for item in $(getItems "$ns" pod); do
item Pod "$ns" "$item"
for entry in $(getItems "$ns" pod); do
item Pod "$ns" "$entry"
done
for item in $(getRs "$ns" replicaset); do
item Rs "$ns" "$item"
for entry in $(getRs "$ns" replicaset); do
item Rs "$ns" "$entry"
done
for item in $(getItems "$ns" job); do
item Job "$ns" "$item"
for entry in $(getItems "$ns" job); do
item Job "$ns" "$entry"
done
for item in $(getItems "$ns" cronjob); do
item Cronjob "$ns" "$item"
for entry in $(getItems "$ns" cronjob); do
item Cronjob "$ns" "$entry"
done
# for item in $(getItems "$ns" cm); do
# item Cm "$ns" "$item"
# for entry in $(getItems "$ns" cm); do
# item Cm "$ns" "$entry"
# done
# for item in $(getItems "$ns" secret|grep -v "token\|helm"); do
# item Secret "$ns" "$item"
# for entry in $(getItems "$ns" secret|grep -v "token\|helm"); do
# item Secret "$ns" "$entry"
# done
cat <<EOF
}
Expand Down
9 changes: 5 additions & 4 deletions bash/trust-server
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ HOSTNAME=$(echo "$FQDN" | awk -F. '{print $1}')
DOMAIN=${FQDN/#${HOSTNAME}./}

CERT_GROUP="local"
if [ -z "$DOMAIN" ]; then
if [ -n "$DOMAIN" ]; then
# shellcheck disable=2034
CERT_GROUP="$DOMAIN"
fi

TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT INT TERM

cd "${TMPDIR}" || error::exit "Cannot cd to $TMPDIR"

Expand All @@ -55,7 +56,7 @@ function ::sudo {
}

log::info "Fetching certificates for $FQDN:$PORT"
openssl::cmd s_client -showcerts -verify 5 \
::openssl s_client -showcerts -verify 5 \
-connect "$FQDN:$PORT" </dev/null 2>/dev/null |
awk '/BEGIN/,/END/{ if(/BEGIN/){a++}; out="cert"a".crt"; print >out}'

Expand Down Expand Up @@ -85,10 +86,10 @@ function add-certs-ubuntu() {
if [ "$cert" = "${_FQDN}.pem" ]; then
continue
fi
INFO "Copying $cert to /etc/ssl/certs"
log::info "Copying $cert to /etc/ssl/certs"
::sudo cp "$cert" /etc/ssl/certs
done
INFO "Updating CA-Trust"
log::info "Updating CA-Trust"
::sudo update-ca-certificates
}

Expand Down
Loading