-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
action.lua
58 lines (50 loc) · 1.83 KB
/
action.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
local os = require("os")
-- This action constructs a Job resource from a CronJob resource, to enable creating a CronJob instance on demand.
-- It returns an array with a single member - a table with the operation to perform (create) and the Job resource.
-- It mimics the output of "kubectl create job --from=<CRON_JOB_NAME>" command, declaratively.
-- Deep-copying an object is a ChatGPT generated code.
-- Since empty tables are treated as empty arrays, the resulting k8s resource might be invalid (arrays instead of maps).
-- So empty tables are not cloned to the target object.
function deepCopy(object)
local lookup_table = {}
local function _copy(obj)
if type(obj) ~= "table" then
return obj
elseif lookup_table[obj] then
return lookup_table[obj]
elseif next(obj) == nil then
return nil
else
local new_table = {}
lookup_table[obj] = new_table
for key, value in pairs(obj) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(obj))
end
end
return _copy(object)
end
job = {}
job.apiVersion = "batch/v1"
job.kind = "Job"
job.metadata = {}
job.metadata.name = obj.metadata.name .. "-" ..os.date("!%Y%m%d%H%M")
job.metadata.namespace = obj.metadata.namespace
ownerRef = {}
ownerRef.apiVersion = obj.apiVersion
ownerRef.kind = obj.kind
ownerRef.name = obj.metadata.name
ownerRef.uid = obj.metadata.uid
job.metadata.ownerReferences = {}
job.metadata.ownerReferences[1] = ownerRef
job.spec = {}
job.spec.suspend = false
job.spec.template = {}
job.spec.template.spec = deepCopy(obj.spec.jobTemplate.spec.template.spec)
impactedResource = {}
impactedResource.operation = "create"
impactedResource.resource = job
result = {}
result[1] = impactedResource
return result