Skip to content

Commit

Permalink
explicitly rescue ResourceNotFound error
Browse files Browse the repository at this point in the history
  • Loading branch information
timothysmith0609 committed Feb 15, 2019
1 parent 511d27f commit 341369e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 28 deletions.
18 changes: 5 additions & 13 deletions lib/kubernetes-deploy/restart_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,8 @@ def build_watchables(kubeclient_resources, started)
def verify_namespace
kubeclient.get_namespace(@namespace)
@logger.info("Namespace #{@namespace} found in context #{@context}")
rescue Kubeclient::HttpError => error
if error.error_code == 404
raise NamespaceNotFoundError.new(@namespace, @context)
else
raise
end
rescue Kubeclient::ResourceNotFoundError
raise NamespaceNotFoundError.new(@namespace, @context)
end

def patch_deployment_with_restart(record)
Expand All @@ -133,7 +129,7 @@ def patch_kubeclient_deployments(deployments)
begin
patch_deployment_with_restart(record)
@logger.info("Triggered `#{record.metadata.name}` restart")
rescue Kubeclient::ResourceNotFoundError, Kubeclient::HttpError => e
rescue Kubeclient::HttpError => e
raise RestartAPIError.new(record.metadata.name, e.message)
end
end
Expand All @@ -144,12 +140,8 @@ def fetch_deployments(list)
record = nil
begin
record = v1beta1_kubeclient.get_deployment(name, @namespace)
rescue Kubeclient::HttpError => error
if error.error_code == 404
raise FatalRestartError, "Deployment `#{name}` not found in namespace `#{@namespace}`"
else
raise
end
rescue Kubeclient::ResourceNotFoundError
raise FatalRestartError, "Deployment `#{name}` not found in namespace `#{@namespace}`"
end
record
end
Expand Down
21 changes: 10 additions & 11 deletions lib/kubernetes-deploy/runner_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ def validate_configuration(task_template, args)
begin
kubeclient.get_namespace(@namespace) if @namespace.present?
@logger.info("Using namespace '#{@namespace}' in context '#{@context}'")
rescue Kubeclient::HttpError => e
msg = e.error_code == 404 ? "Namespace was not found" : "Could not connect to kubernetes cluster"
errors << msg

rescue Kubeclient::ResourceNotFoundError
errors << "Namespace was not found"
rescue Kubeclient::HttpError
errors << "Could not connect to kubernetes cluster"
end

unless errors.empty?
Expand All @@ -140,16 +142,13 @@ def validate_configuration(task_template, args)

def get_template(template_name)
pod_template = kubeclient.get_pod_template(template_name, @namespace)

pod_template.template
rescue Kubeclient::ResourceNotFoundError
msg = "Pod template `#{template_name}` not found in namespace `#{@namespace}`, context `#{@context}`"
@logger.summary.add_paragraph(msg)
raise TaskTemplateMissingError, msg
rescue Kubeclient::HttpError => error
if error.error_code == 404
msg = "Pod template `#{template_name}` not found in namespace `#{@namespace}`, context `#{@context}`"
@logger.summary.add_paragraph(msg)
raise TaskTemplateMissingError, msg
else
raise FatalKubeAPIError, "Error retrieving pod template: #{error.class.name}: #{error.message}"
end
raise FatalKubeAPIError, "Error retrieving pod template: #{error.class.name}: #{error.message}"
end

def build_pod_definition(base_template)
Expand Down
3 changes: 1 addition & 2 deletions test/helpers/fixture_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def refute_resource_exists(type, name, beta: false)
if res.metadata.deletionTimestamp.blank?
flunk("#{type} #{name} unexpectedly existed and is not being deleted.")
end
rescue Kubeclient::HttpError => e
raise unless e.to_s.include?("not found")
rescue Kubeclient::ResourceNotFoundError
end

def assert_pod_status(pod_name, status, count = 1)
Expand Down
3 changes: 1 addition & 2 deletions test/helpers/test_provisioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def claim_namespace(test_name)

def delete_namespace(namespace)
kubeclient.delete_namespace(namespace) if namespace.present?
rescue Kubeclient::HttpError => e
raise unless e.to_s.include?("not found")
rescue Kubeclient::ResourceNotFoundError
end

private
Expand Down

0 comments on commit 341369e

Please sign in to comment.