Skip to content

Commit

Permalink
Force intepret kubectl response encoding as UTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
KnVerey committed Nov 27, 2019
1 parent 95bb20e commit ea4327c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/krane/kubectl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ def run(*args, log_failure: nil, use_context: true, use_namespace: true, output:
(1..attempts).to_a.each do |current_attempt|
logger.debug("Running command (attempt #{current_attempt}): #{cmd.join(' ')}")
out, err, st = Open3.capture3(*cmd)
logger.debug("Kubectl out: " + out.gsub(/\s+/, ' ')) unless output_is_sensitive

# https://github.com/Shopify/krane/issues/395
if out.encoding != Encoding::UTF_8
out = out.dup.force_encoding(Encoding::UTF_8)
end

if logger.debug? && !output_is_sensitive
# don't do the gsub unless we're going to print this
logger.debug("Kubectl out: " + out.gsub(/\s+/, ' '))
end

break if st.success?
raise(ResourceNotFoundError, err) if err.match(ERROR_MATCHERS[:not_found]) && raise_if_not_found
Expand Down
10 changes: 10 additions & 0 deletions test/unit/krane/kubectl_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,16 @@ def test_retry_delay_backoff
end
end

def test_debug_level_output_log_uses_correct_encoding
logger.level = ::Logger::DEBUG
good = "hélas"
bad = good.dup.force_encoding(Encoding::US_ASCII)

stub_open3(%W(kubectl get pods --namespace=testn --context=testc --request-timeout=#{timeout}), resp: bad)
out, _err, _st = build_kubectl.run("get", "pods")
assert_equal good, out
end

private

def timeout
Expand Down

0 comments on commit ea4327c

Please sign in to comment.