KNative Service for creating kubernetes jobs
Please create the below rolebinding to give access to your ksvc to create a job
kubectl apply -f - <<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: default-rb
namespace: demo
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: admin
subjects:
- kind: ServiceAccount
name: default
namespace: demo
EOF
This knative service looks for the job spec in a environment variable named JOB_SPEC
.
Below is an example of a service specification
kubectl apply -f - <<EOF
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: sample-job-trigger-svc
namespace: demo
spec:
template:
spec:
containers:
- env:
- name: JOB_SPEC
value: "{\"Image\": \"murugappans/hw\",\"Name\": \"sample-job\"}"
image: murugappans/job-trigger:v1
EOF
"Name": "JOB_SPEC",
"Value": "{\"Image\": \"murugappans/hw\",\"Name\": \"simplepy\",\"User\": 1002, \"Cmd\": [\"python\", \"job.py\"]}"
"Name": "JOB_SPEC",
"Value": "{\"Image\": \"murugappans/hw\",\"Name\": \"simplego\",\"User\": 1002, \"EnvVariables\": [\"Name\": \"env\",\"Value\": \"prod\"}]}"
"Name": "JOB_SPEC",
"Value": "{\"Image\": \"murugappans/hw\",\"Name\": \"simplego\",\"User\": 1002, \"Secrets\": [{\"Name\": \"secretname\",\"MountPath\": \"pathtomount\"}],\"ConfigMaps\": [{\"Name\": \"cmname\",\"MountPath\": \"pathtomount\"}]}"
"Name": "JOB_SPEC",
"Value": "{\"Image\": \"murugappans/hw\",\"Name\": \"simplego\",\"User\": 1002, \"EnvFromSecretorCM\": [{\"Name\": \"secretname\",\"Type\": \"Secret\"}]}"
At run time you might want to inject some variables. Those variables can be passed as url query parameters
For example if the function url is as below. Key and Value will be mounted as environment variable in the job
http://sample-job-trigger.test.com?key=value
In some cases you might want to label your job. It can be provided in the query string as below.
http://sample-job-trigger.test.com?labels=label1=value,label2=value
To clean up old jobs, specify the history
query parameter. This param specifies the number of job logs to be preseverd.
For example if history is 0, all logs will be will be deleted, for 2 , 2 latest will be preserved.
Default value is 3
http://sample-job-trigger.test.com?history=1