| Here's a pipeline snippet that checks for this behavior. I left out the auth things, because they're specific to the k8s cluster. We use a default service account in some cases and a kubectl config mounted from a k8s secret in other cases, for reference.
def kubectlYaml = """\
apiVersion: v1
kind: Pod
spec:
containers:
- name: kubectl
image: bitnami/kubectl:1.12
stdin: true
tty: true
command:
- '/bin/sh'
args:
- -c
- cat
env:
- name: HUB_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: HUB_UID
valueFrom:
fieldRef:
fieldPath: metadata.uid
- name: HUB_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
securityContext:
runAsUser: 1000
fsGroup: 1000
"""
def name = "test-${UUID.randomUUID()}"
podTemplate(
cloud: '..',
name: name,
label: name,
podRetention: never(),
idleMinutes: 0,
yaml: kubectlYaml,
) {
node(name) {
container('kubectl') {
sh('''\
deploy=$(
kubectl create -o name -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deploy
labels:
app: test-deploy
ownerReferences:
- apiVersion: v1
kind: Pod
controller: true
blockOwnerDeletion: true
name: $HUB_NAME
uid: $HUB_UID
spec:
replicas: 2
selector:
matchLabels:
app: test-deploy
template:
metadata:
labels:
app: test-deploy
spec:
containers:
- name: ubuntu
image: ubuntu
command:
- '/bin/sh'
args:
- -c
- 'sleep infinity'
EOF
)
kubectl rollout status "$deploy"
'''.stripIndent())
}
}
}
def newName = "test-${UUID.randomUUID()}"
podTemplate(
cloud: '..',
name: newName,
label: newName,
podRetention: never(),
idleMinutes: 0,
yaml: kubectlYaml,
) {
node(newName) {
container('kubectl') {
try {
sh('sleep 60 && kubectl get deploy/test-deploy')
error("Expected the deployment to be deleted, but it wasn't.")
} catch(e) {
echo("The deployment was deleted as expected.")
} finally {
try {
sh('kubectl delete deploy/test-deploy')
} catch (e) {
// Ignore
}
}
}
}
}
|