github.com/argoproj/argo-cd/v2@v2.10.9/resource_customizations/batch/CronJob/actions/create-job/action.lua (about)

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