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

Commit 7dfc994

Browse files
authored
Support updated options for --dry-run (none, client, server) in kubectl 1.23+; Add backwards compatibility with existing defaults (#13)
1 parent 418a3f9 commit 7dfc994

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

lib/kube_deploy_tools/deploy.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ def initialize(
6464
end
6565

6666
def do_deploy(dry_run)
67-
success = false
6867
Logger.reset
6968
Logger.phase_heading('Initializing deploy')
70-
Logger.warn('Running in dry-run mode') if dry_run
69+
Logger.warn('Running in dry-run mode') if dry_run != 'none'
7170

7271
if !@namespace.nil? && @namespace != 'default'
7372
Logger.warn("Deploying to non-default Namespace: #{@namespace}")
@@ -98,7 +97,7 @@ def do_deploy(dry_run)
9897
success
9998
end
10099

101-
def run(dry_run: true)
100+
def run(dry_run: 'client')
102101
do_deploy(dry_run)
103102
end
104103

@@ -161,7 +160,7 @@ def read_resource_definition(filepath)
161160
raise FatalDeploymentError, "Template '#{filepath}' cannot be parsed"
162161
end
163162

164-
def kubectl_apply(resources, dry_run: true)
163+
def kubectl_apply(resources, dry_run: 'client')
165164
resources.each do |resource|
166165
@max_retries.times do |try|
167166
args = ['apply', '-f', resource.filepath, "--dry-run=#{dry_run}"]

lib/kube_deploy_tools/deploy/options.rb

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require 'kube_deploy_tools/object'
44

5+
# As of kubernetes 1.23 valid dry-run options are: none, client, server.
6+
VALID_DRY_RUN_VALUES = %w[none client server].freeze
7+
58
module KubeDeployTools
69
class Deploy::Optparser
710
class Options
@@ -21,7 +24,7 @@ class Options
2124
def initialize
2225
self.project = File.basename(`git config remote.origin.url`.chomp, '.git')
2326
self.flavor = 'default'
24-
self.dry_run = true
27+
self.dry_run = 'client'
2528
self.glob_files = []
2629
end
2730

@@ -54,8 +57,19 @@ def define_options(parser)
5457
self.build_number = p
5558
end
5659

57-
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|
58-
self.dry_run = p
60+
# As of kubernetes 1.23 valid dry-run options are: none, client, server.
61+
# Legacy values map accordingly: true => client, false => none
62+
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|
63+
legacy_mapping = { 'true' => 'client', 'false' => 'none' }
64+
65+
if legacy_mapping.include?(p) then
66+
self.dry_run = legacy_mapping[p]
67+
Logger.warn("#{p} is no longer a supported dry-run value. Setting to value '#{self.dry_run}'.")
68+
elsif VALID_DRY_RUN_VALUES.include?(p)
69+
self.dry_run = p
70+
else
71+
raise ArgumentError, "#{p} is not a valid dry-run value. Expect one of '#{VALID_DRY_RUN_VALUES.join(', ')}'"
72+
end
5973
end
6074

6175
parser.on('--include INCLUDE', "Include glob pattern. Example: --include=**/* will include every file. Default is ''.") do |p|

spec/unit/deploy_spec.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def parse(ops)
7878
expect(kubectl).to receive(:run).with('apply', '-f', be_kubernetes_resource_of_kind('Service'), any_args).ordered
7979
expect(kubectl).to receive(:run).with('apply', '-f', be_kubernetes_resource_of_kind('Deployment'), any_args).ordered
8080

81-
deploy.run(dry_run: false)
81+
deploy.run(dry_run: 'none')
8282
end
8383

8484
it "retries kubectl apply 3 times" do
@@ -124,7 +124,7 @@ def parse(ops)
124124

125125
# Ultimately deploy should fail
126126
expect {
127-
deploy.run(dry_run: false)
127+
deploy.run(dry_run: 'none')
128128
}.to raise_error(KubeDeployTools::FatalDeploymentError)
129129
end
130130

@@ -223,7 +223,7 @@ def parse(ops)
223223
expect(options.artifact).to match(artifact)
224224
expect(options.build_number).to match(build_number)
225225
expect(options.context).to match(CONTEXT)
226-
expect(options.dry_run).to be(false)
226+
expect(options.dry_run).to eq('none')
227227

228228
from_files = 'bogus/path/'
229229
options = parse('from-files': from_files,
@@ -232,4 +232,35 @@ def parse(ops)
232232
expect(options.context).to match(CONTEXT)
233233
end
234234

235+
it "fails with invalid dry-run value" do
236+
expect do
237+
parse('dry-run': 'bogus')
238+
end.to raise_error(/Expect one of /)
239+
end
240+
241+
it "supports mapping of legacy dry-run values" do
242+
inputs = {
243+
artifact: artifact,
244+
build: build_number,
245+
context: CONTEXT,
246+
}
247+
inputs['dry-run'] = 'true'
248+
expect(parse(inputs).dry_run).to eq('client')
249+
250+
inputs['dry-run'] = 'false'
251+
expect(parse(inputs).dry_run).to eq('none')
252+
end
253+
254+
it "supports additional dry-run values" do
255+
inputs = {
256+
artifact: artifact,
257+
build: build_number,
258+
context: CONTEXT,
259+
}
260+
VALID_DRY_RUN_VALUES.each do |x|
261+
inputs['dry-run'] = x
262+
expect(parse(inputs).dry_run).to eq(x), "#{x}"
263+
end
264+
end
265+
235266
end

0 commit comments

Comments
 (0)