Skip to content

Commit 43885e7

Browse files
committed
Add style linting with rubocop
1 parent b64c3fb commit 43885e7

File tree

9 files changed

+241
-172
lines changed

9 files changed

+241
-172
lines changed

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
gemfile: kubernetes/Gemfile
22
rvm:
33
- 2.6
4-
script: sh -c 'cd kubernetes && bundle exec rspec'
4+
5+
script:
6+
- cd kubernetes
7+
- bundle exec rubocop ./src
8+
- bundle exec rspec

kubernetes/Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ gemspec
44

55
group :development, :test do
66
gem 'rake', '~> 12.0.0'
7+
gem 'rubocop', '~> 0.65.0'
78
end

kubernetes/Gemfile.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ GEM
1111
ZenTest (4.11.2)
1212
addressable (2.6.0)
1313
public_suffix (>= 2.0.2, < 4.0)
14+
ast (2.4.0)
1415
autotest (4.4.6)
1516
ZenTest (>= 4.4.1)
1617
autotest-fsevent (0.2.14)
@@ -24,8 +25,15 @@ GEM
2425
ffi (>= 1.3.0)
2526
ffi (1.10.0)
2627
hashdiff (0.3.8)
28+
jaro_winkler (1.5.2)
2729
json (2.1.0)
30+
parallel (1.14.0)
31+
parser (2.6.0.0)
32+
ast (~> 2.4.0)
33+
powerpack (0.1.2)
34+
psych (3.1.0)
2835
public_suffix (3.0.3)
36+
rainbow (3.0.0)
2937
rake (12.0.0)
3038
rspec (3.8.0)
3139
rspec-core (~> 3.8.0)
@@ -40,11 +48,22 @@ GEM
4048
diff-lcs (>= 1.2.0, < 2.0)
4149
rspec-support (~> 3.8.0)
4250
rspec-support (3.8.0)
51+
rubocop (0.65.0)
52+
jaro_winkler (~> 1.5.1)
53+
parallel (~> 1.10)
54+
parser (>= 2.5, != 2.5.1.1)
55+
powerpack (~> 0.1)
56+
psych (>= 3.1.0)
57+
rainbow (>= 2.2.2, < 4.0)
58+
ruby-progressbar (~> 1.7)
59+
unicode-display_width (~> 1.4.0)
60+
ruby-progressbar (1.10.0)
4361
safe_yaml (1.0.4)
4462
sys-uname (1.0.4)
4563
ffi (>= 1.0.0)
4664
typhoeus (1.3.1)
4765
ethon (>= 0.9.0)
66+
unicode-display_width (1.4.1)
4867
vcr (3.0.3)
4968
webmock (1.24.6)
5069
addressable (>= 2.3.6)
@@ -62,6 +81,9 @@ DEPENDENCIES
6281
kubernetes!
6382
rake (~> 12.0.0)
6483
rspec (~> 3.6, >= 3.6.0)
84+
rubocop (~> 0.65.0)
6585
vcr (~> 3.0, >= 3.0.1)
6686
webmock (~> 1.24, >= 1.24.3)
6787

88+
BUNDLED WITH
89+
1.16.1

kubernetes/spec/config/kube_config_spec.rb

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -222,43 +222,17 @@
222222
end
223223
end
224224

225-
context '#create_temp_file_with_base64content' do
226-
context 'when it is called at first time' do
227-
it 'should return temp file path' do
228-
expected_path = 'tempfile-path'
229-
content = TEST_DATA_BASE64
230-
io = double('io')
231-
expect(io).to receive(:path).and_return(expected_path)
232-
expect(io).to receive(:write).with(TEST_DATA)
233-
allow(Tempfile).to receive(:open).and_yield(io)
234-
235-
expect(kube_config.send(:create_temp_file_with_base64content, content)).to eq(expected_path)
236-
end
237-
end
238-
239-
context 'when it is already called' do
240-
it 'should return cached value' do
241-
expected_path = 'tempfile-path'
242-
content = TEST_DATA_BASE64
243-
Kubernetes::KubeConfig.class_eval { class_variable_get(:@@temp_files)[content] = expected_path}
244-
io = double('io')
245-
expect(io).not_to receive(:path)
246-
expect(io).not_to receive(:write).with(TEST_DATA)
247225

248-
expect(kube_config.send(:create_temp_file_with_base64content, content)).to eq(expected_path)
249-
end
250-
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
226+
context 'load from defaults' do
227+
before(:each) do
228+
# Clear out everything before each run.
229+
ENV['HOME'] = nil
230+
ENV['KUBECONFIG'] = nil
231+
ENV['KUBERNETES_SERVICE_HOST'] = nil
232+
ENV['KUBERNETES_SERVICE_PORT'] = nil
259233

