From 983c4a50d083be8e9a980613d98937a587450b18 Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Wed, 3 Oct 2018 15:36:00 +1300 Subject: Remove api_groups from KubeClient constructor We should have access to #core_client, #rbac_client, and #extensions_client without having to pass in an awkward array. Also change api_version to default_api_version, which allows us to use a different version for an individual client. Special case for apis/extensions which only go up to v1beta1 Makes #hashed_client private Removes the #clients and #discover! methods which are un-used --- lib/gitlab/kubernetes/kube_client.rb | 47 ++++----- spec/lib/gitlab/kubernetes/kube_client_spec.rb | 129 ++++++++----------------- 2 files changed, 59 insertions(+), 117 deletions(-) diff --git a/lib/gitlab/kubernetes/kube_client.rb b/lib/gitlab/kubernetes/kube_client.rb index 588238de608..0125b6005cf 100644 --- a/lib/gitlab/kubernetes/kube_client.rb +++ b/lib/gitlab/kubernetes/kube_client.rb @@ -19,6 +19,8 @@ module Gitlab 'apis/extensions' ].freeze + LATEST_EXTENSIONS_VERSION = 'v1beta1' + # Core API methods delegates to the core api group client delegate :get_pods, :get_secrets, @@ -55,48 +57,39 @@ module Gitlab :watch_pod_log, to: :core_client - def initialize(api_prefix, api_groups = ['api'], api_version = 'v1', **kubeclient_options) - raise ArgumentError unless check_api_groups_supported?(api_groups) + attr_reader :api_prefix, :kubeclient_options, :default_api_version + def initialize(api_prefix, default_api_version = 'v1', **kubeclient_options) @api_prefix = api_prefix - @api_groups = api_groups - @api_version = api_version @kubeclient_options = kubeclient_options - end + @default_api_version = default_api_version - def discover! - clients.each(&:discover) + @hashed_clients = {} end - def clients - hashed_clients.values + def core_client(api_version: default_api_version) + build_kubeclient('api', api_version) end - def core_client - hashed_clients['api'] + def rbac_client(api_version: default_api_version) + build_kubeclient('apis/rbac.authorization.k8s.io', api_version) end - def rbac_client - hashed_clients['apis/rbac.authorization.k8s.io'] + def extensions_client(api_version: LATEST_EXTENSIONS_VERSION) + build_kubeclient('apis/extensions', api_version) end - def extensions_client - hashed_clients['apis/extensions'] - end + private - def hashed_clients - strong_memoize(:hashed_clients) do - @api_groups.map do |api_group| - api_url = join_api_url(@api_prefix, api_group) - [api_group, ::Kubeclient::Client.new(api_url, @api_version, **@kubeclient_options)] - end.to_h - end - end + def build_kubeclient(api_group, api_version) + raise ArgumentError, "Unknown api group #{api_group}" unless SUPPORTED_API_GROUPS.include?(api_group) - private + key = api_group_with_version(api_group, api_version) + @hashed_clients[key] ||= ::Kubeclient::Client.new(join_api_url(api_prefix, api_group), api_version, **kubeclient_options) + end - def check_api_groups_supported?(api_groups) - api_groups.all? {|api_group| SUPPORTED_API_GROUPS.include?(api_group) } + def api_group_with_version(api_group, api_version) + api_group + '/' + api_version end def join_api_url(api_prefix, api_path) diff --git a/spec/lib/gitlab/kubernetes/kube_client_spec.rb b/spec/lib/gitlab/kubernetes/kube_client_spec.rb index 53c5a4e7c94..ba38e2c31ea 100644 --- a/spec/lib/gitlab/kubernetes/kube_client_spec.rb +++ b/spec/lib/gitlab/kubernetes/kube_client_spec.rb @@ -6,103 +6,87 @@ describe Gitlab::Kubernetes::KubeClient do include KubernetesHelpers let(:api_url) { 'https://kubernetes.example.com/prefix' } - let(:api_groups) { ['api', 'apis/rbac.authorization.k8s.io'] } let(:api_version) { 'v1' } let(:kubeclient_options) { { auth_options: { bearer_token: 'xyz' } } } - let(:client) { described_class.new(api_url, api_groups, api_version, kubeclient_options) } + let(:client) { described_class.new(api_url, api_version, kubeclient_options) } before do stub_kubeclient_discover(api_url) end - describe '#hashed_clients' do - subject { client.hashed_clients } - - it 'has keys from api groups' do - expect(subject.keys).to match_array api_groups + shared_examples 'a Kubeclient' do + it 'is a Kubeclient::Client' do + is_expected.to be_an_instance_of Kubeclient::Client end - it 'has values of Kubeclient::Client' do - expect(subject.values).to all(be_an_instance_of Kubeclient::Client) + it 'has the kubeclient options' do + expect(subject.auth_options).to eq({ bearer_token: 'xyz' }) end end - describe '#clients' do - subject { client.clients } - - it 'is not empty' do - is_expected.to be_present - end - - it 'is an array of Kubeclient::Client objects' do - is_expected.to all(be_an_instance_of Kubeclient::Client) - end - - it 'has each API group url' do - expected_urls = api_groups.map { |group| "#{api_url}/#{group}" } + describe '#core_client' do + subject { client.core_client } - expect(subject.map(&:api_endpoint).map(&:to_s)).to match_array(expected_urls) - end + it_behaves_like 'a Kubeclient' - it 'has the kubeclient options' do - subject.each do |client| - expect(client.auth_options).to eq({ bearer_token: 'xyz' }) - end + it 'has the core API endpoint' do + expect(subject.api_endpoint.to_s).to match(%r{\/api\Z}) end it 'has the api_version' do - subject.each do |client| - expect(client.instance_variable_get(:@api_version)).to eq('v1') - end + expect(subject.instance_variable_get(:@api_version)).to eq('v1') end - end - - describe '#core_client' do - subject { client.core_client } - it 'is a Kubeclient::Client' do - is_expected.to be_an_instance_of Kubeclient::Client - end + context 'different api version' do + subject { client.core_client(api_version: 'v2') } - it 'has the core API endpoint' do - expect(subject.api_endpoint.to_s).to match(%r{\/api\Z}) + it 'has the api_version' do + expect(subject.instance_variable_get(:@api_version)).to eq('v2') + end end end describe '#rbac_client' do subject { client.rbac_client } - it 'is a Kubeclient::Client' do - is_expected.to be_an_instance_of Kubeclient::Client - end + it_behaves_like 'a Kubeclient' it 'has the RBAC API group endpoint' do expect(subject.api_endpoint.to_s).to match(%r{\/apis\/rbac.authorization.k8s.io\Z}) end + + 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 subject { client.extensions_client } - let(:api_groups) { ['apis/extensions'] } - - it 'is a Kubeclient::Client' do - is_expected.to be_an_instance_of Kubeclient::Client - end + it_behaves_like 'a Kubeclient' it 'has the extensions API group endpoint' do expect(subject.api_endpoint.to_s).to match(%r{\/apis\/extensions\Z}) end - end - describe '#discover!' do - it 'makes a discovery request for each API group' do - client.discover! + it 'has the api_version' do + expect(subject.instance_variable_get(:@api_version)).to eq('v1beta1') + end - api_groups.each do |api_group| - discovery_url = api_url + '/' + api_group + '/v1' - expect(WebMock).to have_requested(:get, discovery_url).once + 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 @@ -156,14 +140,6 @@ describe Gitlab::Kubernetes::KubeClient do it 'responds to the method' do expect(client).to respond_to method end - - context 'no rbac client' do - let(:api_groups) { ['api'] } - - it 'throws an error' do - expect { client.public_send(method) }.to raise_error(Module::DelegationError) - end - end end end end @@ -181,22 +157,11 @@ describe Gitlab::Kubernetes::KubeClient do it 'responds to the method' do expect(client).to respond_to :get_deployments end - - context 'no extensions client' do - let(:api_groups) { ['api'] } - let(:api_version) { 'v1' } - - it 'throws an error' do - expect { client.get_deployments }.to raise_error(Module::DelegationError) - end - end end end describe 'non-entity methods' do it 'does not proxy for non-entity methods' do - expect(client.clients.first).to respond_to :proxy_url - expect(client).not_to respond_to :proxy_url end @@ -211,14 +176,6 @@ describe Gitlab::Kubernetes::KubeClient do it 'is delegated to the core client' do expect(client).to delegate_method(:get_pod_log).to(:core_client) end - - context 'when no core client' do - let(:api_groups) { ['apis/extensions'] } - - it 'throws an error' do - expect { client.get_pod_log('pod-name') }.to raise_error(Module::DelegationError) - end - end end describe '#watch_pod_log' do @@ -227,14 +184,6 @@ describe Gitlab::Kubernetes::KubeClient do it 'is delegated to the core client' do expect(client).to delegate_method(:watch_pod_log).to(:core_client) end - - context 'when no core client' do - let(:api_groups) { ['apis/extensions'] } - - it 'throws an error' do - expect { client.watch_pod_log('pod-name') }.to raise_error(Module::DelegationError) - end - end end describe 'methods that do not exist on any client' do -- cgit v1.2.1 From 8d81f2690c5c277065c6f4a1f9eecc5e771d8cfe Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Wed, 3 Oct 2018 17:04:15 +1300 Subject: Update all usages of KubeClient Find and replace everywhere we pass in `api_groups` to KubeClient, as no longer needed --- app/models/clusters/platforms/kubernetes.rb | 5 ++--- app/models/project_services/kubernetes_service.rb | 5 ++--- app/services/clusters/gcp/finalize_creation_service.rb | 6 ++---- .../clusters/gcp/kubernetes/create_service_account_service_spec.rb | 1 - .../clusters/gcp/kubernetes/fetch_kubernetes_token_service_spec.rb | 1 - 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/app/models/clusters/platforms/kubernetes.rb b/app/models/clusters/platforms/kubernetes.rb index e8e943872de..fb11143d5a6 100644 --- a/app/models/clusters/platforms/kubernetes.rb +++ b/app/models/clusters/platforms/kubernetes.rb @@ -107,7 +107,7 @@ module Clusters end def kubeclient - @kubeclient ||= build_kube_client!(api_groups: ['api', 'apis/rbac.authorization.k8s.io']) + @kubeclient ||= build_kube_client! end private @@ -136,7 +136,7 @@ module Clusters Gitlab::NamespaceSanitizer.sanitize(slug) end - def build_kube_client!(api_groups: ['api'], api_version: 'v1') + def build_kube_client!(api_version: 'v1') 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_groups, api_version, auth_options: kubeclient_auth_options, ssl_options: kubeclient_ssl_options, diff --git a/app/models/project_services/kubernetes_service.rb b/app/models/project_services/kubernetes_service.rb index f119555f16b..66b07aea1c2 100644 --- a/app/models/project_services/kubernetes_service.rb +++ b/app/models/project_services/kubernetes_service.rb @@ -144,7 +144,7 @@ class KubernetesService < DeploymentService end def kubeclient - @kubeclient ||= build_kube_client!(api_groups: ['api', 'apis/rbac.authorization.k8s.io']) + @kubeclient ||= build_kube_client! end def deprecated? @@ -182,12 +182,11 @@ class KubernetesService < DeploymentService slug.gsub(/[^-a-z0-9]/, '-').gsub(/^-+/, '') end - def build_kube_client!(api_groups: ['api'], api_version: 'v1') + def build_kube_client!(api_version: 'v1') raise "Incomplete settings" unless api_url && actual_namespace && token Gitlab::Kubernetes::KubeClient.new( api_url, - api_groups, api_version, auth_options: kubeclient_auth_options, ssl_options: kubeclient_ssl_options, diff --git a/app/services/clusters/gcp/finalize_creation_service.rb b/app/services/clusters/gcp/finalize_creation_service.rb index 3ae0a4a19d0..78b6a5cf7cb 100644 --- a/app/services/clusters/gcp/finalize_creation_service.rb +++ b/app/services/clusters/gcp/finalize_creation_service.rb @@ -60,17 +60,15 @@ module Clusters 'https://' + gke_cluster.endpoint, Base64.decode64(gke_cluster.master_auth.cluster_ca_certificate), gke_cluster.master_auth.username, - gke_cluster.master_auth.password, - api_groups: ['api', 'apis/rbac.authorization.k8s.io'] + gke_cluster.master_auth.password ) end - def build_kube_client!(api_url, ca_pem, username, password, api_groups: ['api'], api_version: 'v1') + def build_kube_client!(api_url, ca_pem, username, password, api_version: 'v1') raise "Incomplete settings" unless api_url && username && password Gitlab::Kubernetes::KubeClient.new( api_url, - api_groups, api_version, auth_options: { username: username, password: password }, ssl_options: kubeclient_ssl_options(ca_pem), diff --git a/spec/services/clusters/gcp/kubernetes/create_service_account_service_spec.rb b/spec/services/clusters/gcp/kubernetes/create_service_account_service_spec.rb index 065d021db5e..b096f1fa4fb 100644 --- a/spec/services/clusters/gcp/kubernetes/create_service_account_service_spec.rb +++ b/spec/services/clusters/gcp/kubernetes/create_service_account_service_spec.rb @@ -16,7 +16,6 @@ describe Clusters::Gcp::Kubernetes::CreateServiceAccountService do let(:kubeclient) do Gitlab::Kubernetes::KubeClient.new( api_url, - ['api', 'apis/rbac.authorization.k8s.io'], auth_options: { username: username, password: password } ) end diff --git a/spec/services/clusters/gcp/kubernetes/fetch_kubernetes_token_service_spec.rb b/spec/services/clusters/gcp/kubernetes/fetch_kubernetes_token_service_spec.rb index c543de21d5b..2355827fa5a 100644 --- a/spec/services/clusters/gcp/kubernetes/fetch_kubernetes_token_service_spec.rb +++ b/spec/services/clusters/gcp/kubernetes/fetch_kubernetes_token_service_spec.rb @@ -11,7 +11,6 @@ describe Clusters::Gcp::Kubernetes::FetchKubernetesTokenService do let(:kubeclient) do Gitlab::Kubernetes::KubeClient.new( api_url, - ['api', 'apis/rbac.authorization.k8s.io'], auth_options: { username: username, password: password } ) end -- cgit v1.2.1 From 27979aac0a9975933e246a7f0fb987b255b47d96 Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Tue, 9 Oct 2018 10:18:16 +1300 Subject: Split hashed_clients into one per api_group Essentially make #build_kubeclient do less. Should be much clearer now --- lib/gitlab/kubernetes/kube_client.rb | 43 +++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/gitlab/kubernetes/kube_client.rb b/lib/gitlab/kubernetes/kube_client.rb index 0125b6005cf..4527ca2accb 100644 --- a/lib/gitlab/kubernetes/kube_client.rb +++ b/lib/gitlab/kubernetes/kube_client.rb @@ -63,33 +63,54 @@ module Gitlab @api_prefix = api_prefix @kubeclient_options = kubeclient_options @default_api_version = default_api_version - - @hashed_clients = {} end def core_client(api_version: default_api_version) - build_kubeclient('api', api_version) + core_clients[api_version] end def rbac_client(api_version: default_api_version) - build_kubeclient('apis/rbac.authorization.k8s.io', api_version) + rbac_clients[api_version] end def extensions_client(api_version: LATEST_EXTENSIONS_VERSION) - build_kubeclient('apis/extensions', api_version) + extensions_clients[api_version] end private - def build_kubeclient(api_group, api_version) - raise ArgumentError, "Unknown api group #{api_group}" unless SUPPORTED_API_GROUPS.include?(api_group) + def core_clients + strong_memoize(:core_clients) do + Hash.new do |hash, api_version| + hash[api_version] = build_kubeclient('api', api_version) + end + end + end - key = api_group_with_version(api_group, api_version) - @hashed_clients[key] ||= ::Kubeclient::Client.new(join_api_url(api_prefix, api_group), api_version, **kubeclient_options) + def rbac_clients + strong_memoize(:rbac_clients) do + Hash.new do |hash, api_version| + hash[api_version] = build_kubeclient('apis/rbac.authorization.k8s.io', api_version) + end + end end - def api_group_with_version(api_group, api_version) - api_group + '/' + api_version + def extensions_clients + strong_memoize(:extensions_clients) do + Hash.new do |hash, api_version| + hash[api_version] = build_kubeclient('apis/extensions', api_version) + end + end + end + + def build_kubeclient(api_group, api_version) + raise ArgumentError, "Unknown api group #{api_group}" unless SUPPORTED_API_GROUPS.include?(api_group) + + ::Kubeclient::Client.new( + join_api_url(api_prefix, api_group), + api_version, + **kubeclient_options + ) end def join_api_url(api_prefix, api_path) -- cgit v1.2.1 From 170071e365040e6a802918f23f80f7850d1f4bb5 Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Wed, 17 Oct 2018 16:43:53 +1300 Subject: DRY up *_clients methods Build using `define_method` directly from the constant, saves us writing api_groups strings twice. --- lib/gitlab/kubernetes/kube_client.rb | 46 ++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/gitlab/kubernetes/kube_client.rb b/lib/gitlab/kubernetes/kube_client.rb index 4527ca2accb..310b5a7a98b 100644 --- a/lib/gitlab/kubernetes/kube_client.rb +++ b/lib/gitlab/kubernetes/kube_client.rb @@ -13,11 +13,11 @@ module Gitlab class KubeClient include Gitlab::Utils::StrongMemoize - SUPPORTED_API_GROUPS = [ - 'api', - 'apis/rbac.authorization.k8s.io', - 'apis/extensions' - ].freeze + SUPPORTED_API_GROUPS = { + core: 'api', + rbac: 'apis/rbac.authorization.k8s.io', + extensions: 'apis/extensions' + }.freeze LATEST_EXTENSIONS_VERSION = 'v1beta1' @@ -79,32 +79,16 @@ module Gitlab private - def core_clients - strong_memoize(:core_clients) do + def build_client(cache_name, api_group) + strong_memoize(cache_name) do Hash.new do |hash, api_version| - hash[api_version] = build_kubeclient('api', api_version) - end - end - end - - def rbac_clients - strong_memoize(:rbac_clients) do - Hash.new do |hash, api_version| - hash[api_version] = build_kubeclient('apis/rbac.authorization.k8s.io', api_version) - end - end - end - - def extensions_clients - strong_memoize(:extensions_clients) do - Hash.new do |hash, api_version| - hash[api_version] = build_kubeclient('apis/extensions', 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.include?(api_group) + 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), @@ -121,6 +105,18 @@ 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 -- cgit v1.2.1 From a5419138fd1dd766713a3754c8406133b3d9b99b Mon Sep 17 00:00:00 2001 From: Thong Kuah Date: Tue, 23 Oct 2018 23:52:34 +1300 Subject: 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. --- app/models/clusters/platforms/kubernetes.rb | 3 +- app/models/project_services/kubernetes_service.rb | 3 +- .../clusters/gcp/finalize_creation_service.rb | 3 +- lib/gitlab/kubernetes/kube_client.rb | 55 ++++++---------------- spec/lib/gitlab/kubernetes/kube_client_spec.rb | 28 +---------- 5 files changed, 18 insertions(+), 74 deletions(-) diff --git a/app/models/clusters/platforms/kubernetes.rb b/app/models/clusters/platforms/kubernetes.rb index fb11143d5a6..f0f791742f4 100644 --- a/app/models/clusters/platforms/kubernetes.rb +++ b/app/models/clusters/platforms/kubernetes.rb @@ -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'] diff --git a/app/models/project_services/kubernetes_service.rb b/app/models/project_services/kubernetes_service.rb index 66b07aea1c2..798944d0c06 100644 --- a/app/models/project_services/kubernetes_service.rb +++ b/app/models/project_services/kubernetes_service.rb @@ -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'] diff --git a/app/services/clusters/gcp/finalize_creation_service.rb b/app/services/clusters/gcp/finalize_creation_service.rb index 78b6a5cf7cb..6ee63db8eb9 100644 --- a/app/services/clusters/gcp/finalize_creation_service.rb +++ b/app/services/clusters/gcp/finalize_creation_service.rb @@ -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'] diff --git a/lib/gitlab/kubernetes/kube_client.rb b/lib/gitlab/kubernetes/kube_client.rb index 310b5a7a98b..0350fbab28e 100644 --- a/lib/gitlab/kubernetes/kube_client.rb +++ b/lib/gitlab/kubernetes/kube_client.rb @@ -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 diff --git a/spec/lib/gitlab/kubernetes/kube_client_spec.rb b/spec/lib/gitlab/kubernetes/kube_client_spec.rb index ba38e2c31ea..eed4135d8a2 100644 --- a/spec/lib/gitlab/kubernetes/kube_client_spec.rb +++ b/spec/lib/gitlab/kubernetes/kube_client_spec.rb @@ -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 -- cgit v1.2.1