Skip to content

Commit

Permalink
refractor the code a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Jimil Desai <[email protected]>
  • Loading branch information
jimil749 committed Nov 30, 2023
1 parent 38c2390 commit 7e07cbe
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from kubernetes import client, config

config.load_incluster_config()
secretAPI = client.CoreV1Api()
batchAPI = client.BatchV1Api()
core_api = client.CoreV1Api()
batch_api = client.BatchV1Api()

# get buckets from the cronjob specs
buckets = os.environ.get("BUCKET_LIST")
Expand All @@ -26,7 +26,7 @@
def cleanup_configmap(config_map):
# print(f"cleaning up config map - {config_map}")
try:
secretAPI.delete_namespaced_config_map(
core_api.delete_namespaced_config_map(
name=config_map,
namespace=os.environ["NAMESPACE"],
body=client.V1DeleteOptions(),
Expand All @@ -48,7 +48,7 @@ def backup(bucket_name):
page_num = 0

while True:
jobs = batchAPI.list_namespaced_job(
jobs = batch_api.list_namespaced_job(
namespace=os.environ["NAMESPACE"],
).items

Expand All @@ -69,22 +69,22 @@ def backup(bucket_name):
print(f"{jobs.metadata.name} is completed - deleting")
label_selector = f"job-name={jobs.metadata.name}"
try:
batchAPI.delete_namespaced_job(
batch_api.delete_namespaced_job(
name=jobs.metadata.name, namespace=os.environ["NAMESPACE"]
)
except client.rest.ApiException as e:
if e.status == 404:
print(f"{jobs.metadata.name} deleted")
try:
pod = secretAPI.list_namespaced_pod(
pod = core_api.list_namespaced_pod(
namespace=os.environ["NAMESPACE"], label_selector=label_selector
).items
print(f"pod length: {len(pod)}")
for p in pod:
print(f"{p.metadata.name} pod is being deleted")
# delete the pod
for p in pod:
secretAPI.delete_namespaced_pod(
core_api.delete_namespaced_pod(
name=p.metadata.name, namespace=p.metadata.namespace
)
print(f"{p.metadata.name} is deleted")
Expand Down Expand Up @@ -115,7 +115,7 @@ def backup(bucket_name):
config_map_data = {os.path.basename(file_path): open(file_path).read()}
metadata = client.V1ObjectMeta(name=config_map)
config_map = client.V1ConfigMap(data=config_map_data, metadata=metadata)
secretAPI.create_namespaced_config_map(
core_api.create_namespaced_config_map(
namespace=os.environ["NAMESPACE"], body=config_map
)

Expand Down Expand Up @@ -211,7 +211,7 @@ def backup(bucket_name):
)

# get cronjob uid
cronjob = batchAPI.read_namespaced_cron_job(
cronjob = batch_api.read_namespaced_cron_job(
name=os.environ["PARENT_NAME"], namespace=os.environ["NAMESPACE"]
)
cronjob_uid = cronjob.metadata.uid
Expand All @@ -238,7 +238,7 @@ def backup(bucket_name):
),
)
# wait until the job is created
batchAPI.create_namespaced_job(namespace=os.environ["NAMESPACE"], body=job)
batch_api.create_namespaced_job(namespace=os.environ["NAMESPACE"], body=job)
time.sleep(2)
page_num += 1

Expand Down

0 comments on commit 7e07cbe

Please sign in to comment.