Skip to content

Commit

Permalink
prevent invalid const errors from lowercase resource kind (#413)
Browse files Browse the repository at this point in the history
* capitalize kind to prevent invalid const errors

* catch the error instead of mutating the kind

* PR feedback
  • Loading branch information
dwradcliffe committed Jan 17, 2019
1 parent ee75d48 commit a3d2f05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/kubernetes-deploy/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ def build(namespace:, context:, definition:, logger:, statsd_tags:)
statsd_tags: statsd_tags }
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
inst = new(**opts)
inst.type = definition["kind"]
inst
end

begin
if KubernetesDeploy.const_defined?(definition["kind"])
klass = KubernetesDeploy.const_get(definition["kind"])
return klass.new(**opts)
end
rescue NameError
end

inst = new(**opts)
inst.type = definition["kind"]
inst
end

def timeout
Expand Down
11 changes: 11 additions & 0 deletions test/unit/kubernetes-deploy/kubernetes_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,17 @@ def test_disappeared_is_false_if_resource_has_been_deployed_and_we_get_a_server_
refute_predicate(dummy, :disappeared?)
end

def test_lowercase_custom_resource_kind_does_not_raise
definition = { "kind" => "foobar", "metadata" => { "name" => "test" } }
KubernetesDeploy::KubernetesResource.build(
namespace: 'test',
context: 'test',
definition: definition,
logger: logger,
statsd_tags: []
)
end

private

def kubectl
Expand Down

0 comments on commit a3d2f05

Please sign in to comment.