260-
# Suppress warnings
261-
warn_level = $VERBOSE
234+
# Suppress warnings
235+
warn_level = $VERBOSE
262236
$VERBOSE = nil
263237
Kubernetes::InClusterConfig::SERVICE_TOKEN_FILENAME = '/non/existent/file/token'
264238
Kubernetes::InClusterConfig::SERVICE_CA_CERT_FILENAME = '/non/existent/file/ca.crt'
@@ -304,5 +278,4 @@
304278
expect(config.host).to eq('localhost:8080')
305279
end
306280
end
307-
end
308281
end

kubernetes/spec/utils_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,32 @@
7272
expect(actual).to be_same_configuration_as(expected)
7373
end
7474
end
75+
76+
context '#create_temp_file_with_base64content' do
77+
context 'when it is called at first time' do
78+
it 'should return temp file path' do
79+
expected_path = 'tempfile-path'
80+
content = TEST_DATA_BASE64
81+
io = double('io')
82+
expect(io).to receive(:path).and_return(expected_path)
83+
expect(io).to receive(:write).with(TEST_DATA)
84+
allow(Tempfile).to receive(:open).and_yield(io)
85+
86+
expect(Kubernetes.create_temp_file_with_base64content(content)).to eq(expected_path)
87+
end
88+
end
89+
90+
context 'when it is already called' do
91+
it 'should return cached value' do
92+
expected_path = 'tempfile-path'
93+
content = TEST_DATA_BASE64
94+
Kubernetes::cache_temp_file(content, expected_path)
95+
io = double('io')
96+
expect(io).not_to receive(:path)
97+
expect(io).not_to receive(:write).with(TEST_DATA)
98+
99+
expect(Kubernetes.create_temp_file_with_base64content(content)).to eq(expected_path)
100+
end
101+
end
102+
end
75103
end

kubernetes/src/kubernetes/config/incluster_config.rb

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,35 @@
1616
require 'kubernetes/config/error'
1717

1818
module Kubernetes
19-
19+
# The InClusterConfig class represents configuration for authn/authz in a
20+
# Kubernetes cluster.
2021
class InClusterConfig
21-
22-
SERVICE_HOST_ENV_NAME = "KUBERNETES_SERVICE_HOST"
23-
SERVICE_PORT_ENV_NAME = "KUBERNETES_SERVICE_PORT"
24-
SERVICE_TOKEN_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/token"
25-
SERVICE_CA_CERT_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
22+
# rubocop:disable LineLength
23+
SERVICE_HOST_ENV_NAME = 'KUBERNETES_SERVICE_HOST'.freeze
24+
SERVICE_PORT_ENV_NAME = 'KUBERNETES_SERVICE_PORT'.freeze
25+
SERVICE_TOKEN_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/token'.freeze
26+
SERVICE_CA_CERT_FILENAME = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'.freeze
27+
# rubocop:enable LineLength
2628

2729
attr_accessor :host
2830
attr_accessor :port
2931
attr_accessor :token
3032

3133
def validate
32-
unless (self.host = self.env[SERVICE_HOST_ENV_NAME]) && (self.port = self.env[SERVICE_PORT_ENV_NAME])
33-
raise ConfigError.new("Service host/port is not set")
34+
unless (self.host = env[SERVICE_HOST_ENV_NAME]) &&
35+
(self.port = env[SERVICE_PORT_ENV_NAME])
36+
raise ConfigError, 'Service host/port is not set'
3437
end
35-
raise ConfigError.new("Service token file does not exists") unless File.file?(self.token_file)
36-
raise ConfigError.new("Service token file does not exists") unless File.file?(self.ca_cert)
38+
39+
# rubocop:disable LineLength
40+
raise ConfigError, 'Service token file does not exists' unless File.file?(token_file)
41+
raise ConfigError, 'Service token file does not exists' unless File.file?(ca_cert)
42+
# rubocop:enable LineLength
3743
end
3844

39-
def self.in_cluster?()
40-
return File.exist?(SERVICE_TOKEN_FILENAME) && File.exist?(SERVICE_CA_CERT_FILENAME)
45+
def self.in_cluster?
46+
File.exist?(SERVICE_TOKEN_FILENAME) &&
47+
File.exist?(SERVICE_CA_CERT_FILENAME)
4148
end
4249

4350
def env
@@ -56,19 +63,18 @@ def token_file
5663
end
5764

5865
def load_token
59-
open(self.token_file) do |io|
66+
File.open(token_file) do |io|
6067
self.token = io.read.chomp
6168
end
6269
end
6370

6471
def configure(configuration)
6572
validate
6673
load_token
67-
configuration.api_key['authorization'] = "Bearer #{self.token}"
74+
configuration.api_key['authorization'] = "Bearer #{token}"
6875
configuration.scheme = 'https'
69-
configuration.host = "#{self.host}:#{self.port}"
70-
configuration.ssl_ca_cert = self.ca_cert
76+
configuration.host = "#{host}:#{port}"
77+
configuration.ssl_ca_cert = ca_cert
7178
end
7279
end
73-
7480
end

0 commit comments

Comments
 (0)