From 85939db14790a390d37c22479a522d30d08f065c Mon Sep 17 00:00:00 2001 From: Dirceu Tiegs Date: Fri, 13 Sep 2019 10:18:15 -0400 Subject: [PATCH] Extract extra conditions to RunnerTaskConfigValidator --- lib/kubernetes-deploy/common.rb | 2 ++ lib/kubernetes-deploy/runner_task.rb | 23 +++++++------------ lib/kubernetes-deploy/runner_task_config.rb | 6 +++++ .../runner_task_config_validator.rb | 23 +++++++++++++++++++ 4 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 lib/kubernetes-deploy/runner_task_config.rb create mode 100644 lib/kubernetes-deploy/runner_task_config_validator.rb diff --git a/lib/kubernetes-deploy/common.rb b/lib/kubernetes-deploy/common.rb index 917c007ed..deed20a39 100644 --- a/lib/kubernetes-deploy/common.rb +++ b/lib/kubernetes-deploy/common.rb @@ -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' diff --git a/lib/kubernetes-deploy/runner_task.rb b/lib/kubernetes-deploy/runner_task.rb index c16d1325f..d3624ffd0 100644 --- a/lib/kubernetes-deploy/runner_task.rb +++ b/lib/kubernetes-deploy/runner_task.rb @@ -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 @@ -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) @@ -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) diff --git a/lib/kubernetes-deploy/runner_task_config.rb b/lib/kubernetes-deploy/runner_task_config.rb new file mode 100644 index 000000000..887c6666d --- /dev/null +++ b/lib/kubernetes-deploy/runner_task_config.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true +module KubernetesDeploy + class RunnerTaskConfig < TaskConfig + attr_accessor :template, :args + end +end diff --git a/lib/kubernetes-deploy/runner_task_config_validator.rb b/lib/kubernetes-deploy/runner_task_config_validator.rb new file mode 100644 index 000000000..1472cda75 --- /dev/null +++ b/lib/kubernetes-deploy/runner_task_config_validator.rb @@ -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