Skip to content

Commit

Permalink
Merge pull request #269 from Shopify/json-file
Browse files Browse the repository at this point in the history
Adds file parsing to --bindings option
  • Loading branch information
Peer Allan committed Apr 6, 2018
2 parents 57a723a + 4c24899 commit a07bfc1
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*Features*
- Added `--max-watch-seconds=seconds` to kubernetes-restart and kubernetes-deploy. When set
a timeout error is raised if it takes longer than _seconds_ for any resource to deploy.
- Adds YAML and JSON file reference support to the kubernetes-deploy `--bindings` argument ([#269](https://github.com/Shopify/kubernetes-deploy/pull/269))

*Enhancements*
- Prune resource quotas ([#264](https://github.com/Shopify/kubernetes-deploy/pull/264/files))
Expand Down
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,24 @@ All templates must be YAML formatted. You can also use ERB. The following local
* `current_sha`: The value of `$REVISION`
* `deployment_id`: A randomly generated identifier for the deploy. Useful for creating unique names for task-runner pods (e.g. a pod that runs rails migrations at the beginning of deploys).

You can add additional variables using the `--bindings=BINDINGS` option which can be formated as comma separated or as JSON. For example, `kubernetes-deploy my-app cluster1 --bindings=color=blue,size=large` or `kubernetes-deploy my-app cluster1 --bindings='{"color":"blue","size":"large"}'` will expose `color` and `size` in your templates. Complex JSON data will be converted to a Hash for use in templates.
You can add additional variables using the `--bindings=BINDINGS` option which can be formated as comma separated string, JSON string or path to a JSON or YAML file. Complex JSON or YAML data will be converted to a Hash for use in templates. To load a file the argument should include the relative file path prefixed with an `@` sign. An argument error will be raised if the string argument cannot be parsed, the referenced file does not include a valid extension (`.json`, `.yaml` or `.yml`) or the referenced file does not exist.

#### Bindings examples

```
# Comma separated string. Exposes, 'color' and 'size'
$ kubernetes-deploy my-app cluster1 --bindings=color=blue,size=large
# JSON string. Exposes, 'color' and 'size'
$ kubernetes-deploy my-app cluster1 --bindings='{"color":"blue","size":"large"}'
# Load JSON file from ./config
$ kubernetes-deploy my-app cluster1 --bindings='@config/production.json'
# Load YAML file from ./config (.yaml or .yml supported)
$ kubernetes-deploy my-app cluster1 --bindings='@config/production.yaml'
```


#### Using partials

Expand Down
2 changes: 1 addition & 1 deletion exe/kubernetes-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ max_watch_seconds = nil

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

Expand Down
24 changes: 23 additions & 1 deletion lib/kubernetes-deploy/bindings_parser.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# frozen_string_literal: true
require 'json'
require 'yaml'
require 'csv'

module KubernetesDeploy
module BindingsParser
extend self

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

unless bindings
raise ArgumentError, "Failed to parse bindings."
Expand All @@ -18,6 +19,27 @@ def parse(string)

private

def parse_file(string)
return unless string =~ /\A@/

begin
file_path = string.gsub(/\A@/, '')

case File.extname(file_path)
when '.json'
bindings = parse_json(File.read(file_path))
when '.yaml', '.yml'
bindings = YAML.load(File.read(file_path))
else
raise ArgumentError, "Supplied file does not appear to be JSON or YAML"
end

bindings
rescue Errno::ENOENT
raise ArgumentError, "Supplied file does not exist: #{string}"
end
end

def parse_json(string)
bindings = JSON.parse(string)

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/for_unit_tests/bindings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"foo":"a,b,c","bar":"d","bla":"e,f"}
4 changes: 4 additions & 0 deletions test/fixtures/for_unit_tests/bindings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
foo: a,b,c
bar: d
bla: e,f
4 changes: 4 additions & 0 deletions test/fixtures/for_unit_tests/bindings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
foo: a,b,c
bar: d
bla: e,f
27 changes: 27 additions & 0 deletions test/unit/kubernetes-deploy/bindings_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,33 @@ def test_parse_json
assert_equal expected, parse(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")
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")
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")
end

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

def test_parse_invalid_file_type
assert_raises(ArgumentError) do
parse("@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)
Expand Down

0 comments on commit a07bfc1

Please sign in to comment.