Class: K8s::APIClient
- Inherits:
-
Object
- Object
- K8s::APIClient
- Defined in:
- lib/k8s/api_client.rb
Overview
Per-APIGroup/version client.
Offers access to ResourceClient instances for the APIResource types defined in this apigroup/version
Class Method Summary collapse
Instance Method Summary collapse
-
#api_resources ⇒ Array<K8s::API::MetaV1::APIResource>
Cached APIResources.
-
#api_resources! ⇒ Array<K8s::API::MetaV1::APIResource>
Force-update APIResources.
- #api_resources=(api_resources) ⇒ Object
-
#api_resources? ⇒ Bool
Loaded yet?.
- #api_version ⇒ String
- #client_for_resource(resource, namespace: nil) ⇒ K8s::ResourceClient
-
#initialize(transport, api_version) ⇒ APIClient
constructor
A new instance of APIClient.
-
#list_resources(resources = nil, **options) ⇒ Array<K8s::Resource>
Pipeline list requests for multiple resource types.
- #path(*path) ⇒ String
- #resource(resource_name, namespace: nil) ⇒ K8s::ResourceClient
-
#resources(namespace: nil) ⇒ Array<K8s::ResourceClient>
TODO: skip non-namespaced resources if namespace is given, or ignore namespace?.
Constructor Details
#initialize(transport, api_version) ⇒ APIClient
Returns a new instance of APIClient.
19 20 21 22 |
# File 'lib/k8s/api_client.rb', line 19 def initialize(transport, api_version) @transport = transport @api_version = api_version end |
Class Method Details
.path(api_version) ⇒ String
8 9 10 11 12 13 14 15 |
# File 'lib/k8s/api_client.rb', line 8 def self.path(api_version) if api_version.include? '/' File.join('/apis', api_version) else File.join('/api', api_version) end end |
Instance Method Details
#api_resources ⇒ Array<K8s::API::MetaV1::APIResource>
Cached APIResources
57 58 59 |
# File 'lib/k8s/api_client.rb', line 57 def api_resources @api_resources || api_resources! end |
#api_resources! ⇒ Array<K8s::API::MetaV1::APIResource>
Force-update APIResources
48 49 50 51 52 |
# File 'lib/k8s/api_client.rb', line 48 def api_resources! @api_resources = @transport.get(self.path, response_class: K8s::API::MetaV1::APIResourceList, ).resources end |
#api_resources=(api_resources) ⇒ Object
41 42 43 |
# File 'lib/k8s/api_client.rb', line 41 def api_resources=(api_resources) @api_resources = api_resources end |
#api_resources? ⇒ Bool
Returns loaded yet?.
36 37 38 |
# File 'lib/k8s/api_client.rb', line 36 def api_resources? !!@api_resources end |
#api_version ⇒ String
25 26 27 |
# File 'lib/k8s/api_client.rb', line 25 def api_version @api_version end |
#client_for_resource(resource, namespace: nil) ⇒ K8s::ResourceClient
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/k8s/api_client.rb', line 80 def client_for_resource(resource, namespace: nil) unless @api_version == resource.apiVersion raise K8s::Error::UndefinedResource, "Invalid apiVersion=#{resource.apiVersion} for #{@api_version} client" end unless api_resource = api_resources.find{ |api_resource| api_resource.kind == resource.kind } raise K8s::Error::UndefinedResource, "Unknown resource kind=#{resource.kind} for #{@api_version}" end ResourceClient.new(@transport, self, api_resource, namespace: resource..namespace || namespace, ) end |
#list_resources(resources = nil, **options) ⇒ Array<K8s::Resource>
Pipeline list requests for multiple resource types.
Returns flattened array with mixed resource kinds.
111 112 113 114 115 |
# File 'lib/k8s/api_client.rb', line 111 def list_resources(resources = nil, **) resources ||= self.resources.select{|resource| resource.list? } ResourceClient.list(resource, @transport, **) end |
#path(*path) ⇒ String
31 32 33 |
# File 'lib/k8s/api_client.rb', line 31 def path(*path) @transport.path(self.class.path(@api_version), *path) end |
#resource(resource_name, namespace: nil) ⇒ K8s::ResourceClient
65 66 67 68 69 70 71 72 73 |
# File 'lib/k8s/api_client.rb', line 65 def resource(resource_name, namespace: nil) unless api_resource = api_resources.find{ |api_resource| api_resource.name == resource_name } raise K8s::Error::UndefinedResource, "Unknown resource #{resource_name} for #{@api_version}" end ResourceClient.new(@transport, self, api_resource, namespace: namespace, ) end |
#resources(namespace: nil) ⇒ Array<K8s::ResourceClient>
TODO: skip non-namespaced resources if namespace is given, or ignore namespace?
98 99 100 101 102 |
# File 'lib/k8s/api_client.rb', line 98 def resources(namespace: nil) api_resources.map{ |api_resource| ResourceClient.new(@transport, self, api_resource, namespace: namespace, ) } end |