Skip to content

Commit

Permalink
Extract extra conditions to RunnerTaskConfigValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
dirceu committed Sep 13, 2019
1 parent e18efe8 commit 85939db
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/kubernetes-deploy/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
require 'kubernetes-deploy/statsd'
require 'kubernetes-deploy/task_config'
require 'kubernetes-deploy/task_config_validator'
require 'kubernetes-deploy/runner_task_config'
require 'kubernetes-deploy/runner_task_config_validator'

module KubernetesDeploy
MIN_KUBE_VERSION = '1.11.0'
Expand Down
23 changes: 8 additions & 15 deletions lib/kubernetes-deploy/runner_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TaskTemplateMissingError < TaskConfigurationError; end

def initialize(namespace:, context:, logger: nil, max_watch_seconds: nil)
@logger = logger || KubernetesDeploy::FormattedLogger.build(namespace, context)
@task_config = KubernetesDeploy::TaskConfig.new(context, namespace, @logger)
@task_config = KubernetesDeploy::RunnerTaskConfig.new(context, namespace, @logger)
@namespace = namespace
@context = context
@max_watch_seconds = max_watch_seconds
Expand All @@ -35,7 +35,11 @@ def run!(task_template:, entrypoint:, args:, env_vars: [], verify_result: true)
@logger.reset

@logger.phase_heading("Initializing task")

@logger.info("Validating configuration")
verify_config!(task_template, args)
@logger.info("Using namespace '#{@namespace}' in context '#{@context}'")

pod = build_pod(task_template, entrypoint, args, env_vars, verify_result)
validate_pod(pod)

Expand Down Expand Up @@ -108,25 +112,14 @@ def record_status_once(pod)
end

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 args.blank?
errors << "Args can't be nil"
end

@task_config.template = task_template
@task_config.args = args
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([task_config_validator.errors + errors].map { |err| "- #{err}" }.join("\n"))
@logger.summary.add_paragraph([task_config_validator.errors].map { |err| "- #{err}" }.join("\n"))
raise KubernetesDeploy::TaskConfigurationError
end

@logger.info("Using namespace '#{@namespace}' in context '#{@context}'")
end

def get_template(template_name)
Expand Down
6 changes: 6 additions & 0 deletions lib/kubernetes-deploy/runner_task_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
module KubernetesDeploy
class RunnerTaskConfig < TaskConfig
attr_accessor :template, :args
end
end
23 changes: 23 additions & 0 deletions lib/kubernetes-deploy/runner_task_config_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
module KubernetesDeploy
class RunnerTaskConfigValidator < TaskConfigValidator
DEFAULT_VALIDATIONS += %i(
validate_template
validate_args
).freeze

private

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

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

0 comments on commit 85939db

Please sign in to comment.