Skip to content

Commit

Permalink
Switch namespaced deploy to pruning blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
dturn committed Nov 7, 2019
1 parent 55171af commit 635c039
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 38 deletions.
40 changes: 19 additions & 21 deletions lib/krane/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ def crds
end
end

def global_resource_kinds
@globals ||= fetch_resources(only_globals: true).map { |g| g["kind"] }
end

def prunable_resources
api_versions = fetch_api_versions
fetch_resources.map do |resource|
Expand All @@ -29,25 +25,11 @@ def prunable_resources
end.compact
end

private

def fetch_api_versions
raw, _, st = kubectl.run("api-versions", attempts: 5, use_namespace: false)
if st.success?
rows = raw.split("\n")
rows.each_with_object({}) do |group_version, hash|
group, version = group_version.split("/")
hash[group] ||= []
hash[group] << version
end
else
{}
end
end

def fetch_resources(only_globals: false)
def fetch_resources(only_globals: false, only_namespaced: false)
command = %w(api-resources)
command << "--namespaced=false" if only_globals
command << "--namespaced=true" if only_namespaced
raise "only_globals and only_namespaced are exclusive" if only_globals && only_namespaced
raw, _, st = kubectl.run(*command, output: "wide", attempts: 5,
use_namespace: false)
if st.success?
Expand All @@ -68,6 +50,22 @@ def fetch_resources(only_globals: false)
end
end

private

def fetch_api_versions
raw, _, st = kubectl.run("api-versions", attempts: 5, use_namespace: false)
if st.success?
rows = raw.split("\n")
rows.each_with_object({}) do |group_version, hash|
group, version = group_version.split("/")
hash[group] ||= []
hash[group] << version
end
else
{}
end
end

def fetch_crds
raw_json, _, st = kubectl.run("get", "CustomResourceDefinition", output: "json", attempts: 5,
use_namespace: false)
Expand Down
7 changes: 7 additions & 0 deletions lib/krane/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@ def initialize(**args)
raise "Use Krane::DeployGlobalTask to deploy global resources" if args[:allow_globals]
super(args.merge(allow_globals: false))
end

def prune_whitelist
black_list = %w(batch/v1beta1/Job)
cluster_resource_discoverer.prunable_resources.select do |gvk|
@task_config.namespaced_kinds.any? { |g| gvk.include?(g) } && black_list.none? { |b| gvk.include?(b) }
end
end
end
end
9 changes: 8 additions & 1 deletion lib/krane/task_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ def initialize(context, namespace, logger = nil)
def global_kinds
@global_kinds ||= begin
cluster_resource_discoverer = ClusterResourceDiscovery.new(task_config: self)
cluster_resource_discoverer.global_resource_kinds
cluster_resource_discoverer.fetch_resources(only_globals: true).map { |g| g["kind"] }
end
end

def namespaced_kinds
@namespaced_kinds ||= begin
cluster_resource_discoverer = ClusterResourceDiscovery.new(task_config: self)
cluster_resource_discoverer.fetch_resources(only_namespaced: true).map { |g| g["kind"] }
end
end
end
Expand Down
13 changes: 7 additions & 6 deletions test/unit/cluster_resource_discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
require 'test_helper'

class ClusterResourceDiscoveryTest < Krane::TestCase
def test_global_resource_kinds_failure
def test_fetch_resources_failure
crd = mocked_cluster_resource_discovery(nil, success: false)
kinds = crd.global_resource_kinds
assert_equal(kinds, [])
resources = crd.fetch_resources
assert_equal(resources, [])
end

def test_global_resource_kinds_success
def test_fetch_resources_success
crd = mocked_cluster_resource_discovery(full_response)
kinds = crd.global_resource_kinds
assert_equal(kinds.length, full_response.split("\n").length - 1)
resources = crd.fetch_resources
assert_equal(resources.length, full_response.split("\n").length - 1)
kinds = resources.map { |r| r["kind"] }
%w(MutatingWebhookConfiguration ComponentStatus CustomResourceDefinition).each do |kind|
assert_includes(kinds, kind)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@
class HorizontalPodAutoscalerTest < Krane::TestCase
include ResourceCacheTestHelper

# We can't get integration coverage for HPA right now because the metrics server just isn't reliable enough on our CI
def test_hpa_is_whitelisted_for_pruning
Krane::Kubectl.any_instance.expects("run")
.with("get", "CustomResourceDefinition", output: "json", attempts: 5, use_namespace: false)
.returns(['{ "items": [] }', "", SystemExit.new(0)])
task = Krane::DeployTask.new(namespace: 'test', context: KubeclientHelper::TEST_CONTEXT,
current_sha: 'foo', template_paths: [''], logger: logger)
assert(task.prune_whitelist.one? { |whitelisted_type| whitelisted_type.include?("HorizontalPodAutoscaler") })
end

def test_hpa_succeeds_when_scaling_is_active
conditions = [{
"lastTransitionTime" => 5.seconds.ago,
Expand Down

0 comments on commit 635c039

Please sign in to comment.