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

Raise an error when 'kind' is missing #280

Merged
merged 3 commits into from
Apr 18, 2018
Merged

Raise an error when 'kind' is missing #280

merged 3 commits into from
Apr 18, 2018

Conversation

dturn
Copy link
Contributor

@dturn dturn commented Apr 17, 2018

Currently we crash with a stack dump if a yaml document is missing a Kind. This PR prints a nice error instead of crashing

Fixes: #277

***************************************************************************
 Begin test: test_raise_on_yaml_missing_kind
***************************************************************************

[INFO][2018-04-17 17:24:58 -0700]	
[INFO][2018-04-17 17:24:58 -0700]	------------------------------------Phase 1: Initializing deploy------------------------------------
[INFO][2018-04-17 17:24:58 -0700]	All required parameters and files are present
[INFO][2018-04-17 17:24:58 -0700]	Context minikube found
[INFO][2018-04-17 17:24:58 -0700]	Namespace k8sdeploy-test-raise-on-yaml-missing-kind-b0469b977d430aff found
[INFO][2018-04-17 17:24:58 -0700]	Discovering templates:
[INFO][2018-04-17 17:24:58 -0700]	
[INFO][2018-04-17 17:24:58 -0700]	------------------------------------------Result: FAILURE-------------------------------------------
[FATAL][2018-04-17 17:24:58 -0700]	Failed to render and parse template
[FATAL][2018-04-17 17:24:58 -0700]	
[FATAL][2018-04-17 17:24:58 -0700]	Invalid template: missing_kind.yml
[FATAL][2018-04-17 17:24:58 -0700]	> Error message:
[FATAL][2018-04-17 17:24:58 -0700]	    Template missing 'Kind'
[FATAL][2018-04-17 17:24:58 -0700]	> Template content:
[FATAL][2018-04-17 17:24:58 -0700]	    {"apiVersion"=>"v1", "metadata"=>{"name"=>"test"}, "data"=>{"datapoint"=>"value1"}}

@dturn dturn requested review from kirs and KnVerey April 17, 2018 16:02
@@ -2,6 +2,7 @@
module KubernetesDeploy
class FatalDeploymentError < StandardError; end
class KubectlError < StandardError; end
class InvalidResourceError < StandardError; end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use InvalidTemplateError from the PR I just finally rebased and merged. (please rebase 🙏 )

@dturn
Copy link
Contributor Author

dturn commented Apr 18, 2018

Rebase completed, its a hair different as a result.

@dturn dturn requested review from klautcomputing and removed request for kirs April 18, 2018 04:53
Copy link
Contributor

@KnVerey KnVerey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just minor comments.

@@ -31,7 +31,9 @@ class << self
def build(namespace:, context:, definition:, logger:, statsd_tags:)
opts = { namespace: namespace, context: context, definition: definition, logger: logger,
statsd_tags: statsd_tags }
if KubernetesDeploy.const_defined?(definition["kind"])
if definition["kind"].blank?
raise InvalidTemplateError.new("Template missing 'Kind'", filename: nil, content: definition.to_s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have a valid case for not requiring filename now, let's make the default nil in the initializer so you can leave it off here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should to_yaml the definition instead of to_s it, for consistency with the other errors (and readability when the definition is long). Compare:

image

Versus test_invalid_yaml_fails_fast:

image

assert_logs_match_all([
"Invalid template: missing_kind.yml",
"> Error message:",
"Template missing 'Kind'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add an assertion that proves the content is printed too

CHANGELOG.md Outdated
@@ -14,6 +14,8 @@
*Bug Fixes*
- Prevent calling sleep with a negative value ([#273](https://github.com/Shopify/kubernetes-deploy/pull/273))
- Prevent no-op redeploys of bad code from hanging forever ([#262](https://github.com/Shopify/kubernetes-deploy/pull/262))
- Display a nice error instead of crashing when a YAML document is missing 'Kind'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be moved

Copy link
Contributor

@KnVerey KnVerey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎩 of the new test looks good

image

@dturn dturn merged commit 6eb7090 into master Apr 18, 2018
@dturn dturn deleted the rescue-missing-kind branch April 18, 2018 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants