diff options
author | Thong Kuah <tkuah@gitlab.com> | 2018-10-17 16:43:53 +1300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2018-10-23 23:38:44 +1300 |
commit | 170071e365040e6a802918f23f80f7850d1f4bb5 (patch) | |
tree | 1e963ee8adf92862de907d2e5479c6b0c6afc316 /lib | |
parent | 27979aac0a9975933e246a7f0fb987b255b47d96 (diff) | |
download | gitlab-ce-170071e365040e6a802918f23f80f7850d1f4bb5.tar.gz |
DRY up *_clients methods
Build using `define_method` directly from the constant, saves us writing
api_groups strings twice.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/kubernetes/kube_client.rb | 46 |
1 files 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 |