Skip to content

Commit

Permalink
Raise an error when 'kind' is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
dturn committed Apr 18, 2018
1 parent 8e76f15 commit 05f10db
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
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
3 changes: 2 additions & 1 deletion lib/kubernetes-deploy/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class FatalDeploymentError < StandardError; end
class KubectlError < StandardError; end

class InvalidTemplateError < FatalDeploymentError
attr_reader :filename, :content
attr_reader :content
attr_accessor :filename
def initialize(err, filename:, content: nil)
@filename = filename
@content = content
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'", filename: nil, content: definition.to_s)
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
10 changes: 10 additions & 0 deletions test/integration/kubernetes_deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -986,4 +986,14 @@ 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'"
], in_order: true)
end
end

0 comments on commit 05f10db

Please sign in to comment.