Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop deploys if ClusterResourceDiscovery's kubectl calls fail #701

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/krane/cluster_resource_discovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def prunable_resources(namespaced:)
def fetch_resources(namespaced: false)
command = %w(api-resources)
command << "--namespaced=#{namespaced}"
raw, _, st = kubectl.run(*command, output: "wide", attempts: 5,
raw, err, st = kubectl.run(*command, output: "wide", attempts: 5,
use_namespace: false)
if st.success?
rows = raw.split("\n")
Expand All @@ -59,7 +59,7 @@ def fetch_resources(namespaced: false)
resource
end
else
[]
raise FatalKubeAPIError, "Error retrieving api-resources: #{err}"
end
end

Expand All @@ -68,7 +68,7 @@ def fetch_resources(namespaced: false)
# kubectl api-versions returns a list of group/version strings e.g. autoscaling/v2beta2
# A kind may not exist in all versions of the group.
def fetch_api_versions
raw, _, st = kubectl.run("api-versions", attempts: 5, use_namespace: false)
raw, err, st = kubectl.run("api-versions", attempts: 5, use_namespace: false)
# The "core" group is represented by an empty string
versions = { "" => %w(v1) }
if st.success?
Expand All @@ -78,6 +78,8 @@ def fetch_api_versions
versions[group] ||= []
versions[group] << version
end
else
raise FatalKubeAPIError, "Error retrieving api-versions: #{err}"
end
versions
end
Expand All @@ -97,12 +99,12 @@ def version_for_kind(versions, kind)
end

def fetch_crds
raw_json, _, st = kubectl.run("get", "CustomResourceDefinition", output: "json", attempts: 5,
raw_json, err, st = kubectl.run("get", "CustomResourceDefinition", output: "json", attempts: 5,
use_namespace: false)
if st.success?
JSON.parse(raw_json)["items"]
else
[]
raise FatalKubeAPIError, "Error retrieving CustomResourceDefinition: #{err}"
end
end

Expand Down
22 changes: 22 additions & 0 deletions test/integration-serial/serial_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -558,4 +558,26 @@ def test_global_deploy_validation_catches_namespaced_cr
"#{add_unique_prefix_for_test('my-first-mail')} (#{add_unique_prefix_for_test('Mail')})",
])
end

def test_resource_discovery_stops_deploys_when_fetch_resources_kubectl_errs
failure_msg = "Stubbed failure reason"
Krane::ClusterResourceDiscovery.any_instance.expects(:fetch_resources).raises(Krane::FatalKubeAPIError, failure_msg)
assert_deploy_failure(deploy_fixtures("hello-cloud", subset: ["configmap-data.yml"]))

assert_logs_match_all([
"Result: FAILURE",
failure_msg,
], in_order: true)
end

def test_resource_discovery_stops_deploys_when_fetch_crds_kubectl_errs
failure_msg = "Stubbed failure reason"
Krane::ClusterResourceDiscovery.any_instance.expects(:crds).raises(Krane::FatalKubeAPIError, failure_msg)
assert_deploy_failure(deploy_fixtures("hello-cloud", subset: ["configmap-data.yml"]))

assert_logs_match_all([
"Result: FAILURE",
failure_msg,
], in_order: true)
end
end
5 changes: 3 additions & 2 deletions test/unit/cluster_resource_discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ class ClusterResourceDiscoveryTest < Krane::TestCase

def test_fetch_resources_failure
crd = mocked_cluster_resource_discovery(nil, success: false)
resources = crd.fetch_resources
assert_equal(resources, [])
assert_raises_message(Krane::FatalKubeAPIError, "Error retrieving api-resources:") do
crd.fetch_resources
end
end

def test_fetch_resources_not_namespaced
Expand Down