Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Hostname override added for AWS
Browse files Browse the repository at this point in the history
* allows externalTrafficPolicy=local to work with AWS

[#157044566]

Signed-off-by: Iain Sproat <[email protected]>
  • Loading branch information
alex-slynko authored and iainsproat committed Nov 28, 2018
1 parent 86151d5 commit 9dc273c
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
14 changes: 14 additions & 0 deletions jobs/kube-proxy/templates/bin/kube_proxy_ctl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,21 @@ send_process_stderr_to_logfile() {
exec 2>> "$LOG_DIR/$NAME.stderr.log"
}

get_hostname_override() {
if [[ "aws" == "$cloud_provider" ]]; then
# The hostname needs to be set to the instance name so the gce cloud provider can manage the instance
hostname_override=$(curl http://169.254.169.254/latest/meta-data/hostname)
else
hostname_override=""
fi

echo $hostname_override
}

start_kubernetes_proxy() {

sed -i "s|HOSTNAMEOVERRIDE|$(get_hostname_override)|g" /var/vcap/jobs/kube-proxy/config/config.yml

kube-proxy \
<%-
if_p('k8s-args') do |args|
Expand Down
1 change: 1 addition & 0 deletions jobs/kube-proxy/templates/config/config.yml.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<% require 'yaml' %><%= p('kube-proxy-configuration').to_yaml %>
hostnameOverride: HOSTNAMEOVERRIDE
73 changes: 73 additions & 0 deletions spec/kube_proxy_ctl_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

require 'rspec'
require 'spec_helper'
require 'fileutils'
require 'open3'

def run_get_hostname_override(rendered_contents, executable_path)
File.open(executable_path, 'w', 0o777) do |f|
f.write(rendered_contents)
end

# exercise bash function by first changing path for any necessary executables to our mocks
cmd = format('PATH=%<dirname>s:%<env_path>s /bin/bash -c "source %<exe>s && get_hostname_override"',
dirname: File.dirname(executable_path), env_path: ENV['PATH'], exe: executable_path)
# capturing stderr (ignored) prevents expected warnings from showing up in test console
result, = Open3.capture3(cmd)
result
end

describe 'kube-proxy can set hostnameOverride' do
let(:config_properties) do
{
'kube-proxy-configuration' => { 'mode': 'iptables' }
}
end
let(:rendered_template) { compiled_template('kube-proxy', 'config/config.yml', config_properties, {}) }

# Check that the config file has HOSTNAMEOVERRIDE so that start script can find
# and replace it at runtime
it 'kube-proxy config has HOSTNAMEOVERRIDE key word' do
expect(rendered_template).to include('HOSTNAMEOVERRIDE')
end
end

describe 'kube_proxy_ctl setting of hostnameOverride property' do
let(:test_context) do
mock_dir = '/tmp/kube_proxy_mock'
FileUtils.remove_dir(mock_dir, true)
FileUtils.mkdir(mock_dir)
kube_proxy_ctl_file = mock_dir + '/kube_proxy_ctl'

{ 'mock_dir' => mock_dir, 'kube_proxy_ctl_file' => kube_proxy_ctl_file }
end
after(:each) do
FileUtils.remove_dir(test_context['mock_dir'], true)
end

describe 'when cloud-provider is AWS' do
it 'sets hostname_override to aws container hostname' do
expected_aws_hostname = 'foohost.aws.internal'

echo_mock_file = test_context['mock_dir'] + '/curl'
File.open(echo_mock_file, 'w', 0o777) do |f|
f.write("#!/bin/bash\n")
f.write("echo #{expected_aws_hostname}")
end

test_link = { 'cloud-provider' => {
'instances' => [],
'properties' => {
'cloud-provider' => {
'type' => 'aws',
}
}
} }
rendered_kube_proxy_ctl = compiled_template('kube-proxy', 'bin/kube_proxy_ctl', { 'cloud-provider' => 'aws' }, test_link)
result = run_get_hostname_override(rendered_kube_proxy_ctl, test_context['kube_proxy_ctl_file'])

expect(result).to include(expected_aws_hostname)
end
end
end

0 comments on commit 9dc273c

Please sign in to comment.