Skip to content

Commit

Permalink
Use TaskConfigValidator for RunnerTask
Browse files Browse the repository at this point in the history
  • Loading branch information
dirceu committed Sep 13, 2019
1 parent c5e04ec commit e18efe8
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 60 deletions.
29 changes: 7 additions & 22 deletions lib/kubernetes-deploy/runner_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run!(task_template:, entrypoint:, args:, env_vars: [], verify_result: true)
@logger.reset

@logger.phase_heading("Initializing task")
validate_configuration(task_template, args)
verify_config!(task_template, args)
pod = build_pod(task_template, entrypoint, args, env_vars, verify_result)
validate_pod(pod)

Expand Down Expand Up @@ -107,41 +107,26 @@ def record_status_once(pod)
@logger.summary.add_paragraph(warning)
end

def validate_configuration(task_template, args)
def verify_config!(task_template, args)
@logger.info("Validating configuration")
errors = []

if task_template.blank?
errors << "Task template name can't be nil"
end

if @namespace.blank?
errors << "Namespace can't be empty"
end

if args.blank?
errors << "Args can't be nil"
end

begin
kubeclient.get_namespace(@namespace) if @namespace.present?
@logger.info("Using namespace '#{@namespace}' in context '#{@context}'")

rescue Kubeclient::ResourceNotFoundError
errors << "Namespace was not found"
rescue Kubeclient::HttpError
errors << "Could not connect to kubernetes cluster"
rescue KubernetesDeploy::KubeclientBuilder::ContextMissingError
errors << "Could not connect to kubernetes cluster - context invalid"
end

unless errors.empty?
task_config_validator = TaskConfigValidator.new(@task_config, kubectl, kubeclient_builder)
unless task_config_validator.valid?
@logger.summary.add_action("Configuration invalid")
@logger.summary.add_paragraph(errors.map { |err| "- #{err}" }.join("\n"))
raise TaskConfigurationError, "Configuration invalid: #{errors.join(', ')}"
@logger.summary.add_paragraph([task_config_validator.errors + errors].map { |err| "- #{err}" }.join("\n"))
raise KubernetesDeploy::TaskConfigurationError
end

TaskConfigValidator.new(@task_config, kubectl, kubeclient_builder, only: [:validate_server_version]).valid?
@logger.info("Using namespace '#{@namespace}' in context '#{@context}'")
end

def get_template(template_name)
Expand Down
1 change: 0 additions & 1 deletion test/integration-serial/serial_task_run_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_run_without_verify_result_fails_if_pod_was_not_created
# Sketchy, but stubbing the kubeclient doesn't work (and wouldn't be concurrency-friendly)
# Finding a way to reliably trigger a create failure would be much better, if possible
mock = mock()
mock.expects(:get_namespace)
template = kubeclient.get_pod_template('hello-cloud-template-runner', @namespace)
mock.expects(:get_pod_template).returns(template)
mock.expects(:create_pod).raises(Kubeclient::HttpError.new("409", "Pod with same name exists", {}))
Expand Down
6 changes: 3 additions & 3 deletions test/integration/runner_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,15 @@ def test_run_bang_raises_exceptions_as_well_as_printing_failure
end

def test_run_fails_if_context_is_invalid
task_runner = build_task_runner(context: "missing")
task_runner = build_task_runner(context: "unknown")
assert_task_run_failure(task_runner.run(run_params))

assert_logs_match_all([
"Initializing task",
"Validating configuration",
"Result: FAILURE",
"Configuration invalid",
"- Could not connect to kubernetes cluster - context invalid",
"Context unknown missing from your kubeconfig file(s)",
], in_order: true)
end

Expand All @@ -200,7 +200,7 @@ def test_run_fails_if_namespace_is_missing
"Validating configuration",
"Result: FAILURE",
"Configuration invalid",
"- Namespace was not found",
"Could not find Namespace:",
], in_order: true)
end

Expand Down
34 changes: 0 additions & 34 deletions test/unit/kubernetes-deploy/runner_task_test.rb

This file was deleted.

0 comments on commit e18efe8

Please sign in to comment.