Skip to content

Instantly share code, notes, and snippets.

@ang3lkar
Last active March 27, 2019 15:30
Show Gist options
  • Save ang3lkar/3c8474d71e5f822be4453697b1901758 to your computer and use it in GitHub Desktop.
Save ang3lkar/3c8474d71e5f822be4453697b1901758 to your computer and use it in GitHub Desktop.
Export our Heroku scheduler jobs into the k8s equivalent configuration
// Save page as HTML and add JQuery for convenience
arr = []
$('.item_well').each((index, element) => {
arr[index] = {}
let dyno = $(element).find('div.dyno_size option:selected').text()
let job = $(element).find('input.input--code')[0].value
let frequency = $(element).find('div.frequency option:selected').text()
let nextRun = null;
let runAt = null;
if (frequency === 'Daily' && job !== "") {
let nextRun = $(element).find('.next-run span.timestamp')[0]
if (nextRun) {
runAt = nextRun.innerText.split(' ')[2]
}
}
console.log([dyno, job, frequency, runAt])
arr[index].dyno = dyno
arr[index].job = job
arr[index].frequency = frequency
arr[index].runAt = runAt
})
JSON.stringify(arr)
console.log(arr)
require 'json'
require 'yaml'
file = File.read('source.json')
data = JSON.parse(file)
# SOURCE
# {
# "dyno"=>"Standard 1X",
# "job"=>"rake resume_caches:cleanup",
# "frequency"=>"Daily",
# "runAt"=>"4:00"
# }
# TARGET
# - name: "rake.resume.caches.cleanup"
# process: worker
# command: ["rake resume_caches:cleanup"]
# args: [""]
# resources:
# requests:
# cpu: 30m
# memory: 128Mi
# limits:
# cpu: 60m
# memory: 256Mi
# schedule: "* 4 * * *"
# concurrencyPolicy: "Forbid"
RESOURCES = {
"Standard 1X" => {
"cpu" => "30m",
"memory" => "128Mi",
"cpuLimit" => "60m",
"memoryLimit" => "256Mi"
},
"Standard 2X" => {
"cpu" => "60m",
"memory" => "256Mi",
"cpuLimit" => "120m",
"memoryLimit" => "512Mi"
}
}
convertToCron = lambda { |frequency, runAt|
case frequency
when "Every 10 minutes"
"*/10 * * * *"
when "Hourly"
"0 * * * *"
when "Daily"
hour = runAt.split(":").first
minutes = runAt.split(":").last
if minutes != "00"
"*/#{minutes} #{hour} * * *"
else
"0 #{hour} * * *"
end
else
""
end
}
generateJobName = lambda { |str|
# By convention, the names of Kubernetes resources should be up to maximum length of 253 characters
# and consist of lower case alphanumeric characters, -, and ., but certain resources have more
# specific restrictions.
str.downcase.gsub(/'|"/, '').
gsub(/\s|\/|,|_|:|\[|\]/, '.').
gsub(/\.{2,}/, '.').
gsub(/\.$/, '')[0...253]
}
k8s_data = data.map do |job|
{
name: generateJobName.call(job["job"]),
command: job["job"],
args: [],
resources: {
requests: {
cpu: RESOURCES[job["dyno"]]["cpu"],
memory: RESOURCES[job["dyno"]]["memory"]
},
limits: {
cpu: RESOURCES[job["dyno"]]["cpuLimit"],
memory: RESOURCES[job["dyno"]]["memoryLimit"]
}
},
schedule: convertToCron.call(job["frequency"], job["runAt"]),
concurrencyPolicy: "Forbid"
}
end
puts k8s_data.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment