Skip to content

Commit 7f0dce5

Browse files
committed
Refactor to add helper utility for config
1 parent 072bb1d commit 7f0dce5

File tree

7 files changed

+303
-200
lines changed

7 files changed

+303
-200
lines changed

kubernetes/README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,10 @@ Please follow the [installation](#installation) procedure and then run the follo
5353
```ruby
5454
# Load the gem
5555
require 'kubernetes'
56+
require 'kubernetes/utils'
5657

57-
# Setup authorization
58-
Kubernetes.configure do |config|
59-
# Configure API key authorization: BearerToken
60-
config.api_key['authorization'] = 'YOUR API KEY'
61-
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
62-
#config.api_key_prefix['authorization'] = 'Bearer'
63-
end
58+
# Configs can be set in Configuration class directly or using helper utility
59+
Kubernetes.load_kube_config
6460

6561
api_instance = Kubernetes::AdmissionregistrationApi.new
6662

kubernetes/lib/kubernetes/config/incluster_config.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,6 @@ module Kubernetes
1919

2020
class InClusterConfig
2121

22-
class << self
23-
24-
#
25-
# Use the service account kubernetes gives to pods to connect to kubernetes
26-
# cluster. It's intended for clients that expect to be running inside a pod
27-
# running on kubernetes. It will raise an exception if called from a process
28-
# not running in a kubernetes environment.
29-
def load(client_configuration: Configuration.default)
30-
config = self.new
31-
config.configure(client_configuration)
32-
end
33-
34-
end
35-
3622
SERVICE_HOST_ENV_NAME = "KUBERNETES_SERVICE_HOST"
3723
SERVICE_PORT_ENV_NAME = "KUBERNETES_SERVICE_PORT"
3824
SERVICE_TOKEN_FILENAME = "/var/run/secrets/kubernetes.io/serviceaccount/token"

kubernetes/lib/kubernetes/config/kube_config.rb

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,6 @@ class KubeConfig
2929

3030
class << self
3131

32-
#
33-
# Loads authentication and cluster information from kube-config file
34-
# and stores them in Kubernetes::Configuration.
35-
# @param config_file [String] Path of the kube-config file.
36-
# @param context [String] Set the active context. If is set to nil, current_context from config file will be used.
37-
# @param client_configuration [Kubernetes::Configuration] The Kubernetes::Configuration tp set configs to.
38-
def load(
39-
config_file=ENV['KUBE_CONFIG'],
40-
context: nil,
41-
client_configuration: Configuration.default
42-
)
43-
config_file ||= KUBE_CONFIG_DEFAULT_LOCATION
44-
config = self.new(config_file)
45-
config.configure(client_configuration, context)
46-
end
47-
48-
#
49-
# Loads configuration the same as load_kube_config but returns an ApiClient
50-
# to be used with any API object. This will allow the caller to concurrently
51-
# talk with multiple clusters.
52-
# @param config_file [String] Path of the kube-config file.
53-
# @param context [String] Set the active context. If is set to nil, current_context from config file will be used.
54-
# @return [Kubernetes::ApiClient] Api client for Kubernetes cluster
55-
def new_client(
56-
config_file=ENV['KUBE_CONFIG'],
57-
context: nil
58-
)
59-
config_file ||= KUBE_CONFIG_DEFAULT_LOCATION
60-
client_configuration = Configuration.new
61-
load(config_file, context: context, client_configuration: client_configuration)
62-
ApiClient.new(client_configuration)
63-
end
64-
6532
def list_context_names(config_file=KUBE_CONFIG_DEFAULT_LOCATION)
6633
config = self.new(config_file)
6734
return config.list_context_names

kubernetes/lib/kubernetes/utils.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright 2017 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'kubernetes/config/incluster_config'
16+
require 'kubernetes/config/kube_config'
17+
18+
module Kubernetes
19+
20+
#
21+
# Use the service account kubernetes gives to pods to connect to kubernetes
22+
# cluster. It's intended for clients that expect to be running inside a pod
23+
# running on kubernetes. It will raise an exception if called from a process
24+
# not running in a kubernetes environment.
25+
def load_incluster_config(client_configuration: Configuration.default)
26+
config = InClusterConfig.new
27+
config.configure(client_configuration)
28+
end
29+
30+
#
31+
# Loads authentication and cluster information from kube-config file
32+
# and stores them in Kubernetes::Configuration.
33+
# @param config_file [String] Path of the kube-config file.
34+
# @param context [String] Set the active context. If is set to nil, current_context from config file will be used.
35+
# @param client_configuration [Kubernetes::Configuration] The Kubernetes::Configuration tp set configs to.
36+
def load_kube_config(
37+
config_file=ENV['KUBECONFIG'],
38+
context: nil,
39+
client_configuration: Configuration.default
40+
)
41+
config_file ||= KubeConfig::KUBE_CONFIG_DEFAULT_LOCATION
42+
config = KubeConfig.new(config_file)
43+
config.configure(client_configuration, context)
44+
end
45+
46+
#
47+
# Loads configuration the same as load_kube_config but returns an ApiClient
48+
# to be used with any API object. This will allow the caller to concurrently
49+
# talk with multiple clusters.
50+
# @param config_file [String] Path of the kube-config file.
51+
# @param context [String] Set the active context. If is set to nil, current_context from config file will be used.
52+
# @return [Kubernetes::ApiClient] Api client for Kubernetes cluster
53+
def new_client_from_config(
54+
config_file=ENV['KUBECONFIG'],
55+
context: nil
56+
)
57+
config_file ||= KubeConfig::KUBE_CONFIG_DEFAULT_LOCATION
58+
client_configuration = Configuration.new
59+
load_kube_config(config_file, context: context, client_configuration: client_configuration)
60+
ApiClient.new(client_configuration)
61+
end
62+
63+
extend self
64+
end

kubernetes/spec/config/kube_config_spec.rb

Lines changed: 1 addition & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -15,156 +15,11 @@
1515
require 'base64'
1616
require 'spec_helper'
1717
require 'config/matchers'
18+
require 'fixtures/config/kube_config_hash'
1819

1920
require 'kubernetes/config/kube_config'
2021

2122

22-
TEST_TOKEN_FILE = Kubernetes::Testing.file_fixture('tokens/token')
23-
24-
TEST_DATA = "test-data"
25-
TEST_DATA_BASE64 = Base64.strict_encode64(TEST_DATA)
26-
27-
TEST_HOST = "http://test-host"
28-
TEST_USERNAME = "me"
29-
TEST_PASSWORD = "pass"
30-
# token for me:pass
31-
TEST_BASIC_TOKEN = "Basic bWU6cGFzcw=="
32-
33-
TEST_SSL_HOST = "https://test-host"
34-
TEST_CERTIFICATE_AUTH = "cert-auth"
35-
TEST_CERTIFICATE_AUTH_BASE64 = Base64.strict_encode64(TEST_CERTIFICATE_AUTH)
36-
TEST_CLIENT_KEY = "client-key"
37-
TEST_CLIENT_KEY_BASE64 = Base64.strict_encode64(TEST_CLIENT_KEY)
38-
TEST_CLIENT_CERT = "client-cert"
39-
TEST_CLIENT_CERT_BASE64 = Base64.strict_encode64(TEST_CLIENT_CERT)
40-
41-
TEST_CONTEXT_DEFAULT = {
42-
'name' => 'default',
43-
'context' => {
44-
'cluster' => 'default',
45-
'user' => 'default',
46-
}
47-
}
48-
TEST_CONTEXT_NO_USER = {
49-
'name' => 'no_user',
50-
'context' => {
51-
'cluster' => 'default',
52-
}
53-
}
54-
TEST_CONTEXT_SSL = {
55-
'name' => 'context_ssl',
56-
'context' => {
57-
'cluster' => 'ssl-file',
58-
'user' => 'user_cert_file',
59-
}
60-
}
61-
TEST_CONTEXT_TOKEN = {
62-
'name' => 'context_token',
63-
'context' => {
64-
'cluster' => 'ssl-file',
65-
'user' => 'simple_token',
66-
}
67-
}
68-
69-
TEST_CLUSTER_DEFAULT = {
70-
'name' => 'default',
71-
'cluster' => {
72-
'server' => TEST_HOST,
73-
},
74-
}
75-
TEST_CLUSTER_SSL_FILE = {
76-
'name' => 'ssl-file',
77-
'cluster' => {
78-
'server' => TEST_SSL_HOST,
79-
'certificate-authority' => Kubernetes::Testing.file_fixture('certs/ca.crt').to_s,
80-
},
81-
}
82-
TEST_CLUSTER_SSL_DATA = {
83-
'name' => 'ssl-data',
84-
'cluster' => {
85-
'server' => TEST_SSL_HOST,
86-
'certificate-authority-data' => TEST_CERTIFICATE_AUTH_BASE64,
87-
},
88-
}
89-
TEST_CLUSTER_INSECURE = {
90-
'name' => 'ssl-data-insecure',
91-
'cluster' => {
92-
'server' => TEST_SSL_HOST,
93-
'certificate-authority-data' => TEST_CERTIFICATE_AUTH_BASE64,
94-
'insecure-skip-tls-verify' => true,
95-
},
96-
}
97-
98-
TEST_USER_DEFAULT = {
99-
'name' => 'default',
100-
'user' => {
101-
'token' => TEST_DATA_BASE64,
102-
}
103-
}
104-
TEST_USER_SIMPLE_TOKEN = {
105-
'name' => 'simple_token',
106-
'user' => {
107-
'token' => TEST_DATA_BASE64,
108-
'username' => TEST_USERNAME, # should be ignored
109-
'password' => TEST_PASSWORD, # should be ignored
110-
},
111-
}
112-
TEST_USER_SIMPLE_TOKEN_FILE = {
113-
'name' => 'simple_token_file',
114-
'user' => {
115-
'tokenFile' => TEST_TOKEN_FILE,
116-
'username' => TEST_USERNAME, # should be ignored
117-
'password' => TEST_PASSWORD, # should be ignored
118-
},
119-
}
120-
TEST_USER_USER_PASS = {
121-
'name' => 'user_pass',
122-
'user' => {
123-
'username' => TEST_USERNAME, # should be ignored
124-
'password' => TEST_PASSWORD, # should be ignored
125-
},
126-
}
127-
TEST_USER_CERT_DATA = {
128-
'name' => 'user_cert_data',
129-
'user' => {
130-
'token' => TEST_DATA_BASE64,
131-
'client-certificate-data' => TEST_CLIENT_CERT_BASE64,
132-
'client-key-data' => TEST_CLIENT_KEY_BASE64,
133-
},
134-
}
135-
TEST_USER_CERT_FILE = {
136-
'name' => 'user_cert_file',
137-
'user' => {
138-
'client-certificate' => Kubernetes::Testing.file_fixture('certs/client.crt').to_s,
139-
'client-key' => Kubernetes::Testing.file_fixture('certs/client.key').to_s,
140-
},
141-
}
142-
143-
TEST_KUBE_CONFIG = {
144-
'current-context' => 'no_user',
145-
'contexts' => [
146-
TEST_CONTEXT_DEFAULT,
147-
TEST_CONTEXT_NO_USER,
148-
TEST_CONTEXT_SSL,
149-
TEST_CONTEXT_TOKEN,
150-
],
151-
'clusters' => [
152-
TEST_CLUSTER_DEFAULT,
153-
TEST_CLUSTER_SSL_FILE,
154-
TEST_CLUSTER_SSL_DATA,
155-
TEST_CLUSTER_INSECURE,
156-
],
157-
'users' => [
158-
TEST_USER_DEFAULT,
159-
TEST_USER_SIMPLE_TOKEN,
160-
TEST_USER_SIMPLE_TOKEN_FILE,
161-
TEST_USER_USER_PASS,
162-
TEST_USER_CERT_DATA,
163-
TEST_USER_CERT_FILE,
164-
],
165-
}
166-
167-
16823
describe Kubernetes::KubeConfig do
16924
let(:kube_config) { Kubernetes::KubeConfig.new(file_fixture('config/config').to_s, TEST_KUBE_CONFIG) }
17025

0 commit comments

Comments
 (0)