Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for cp4na issue 264: dynamic value population for readyCheck #45

Merged
merged 1 commit into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions kubedriver/kegd/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def __build_strategy_execution(self, kegd_strategy, operation_name, kegd_files,
deploy_task_group = self.__build_deploy_task_group(compose_script, kegd_files, render_context)
task_groups.append(deploy_task_group)
if compose_script.ready_check != None:
ready_check_task = self.__build_ready_check_task(compose_script, kegd_files)
ready_check_task = self.__build_ready_check_task(compose_script, kegd_files, render_context)
if compose_script.output_extraction != None:
output_extraction_task = self.__build_output_extraction_task(compose_script, kegd_files)
run_cleanup = False
Expand Down Expand Up @@ -177,11 +177,13 @@ def __build_deploy_task_group(self, compose_script, kegd_files, render_context):
task_group.deploy_tasks.append(deploy_task)
return task_group

def __build_ready_check_task(self, compose_script, kegd_files):
ready_script_name = compose_script.ready_check.script
def __build_ready_check_task(self, compose_script, kegd_files, render_context):
unrendered_ready_script_name = compose_script.ready_check.script
ready_script_name = self.templating.render(unrendered_ready_script_name, render_context)
file_path = kegd_files.get_script_file(ready_script_name)
with open(file_path, 'r') as f:
ready_script_content = f.read()
unrendered_ready_script_content = f.read()
ready_script_content = self.templating.render(unrendered_ready_script_content, render_context)
retry_settings = compose_script.ready_check.retry_settings
if retry_settings == None:
retry_settings = RetrySettings()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
compose:
- name: Create
deploy:
- objects:
file: simple.yaml
immediateCleanupOn: Failure
checkReady:
script: "{{ system_properties.resourceName }}-check-ready.py"
maxAttempts: 100
timeoutSeconds: 0.01
intervalSeconds: 0.1


Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ system_properties.resource_subdomain }}-a
data: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def checkReady(keg, props, resultBuilder, log, *args, **kwargs):
return resultBuilder.notReady()
44 changes: 44 additions & 0 deletions tests/unit/kegd/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,50 @@ def test_immediate_cleanup_on_retry_attempts_exceeded(self):
# Confirm cleanup of object took place
self.api_ctl.delete_object.assert_called_once_with('v1', 'ConfigMap', 'just-testing-123-a', namespace='default')

def test_immediate_cleanup_with_templated_ready(self):
render_context = generate_base_render_context()
keg_name = render_context['system_properties']['resourceName']
kegd_files = get_kegd_files('immediate-cleanup-with-templated-ready-script')
kegd_strategy = parse_strategy(kegd_files.get_strategy_file())
job = self.manager.build_process_strategy_job(
keg_name=keg_name,
kegd_strategy=kegd_strategy,
operation_name='Create',
kegd_files=kegd_files,
render_context=render_context
)

# Configure Mocks
resource_subdomain = render_context['system_properties']['resource_subdomain']
mock_obj = MagicMock()
mock_obj.to_dict.return_value = {
'apiVersion': 'v1',
'kind': 'ConfigMap',
'metadata': {
'name': f'{resource_subdomain}-a'
},
'data': {}
}
self.api_ctl.safe_read_object.return_value = (True, mock_obj)

# Run
finished = self.processor.handle_process_strategy_job(job)
# Not Ready
self.assertFalse(finished)

# Wait for timeout
time.sleep(0.2)

# Run again
finished = self.processor.handle_process_strategy_job(job)
self.assertTrue(finished)

self.assertTrue(len(self.kegd_persister.update.call_args_list)>0)
before_end_calls = self.kegd_persister.update.call_args_list[-3]
self.assertEqual(before_end_calls[0][1].phase, 'Immediate cleanup on failure')

# Confirm cleanup of object took place
self.api_ctl.delete_object.assert_called_once_with('v1', 'ConfigMap', 'just-testing-123-a', namespace='default')

def test_deploy_object(self):
render_context = generate_base_render_context()
Expand Down