Skip to content

Commit f09b1a9

Browse files
committed
Add tests for default loading, address comments.
1 parent 0fae617 commit f09b1a9

File tree

7 files changed

+103
-33
lines changed

7 files changed

+103
-33
lines changed

kubernetes/lib/kubernetes/configuration.rb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -153,35 +153,6 @@ def self.default
153153
@@default ||= Configuration.new
154154
end
155155

156-
def self.default_config()
157-
# KUBECONFIG environment variable
158-
result = Configuration.new()
159-
kc = "#{ENV['KUBECONFIG']}"
160-
if File.exist?(kc)
161-
k_config = KubeConfig.new(kc)
162-
k_config.configure(result)
163-
return result
164-
end
165-
# default home location
166-
kc = "#{ENV['HOME']}/.kube/config"
167-
if File.exist?(kc)
168-
k_config = KubeConfig.new(kc)
169-
k_config.configure(result)
170-
return result
171-
end
172-
# In cluster config
173-
if InClusterConfig::is_in_cluster()
174-
k_config = InClusterConfig.new()
175-
k_config.configure(result)
176-
return result
177-
end
178-
179-
result.scheme = 'http'
180-
result.host = 'localhost:8080'
181-
return result
182-
end
183-
184-
185156
def configure
186157
yield(self) if block_given?
187158
end

kubernetes/spec/config/kube_config_spec.rb

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# limitations under the License.
1414

1515
require 'base64'
16-
require 'spec_helper'
1716
require 'config/matchers'
1817
require 'fixtures/config/kube_config_hash'
1918
require 'helpers/file_fixtures'
19+
require 'spec_helper'
2020

2121
require 'kubernetes/config/kube_config'
22+
require 'kubernetes/loader'
2223

2324

2425
describe Kubernetes::KubeConfig do
@@ -247,5 +248,61 @@
247248
expect(kube_config.send(:create_temp_file_with_base64content, content)).to eq(expected_path)
248249
end
249250
end
251+
252+
context 'load from defaults' do
253+
before(:each) do
254+
# Clear out everything before each run.
255+
ENV['HOME'] = nil
256+
ENV['KUBECONFIG'] = nil
257+
ENV['KUBERNETES_SERVICE_HOST'] = nil
258+
ENV['KUBERNETES_SERVICE_PORT'] = nil
259+
260+
# Suppress warnings
261+
warn_level = $VERBOSE
262+
$VERBOSE = nil
263+
Kubernetes::InClusterConfig::SERVICE_TOKEN_FILENAME = '/non/existent/file/token'
264+
Kubernetes::InClusterConfig::SERVICE_CA_CERT_FILENAME = '/non/existent/file/ca.crt'
265+
$VERBOSE = warn_level
266+
end
267+
268+
it 'should load from KUBECONFIG' do
269+
ENV['KUBECONFIG'] = Kubernetes::Testing::file_fixture('config/config_2').to_s
270+
ENV['HOME'] = Kubernetes::Testing::file_fixture('config').to_s
271+
config = Kubernetes::Configuration::default_config
272+
273+
expect(config.host).to eq('other:8080')
274+
end
275+
276+
it 'should load from HOME' do
277+
ENV['HOME'] = Kubernetes::Testing::file_fixture('config').to_s
278+
config = Kubernetes::Configuration::default_config
279+
280+
expect(config.host).to eq('firstconfig:8080')
281+
end
282+
283+
it 'should load from cluster' do
284+
ENV['KUBERNETES_SERVICE_HOST'] = 'kubernetes'
285+
ENV['KUBERNETES_SERVICE_PORT'] = '8888'
286+
287+
# Suppress warnings
288+
warn_level = $VERBOSE
289+
$VERBOSE = nil
290+
291+
# Override constants for token and cert locations
292+
Kubernetes::InClusterConfig::SERVICE_TOKEN_FILENAME = Kubernetes::Testing::file_fixture('config/config').to_s
293+
Kubernetes::InClusterConfig::SERVICE_CA_CERT_FILENAME = Kubernetes::Testing::file_fixture('certs/ca.crt').to_s
294+
$VERBOSE = warn_level
295+
296+
config = Kubernetes::Configuration::default_config
297+
298+
expect(config.host).to eq('kubernetes:8888')
299+
end
300+
301+
it 'should default to localhost' do
302+
config = Kubernetes::Configuration::default_config
303+
304+
expect(config.host).to eq('localhost:8080')
305+
end
306+
end
250307
end
251308
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
clusters:
3+
- name: default
4+
cluster:
5+
server: http://firstconfig:8080
6+
contexts:
7+
- name: first_user
8+
context:
9+
cluster: default
10+
user: first_user
11+
current-context: first_user
12+
kind: Config
13+
preferences: {}
14+
users:
15+
- name: first_user
16+
token: foobar
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
apiVersion: v1
2+
clusters:
3+
- name: default
4+
cluster:
5+
server: http://other:8080
6+
contexts:
7+
- name: some_user
8+
context:
9+
cluster: default
10+
user: someone
11+
current-context: some_user
12+
kind: Config
13+
preferences: {}
14+
users:
15+
- name: someone

kubernetes/spec/helpers/file_fixtures.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def file_fixture(fixture_name)
2828
path
2929
else
3030
msg = "the directory '%s' does not contain a file named '%s'"
31-
raise ArgumentError, msg % [file_fixture_path, fixture_name]
31+
raise ArgumentError, msg % [FIXTURES_DIR_PATH, fixture_name]
3232
end
3333
end
3434
end

kubernetes/src/kubernetes/config/incluster_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def validate
3636
raise ConfigError.new("Service token file does not exists") unless File.file?(self.ca_cert)
3737
end
3838

39-
def self.is_in_cluster()
39+
def self.in_cluster?()
4040
return File.exist?(SERVICE_TOKEN_FILENAME) && File.exist?(SERVICE_CA_CERT_FILENAME)
4141
end
4242

kubernetes/src/kubernetes/config/kube_config.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ def config
5757

5858
def configure(configuration, context_name=nil)
5959
context = context_name ? self.find_context(context_name) : self.current_context
60+
if !context
61+
return
62+
end
6063
user = context['user'] || {}
6164
cluster = context['cluster'] || {}
6265

@@ -92,6 +95,9 @@ def find_cluster(name)
9295

9396
def find_user(name)
9497
self.find_by_name(self.config['users'], 'user', name).tap do |user|
98+
if !user
99+
return
100+
end
95101
create_temp_file_and_set(user, 'client-certificate')
96102
create_temp_file_and_set(user, 'client-key')
97103
# If tokenFile is specified, then set token
@@ -122,7 +128,9 @@ def find_context(name)
122128
end
123129

124130
def current_context
125-
find_context(self.config['current-context'])
131+
if self.config
132+
find_context(self.config['current-context'])
133+
end
126134
end
127135

128136
protected
@@ -133,6 +141,9 @@ def find_by_name(list, key, name)
133141
end
134142

135143
def create_temp_file_and_set(obj, key)
144+
if !obj
145+
return
146+
end
136147
if !obj[key] && obj["#{key}-data"]
137148
obj[key] = create_temp_file_with_base64content(obj["#{key}-data"])
138149
end

0 commit comments

Comments
 (0)