diff --git a/lib/kube_deploy_tools/deploy.rb b/lib/kube_deploy_tools/deploy.rb index 2dd54f2..b9df096 100644 --- a/lib/kube_deploy_tools/deploy.rb +++ b/lib/kube_deploy_tools/deploy.rb @@ -64,10 +64,9 @@ def initialize( end def do_deploy(dry_run) - success = false Logger.reset Logger.phase_heading('Initializing deploy') - Logger.warn('Running in dry-run mode') if dry_run + Logger.warn('Running in dry-run mode') if dry_run != 'none' if !@namespace.nil? && @namespace != 'default' Logger.warn("Deploying to non-default Namespace: #{@namespace}") @@ -98,7 +97,7 @@ def do_deploy(dry_run) success end - def run(dry_run: true) + def run(dry_run: 'client') do_deploy(dry_run) end @@ -161,7 +160,7 @@ def read_resource_definition(filepath) raise FatalDeploymentError, "Template '#{filepath}' cannot be parsed" end - def kubectl_apply(resources, dry_run: true) + def kubectl_apply(resources, dry_run: 'client') resources.each do |resource| @max_retries.times do |try| args = ['apply', '-f', resource.filepath, "--dry-run=#{dry_run}"] diff --git a/lib/kube_deploy_tools/deploy/options.rb b/lib/kube_deploy_tools/deploy/options.rb index 56a7b09..74d5b9f 100644 --- a/lib/kube_deploy_tools/deploy/options.rb +++ b/lib/kube_deploy_tools/deploy/options.rb @@ -2,6 +2,9 @@ require 'kube_deploy_tools/object' +# As of kubernetes 1.23 valid dry-run options are: none, client, server. +VALID_DRY_RUN_VALUES = %w[none client server].freeze + module KubeDeployTools class Deploy::Optparser class Options @@ -21,7 +24,7 @@ class Options def initialize self.project = File.basename(`git config remote.origin.url`.chomp, '.git') self.flavor = 'default' - self.dry_run = true + self.dry_run = 'client' self.glob_files = [] end @@ -54,8 +57,19 @@ def define_options(parser) self.build_number = p end - parser.on('--dry-run DRY_RUN', TrueClass, "If true, will only dry-run apply Kubernetes manifests without sending them to the apiserver. Default is dry-run mode: #{dry_run}.") do |p| - self.dry_run = p + # As of kubernetes 1.23 valid dry-run options are: none, client, server. + # Legacy values map accordingly: true => client, false => none + parser.on('--dry-run DRY_RUN', "Will only dry-run apply Kubernetes manifests without sending them to the apiserver. Default is dry-run mode: #{dry_run}. Must be '#{VALID_DRY_RUN_VALUES}'") do |p| + legacy_mapping = { 'true' => 'client', 'false' => 'none' } + + if legacy_mapping.include?(p) then + self.dry_run = legacy_mapping[p] + Logger.warn("#{p} is no longer a supported dry-run value. Setting to value '#{self.dry_run}'.") + elsif VALID_DRY_RUN_VALUES.include?(p) + self.dry_run = p + else + raise ArgumentError, "#{p} is not a valid dry-run value. Expect one of '#{VALID_DRY_RUN_VALUES.join(', ')}'" + end end parser.on('--include INCLUDE', "Include glob pattern. Example: --include=**/* will include every file. Default is ''.") do |p| diff --git a/spec/unit/deploy_spec.rb b/spec/unit/deploy_spec.rb index 94860d0..2c32501 100644 --- a/spec/unit/deploy_spec.rb +++ b/spec/unit/deploy_spec.rb @@ -78,7 +78,7 @@ def parse(ops) expect(kubectl).to receive(:run).with('apply', '-f', be_kubernetes_resource_of_kind('Service'), any_args).ordered expect(kubectl).to receive(:run).with('apply', '-f', be_kubernetes_resource_of_kind('Deployment'), any_args).ordered - deploy.run(dry_run: false) + deploy.run(dry_run: 'none') end it "retries kubectl apply 3 times" do @@ -124,7 +124,7 @@ def parse(ops) # Ultimately deploy should fail expect { - deploy.run(dry_run: false) + deploy.run(dry_run: 'none') }.to raise_error(KubeDeployTools::FatalDeploymentError) end @@ -223,7 +223,7 @@ def parse(ops) expect(options.artifact).to match(artifact) expect(options.build_number).to match(build_number) expect(options.context).to match(CONTEXT) - expect(options.dry_run).to be(false) + expect(options.dry_run).to eq('none') from_files = 'bogus/path/' options = parse('from-files': from_files, @@ -232,4 +232,35 @@ def parse(ops) expect(options.context).to match(CONTEXT) end + it "fails with invalid dry-run value" do + expect do + parse('dry-run': 'bogus') + end.to raise_error(/Expect one of /) + end + + it "supports mapping of legacy dry-run values" do + inputs = { + artifact: artifact, + build: build_number, + context: CONTEXT, + } + inputs['dry-run'] = 'true' + expect(parse(inputs).dry_run).to eq('client') + + inputs['dry-run'] = 'false' + expect(parse(inputs).dry_run).to eq('none') + end + + it "supports additional dry-run values" do + inputs = { + artifact: artifact, + build: build_number, + context: CONTEXT, + } + VALID_DRY_RUN_VALUES.each do |x| + inputs['dry-run'] = x + expect(parse(inputs).dry_run).to eq(x), "#{x}" + end + end + end