diff options
author | Thong Kuah <tkuah@gitlab.com> | 2018-10-23 23:52:34 +1300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2018-10-23 23:52:34 +1300 |
commit | a5419138fd1dd766713a3754c8406133b3d9b99b (patch) | |
tree | 563efbb8e705745f63148821e92e930bc4eb3286 | |
parent | 170071e365040e6a802918f23f80f7850d1f4bb5 (diff) | |
download | gitlab-ce-a5419138fd1dd766713a3754c8406133b3d9b99b.tar.gz |
Store version within SUPPORTED_API_GROUPS hashrefactor_gitlab_kube_client
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.
-rw-r--r-- | app/models/clusters/platforms/kubernetes.rb | 3 | ||||
-rw-r--r-- | app/models/project_services/kubernetes_service.rb | 3 | ||||
-rw-r--r-- | app/services/clusters/gcp/finalize_creation_service.rb | 3 | ||||
-rw-r--r-- | lib/gitlab/kubernetes/kube_client.rb | 55 | ||||
-rw-r--r-- | 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 |