From 4217811e35fd99c4c6dbc7c3e6347c132bfba11f Mon Sep 17 00:00:00 2001 From: "Dustin C. Hatch" Date: Sat, 11 Oct 2025 11:57:25 -0500 Subject: [PATCH] kubeRolloutRestart: Add generic restart function As its name implies, the `kubeRestartDeployment` function would only restart pods managed by a Deployment. In order to restart pods managed by other controllers, such as StatefulSets or DaemonSets, I've created a new function, `kubeRolloutRestart`. To preserve backward compatibility, the `kubeRestartDeployment` function delegates to this new function. --- vars/kubeRestartDeployment.groovy | 34 ++++----------------------- vars/kubeRolloutRestart.groovy | 39 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 vars/kubeRolloutRestart.groovy diff --git a/vars/kubeRestartDeployment.groovy b/vars/kubeRestartDeployment.groovy index 2f4c417..b4975df 100644 --- a/vars/kubeRestartDeployment.groovy +++ b/vars/kubeRestartDeployment.groovy @@ -1,34 +1,10 @@ -@groovy.transform.Field -def POD_YAML = '''\ -spec: - containers: - - name: jnlp - volumeMounts: - - name: kubectl - mountPath: /bin/kubectl - readOnly: true - volumes: - - name: kubectl - hostPath: - path: /usr/bin/kubectl - type: File -''' - def call(args) { def namespace = args?.namespace def name = args?.name - if (name == null) { - name = env.JOB_NAME.split('/')[1] - } - - if (namespace == null) { - namespace = name - } - - podTemplate(yaml: POD_YAML) { - node(POD_LABEL) { - sh "kubectl rollout restart deployment -n ${namespace} ${name}" - } - } + kubeRolloutRestart( + kind: 'deployment', + namespace: namespace, + name: name, + ) } diff --git a/vars/kubeRolloutRestart.groovy b/vars/kubeRolloutRestart.groovy new file mode 100644 index 0000000..c5e5210 --- /dev/null +++ b/vars/kubeRolloutRestart.groovy @@ -0,0 +1,39 @@ +@groovy.transform.Field +def POD_YAML = '''\ +spec: + containers: + - name: jnlp + volumeMounts: + - name: kubectl + mountPath: /bin/kubectl + readOnly: true + volumes: + - name: kubectl + hostPath: + path: /usr/bin/kubectl + type: File +''' + +def call(args) { + def kind = args?.kind + def namespace = args?.namespace + def name = args?.name + + if (kind == null) { + kind = 'deployment' + } + + if (name == null) { + name = env.JOB_NAME.split('/')[1] + } + + if (namespace == null) { + namespace = name + } + + podTemplate(yaml: POD_YAML) { + node(POD_LABEL) { + sh "kubectl rollout restart ${kind} -n ${namespace} ${name}" + } + } +}