Skip to content

Instantly share code, notes, and snippets.

@egasimus
Last active April 29, 2020 00:23
Show Gist options
  • Save egasimus/29d26bad0d104b0c03c39450be760632 to your computer and use it in GitHub Desktop.
Save egasimus/29d26bad0d104b0c03c39450be760632 to your computer and use it in GitHub Desktop.
Run Kubernetes CronJob manually, formatting output and deleting it afterwards.
#!/usr/bin/env bash
set -aeu
Bold="\e[1m"
Normal="\e[0m"
Red="\e[31m"
White="\e[39m"
pretty () {
T=`echo "$1" | jq -r ".T" 2>/dev/null`
E=`echo "$1" | jq -r ".event" 2>/dev/null`
P=`echo "$1" | jq -C "del(.T,.event)" 2>/dev/null || echo "$Red$Log$White"`
echo -e "T=$Bold$T $E$Normal $P"
}
Cronjob="$1"
Command="kubectl create job -o json --from=cronjob/$Cronjob $Cronjob-manual-`date +%s`"
echo $Command
Job=$($Command)
Name=`echo $Job | jq -r .metadata.name`
trap "kubectl delete job $Name" EXIT
echo $Name
Command="kubectl wait --for=condition=ready pod -l job-name=$Name --timeout=60s"
echo $Command
$Command || true
kubectl logs -f "job.batch/$Name" | while read -r Log; do
pretty "$Log" || echo "$Log"
done
#!/usr/bin/env bash
Jobs=`kubectl get job --no-headers -o custom-columns=":metadata.name"`
echo "$Jobs" | grep manual | while read -r Job; do
kubectl delete job $Job
done
#!/usr/bin/env bash
set -aeu
pushd `dirname $0`; trap "popd" EXIT
Arg=foo
NoWatch=true
./services/my-cron-job/build \
| ./7_substituteImage MyCronJob ./values.yaml \
&& ./5_deploy \
&& ./1_run_manual.sh $Arg
./2_killall_manual.sh
#!/usr/bin/env bash
# This script must be symlinked into a service directory in order to work
# Direct invocation does nothing
. "$(dirname $(realpath $0))/prelude.sh"
# start building value of --substitutions arg of "gcloud builds submit"
Substitutions=""
ServiceName=$(basename $(pwd))
log "Service name: $ServiceName"
Substitutions="_SERVICE_NAME=$ServiceName"
ProjectName="my-google-cloud-project"
ServiceImage="gcr.io/$ProjectName/$ServiceName"
. "$OpsDir/getVersion.sh"
TagToBuild=`getVersion`
Substitutions="$Substitutions,_TAG=$TagToBuild"
# populate variables in google cloud build config:
ServiceRoot=$(realpath --relative-to="$ProjectDir" `pwd`)
log "Service root: $ServiceRoot"
Substitutions="$Substitutions,_SERVICE_ROOT=$ServiceRoot"
. "$OpsDir/generateDockerfile.sh"
. "$OpsDir/useBuildCache.sh"
# run build
log "Substitutions: $Substitutions"
BuildCmd="gcloud builds submit"
BuildCmd="$BuildCmd --timeout=20m"
BuildCmd="$BuildCmd --config $OpsDir/default-cloudbuild.yaml"
BuildCmd="$BuildCmd --substitutions $Substitutions"
BuildCmd="$BuildCmd $ProjectDir"
log $BuildCmd
notify Building on GCB: "$ServiceImage:$TagToBuild"
$BuildCmd 1>&2
# if successful, store the result
echo "{\"image\":\"$ServiceImage\",\"tag\":\"$TagToBuild\"}" > $DotLatest
notify Built on GCB: "$ServiceImage:$TagToBuild"
echo "$ServiceImage:$TagToBuild"
#!/usr/bin/env bash
# helm has separate install and upgrade commands
# here we wrap them into a single deploy command
. "$(dirname $(realpath $0))/prelude.sh"
Name=$(basename $(pwd))
Override=
deploy () {
if [[ -d brands ]]; then
Brand=$1; shift
. "`dirname $(dirname $(pwd))`/platforms/helm/brandOverride.sh"
fi
echo "Name: $Name"
helm template --name $Name $Override . | cat -n
if [[ `helm ls "^$Name\$"` ]]; then
echo "Name $Name exists, deploying with 'helm upgrade'"
upgrade $@
else
echo "Name $Name does not exist, deploying with 'helm install'"
install $@
fi
_NoWatch=${NoWatch:-}
if [ -z $_NoWatch ]; then
watch -n0.5 helm status $Name
fi
}
install () {
Command="helm install --debug --name $Name $Override . $@"
echo -e "Command: $Command\n"
notify Helm installing $Name
$Command
notify Helm installed $Name
}
upgrade () {
Command="helm upgrade $Name $Override . $@"
echo -e "Running: $Command\n"
notify Helm upgrading $Name
$Command
notify Helm upgraded $Name
}
deploy $@
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: my-cron-job
spec:
schedule: 0 0 * * *
jobTemplate:
spec:
template:
metadata:
labels:
app: my-cron-job
spec:
restartPolicy: Never
volumes:
- name: service-account-secret
secret:
secretName: {{.Values.ServiceAccount}}
containers:
- name: my-cron-job-container
image: {{.Values.Image}}
command:
- 'node'
args:
- '--expose-gc'
- '--max-old-space-size=3000'
- 'index.js'
volumeMounts:
- { readOnly: true, name: service-account-secret, mountPath: /secrets/service_account }
env:
- { name: KEY, value: 'value' }
#!/usr/bin/env bash
Slot=$1
File=$2
Image=$3
if [ -z "$Image" ]; then Image=$(</dev/stdin); fi
echo $Slot
echo $Image
cat $File | sed -E "s|^($Slot: +).*|\1$Image|" > $File.next
mv $File.next $File
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment