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
Instance Attribute Summary collapse
-
#api_resources ⇒ Array<K8s::Resource>
Cached APIResources.
- #api_version ⇒ String readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#api_resources! ⇒ Array<K8s::Resource>
Force-update APIResources.
-
#api_resources? ⇒ Bool
Loaded yet?.
- #client_for_resource(resource, namespace: nil) ⇒ K8s::ResourceClient
- #find_api_resource(resource_name) ⇒ K8s::Resource
-
#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.
20 21 22 23 |
# File 'lib/k8s/api_client.rb', line 20 def initialize(transport, api_version) @transport = transport @api_version = api_version end |
Instance Attribute Details
#api_resources ⇒ Array<K8s::Resource>
Cached APIResources
52 53 54 |
# File 'lib/k8s/api_client.rb', line 52 def api_resources @api_resources || api_resources! end |
#api_version ⇒ String (readonly)
26 27 28 |
# File 'lib/k8s/api_client.rb', line 26 def api_version @api_version end |
Class Method Details
.path(api_version) ⇒ String
10 11 12 13 14 15 16 |
# File 'lib/k8s/api_client.rb', line 10 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::Resource>
Force-update APIResources
45 46 47 |
# File 'lib/k8s/api_client.rb', line 45 def api_resources! @api_resources = @transport.get(path).resources end |
#api_resources? ⇒ Bool
Returns loaded yet?.
35 36 37 |
# File 'lib/k8s/api_client.rb', line 35 def api_resources? !!@api_resources end |
#client_for_resource(resource, namespace: nil) ⇒ K8s::ResourceClient
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/k8s/api_client.rb', line 79 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 found_resource = api_resources.find{ |api_resource| api_resource.kind == resource.kind } found_resource ||= api_resources!.find{ |api_resource| api_resource.kind == resource.kind } raise K8s::Error::UndefinedResource, "Unknown resource kind=#{resource.kind} for #{@api_version}" unless found_resource ResourceClient.new(@transport, self, found_resource, namespace: resource..namespace || namespace) end |
#find_api_resource(resource_name) ⇒ K8s::Resource
59 60 61 62 63 64 65 |
# File 'lib/k8s/api_client.rb', line 59 def find_api_resource(resource_name) found_resource = api_resources.find{ |api_resource| api_resource.name == resource_name } found_resource ||= api_resources!.find{ |api_resource| api_resource.name == resource_name } raise K8s::Error::UndefinedResource, "Unknown resource #{resource_name} for #{@api_version}" unless found_resource found_resource end |
#list_resources(resources = nil, **options) ⇒ Array<K8s::Resource>
Pipeline list requests for multiple resource types.
Returns flattened array with mixed resource kinds.
109 110 111 112 113 |
# File 'lib/k8s/api_client.rb', line 109 def list_resources(resources = nil, **) resources ||= self.resources.select(&:list?) ResourceClient.list(resources, @transport, **) end |
#path(*path) ⇒ String
30 31 32 |
# File 'lib/k8s/api_client.rb', line 30 def path(*path) @transport.path(self.class.path(@api_version), *path) end |
#resource(resource_name, namespace: nil) ⇒ K8s::ResourceClient
71 72 73 |
# File 'lib/k8s/api_client.rb', line 71 def resource(resource_name, namespace: nil) ResourceClient.new(@transport, self, find_api_resource(resource_name), namespace: namespace) end |
#resources(namespace: nil) ⇒ Array<K8s::ResourceClient>
TODO: skip non-namespaced resources if namespace is given, or ignore namespace?
95 96 97 98 99 100 |
# File 'lib/k8s/api_client.rb', line 95 def resources(namespace: nil) api_resources.map{ |api_resource| ResourceClient.new(@transport, self, api_resource, namespace: namespace) } end |