-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_client.cr
334 lines (292 loc) · 10.9 KB
/
resource_client.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require "./transport"
require "./api_client"
module Kube
# Per-APIResource type client.
#
# Used to get/list/update/patch/delete specific types of resources, optionally in some specific namespace.
class ResourceClient(T)
@@logger = ::Log.for(Kube::ResourceClient)
@logger : ::Log
def logger
@logger
end
def self.logger
@@logger
end
# Pipeline list requests for multiple resource types.
#
# Returns flattened array with mixed resource kinds.
#
# `skip_forbidden` [Boolean] skip resources that return HTTP 403 errors
def self.list(resources : Array(ResourceClient), transport : Transport, namespace : String? = nil,
label_selector = nil, field_selector = nil, skip_forbidden = false) : Indexable
api_paths = resources.map(&.path(namespace: namespace))
api_lists = transport.gets(
api_paths,
response_class: K8S::Kubernetes::Resource,
query: make_query({
"labelSelector" => selector_query(label_selector),
"fieldSelector" => selector_query(field_selector),
}),
skip_forbidden: skip_forbidden
)
# api_lists.each { |r| puts r.class }
# resources.zip(api_lists)
resources.zip(api_lists).flat_map do |resource, api_list|
api_list ? resource.process_list(api_list) : [] of K8S::Kubernetes::Resource
end.reject(Nil)
end
module Utils
def selector_query(selector : String | Symbol | Hash(String, String) | Nil) : String?
case selector
when Nil
nil
when Symbol
selector.to_s
when String
selector
when Hash
selector.map { |k, v| "#{k}=#{v}" }.join ","
else
raise Kube::Error.new "Invalid selector type. #{selector.inspect}"
end
end
def make_query(options) : Hash(String, String | Array(String))?
query = Hash(String, String | Array(String)).new
options.each do |k, v|
query[k] = v unless v.nil?
end
return nil if query.empty?
query
end
end
include Utils
extend Utils
private property transport : Kube::Transport
private property api_client : Kube::ApiClient
getter api_resource : K8S::Apimachinery::Apis::Meta::V1::APIResource
getter namespace : String? = nil
getter resource : String
getter subresource : String? = nil
getter resource_class = K8S::Kubernetes::Resource
private macro define_new
def self.new(transport, api_client, api_resource : K8S::Apimachinery::Apis::Meta::V1::APIResource, namespace = nil)
ver = (api_resource.version.nil? || api_resource.version.not_nil!.empty?) ? "v1" : api_resource.version.not_nil!
group = api_resource.group.nil? ? "" : api_resource.group.not_nil!
klass = k8s_resource_class(group, ver, api_resource.kind)
case klass
{% for resource in K8S::Kubernetes::Resource.all_subclasses %}
{% if !resource.abstract? && resource.annotation(::K8S::GroupVersionKind) %}
when {{resource.id}}.class
::Kube::ResourceClient({{resource.id}}).new(transport, api_client, api_resource, namespace, {{resource.id}})
{% end %}{% end %}
else
raise Kube::Error::UndefinedResource.new("Unknown resource kind: #{klass.inspect}")
end
rescue ex : K8S::Error::UnknownResource
::Kube::ResourceClient(K8S::Kubernetes::Resource).new(transport, api_client, api_resource, namespace, K8S::Kubernetes::Resource)
end
end
macro finished
define_new
end
def initialize(@transport, @api_client, @api_resource, @namespace, @resource_class)
@logger = ::Log.for("Kube::ResourceClient(#{@resource_class}")
if @api_resource.name.includes? "/"
@resource, @subresource = @api_resource.name.split("/", 2)
else
@resource = @api_resource.name
@subresource = nil
end
# ameba:disable Style/NegatedConditionsInUnless
raise Kube::Error.new("Resource #{api_resource.name} is not namespaced") unless api_resource.namespaced || !namespace
end
def api_version : String
@api_client.api_version
end
# resource or resource/subresource
def name : String
@api_resource.name
end
def kind
@api_resource.kind
end
def subresource?
!!@subresource
end
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
def create? : Bool
@api_resource.verbs.includes? "create"
end
# resource [#metadata] with metadata.namespace and metadata.name set
def create_resource(resource : T) : T
@transport.request(
method: "POST",
path: path(namespace: resource.metadata!.namespace),
request_object: resource,
response_class: @resource_class
).as(T)
end
def get? : Bool
@api_resource.verbs.includes? "get"
end
# raises [`Kube::Error::NotFound`] if resource is not found
def get(name, namespace = @namespace) : T
@transport.request(
method: "GET",
path: path(name, namespace: namespace),
response_class: @resource_class
).as(T)
end
# returns response body as a String instead of T
def get_as_string(name, namespace = @namespace)
@transport.request(
method: "GET",
path: path(name, namespace: namespace),
response_class: @resource_class
).as(String)
end
# raises [`Kube::Error::NotFound`] if resource is not found
def get(resource : T) : T
@transport.request(
method: "GET",
path: path(resource.metadata!.name, namespace: resource.metadata!.namespace),
response_class: @resource_class
).as(T)
end
def list?
@api_resource.verbs.includes? "list"
end
def process_list(list) : Indexable(T)
if (list.is_a?(K8S::Kubernetes::Resource::List(T)))
list.items
else
Array(T).new
end
end
# returns array of instances of resource_class
def list(label_selector : String | Hash(String, String) | Nil = nil,
field_selector : String | Hash(String, String) | Nil = nil, namespace = @namespace) : Indexable(T)
list = meta_list(label_selector: label_selector, field_selector: field_selector, namespace: namespace)
process_list(list)
end
def meta_list(label_selector : String | Hash(String, String) | Nil = nil, field_selector : String | Hash(String, String) | Nil = nil, namespace = @namespace)
@transport.request(
method: "GET",
path: path(namespace: namespace),
query: make_query({
"labelSelector" => selector_query(label_selector),
"fieldSelector" => selector_query(field_selector),
})
)
end
def watch(label_selector : String | Hash(String, String) | Nil = nil,
field_selector : String | Hash(String, String) | Nil = nil,
resource_version : String? = nil, timeout : Int32? = nil,
namespace = @namespace) : Kube::WatchChannel(T)
query = make_query({
"labelSelector" => selector_query(label_selector),
"fieldSelector" => selector_query(field_selector),
"resourceVersion" => resource_version,
"timeoutSeconds" => timeout,
"watch" => "true",
})
logger.warn { "Watching #{query}" }
wc = Kube::WatchChannel(T).new(@transport)
@transport.watch_request(
path: path(namespace: namespace),
query: query,
response_class: K8S::Kubernetes::WatchEvent(T),
response_channel: wc.channel,
)
wc
end
def update? : Bool
@api_resource.verbs.includes? "update"
end
# returns instance of resource_class
def update_resource(resource : T) : T
@transport.request(
method: "PUT",
path: path(resource.metadata!.name, namespace: resource.metadata!.namespace),
request_object: resource,
response_class: @resource_class
).as(T)
end
def patch? : Bool
@api_resource.verbs.includes? "patch"
end
# name [String]
# obj [#to_json]
# namespace [String, nil]
# strategic_merge [Boolean] use kube Strategic Merge Patch instead of standard Merge Patch (arrays of objects are merged by name)
def merge_patch(name, obj, namespace = @namespace, strategic_merge = true) : T
@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
).as(T)
end
# name [String]
# ops [Hash] json-patch operations
# namespace [String, nil]
def json_patch(name, ops, namespace = @namespace) : T
@transport.request(
method: "PATCH",
path: path(name, namespace: namespace),
content_type: "application/json-patch+json",
request_object: ops,
response_class: @resource_class
).as(T)
end
def delete? : Bool
@api_resource.verbs.includes? "delete"
end
# name [String]
# namespace [String, nil]
# propagationPolicy [String, nil] The propagationPolicy to use for the API call. Possible values include “Orphan”, “Foreground”, or “Background”
def delete(name, namespace = @namespace, propagation_policy = nil) : T
@transport.request(
method: "DELETE",
path: path(name, namespace: namespace),
query: make_query(
{"propagationPolicy" => propagation_policy}
),
response_class: @resource_class,
).as(T)
end
# returns array of instances of resource_class
def delete_collection(namespace = @namespace,
label_selector : String | Hash(String, String) | Nil = nil,
field_selector : String | Hash(String, String) | Nil = nil,
propagation_policy : String? = nil) : Indexable(T)
list = @transport.request(
method: "DELETE",
path: path(namespace: namespace),
query: make_query({
"labelSelector" => selector_query(label_selector),
"fieldSelector" => selector_query(field_selector),
"propagationPolicy" => propagation_policy,
})
)
process_list(list)
end
# resource [resource_class] with metadata
# options [Hash]
# see #delete for possible options
def delete_resource(resource : T, **options)
delete(resource.metadata!.name, **options.merge({namespace: resource.metadata!.namespace}))
end
end
end