Skip to content

Commit

Permalink
Make transparently compatible with pre 1.26 client
Browse files Browse the repository at this point in the history
  • Loading branch information
timothysmith0609 committed Dec 8, 2023
1 parent 1209f24 commit 54677a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 9 additions & 0 deletions lib/krane/kubectl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Kubectl
DEFAULT_TIMEOUT = 15
MAX_RETRY_DELAY = 16
SERVER_DRY_RUN_MIN_VERSION = "1.13"
ALLOW_LIST_MIN_VERSION = "1.26"

class ResourceNotFoundError < StandardError; end

Expand Down Expand Up @@ -112,6 +113,14 @@ def dry_run_flag
"--dry-run=server"
end

def allowlist_flag
if client_version >= Gem::Version.new(ALLOW_LIST_MIN_VERSION)
"--prune-allowlist"
else
"--prune-whitelist"
end
end

private

def build_command_from_options(args, use_namespace, use_context, output)
Expand Down
3 changes: 2 additions & 1 deletion lib/krane/resource_deployer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ def apply_all(resources, prune, dry_run: false)
else
command.push("--all")
end
@prune_allowlist.each { |type| command.push("--prune-allowlist=#{type}") }
allow_list_flag = kubectl.allowlist_flag
@prune_allowlist.each { |type| command.push("#{allow_list_flag}=#{type}") }
end

command.push(kubectl.dry_run_flag) if dry_run
Expand Down
12 changes: 6 additions & 6 deletions test/unit/krane/resource_deployer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

class ResourceDeployerTest < Krane::TestCase
def test_deploy_prune_builds_whitelist
whitelist_kind = "fake_kind"
allowlist_kind = "fake_kind"
resource = build_mock_resource
Krane::Kubectl.any_instance.expects(:run).with do |*args|
args.include?("--prune-allowlist=#{whitelist_kind}")
args.any? { |arg| arg == "--prune-allowlist=#{allowlist_kind}" || arg == "--prune-whitelist=#{allowlist_kind}"}
end.returns(["", "", stub(success?: true)])
resource_deployer(kubectl_times: 0, prune_allowlist: [whitelist_kind]).deploy!([resource], false, true)
resource_deployer(kubectl_times: 0, prune_allowlist: [allowlist_kind]).deploy!([resource], false, true)
end

def test_deploy_no_prune_doesnt_prune
whitelist_kind = "fake_kind"
allowlist_kind = "fake_kind"
resource = build_mock_resource
Krane::Kubectl.any_instance.expects(:run).with do |*args|
!args.include?("--prune-allowlist=#{whitelist_kind}")
args.any? { |arg| arg == "--prune-allowlist=#{allowlist_kind}" || arg == "--prune-whitelist=#{allowlist_kind}"}
end.returns(["", "", stub(success?: true)])
resource_deployer(kubectl_times: 0, prune_allowlist: [whitelist_kind]).deploy!([resource], false, false)
resource_deployer(kubectl_times: 0, prune_allowlist: [allowlist_kind]).deploy!([resource], false, false)
end

def test_deploy_verify_false_message
Expand Down

0 comments on commit 54677a0

Please sign in to comment.