Skip to content

Commit

Permalink
Do a deep merge on bindings rather than a shallow
Browse files Browse the repository at this point in the history
  • Loading branch information
n1koo committed Feb 22, 2019
1 parent 6a0dd66 commit 774aecd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
6 changes: 3 additions & 3 deletions exe/kubernetes-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ verbose_log_prefix = false
max_watch_seconds = nil

ARGV.options do |opts|
binds = []
opts.on("--bindings=BINDINGS", "Expose additional variables to ERB templates " \
"(format: k1=v1,k2=v2, JSON string or file (JSON or YAML) path prefixed by '@')") do |binds|
bindings.merge!(KubernetesDeploy::BindingsParser.parse(binds))
end
"(format: k1=v1,k2=v2, JSON string or file (JSON or YAML) path prefixed by '@')") { |b| binds << b }

opts.on("--skip-wait", "Skip verification of non-priority-resource success (not recommended)") { skip_wait = true }
prot_ns = KubernetesDeploy::DeployTask::PROTECTED_NAMESPACES.join(', ')
Expand All @@ -38,6 +37,7 @@ ARGV.options do |opts|
exit
end
opts.parse!
bindings = KubernetesDeploy::BindingsParser.parse(binds)
end

namespace = ARGV[0]
Expand Down
6 changes: 3 additions & 3 deletions exe/kubernetes-render
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ template_dir = nil
bindings = {}

ARGV.options do |opts|
binds = []
opts.on("--bindings=BINDINGS", "Expose additional variables to ERB templates " \
"(format: k1=v1,k2=v2, JSON string or file (JSON or YAML) path prefixed by '@')") do |binds|
bindings.merge!(KubernetesDeploy::BindingsParser.parse(binds))
end
"(format: k1=v1,k2=v2, JSON string or file (JSON or YAML) path prefixed by '@')") { |b| binds << b }
opts.on("--template-dir=DIR", "Set the template dir (default: config/deploy/$ENVIRONMENT)") { |v| template_dir = v }
opts.parse!
bindings = KubernetesDeploy::BindingsParser.parse(binds)
end

templates = ARGV
Expand Down
10 changes: 9 additions & 1 deletion lib/kubernetes-deploy/bindings_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ module KubernetesDeploy
module BindingsParser
extend self

def parse(string)
def parse(binds)
bindings = {}
binds.each do |bind|
bindings.deep_merge!(parse_binding(bind))
end
bindings
end

def parse_binding(string)
bindings = parse_file(string) || parse_json(string) || parse_csv(string)

unless bindings
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/for_unit_tests/bindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
foo: a,b,c
bar: d
bla: e,f
nes:
ted: foo
cats: awesome
2 changes: 2 additions & 0 deletions test/fixtures/for_unit_tests/bindings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
foo: a,b,c
bar: d
bla: e,f
nes:
ted: bar
44 changes: 27 additions & 17 deletions test/unit/kubernetes-deploy/bindings_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,86 @@
class BindingsParserTest < ::Minitest::Test
def test_parse_json
expected = { "foo" => 42, "bar" => "hello" }
assert_equal(expected, parse(expected.to_json))
assert_equal(expected, parse_binding(expected.to_json))
end

def test_parse_json_file
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f" }
assert_equal(expected, parse("@test/fixtures/for_unit_tests/bindings.json"))
assert_equal(expected, parse_binding("@test/fixtures/for_unit_tests/bindings.json"))
end

def test_parse_yaml_file_with_yml_ext
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f" }
assert_equal(expected, parse("@test/fixtures/for_unit_tests/bindings.yml"))
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f", "nes" => { "ted" => "bar" } }
assert_equal(expected, parse_binding("@test/fixtures/for_unit_tests/bindings.yml"))
end

def test_parse_yaml_file_with_yaml_ext
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f" }
assert_equal(expected, parse("@test/fixtures/for_unit_tests/bindings.yaml"))
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f", "nes" => { "cats" => "awesome", "ted" => "foo" } }
assert_equal(expected, parse_binding("@test/fixtures/for_unit_tests/bindings.yaml"))
end

def test_parse_nonexistent_file
assert_raises(ArgumentError) do
parse("@fake/file.json")
parse_binding("@fake/file.json")
end
end

def test_parse_invalid_file_type
assert_raises(ArgumentError) do
parse("@fake/file.ini")
parse_binding("@fake/file.ini")
end
end

def test_parse_complex_json
expected = { "foo" => 42, "bar" => [1, 2, 3], "bla" => { "hello" => 17 } }
assert_equal(expected, parse(expected.to_json))
assert_equal(expected, parse_binding(expected.to_json))
end

def test_parse_json_not_hash
assert_raises(ArgumentError) do
parse([1, 2, 3].to_json)
parse_binding([1, 2, 3].to_json)
end
end

def test_parse_csv
expected = { "foo" => "42", "bar" => "17" }
assert_equal(expected, parse("foo=42,bar=17"))
assert_equal(expected, parse_binding("foo=42,bar=17"))
end

def test_parse_csv_with_comma_in_values
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f" }
assert_equal(expected, parse('"foo=a,b,c",bar=d,"bla=e,f"'))
assert_equal(expected, parse_binding('"foo=a,b,c",bar=d,"bla=e,f"'))
end

def test_parse_csv_with_equality_sign
expected = { "foo" => "1=2=3", "bar" => "3", "bla" => "4=7" }
assert_equal(expected, parse("foo=1=2=3,bar=3,bla=4=7"))
assert_equal(expected, parse_binding("foo=1=2=3,bar=3,bla=4=7"))
end

def test_parse_csv_with_no_value
expected = { "bla" => nil, "foo" => "" }
assert_equal(expected, parse("bla,foo="))
assert_equal(expected, parse_binding("bla,foo="))
end

def test_parse_csv_with_no_key
assert_raises(ArgumentError) do
parse("=17,foo=42")
parse_binding("=17,foo=42")
end
end

def test_parse_nested_values
expected = { "foo" => "a,b,c", "bar" => "d", "bla" => "e,f", "nes" => { "cats" => "awesome", "ted" => "bar" } }
assert_equal(expected, parse(["@test/fixtures/for_unit_tests/bindings.yaml",
"@test/fixtures/for_unit_tests/bindings.yml"]))
end

private

def parse(string)
KubernetesDeploy::BindingsParser.parse(string)
def parse_binding(string)
KubernetesDeploy::BindingsParser.parse_binding(string)
end

def parse(array)
KubernetesDeploy::BindingsParser.parse(array)
end
end

0 comments on commit 774aecd

Please sign in to comment.