Store version within SUPPORTED_API_GROUPS hash

This removes the ability to pass in a different version. We can instead
create a new entry in the SUPPORTED_API_GROUPS hash for a different
version if need be.
This commit is contained in:
Thong Kuah 2018-10-23 23:52:34 +13:00
parent 170071e365
commit a5419138fd
5 changed files with 18 additions and 74 deletions

View File

@ -136,7 +136,7 @@ module Clusters
Gitlab::NamespaceSanitizer.sanitize(slug)
end
def build_kube_client!(api_version: 'v1')
def build_kube_client!
raise "Incomplete settings" unless api_url && actual_namespace
unless (username && password) || token
@ -145,7 +145,6 @@ module Clusters
Gitlab::Kubernetes::KubeClient.new(
api_url,
api_version,
auth_options: kubeclient_auth_options,
ssl_options: kubeclient_ssl_options,
http_proxy_uri: ENV['http_proxy']

View File

@ -182,12 +182,11 @@ class KubernetesService < DeploymentService
slug.gsub(/[^-a-z0-9]/, '-').gsub(/^-+/, '')
end
def build_kube_client!(api_version: 'v1')
def build_kube_client!
raise "Incomplete settings" unless api_url && actual_namespace && token
Gitlab::Kubernetes::KubeClient.new(
api_url,
api_version,
auth_options: kubeclient_auth_options,
ssl_options: kubeclient_ssl_options,
http_proxy_uri: ENV['http_proxy']

View File

@ -64,12 +64,11 @@ module Clusters
)
end
def build_kube_client!(api_url, ca_pem, username, password, api_version: 'v1')
def build_kube_client!(api_url, ca_pem, username, password)
raise "Incomplete settings" unless api_url && username && password
Gitlab::Kubernetes::KubeClient.new(
api_url,
api_version,
auth_options: { username: username, password: password },
ssl_options: kubeclient_ssl_options(ca_pem),
http_proxy_uri: ENV['http_proxy']

View File

@ -14,12 +14,20 @@ module Gitlab
include Gitlab::Utils::StrongMemoize
SUPPORTED_API_GROUPS = {
core: 'api',
rbac: 'apis/rbac.authorization.k8s.io',
extensions: 'apis/extensions'
core: { group: 'api', version: 'v1' },
rbac: { group: 'apis/rbac.authorization.k8s.io', version: 'v1' },
extensions: { group: 'apis/extensions', version: 'v1beta1' }
}.freeze
LATEST_EXTENSIONS_VERSION = 'v1beta1'
SUPPORTED_API_GROUPS.each do |name, params|
client_method_name = "#{name}_client".to_sym
define_method(client_method_name) do
strong_memoize(client_method_name) do
build_kubeclient(params[:group], params[:version])
end
end
end
# Core API methods delegates to the core api group client
delegate :get_pods,
@ -57,39 +65,16 @@ module Gitlab
:watch_pod_log,
to: :core_client
attr_reader :api_prefix, :kubeclient_options, :default_api_version
attr_reader :api_prefix, :kubeclient_options
def initialize(api_prefix, default_api_version = 'v1', **kubeclient_options)
def initialize(api_prefix, **kubeclient_options)
@api_prefix = api_prefix
@kubeclient_options = kubeclient_options
@default_api_version = default_api_version
end
def core_client(api_version: default_api_version)
core_clients[api_version]
end
def rbac_client(api_version: default_api_version)
rbac_clients[api_version]
end
def extensions_client(api_version: LATEST_EXTENSIONS_VERSION)
extensions_clients[api_version]
end
private
def build_client(cache_name, api_group)
strong_memoize(cache_name) do
Hash.new do |hash, api_version|
hash[api_version] = build_kubeclient(api_group, api_version)
end
end
end
def build_kubeclient(api_group, api_version)
raise ArgumentError, "Unknown api group #{api_group}" unless SUPPORTED_API_GROUPS.values.include?(api_group)
::Kubeclient::Client.new(
join_api_url(api_prefix, api_group),
api_version,
@ -105,18 +90,6 @@ module Gitlab
url.to_s
end
SUPPORTED_API_GROUPS.each do |name, api_group|
clients_method_name = "#{name}_clients".to_sym
define_method(clients_method_name) do
strong_memoize(clients_method_name.to_sym) do
Hash.new do |hash, api_version|
hash[api_version] = build_kubeclient(api_group, api_version)
end
end
end
end
end
end
end

View File

@ -6,10 +6,9 @@ describe Gitlab::Kubernetes::KubeClient do
include KubernetesHelpers
let(:api_url) { 'https://kubernetes.example.com/prefix' }
let(:api_version) { 'v1' }
let(:kubeclient_options) { { auth_options: { bearer_token: 'xyz' } } }
let(:client) { described_class.new(api_url, api_version, kubeclient_options) }
let(:client) { described_class.new(api_url, kubeclient_options) }
before do
stub_kubeclient_discover(api_url)
@ -37,14 +36,6 @@ describe Gitlab::Kubernetes::KubeClient do
it 'has the api_version' do
expect(subject.instance_variable_get(:@api_version)).to eq('v1')
end
context 'different api version' do
subject { client.core_client(api_version: 'v2') }
it 'has the api_version' do
expect(subject.instance_variable_get(:@api_version)).to eq('v2')
end
end
end
describe '#rbac_client' do
@ -59,14 +50,6 @@ describe Gitlab::Kubernetes::KubeClient do
it 'has the api_version' do
expect(subject.instance_variable_get(:@api_version)).to eq('v1')
end
context 'different api version' do
subject { client.rbac_client(api_version: 'v2') }
it 'has the api_version' do
expect(subject.instance_variable_get(:@api_version)).to eq('v2')
end
end
end
describe '#extensions_client' do
@ -81,14 +64,6 @@ describe Gitlab::Kubernetes::KubeClient do
it 'has the api_version' do
expect(subject.instance_variable_get(:@api_version)).to eq('v1beta1')
end
context 'different api version' do
subject { client.extensions_client(api_version: 'v2') }
it 'has the api_version' do
expect(subject.instance_variable_get(:@api_version)).to eq('v2')
end
end
end
describe 'core API' do
@ -146,7 +121,6 @@ describe Gitlab::Kubernetes::KubeClient do
describe 'extensions API group' do
let(:api_groups) { ['apis/extensions'] }
let(:api_version) { 'v1beta1' }
let(:extensions_client) { client.extensions_client }
describe '#get_deployments' do