Skip to content

Commit

Permalink
Created a function to get/parse cronjob data
Browse files Browse the repository at this point in the history
  • Loading branch information
DilaraOflaz committed Dec 16, 2024
1 parent fbfffd0 commit 9dee2af
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
30 changes: 14 additions & 16 deletions kubetools/kubernetes/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,18 @@ def _get_containers_data(
return all_containers, all_container_ports


def _get_cronjob_data(cronjob):
cronjob_spec = {}

cronjob_spec['schedule'] = cronjob['schedule']
cronjob_spec['concurrency_policy'] = cronjob['concurrency_policy']
cronjob_spec['batch-api-version'] = cronjob.get('batch-api-version')
cronjob_spec['successfulJobsHistoryLimit'] = cronjob.get('successfulJobsHistoryLimit')
cronjob_spec['failedJobsHistoryLimit'] = cronjob.get('failedJobsHistoryLimit')

def _additional_cronjob_params(cronjob, cronjob_spec):
for param, value in cronjob.items():
if param != 'containers':
cronjob_spec[param] = value
return cronjob_spec

def _required_cronjob_params(cronjob, cronjob_spec):
required_params = ['schedule', 'concurrency_policy']
for param in required_params:
cronjob_spec[param] = cronjob.pop(param)
return cronjob, cronjob_spec


def _get_replicas(deployment, default=1):
replicas = default
Expand Down Expand Up @@ -346,9 +347,9 @@ def generate_kubernetes_configs_for_project(
cronjob.get('labels', {}),
)

node_selector_labels = cronjob.get('nodeSelector', None)
service_account_name = cronjob.get('serviceAccountName', None)
secrets = cronjob.get('secrets', None)
cronjob_spec = {}
cronjob, cronjob_spec = _required_cronjob_params(cronjob, cronjob_spec)
cronjob_spec = _additional_cronjob_params(cronjob, cronjob_spec)

containers, container_ports = _get_containers_data(
cronjob['containers'],
Expand All @@ -365,14 +366,11 @@ def generate_kubernetes_configs_for_project(
cronjobs.append(make_cronjob_config(
config,
name,
_get_cronjob_data(cronjob),
cronjob_spec,
containers,
labels=cronjob_labels,
annotations=app_annotations,
envvars=envvars,
node_selector_labels=node_selector_labels,
service_account_name=service_account_name,
secrets=secrets,
))

return services, deployments, jobs, cronjobs
28 changes: 16 additions & 12 deletions kubetools/kubernetes/config/cronjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
def make_cronjob_config(
config,
cronjob_name,
cronjob_data,
cronjob_spec,
containers,
labels=None,
annotations=None,
envvars=None,
node_selector_labels=None,
service_account_name=None,
secrets=None,
):
'''
Builds a Kubernetes cronjob configuration dict.
Expand All @@ -24,6 +21,10 @@ def make_cronjob_config(
labels = labels or {}
annotations = annotations or {}

node_selector_labels = cronjob_spec.get('nodeSelector', None)
service_account_name = cronjob_spec.get('serviceAccountName', None)
secrets = cronjob_spec.get('secrets', None)

# Build our container list
kubernetes_containers = []
for container_name, container in containers.items():
Expand Down Expand Up @@ -76,9 +77,9 @@ def make_cronjob_config(
'annotations': annotations,
},
'spec': {
'schedule': cronjob_data['schedule'],
'schedule': cronjob_spec['schedule'],
'startingDeadlineSeconds': 10,
'concurrencyPolicy': cronjob_data['concurrency_policy'],
'concurrencyPolicy': cronjob_spec['concurrency_policy'],
'jobTemplate': {
'spec': {
'template': {
Expand All @@ -93,13 +94,16 @@ def make_cronjob_config(
},
},
}
if cronjob_data['batch-api-version']:
apiVersion = cronjob_spec.get('batch-api-version', None)
if apiVersion is not None:
# Only set here if user has specified it in the config
cronjob['apiVersion'] = cronjob_data['batch-api-version']
cronjob['apiVersion'] = apiVersion

if cronjob_data['successfulJobsHistoryLimit'] is not None:
cronjob['spec']['successfulJobsHistoryLimit'] = cronjob_data['successfulJobsHistoryLimit']
if cronjob_data['failedJobsHistoryLimit'] is not None:
cronjob['spec']['failedJobsHistoryLimit'] = cronjob_data['failedJobsHistoryLimit']
successfulJobsHistoryLimit = cronjob_spec.get('successfulJobsHistoryLimit', None)
if successfulJobsHistoryLimit is not None:
cronjob['spec']['successfulJobsHistoryLimit'] = successfulJobsHistoryLimit
failedJobsHistoryLimit = cronjob_spec.get('failedJobsHistoryLimit', None)
if failedJobsHistoryLimit is not None:
cronjob['spec']['failedJobsHistoryLimit'] = failedJobsHistoryLimit

return cronjob

0 comments on commit 9dee2af

Please sign in to comment.