From c7ce3829b0b7316a8816024bcd4705f8feb529b2 Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Wed, 19 Feb 2020 10:25:52 -0800 Subject: [PATCH 1/2] Stop deploys if ClusterResourceDiscovery's kubectl calls fail --- lib/krane/cluster_resource_discovery.rb | 12 +++++++----- test/integration-serial/serial_deploy_test.rb | 11 +++++++++++ test/unit/cluster_resource_discovery_test.rb | 5 +++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/krane/cluster_resource_discovery.rb b/lib/krane/cluster_resource_discovery.rb index d18d8697c..2b6133496 100644 --- a/lib/krane/cluster_resource_discovery.rb +++ b/lib/krane/cluster_resource_discovery.rb @@ -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") @@ -59,7 +59,7 @@ def fetch_resources(namespaced: false) resource end else - [] + raise FatalKubeAPIError, "Error retrieving api-resources: #{err}" end end @@ -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? @@ -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 @@ -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 diff --git a/test/integration-serial/serial_deploy_test.rb b/test/integration-serial/serial_deploy_test.rb index 7ec11407e..0f127c32d 100644 --- a/test/integration-serial/serial_deploy_test.rb +++ b/test/integration-serial/serial_deploy_test.rb @@ -558,4 +558,15 @@ 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_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 end diff --git a/test/unit/cluster_resource_discovery_test.rb b/test/unit/cluster_resource_discovery_test.rb index 542a5aa4b..c7322e441 100644 --- a/test/unit/cluster_resource_discovery_test.rb +++ b/test/unit/cluster_resource_discovery_test.rb @@ -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 From 6214e3a0c4e042901b55f7305f69c51c7cd22479 Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Wed, 19 Feb 2020 14:05:24 -0800 Subject: [PATCH 2/2] More tests better --- test/integration-serial/serial_deploy_test.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/integration-serial/serial_deploy_test.rb b/test/integration-serial/serial_deploy_test.rb index 0f127c32d..15b541ac4 100644 --- a/test/integration-serial/serial_deploy_test.rb +++ b/test/integration-serial/serial_deploy_test.rb @@ -559,7 +559,7 @@ def test_global_deploy_validation_catches_namespaced_cr ]) end - def test_resource_discovery_stops_deploys_when_kubectl_errs + 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"])) @@ -569,4 +569,15 @@ def test_resource_discovery_stops_deploys_when_kubectl_errs 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