diff options
author | Thong Kuah <tkuah@gitlab.com> | 2018-10-03 15:36:00 +1300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2018-10-23 23:36:43 +1300 |
commit | 983c4a50d083be8e9a980613d98937a587450b18 (patch) | |
tree | b8d579898ad79b9f6cd408f00d38b54b85b6d3fa /lib | |
parent | 4cf1845e7ea5d9a52fe5af49dd1644f98a921010 (diff) | |
download | gitlab-ce-983c4a50d083be8e9a980613d98937a587450b18.tar.gz |
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
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/kubernetes/kube_client.rb | 47 |
1 files changed, 20 insertions, 27 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) |