Skip to content

Commit

Permalink
Set Job as owner of ConfigMap so they delete together
Browse files Browse the repository at this point in the history
  • Loading branch information
manning-ncsa committed Dec 11, 2023
1 parent e5da608 commit 89487ca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
1 change: 0 additions & 1 deletion jobutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2208,7 +2208,6 @@ def submit_job(params):
response['synchronous'] = True
JOBSDB.update_job_complete(config['metadata']['apiToken'], response)
else:
kubejob.create_configmap(conf)
kubejob_status, kubejob_msg = kubejob.create_job(conf)
if kubejob_status == STATUS_ERROR:
status = STATUS_ERROR
Expand Down
38 changes: 22 additions & 16 deletions kubejob.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ def delete_job(input):
logger.error(
"Exception when calling BatchV1Api->delete_namespaced_job: {}\n".format(e)
)
try:
api_response = api_v1.delete_namespaced_config_map(
name=input["cm_name"],
namespace=input["namespace"],
body=client.V1DeleteOptions(
propagation_policy="Foreground", grace_period_seconds=5
),
)
# logger.info("Config Map {} deleted".format(input["cm_name"]))
except ApiException as e:
logger.error(
"Exception when calling V1Api->delete_namespaced_configmap: {}\n".format(e)
)
# try:
# api_response = api_v1.delete_namespaced_config_map(
# name=input["cm_name"],
# namespace=input["namespace"],
# body=client.V1DeleteOptions(
# propagation_policy="Foreground", grace_period_seconds=5
# ),
# )
# # logger.info("Config Map {} deleted".format(input["cm_name"]))
# except ApiException as e:
# logger.error(
# "Exception when calling V1Api->delete_namespaced_configmap: {}\n".format(e)
# )

return

Expand All @@ -119,6 +119,7 @@ def create_job(input):
namespace=input["namespace"], body=job(input)
)
# logger.info("Job {} created".format(input["configjob"]["metadata"]["jobId"]))
create_configmap(input, job_uid=api_response.metadata.uid)
except ApiException as e:
logger.error(
"Exception when calling BatchV1Api->create_namespaced_job: {}\n".format(e)
Expand All @@ -128,7 +129,7 @@ def create_job(input):
return status, msg


def config_map(input):
def config_map(input, job_uid=''):
keyfile = "configjob.yaml"
meta = client.V1ObjectMeta(
name=input["cm_name"],
Expand All @@ -137,6 +138,11 @@ def config_map(input):
"task": input["job"],
"username": input["configjob"]["metadata"]["username"],
},
owner_references=[client.V1OwnerReference(
api_version='batch/v1', kind='Job',
name=input["configjob"]["metadata"]["jobId"],
uid=job_uid,
)],
)
body = client.V1ConfigMap(
api_version="v1",
Expand All @@ -147,10 +153,10 @@ def config_map(input):
return body


def create_configmap(input):
def create_configmap(input, job_uid=''):
try:
api_response = api_v1.create_namespaced_config_map(
namespace=input["namespace"], body=config_map(input)
namespace=input["namespace"], body=config_map(input, job_uid=job_uid)
)
# logger.info("ConfigMap {} created".format(input["cm_name"]))

Expand Down

0 comments on commit 89487ca

Please sign in to comment.