Commit 170071e3 authored by Thong Kuah's avatar Thong Kuah

DRY up *_clients methods

Build using `define_method` directly from the constant, saves us writing
api_groups strings twice.
parent 27979aac
...@@ -13,11 +13,11 @@ module Gitlab ...@@ -13,11 +13,11 @@ module Gitlab
class KubeClient class KubeClient
include Gitlab::Utils::StrongMemoize include Gitlab::Utils::StrongMemoize
SUPPORTED_API_GROUPS = [ SUPPORTED_API_GROUPS = {
'api', core: 'api',
'apis/rbac.authorization.k8s.io', rbac: 'apis/rbac.authorization.k8s.io',
'apis/extensions' extensions: 'apis/extensions'
].freeze }.freeze
LATEST_EXTENSIONS_VERSION = 'v1beta1' LATEST_EXTENSIONS_VERSION = 'v1beta1'
...@@ -79,32 +79,16 @@ module Gitlab ...@@ -79,32 +79,16 @@ module Gitlab
private private
def core_clients def build_client(cache_name, api_group)
strong_memoize(:core_clients) do strong_memoize(cache_name) do
Hash.new do |hash, api_version| Hash.new do |hash, api_version|
hash[api_version] = build_kubeclient('api', api_version) hash[api_version] = build_kubeclient(api_group, 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)
end end
end end
end end
def build_kubeclient(api_group, api_version) 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( ::Kubeclient::Client.new(
join_api_url(api_prefix, api_group), join_api_url(api_prefix, api_group),
...@@ -121,6 +105,18 @@ module Gitlab ...@@ -121,6 +105,18 @@ module Gitlab
url.to_s url.to_s
end 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 end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment