Class: K8s::ResourceClient
- Inherits:
-
Object
- Object
- K8s::ResourceClient
- Extended by:
- Utils
- Includes:
- Utils
- Defined in:
- lib/k8s/resource_client.rb
Overview
Per-APIResource type client.
Used to get/list/update/patch/delete specific types of resources, optionally in some specific namespace.
Defined Under Namespace
Modules: Utils
Instance Attribute Summary collapse
- #namespace ⇒ String? readonly
- #resource ⇒ String readonly
-
#resource_class ⇒ class
readonly
K8s::Resource.
- #subresource ⇒ String? readonly
Class Method Summary collapse
-
.list(resources, transport, namespace: nil, labelSelector: nil, fieldSelector: nil, skip_forbidden: false) ⇒ Array<K8s::Resource>
Pipeline list requests for multiple resource types.
Instance Method Summary collapse
- #api_version ⇒ String
- #create? ⇒ Bool
-
#create_resource(resource) ⇒ Object
Instance of resource_class.
- #delete(name, namespace: @namespace, propagationPolicy: nil) ⇒ K8s::Resource
- #delete? ⇒ Boolean
-
#delete_collection(namespace: @namespace, labelSelector: nil, fieldSelector: nil, propagationPolicy: nil) ⇒ Array<Object>
Array of instances of resource_class.
- #delete_resource(resource, **options) ⇒ K8s::Resource
-
#get(name, namespace: @namespace) ⇒ Object
Instance of resource_class.
- #get? ⇒ Bool
-
#get_resource(resource) ⇒ Object
Instance of resource_class.
-
#initialize(transport, api_client, api_resource, namespace: nil, resource_class: K8s::Resource) ⇒ ResourceClient
constructor
A new instance of ResourceClient.
-
#json_patch(name, ops, namespace: @namespace) ⇒ Object
Instance of resource_class.
- #kind ⇒ String
-
#list(labelSelector: nil, fieldSelector: nil, namespace: @namespace) ⇒ Array<Object>
Array of instances of resource_class.
- #list? ⇒ Bool
-
#merge_patch(name, obj, namespace: @namespace, strategic_merge: true) ⇒ Object
Instance of resource_class.
- #meta_list(labelSelector: nil, fieldSelector: nil, namespace: @namespace) ⇒ K8s::Resource
-
#name ⇒ String
Resource or resource/subresource.
- #namespaced? ⇒ Boolean
- #patch? ⇒ Boolean
- #path(name = nil, subresource: @subresource, namespace: @namespace) ⇒ String
-
#process_list(list) ⇒ Array<Object>
Array of instances of resource_class.
- #subresource? ⇒ Boolean
- #update? ⇒ Boolean
-
#update_resource(resource) ⇒ Object
Instance of resource_class.
- #watch(labelSelector: nil, fieldSelector: nil, resourceVersion: nil, timeout: nil, namespace: @namespace) {|K8S::WatchEvent| ... } ⇒ Object
Methods included from Utils
Constructor Details
#initialize(transport, api_client, api_resource, namespace: nil, resource_class: K8s::Resource) ⇒ ResourceClient
Returns a new instance of ResourceClient.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/k8s/resource_client.rb', line 72 def initialize(transport, api_client, api_resource, namespace: nil, resource_class: K8s::Resource) @transport = transport @api_client = api_client @api_resource = api_resource @namespace = namespace @resource_class = resource_class if @api_resource.name.include? '/' @resource, @subresource = @api_resource.name.split('/', 2) else @resource = @api_resource.name @subresource = nil end fail "Resource #{api_resource.name} is not namespaced" unless api_resource.namespaced || !namespace end |
Instance Attribute Details
#namespace ⇒ String? (readonly)
100 101 102 |
# File 'lib/k8s/resource_client.rb', line 100 def namespace @namespace end |
#resource ⇒ String (readonly)
103 104 105 |
# File 'lib/k8s/resource_client.rb', line 103 def resource @resource end |
#resource_class ⇒ class (readonly)
Returns K8s::Resource.
119 120 121 |
# File 'lib/k8s/resource_client.rb', line 119 def resource_class @resource_class end |
#subresource ⇒ String? (readonly)
111 112 113 |
# File 'lib/k8s/resource_client.rb', line 111 def subresource @subresource end |
Class Method Details
.list(resources, transport, namespace: nil, labelSelector: nil, fieldSelector: nil, skip_forbidden: false) ⇒ Array<K8s::Resource>
Pipeline list requests for multiple resource types.
Returns flattened array with mixed resource kinds.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/k8s/resource_client.rb', line 52 def self.list(resources, transport, namespace: nil, labelSelector: nil, fieldSelector: nil, skip_forbidden: false) api_paths = resources.map{ |resource| resource.path(namespace: namespace) } api_lists = transport.gets( *api_paths, response_class: K8s::Resource, query: make_query( 'labelSelector' => selector_query(labelSelector), 'fieldSelector' => selector_query(fieldSelector) ), skip_forbidden: skip_forbidden ) resources.zip(api_lists).map { |resource, api_list| api_list ? resource.process_list(api_list) : [] }.flatten end |
Instance Method Details
#api_version ⇒ String
90 91 92 |
# File 'lib/k8s/resource_client.rb', line 90 def api_version @api_client.api_version end |
#create? ⇒ Bool
143 144 145 |
# File 'lib/k8s/resource_client.rb', line 143 def create? @api_resource.verbs.include? 'create' end |
#create_resource(resource) ⇒ Object
Returns instance of resource_class.
149 150 151 152 153 154 155 156 |
# File 'lib/k8s/resource_client.rb', line 149 def create_resource(resource) @transport.request( method: 'POST', path: path(namespace: resource..namespace), request_object: resource, response_class: @resource_class ) end |
#delete(name, namespace: @namespace, propagationPolicy: nil) ⇒ K8s::Resource
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/k8s/resource_client.rb', line 312 def delete(name, namespace: @namespace, propagationPolicy: nil) @transport.request( method: 'DELETE', path: path(name, namespace: namespace), query: make_query( 'propagationPolicy' => propagationPolicy ), response_class: @resource_class ) end |
#delete? ⇒ Boolean
304 305 306 |
# File 'lib/k8s/resource_client.rb', line 304 def delete? @api_resource.verbs.include? 'delete' end |
#delete_collection(namespace: @namespace, labelSelector: nil, fieldSelector: nil, propagationPolicy: nil) ⇒ Array<Object>
Returns array of instances of resource_class.
328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/k8s/resource_client.rb', line 328 def delete_collection(namespace: @namespace, labelSelector: nil, fieldSelector: nil, propagationPolicy: nil) list = @transport.request( method: 'DELETE', path: path(namespace: namespace), query: make_query( 'labelSelector' => selector_query(labelSelector), 'fieldSelector' => selector_query(fieldSelector), 'propagationPolicy' => propagationPolicy ) ) process_list(list) end |
#delete_resource(resource, **options) ⇒ K8s::Resource
345 346 347 |
# File 'lib/k8s/resource_client.rb', line 345 def delete_resource(resource, **) delete(resource..name, namespace: resource..namespace, **) end |
#get(name, namespace: @namespace) ⇒ Object
Returns instance of resource_class.
166 167 168 169 170 171 172 |
# File 'lib/k8s/resource_client.rb', line 166 def get(name, namespace: @namespace) @transport.request( method: 'GET', path: path(name, namespace: namespace), response_class: @resource_class ) end |
#get? ⇒ Bool
159 160 161 |
# File 'lib/k8s/resource_client.rb', line 159 def get? @api_resource.verbs.include? 'get' end |
#get_resource(resource) ⇒ Object
Returns instance of resource_class.
176 177 178 179 180 181 182 |
# File 'lib/k8s/resource_client.rb', line 176 def get_resource(resource) @transport.request( method: 'GET', path: path(resource..name, namespace: resource..namespace), response_class: @resource_class ) end |
#json_patch(name, ops, namespace: @namespace) ⇒ Object
Returns instance of resource_class.
293 294 295 296 297 298 299 300 301 |
# File 'lib/k8s/resource_client.rb', line 293 def json_patch(name, ops, namespace: @namespace) @transport.request( method: 'PATCH', path: path(name, namespace: namespace), content_type: 'application/json-patch+json', request_object: ops, response_class: @resource_class ) end |
#kind ⇒ String
114 115 116 |
# File 'lib/k8s/resource_client.rb', line 114 def kind @api_resource.kind end |
#list(labelSelector: nil, fieldSelector: nil, namespace: @namespace) ⇒ Array<Object>
Returns array of instances of resource_class.
202 203 204 205 |
# File 'lib/k8s/resource_client.rb', line 202 def list(labelSelector: nil, fieldSelector: nil, namespace: @namespace) list = (labelSelector: labelSelector, fieldSelector: fieldSelector, namespace: namespace) process_list(list) end |
#list? ⇒ Bool
185 186 187 |
# File 'lib/k8s/resource_client.rb', line 185 def list? @api_resource.verbs.include? 'list' end |
#merge_patch(name, obj, namespace: @namespace, strategic_merge: true) ⇒ Object
Returns instance of resource_class.
279 280 281 282 283 284 285 286 287 |
# File 'lib/k8s/resource_client.rb', line 279 def merge_patch(name, obj, namespace: @namespace, strategic_merge: true) @transport.request( method: 'PATCH', path: path(name, namespace: namespace), content_type: strategic_merge ? 'application/strategic-merge-patch+json' : 'application/merge-patch+json', request_object: obj, response_class: @resource_class ) end |
#meta_list(labelSelector: nil, fieldSelector: nil, namespace: @namespace) ⇒ K8s::Resource
211 212 213 214 215 216 217 218 219 220 |
# File 'lib/k8s/resource_client.rb', line 211 def (labelSelector: nil, fieldSelector: nil, namespace: @namespace) @transport.request( method: 'GET', path: path(namespace: namespace), query: make_query( 'labelSelector' => selector_query(labelSelector), 'fieldSelector' => selector_query(fieldSelector) ) ) end |
#name ⇒ String
Returns resource or resource/subresource.
95 96 97 |
# File 'lib/k8s/resource_client.rb', line 95 def name @api_resource.name end |
#namespaced? ⇒ Boolean
122 123 124 |
# File 'lib/k8s/resource_client.rb', line 122 def namespaced? !!@api_resource.namespaced end |
#patch? ⇒ Boolean
270 271 272 |
# File 'lib/k8s/resource_client.rb', line 270 def patch? @api_resource.verbs.include? 'patch' end |
#path(name = nil, subresource: @subresource, namespace: @namespace) ⇒ String
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/k8s/resource_client.rb', line 130 def path(name = nil, subresource: @subresource, namespace: @namespace) namespace_part = namespace ? ['namespaces', namespace] : [] if name && subresource @api_client.path(*namespace_part, @resource, name, subresource) elsif name @api_client.path(*namespace_part, @resource, name) else @api_client.path(*namespace_part, @resource) end end |
#process_list(list) ⇒ Array<Object>
Returns array of instances of resource_class.
191 192 193 194 195 196 |
# File 'lib/k8s/resource_client.rb', line 191 def process_list(list) list.items.map { |item| # list items omit kind/apiVersion @resource_class.new(item.merge('apiVersion' => list.apiVersion, 'kind' => @api_resource.kind)) } end |
#subresource? ⇒ Boolean
106 107 108 |
# File 'lib/k8s/resource_client.rb', line 106 def subresource? !!@subresource end |
#update? ⇒ Boolean
254 255 256 |
# File 'lib/k8s/resource_client.rb', line 254 def update? @api_resource.verbs.include? 'update' end |
#update_resource(resource) ⇒ Object
Returns instance of resource_class.
260 261 262 263 264 265 266 267 |
# File 'lib/k8s/resource_client.rb', line 260 def update_resource(resource) @transport.request( method: 'PUT', path: path(resource..name, namespace: resource..namespace), request_object: resource, response_class: @resource_class ) end |
#watch(labelSelector: nil, fieldSelector: nil, resourceVersion: nil, timeout: nil, namespace: @namespace) {|K8S::WatchEvent| ... } ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/k8s/resource_client.rb', line 228 def watch(labelSelector: nil, fieldSelector: nil, resourceVersion: nil, timeout: nil, namespace: @namespace) method = 'GET' path = path(namespace: namespace) parser = K8s::JSONParser.new do |data| yield K8s::WatchEvent.new(data) end @transport.request( method: method, path: path, read_timeout: nil, query: make_query( 'labelSelector' => selector_query(labelSelector), 'fieldSelector' => selector_query(fieldSelector), 'resourceVersion' => resourceVersion, 'watch' => '1', 'timeoutSeconds' => timeout ), response_block: lambda do |chunk, _, _| parser << chunk end ) end |