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 11, 2023
1 parent b474ab2 commit f442cb6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# 3.4.0

- Use `prune-allowlist` instead of `prune-whitelist`. This change requires kubernetes 1.26 and higher. [#940](https://github.com/Shopify/krane/pull/940)
- Use `prune-allowlist` instead of `prune-whitelist` for 1.26+ clusters. Clusters running 1.25 or less will continue to use `--prune-whitelist`. [#940](https://github.com/Shopify/krane/pull/940)

## 3.3.0

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ Krane provides support for official upstream supported versions [Kubernetes](htt
| 1.20 | No | 2.4.9 |
| 1.21 | No | 2.4.9 |
| 1.22 | No | 3.0.1 |
| 1.23 | Yes | 3.3.0 |
| 1.24 | Yes | 3.3.0 |
| 1.25 | No | 3.3.0 |
| 1.23 | Yes | -- |
| 1.24 | Yes | -- |
| 1.25 | No | -- |
| 1.26 | Yes | -- |
| 1.27 | Yes | -- |

Expand Down
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
13 changes: 7 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,22 @@

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(:client_version).returns(Gem::Version.new("1.26"))
Krane::Kubectl.any_instance.expects(:run).with do |*args|
args.include?("--prune-allowlist=#{whitelist_kind}")
args.include?("--prune-allowlist=#{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.include?("--prune-allowlist=#{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 f442cb6

Please sign in to comment.