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