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 kube node config files to autoconfig search paths #154

Merged
merged 1 commit into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions lib/k8s/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ def self.autoconfig(namespace: nil, **options)
configuration = K8s::Config.build(server: ENV['KUBE_SERVER'], ca: ENV['KUBE_CA'], auth_token: token)
elsif !ENV['KUBECONFIG'].to_s.empty?
configuration = K8s::Config.from_kubeconfig_env(ENV['KUBECONFIG'])
elsif File.exist?(File.join(Dir.home, '.kube', 'config'))
configuration = K8s::Config.load_file(File.join(Dir.home, '.kube', 'config'))
else
found_config = [
File.join(Dir.home, '.kube', 'config'),
'/etc/kubernetes/admin.conf',
'/etc/kubernetes/kubelet.conf'
].find { |f| File.exist?(f) && File.readable?(f) }

configuration = K8s::Config.load_file(found_config) if found_config
end

if configuration
Expand Down
16 changes: 11 additions & 5 deletions spec/k8s/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,23 @@
end
end

context 'from default ~/.kube/config' do
it 'loads the file' do
expect(File).to receive(:exist?).with(default_kubeconfig_path).and_return(true)
expect(File).to receive(:read).with(default_kubeconfig_path).and_return(kubeconfig)
context 'from default file locations' do
before do
expect(File).to receive(:exist?).with(default_kubeconfig_path).and_return(false)
expect(File).to receive(:exist?).with('/etc/kubernetes/admin.conf').and_return(false)
expect(File).to receive(:exist?).with('/etc/kubernetes/kubelet.conf').and_return(true)
expect(File).to receive(:readable?).with('/etc/kubernetes/kubelet.conf').and_return(true)
end

it 'loads a file if found' do
expect(File).to receive(:read).with('/etc/kubernetes/kubelet.conf').and_return(kubeconfig)
expect(subject.autoconfig).to be_a K8s::Client
end
end

context 'from in_cluster_config' do
before do
allow(File).to receive(:exist?).with(default_kubeconfig_path).and_return(false)
allow(File).to receive(:exist?).with(anything).and_return(false)
stub_const("ENV", {})
end

Expand Down