Skip to content

Commit

Permalink
Add optional revision flag (#613)
Browse files Browse the repository at this point in the history
* Add optional current-sha flag
* Add current-sha to exe/kubernetes- tasks for backwards compatibility
* Avoid sending the sha: statsd tag unnecessarily
  • Loading branch information
dturn authored and dirceu committed Nov 11, 2019
1 parent 42f0a97 commit 390b34e
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 20 deletions.
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
current_sha = 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("--current-sha=CURRENT_SHA", "Expose SHA `current_sha` in ERB bindings") { |r| current_sha = 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: current_sha,
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 = {}
current_sha = 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("--current-sha=CURRENT_SHA", "Expose SHA `current_sha` in ERB bindings") { |r| current_sha = 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: current_sha,
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" },
"current-sha" => { 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['current-sha'],
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' },
'current-sha': { 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['current-sha'],
template_paths: paths,
bindings: bindings_parser.parse,
)
Expand Down
3 changes: 2 additions & 1 deletion lib/krane/deprecated_deploy_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ def ejson_keys_secret
end

def statsd_tags
%W(namespace:#{@namespace} sha:#{@current_sha} context:#{@context}) | @namespace_tags
tags = %W(namespace:#{@namespace} context:#{@context}) | @namespace_tags
@current_sha.nil? ? tags : %W(sha:#{@current_sha}) | tags
end

def with_retries(limit)
Expand Down
6 changes: 6 additions & 0 deletions test/exe/deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ def test_deploy_fails_without_filename
end
end

def test_deploy_uses_current_sha
test_sha = "TEST"
set_krane_deploy_expectations(new_args: { current_sha: test_sha })
krane_deploy!(flags: "--current-sha #{test_sha}")
end

private

def set_krane_deploy_expectations(new_args: {}, run_args: {})
Expand Down
10 changes: 4 additions & 6 deletions test/exe/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'krane/cli/krane'

class RendertTest < Krane::TestCase
include EnvTestHelper
def test_render_with_default_options
install_krane_render_expectations
krane_render!
Expand All @@ -25,10 +24,8 @@ def test_render_parses_bindings

def test_render_uses_current_sha
test_sha = "TEST"
with_env("REVISION", test_sha) do
install_krane_render_expectations
krane_render!
end
install_krane_render_expectations(current_sha: test_sha)
krane_render!("--current-sha #{test_sha}")
end

private
Expand All @@ -40,7 +37,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
12 changes: 3 additions & 9 deletions test/integration/krane_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require 'integration_test_helper'

class KraneTest < Krane::IntegrationTest
include EnvTestHelper

def test_restart_black_box
assert_deploy_success(deploy_fixtures("hello-cloud", subset: ["configmap-data.yml", "web.yml.erb", "redis.yml"]))
refute(fetch_restarted_at("web"), "RESTARTED_AT env on fresh deployment")
Expand Down Expand Up @@ -38,19 +36,15 @@ def test_render_black_box
bindings = "data=#{data_value}"
test_sha = rand(10_000).to_s

out, err, status = nil
with_env("REVISION", test_sha) do
out, err, status = krane_black_box("render", "-f #{paths.join(' ')} --bindings #{bindings}")
end
out, err, status = krane_black_box("render",
"-f #{paths.join(' ')} --bindings #{bindings} --current-sha #{test_sha}")

assert_predicate(status, :success?)
assert_match("Success", err)
assert_match(test_sha, out)
assert_match(data_value, out)

with_env("REVISION", test_sha) do
out, err, status = krane_black_box("render", "-f #{paths.join(' ')}")
end
out, err, status = krane_black_box("render", "-f #{paths.join(' ')} --current-sha #{test_sha}")

refute_predicate(status, :success?)
assert_match("FAILURE", err)
Expand Down

0 comments on commit 390b34e

Please sign in to comment.