From b9f084215e783a96bd4255c2714d5b69c3e0fb5a Mon Sep 17 00:00:00 2001 From: Kimmo Lehto Date: Thu, 8 Aug 2019 14:02:11 +0300 Subject: [PATCH] Add kube node config files to autoconfig search paths --- lib/k8s/client.rb | 10 ++++++++-- spec/k8s/client_spec.rb | 16 +++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/k8s/client.rb b/lib/k8s/client.rb index 1f810ce..f517b27 100644 --- a/lib/k8s/client.rb +++ b/lib/k8s/client.rb @@ -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 diff --git a/spec/k8s/client_spec.rb b/spec/k8s/client_spec.rb index c9b3682..b3fa80c 100644 --- a/spec/k8s/client_spec.rb +++ b/spec/k8s/client_spec.rb @@ -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