Skip to content

Instantly share code, notes, and snippets.

@stealthybox
Last active April 25, 2024 21:15
Show Gist options
  • Save stealthybox/412374e1da8e6c35d0331e6b82bad5c1 to your computer and use it in GitHub Desktop.
Save stealthybox/412374e1da8e6c35d0331e6b82bad5c1 to your computer and use it in GitHub Desktop.
PreStop Sleep lifecycle patches /w the v1.29 Kubernetes Pod API changes
#@ load("@ytt:overlay", "overlay")
#@ def pod_lifecycle_patch():
spec:
#@overlay/match-child-defaults expects="0+"
containers:
#@overlay/match by=overlay.all
- lifecycle:
#! This "sleep" preStop hook delays the
#! Pod shutdown until after our Ingress
#! controller notices our Pod's Endpoint
#! has been deleted
preStop:
sleep:
seconds: 20
#@ end
#@ def sleepOptOut():
metadata:
labels:
lifecycle-sleep: opt-out
#@ end
#@ def podAPI():
apiVersion: v1
kind: Pod
#@ end
---
#@ def hasPodTemplate():
- apiVersion: apps/v1
kind: Deployment
- apiVersion: apps/v1
kind: ReplicaSet
- apiVersion: apps/v1
kind: StatefulSet
- apiVersion: apps/v1
kind: DaemonSet
- apiVersion: v1
kind: ReplicationController
- apiVersion: batch/v1
kind: Job
#@ end
---
#@ def hasJobTemplate():
- apiVersion: batch/v1
kind: CronJob
#@ end
#@ notSleepOptOut = overlay.not_op(overlay.subset(sleepOptOut()))
#@overlay/match by=overlay.and_op( overlay.subset(podAPI()), notSleepOptOut ), expects="0+"
--- #@ pod_lifecycle_patch()
#@ for/end api in hasPodTemplate():
#@overlay/match by=overlay.and_op( overlay.subset(api), notSleepOptOut ), expects="0+"
---
spec:
template: #@ pod_lifecycle_patch()
#@ for/end api in hasJobTemplate():
#@overlay/match by=overlay.and_op( overlay.subset(api), notSleepOptOut ), expects="0+"
---
spec:
jobTemplate:
spec:
template: #@ pod_lifecycle_patch()
---
apiVersion: v1
kind: Pod
metadata:
name: bare-pod
labels:
test: a
spec:
containers:
- name: workload
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
test: a
spec:
template:
spec:
containers:
- name: nginx
- name: sidecar
---
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: app
labels:
test: a
lifecycle-sleep: opt-out
spec:
template:
spec:
containers:
- name: app
- name: sidecar
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: db
spec:
template:
spec:
containers:
- name: db
- name: indexer
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: lb
spec:
template:
spec:
containers:
- name: proxy
---
apiVersion: v1
kind: ReplicationController
metadata:
name: legacy-app
spec:
template:
spec:
containers:
- name: legacy-app
- name: cache
- name: db
---
apiVersion: batch/v1
kind: Job
metadata:
name: processor
spec:
template:
spec:
containers:
- name: app
- name: sidecar
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: task-with-server
labels:
other: test
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: app
- name: sidecar
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: special-task
labels:
lifecycle-sleep: opt-out
test: hi
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: app
- name: sidecar
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd "${SCRIPT_DIR}"
trap exit INT
while true; do ls | entr -cc -dd bash -c 'ytt -f . | bat -P -lyaml' ; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment