Kubernetes Launch Script Functionality Improvement
Changed the way this script handles being called upon so that it can
support more than just the build-setup.sh. It will now call a
templated YAML file. This template will be filled out and launched via
kubectl. A case check will be used to set the default variables that are
needed by the specific invoker. To satisfy these changes updated the
build-setup.sh, added a templates folder, and added two templates for
the build-setup.sh script.
Change-Id: I9ef7426e638cae52677a8855a91e76c5e4788fb1
Signed-off-by: Alanny Lopez <alanny.lopez@ibm.com>
diff --git a/build-setup.sh b/build-setup.sh
index f79e19b..5d80957 100755
--- a/build-setup.sh
+++ b/build-setup.sh
@@ -320,11 +320,11 @@
elif [[ "${launch}" == "job" || "${launch}" == "pod" ]]; then
# Source and run the helper script to launch the pod or job
- . kubernetes/kubernetes-launch.sh
+ . ./kubernetes/kubernetes-launch.sh OpenBMC-build true true
else
echo "Launch Parameter is invalid"
fi
# Timestamp for build
-echo "Build completed, $(date)"
\ No newline at end of file
+echo "Build completed, $(date)"
diff --git a/kubernetes/Templates/OpenBMC-build-job.yaml b/kubernetes/Templates/OpenBMC-build-job.yaml
new file mode 100644
index 0000000..8588cac
--- /dev/null
+++ b/kubernetes/Templates/OpenBMC-build-job.yaml
@@ -0,0 +1,53 @@
+apiVersion: batch/v1
+kind: Job
+metadata:
+ name: openbmc${BUILD_ID}-${target}
+ namespace: ${namespace}
+ labels:
+ app: openbmc
+ stage: build
+spec:
+ template:
+ metadata:
+ name: ${podname}
+ labels:
+ target: ${target}
+ spec:
+ nodeSelector:
+ worker: \"true\"
+ arch: ${ARCH}
+ volumes:
+ - name: home
+ persistentVolumeClaim:
+ claimName: ${hclaim}
+ - name: sscdir
+ persistentVolumeClaim:
+ claimName: ${sclaim}
+ - name: obmccache
+ persistentVolumeClaim:
+ claimName: ${oclaim}
+ restartPolicy: Never
+ hostNetwork: True
+ containers:
+ - image: ${imgname}
+ name: builder
+ command: [\"${WORKSPACE}/build.sh\"]
+ workingDir: ${HOME}
+ env:
+ - name: WORKSPACE
+ value: ${WORKSPACE}
+ - name: obmcdir
+ value: ${obmcdir}
+ securityContext:
+ capabilities:
+ add:
+ - SYS_ADMIN
+ volumeMounts:
+ - name: home
+ mountPath: ${HOME}
+ - name: sscdir
+ mountPath: ${sscdir}
+ - name: obmccache
+ mountPath: ${obmcext}
+ imagePullSecrets:
+ - name: ${imgplsec}
diff --git a/kubernetes/Templates/OpenBMC-build-pod.yaml b/kubernetes/Templates/OpenBMC-build-pod.yaml
new file mode 100644
index 0000000..7ecc03a
--- /dev/null
+++ b/kubernetes/Templates/OpenBMC-build-pod.yaml
@@ -0,0 +1,43 @@
+apiVersion: v1
+kind: Pod
+metadata:
+ name: ${podname}
+ namespace: ${namespace}
+spec:
+ nodeSelector:
+ worker: \"true\"
+ arch: ${ARCH}
+ volumes:
+ - name: home
+ persistentVolumeClaim:
+ claimName: ${hclaim}
+ - name: sscdir
+ persistentVolumeClaim:
+ claimName: ${sclaim}
+ - name: obmccache
+ persistentVolumeClaim:
+ claimName: ${oclaim}
+ hostNetwork: True
+ containers:
+ - image: ${imgname}
+ name: builder
+ command: [\"${WORKSPACE}/build.sh\"]
+ workingDir: ${HOME}
+ env:
+ - name: WORKSPACE
+ value: ${WORKSPACE}
+ - name: obmcdir
+ value: ${obmcdir}
+ securityContext:
+ capabilities:
+ add:
+ - SYS_ADMIN
+ volumeMounts:
+ - name: home
+ mountPath: ${HOME}
+ - name: sscdir
+ mountPath: ${sscdir}
+ - name: obmccache
+ mountPath: ${obmcext}
+ imagePullSecrets:
+ - name: ${imgplsec}
diff --git a/kubernetes/kubernetes-launch.sh b/kubernetes/kubernetes-launch.sh
old mode 100644
new mode 100755
index c6ea4bd..c403e12
--- a/kubernetes/kubernetes-launch.sh
+++ b/kubernetes/kubernetes-launch.sh
@@ -2,8 +2,8 @@
###############################################################################
#
# Script used to assist in launching Kubernetes jobs/pods. Expects to be used
-# as an supplemental script to the build-setup.sh script as such will use some
-# of the variables it expects to carry over from that script.
+# as an supplemental script to the scripts that want to launch their containers
+# on a Kubernetes cluster.
#
###############################################################################
#
@@ -21,9 +21,6 @@
###############################################################################
# Variables used to create Kubernetes Job:
# namespace = the namespace to be used within the Kubernetes cluster
-# hclaim = name of the Jenkins slave home PVC on Kubernetes cluster
-# sclaim = name of the shared state cache PVC on Kubernetes cluster
-# oclaim = name of OpenBMC cache repo PVC on the Kubernetes cluster
# registry = the registry to use to pull and push images
# imgplsec = the image pull secret used to access registry if needed
# timeout = the amount of time in seconds that the build will wait for
@@ -33,19 +30,51 @@
# podname = the name of the pod, will be needed to trace down the logs
#
###############################################################################
+# Variables that act as script options:
+# invoker = name of what this script is being called by or for, used to
+# determine the template to use for YAML file
+# log = set to true to make the script tail the container logs of pod
+# purge = set to true delete the created object once script completes
+# launch = used to determine the template for YAML file, Usually brought
+# in by sourcing from another script but can be declared
+#
+###############################################################################
# Kubernetes Variables
namespace=${namespace:-openbmc}
-hclaim=${hclaim:-jenkins}
-sclaim=${sclaim:-shared-state-cache}
-oclaim=${oclaim:-openbmc-reference-repo}
imgrepo=${imgrepo:-master.cfc:8500/openbmc/}
imgplsec=${imgplsec:-regkey}
timeout=${timeout:-60}
-# Give the Docker image and the pod a name
-imgname=${imgname:-${imgrepo}${distro}:${imgtag}-${ARCH}}
-podname=${podname:-openbmc${BUILD_ID}-${target}-builder}
+# Options which decide script behavior
+invoker=${invoker:-${1}}
+log=${log:-${2}}
+purge=${purge:-${3}}
+launch=${launch:-${4}}
+
+# Set the variables for the specific invoker to fill in the YAML template
+# Other variables in the template not declared here are expected to be declared by invoker.
+case ${invoker} in
+ OpenBMC-build)
+ hclaim=${hclaim:-jenkins}
+ sclaim=${sclaim:-shared-state-cache}
+ oclaim=${oclaim:-openbmc-reference-repo}
+ imgname=${imgname:-${imgrepo}${distro}:${imgtag}-${ARCH}}
+ podname=${podname:-openbmc${BUILD_ID}-${target}-builder}
+ ;;
+ QEMU-build)
+ ;;
+ QEMU-launch)
+ ;;
+ XCAT-launch)
+ ;;
+ generic)
+ ;;
+ *)
+ exit 1
+ ;;
+esac
+
# Build the Docker image, using the Dockerfile carried from build-setup.sh
docker build -t ${imgname} - <<< "${Dockerfile}"
@@ -53,129 +82,26 @@
# Push the image that was built to the image repository
docker push ${imgname}
-if [[ "${launch}" == "pod" ]]; then
- yamlfile=$(cat << EOF
- apiVersion: v1
- kind: Pod
- metadata:
- name: ${podname}
- namespace: ${namespace}
- spec:
- nodeSelector:
- worker: "true"
- arch: ${ARCH}
- volumes:
- - name: home
- persistentVolumeClaim:
- claimName: ${hclaim}
- - name: sscdir
- persistentVolumeClaim:
- claimName: ${sclaim}
- - name: obmccache
- persistentVolumeClaim:
- claimName: ${oclaim}
- hostNetwork: True
- containers:
- - image: ${imgname}
- name: builder
- command: ["${WORKSPACE}/build.sh"]
- workingDir: ${HOME}
- env:
- - name: WORKSPACE
- value: ${WORKSPACE}
- - name: obmcdir
- value: ${obmcdir}
- securityContext:
- capabilities:
- add:
- - SYS_ADMIN
- volumeMounts:
- - name: home
- mountPath: ${HOME}
- - name: sscdir
- mountPath: ${sscdir}
- - name: obmccache
- mountPath: ${ocache}
- imagePullSecrets:
- - name: ${imgplsec}
-EOF
-)
-
-elif [[ "${launch}" == "job" ]]; then
- yamlfile=$(cat << EOF
- apiVersion: batch/v1
- kind: Job
- metadata:
- name: openbmc${BUILD_ID}-${target}
- namespace: ${namespace}
- labels:
- app: openbmc
- stage: build
- spec:
- template:
- metadata:
- name: ${podname}
- labels:
- target: ${target}
- spec:
- nodeSelector:
- worker: "true"
- arch: ${ARCH}
- volumes:
- - name: home
- persistentVolumeClaim:
- claimName: ${hclaim}
- - name: sscdir
- persistentVolumeClaim:
- claimName: ${sclaim}
- - name: obmccache
- persistentVolumeClaim:
- claimName: ${oclaim}
- restartPolicy: Never
- hostNetwork: True
- containers:
- - image: ${imgname}
- name: builder
- command: ["${WORKSPACE}/build.sh"]
- workingDir: ${HOME}
- env:
- - name: WORKSPACE
- value: ${WORKSPACE}
- - name: obmcdir
- value: ${obmcdir}
- securityContext:
- capabilities:
- add:
- - SYS_ADMIN
- volumeMounts:
- - name: home
- mountPath: ${HOME}
- - name: sscdir
- mountPath: ${sscdir}
- - name: obmccache
- mountPath: ${ocache}
- imagePullSecrets:
- - name: ${imgplsec}
-EOF
-)
-fi
-
+yamlfile=$(eval "echo \"$(<./Templates/${invoker}-${launch}.yaml)\"" )
kubectl create -f - <<< "${yamlfile}"
-# Wait for Pod to be running before tailing log file
-while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ]; do
- if [ ${timeout} -lt 0 ];then
- kubectl delete -f - <<< "${yamlfile}"
- echo "Timeout Occured: Job failed to start running in time"
- exit 1
- else
- sleep 1
- let timeout-=1
- fi
-done
-
# Once pod is running track logs
-kubectl logs -f ${podname} -n ${namespace}
+if [[ "${log}" == true ]]; then
+ # Wait for Pod to be running
+ while [ -z "$(kubectl describe pod ${podname} -n ${namespace} | grep Status: | grep Running)" ]; do
+ if [ ${timeout} -lt 0 ];then
+ kubectl delete -f - <<< "${yamlfile}"
+ echo "Timeout Occured: Job failed to start running in time"
+ exit 1
+ else
+ sleep 1
+ let timeout-=1
+ fi
+ done
+ kubectl logs -f ${podname} -n ${namespace}
+fi
-# When job is completed wipe the job
-kubectl delete -f - <<< "${yamlfile}"
\ No newline at end of file
+# Delete the object if purge is true
+if [[ "${purge}" == true ]]; then
+ kubectl delete -f - <<< "${yamlfile}"
+fi