Skip to content

Commit

Permalink
Raise an error when 'kind' is missing (#280)
Browse files Browse the repository at this point in the history
* Raise an error instead of crashing when 'kind' is missing (#277)
* Update changelog
  • Loading branch information
dturn authored Apr 18, 2018
1 parent 8e76f15 commit 6eb7090
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*Features*

*Bug Fixes*
- Display a nice error instead of crashing when a YAML document is missing 'Kind'
([#280](https://github.com/Shopify/kubernetes-deploy/pull/280))

*Enhancements*

Expand Down
1 change: 1 addition & 0 deletions lib/kubernetes-deploy/deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def split_templates(filename)
yield doc
end
rescue InvalidTemplateError => e
e.filename ||= filename
record_invalid_template(err: e.message, filename: e.filename, content: e.content)
raise FatalDeploymentError, "Failed to render and parse template"
rescue Psych::SyntaxError => e
Expand Down
5 changes: 3 additions & 2 deletions lib/kubernetes-deploy/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ class FatalDeploymentError < StandardError; end
class KubectlError < StandardError; end

class InvalidTemplateError < FatalDeploymentError
attr_reader :filename, :content
def initialize(err, filename:, content: nil)
attr_reader :content
attr_accessor :filename
def initialize(err, filename: nil, content: nil)
@filename = filename
@content = content
super(err)
Expand Down
4 changes: 3 additions & 1 deletion lib/kubernetes-deploy/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'", content: definition.to_yaml)
elsif KubernetesDeploy.const_defined?(definition["kind"])
klass = KubernetesDeploy.const_get(definition["kind"])
klass.new(**opts)
else
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/invalid-resources/missing_kind.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
metadata:
name: test
data:
datapoint: value1
17 changes: 17 additions & 0 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,21 @@ def test_adds_namespace_labels_to_statsd_tags
assert_empty desired_tags - metric.tags
end
end

def test_raise_on_yaml_missing_kind
result = deploy_fixtures("invalid-resources", subset: ["missing_kind.yml"])
assert_deploy_failure(result)
assert_logs_match_all([
"Invalid template: missing_kind.yml",
"> Error message:",
"Template missing 'Kind'",
"> Template content:",
"---",
"apiVersion: v1",
"metadata:",
" name: test",
"data:",
" datapoint: value1"
], in_order: true)
end
end

0 comments on commit 6eb7090

Please sign in to comment.