Skip to content

Instantly share code, notes, and snippets.

@aliasmee
Created August 23, 2018 06:36
Show Gist options
  • Save aliasmee/f35b368165463d7c7074e9ab3fef85a0 to your computer and use it in GitHub Desktop.
Save aliasmee/f35b368165463d7c7074e9ab3fef85a0 to your computer and use it in GitHub Desktop.
Extract job information from k8s_jobs.txt. Generate kubernetes cronjob yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: ${jobName}
labels:
app: data-job
spec:
schedule: "${cronTime}"
concurrencyPolicy: Forbid
successfulJobsHistoryLimit: 3
failedJobsHistoryLimit: 3
jobTemplate:
spec:
template:
spec:
containers:
- name: data
image: hub.docker.io/testimage:${imageTag}
envFrom:
- configMapRef:
name: some-cm
args:
- /bin/bash
- -l
- -c
- ${jobCommand}
restartPolicy: OnFailure
0,15,30,45 * * * * JOB_NAME=Etl::Invest cd /app && some command
0,15,30,45 * * * * JOB_NAME=Etl::NewsWorker cd /app && some command
0 * * * * JOB_NAME=Takeover cd /app && some command
0 * * * * JOB_NAME=JointInves cd /app && some command
#!/usr/bin/env python3
import re
import os
import errno
from jinja2 import Environment, FileSystemLoader
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
jobSource = './job.txt'
jobPattern = "^(\S.*\*+?)\s+JOB_NAME=(\S.*?)\s(.*?)$"
templateJobFile = "./cronJobTemplate.yaml"
targetPath = "./jobsFile"
def checkPath():
if not os.path.exists(targetPath):
try:
os.makedirs(targetPath)
except OSError as exec:
if exc.errno != errno.EEXIST:
raise
def generateJob():
with open(jobSource) as fp:
for line in fp:
z = re.match(jobPattern, line)
if z:
content = printContent((z.groups()[1]).lower().replace("::","-"), z.groups()[0], 'v1.0', z.groups()[2])
jobFileName = targetPath + '/' + (z.groups()[1]).replace("::","-")
print(jobFileName)
with open(jobFileName, "w") as newFile:
newFile.write(content)
def printContent(JOB_NAME, CRON_TIME, IMAGE_TAG, COMMAND):
j2_env = Environment(loader=FileSystemLoader(THIS_DIR),
trim_blocks=True)
return j2_env.get_template(templateJobFile).render(job={'Name': JOB_NAME,'Time': CRON_TIME, 'Tag': IMAGE_TAG, 'Command': COMMAND})
if __name__ == '__main__':
checkPath()
generateJob()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment