Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional revision flag #613

Merged
merged 4 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion exe/kubernetes-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bindings = {}
verbose_log_prefix = false
max_watch_seconds = nil
selector = nil
revision = ENV["REVISION"]

ARGV.options do |opts|
parser = Krane::BindingsParser.new
Expand Down Expand Up @@ -52,6 +53,7 @@ ARGV.options do |opts|
puts "v#{Krane::VERSION}"
exit
end
opts.on("--revision=REVISION", "Expose SHA `current_sha` in ERB bindings") { |r| revision = r }
opts.parse!
bindings = parser.parse
end
Expand All @@ -72,7 +74,7 @@ begin
runner = KubernetesDeploy::DeployTask.new(
namespace: namespace,
context: context,
current_sha: ENV["REVISION"],
current_sha: revision,
template_paths: paths,
bindings: bindings,
logger: logger,
Expand Down
4 changes: 3 additions & 1 deletion exe/kubernetes-render
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require 'optparse'

template_dir = []
bindings = {}
revision = ENV["REVISION"]

ARGV.options do |opts|
parser = Krane::BindingsParser.new
Expand All @@ -17,6 +18,7 @@ ARGV.options do |opts|
opts.on("--template-dir=DIR", "Set the template dir (default: config/deploy/$ENVIRONMENT).") do |d|
template_dir = [d]
end
opts.on("--revision=REVISION", "Expose SHA `current_sha` in ERB bindings") { |r| revision = r }
opts.parse!
bindings = parser.parse
end
Expand All @@ -27,7 +29,7 @@ logger = Krane::FormattedLogger.build(verbose_prefix: false)
begin
Krane::OptionsHelper.with_processed_template_paths(template_dir) do |dir|
runner = KubernetesDeploy::RenderTask.new(
current_sha: ENV["REVISION"],
current_sha: revision,
template_dir: dir.first,
bindings: bindings,
)
Expand Down
4 changes: 3 additions & 1 deletion lib/krane/cli/deploy_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DeployCommand
default: true },
"verify-result" => { type: :boolean, default: true,
desc: "Verify workloads correctly deployed" },
"revision" => { type: :string, banner: "SHA", desc: "Expose SHA `current_sha` in ERB bindings" },

}

def self.from_options(namespace, context, options)
Expand All @@ -57,7 +59,7 @@ def self.from_options(namespace, context, options)
deploy = ::Krane::DeployTask.new(
namespace: namespace,
context: context,
current_sha: ENV["REVISION"],
current_sha: options.fetch(:revision, ENV["REVISION"]),
template_paths: paths,
bindings: bindings_parser.parse,
logger: logger,
Expand Down
3 changes: 2 additions & 1 deletion lib/krane/cli/render_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class RenderCommand
bindings: { type: :array, banner: "foo=bar abc=def", desc: 'Bindings for erb' },
filenames: { type: :array, banner: 'config/deploy/production config/deploy/my-extra-resource.yml',
required: true, aliases: 'f', desc: 'Directories and files to render' },
revision: { type: :string, banner: "SHA", desc: "Expose SHA `current_sha` in ERB bindings" },
}

def self.from_options(options)
Expand All @@ -19,7 +20,7 @@ def self.from_options(options)

::Krane::OptionsHelper.with_processed_template_paths(options[:filenames]) do |paths|
runner = ::Krane::RenderTask.new(
current_sha: ENV["REVISION"],
current_sha: options.fetch(:revision, ENV["REVISION"]),
template_paths: paths,
bindings: bindings_parser.parse,
)
Expand Down
25 changes: 25 additions & 0 deletions test/exe/deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'krane/bindings_parser'

class DeployTest < Krane::TestCase
include EnvTestHelper

def test_deploy_with_default_options
set_krane_deploy_expectations
krane_deploy!
Expand Down Expand Up @@ -79,6 +81,29 @@ def test_deploy_fails_without_filename
end
end

def test_deploy_uses_revision_env_var
test_sha = "TEST"
with_env("REVISION", test_sha) do
set_krane_deploy_expectations(new_args: { current_sha: test_sha })
krane_deploy!
end
end

def test_deploy_uses_revision
test_sha = "TEST"
set_krane_deploy_expectations(new_args: { current_sha: test_sha })
krane_deploy!(flags: "--revision #{test_sha}")
end

def test_render_uses_revision_flag_over_env_var
env_test_sha = "NOT_TEST_VALUE_TEST"
test_sha = "TEST"
with_env("REVISION", env_test_sha) do
set_krane_deploy_expectations(new_args: { current_sha: test_sha })
krane_deploy!(flags: "--revision #{test_sha}")
end
end

private

def set_krane_deploy_expectations(new_args: {}, run_args: {})
Expand Down
18 changes: 17 additions & 1 deletion test/exe/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def test_render_uses_current_sha
end
end

def test_render_uses_revision
test_sha = "TEST"
install_krane_render_expectations(current_sha: test_sha)
krane_render!("--revision #{test_sha}")
end

def test_render_uses_revision_flag_over_env_current_sha
env_test_sha = "NOT_TEST_VALUE_TEST"
test_sha = "TEST"
with_env("REVISION", env_test_sha) do
install_krane_render_expectations(current_sha: test_sha)
krane_render!("--revision #{test_sha}")
end
end

private

def install_krane_render_expectations(new_args = {})
Expand All @@ -40,7 +55,8 @@ def install_krane_render_expectations(new_args = {})
Krane::RenderTask.expects(:new).with(options).returns(response)
end

def krane_render!(flags = '-f /dev/null')
def krane_render!(flags = "")
flags += ' -f /dev/null' unless flags.include?("-f")
krane = Krane::CLI::Krane.new(
[],
flags.split
Expand Down