Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Support updated options for --dry-run (none, client, server) in kubec…
Browse files Browse the repository at this point in the history
…tl 1.23+; Add backwards compatibility with existing defaults (#13)
  • Loading branch information
trcado committed Jan 7, 2022
1 parent 418a3f9 commit 7dfc994
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
7 changes: 3 additions & 4 deletions lib/kube_deploy_tools/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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}"]
Expand Down
20 changes: 17 additions & 3 deletions lib/kube_deploy_tools/deploy/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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|
Expand Down
37 changes: 34 additions & 3 deletions spec/unit/deploy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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

0 comments on commit 7dfc994

Please sign in to comment.