k8s-client 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/k8s-client.rb +1 -0
- data/lib/k8s/client.rb +23 -9
- data/lib/k8s/client/version.rb +1 -1
- data/lib/k8s/stack.rb +1 -0
- data/lib/k8s/transport.rb +8 -7
- data/lib/k8s/util.rb +22 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 767ebc980b6e01f66e4739cc341aa7518245287d88273068460358b88f8b110f
|
4
|
+
data.tar.gz: 73a21392fdf0ad9e56060571aced26e0b53983f6f2c1b727280a16d72dbaa8a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2943973befbad7b7f24b6f9aa9aa186a20ca5e970259bcb2a354e8161511af4a42d29aeceb75ab26f9031c414f56d0f7f7cc606723e4f585c637427d4e59e6d
|
7
|
+
data.tar.gz: a8161a1c2b74a027395a33ee3ff39ea4b87554615d861933a5a0c04cac4525086bc84ca91234ab7c62b308b067ec441b7b396c7fb9e3e08493141814a3acf0b2
|
data/lib/k8s-client.rb
CHANGED
data/lib/k8s/client.rb
CHANGED
@@ -58,19 +58,22 @@ module K8s
|
|
58
58
|
end
|
59
59
|
|
60
60
|
# @param api_versions [Array<String>] defaults to all APIs
|
61
|
+
# @param prefetch_resources [Boolean] prefetch any missing api_resources for each api_version
|
62
|
+
# @param skip_missing [Boolean] return APIClient without api_resources? if 404
|
61
63
|
# @return [Array<APIClient>]
|
62
|
-
def apis(api_versions = nil, prefetch_resources: false)
|
64
|
+
def apis(api_versions = nil, prefetch_resources: false, skip_missing: false)
|
63
65
|
api_versions ||= ['v1'] + self.api_groups
|
64
66
|
|
65
67
|
if prefetch_resources
|
66
68
|
# api groups that are missing their api_resources
|
67
69
|
api_paths = api_versions
|
70
|
+
.uniq
|
68
71
|
.select{|api_version| !api(api_version).api_resources? }
|
69
72
|
.map{|api_version| APIClient.path(api_version) }
|
70
73
|
|
71
74
|
# load into APIClient.api_resources=
|
72
|
-
@transport.gets(*api_paths, response_class: K8s::API::MetaV1::APIResourceList).each do |api_resource_list|
|
73
|
-
api(api_resource_list.groupVersion).api_resources = api_resource_list.resources
|
75
|
+
@transport.gets(*api_paths, response_class: K8s::API::MetaV1::APIResourceList, skip_missing: skip_missing).each do |api_resource_list|
|
76
|
+
api(api_resource_list.groupVersion).api_resources = api_resource_list.resources if api_resource_list
|
74
77
|
end
|
75
78
|
end
|
76
79
|
|
@@ -116,21 +119,32 @@ module K8s
|
|
116
119
|
client_for_resource(resource).get_resource(resource)
|
117
120
|
end
|
118
121
|
|
122
|
+
# Returns nils for any resources that do not exist.
|
123
|
+
# This includes custom resources that were not yet defined.
|
124
|
+
#
|
119
125
|
# @param resources [Array<K8s::Resource>]
|
120
|
-
# @return [Array<K8s::Resource, nil>]
|
126
|
+
# @return [Array<K8s::Resource, nil>] matching resources array 1:1
|
121
127
|
def get_resources(resources)
|
122
|
-
# prefetch api resources
|
123
|
-
apis(resources.map{|resource| resource.apiVersion }
|
128
|
+
# prefetch api resources, skip missing APIs
|
129
|
+
resource_apis = apis(resources.map{ |resource| resource.apiVersion }, prefetch_resources: true, skip_missing: true)
|
130
|
+
|
131
|
+
# map each resource to excon request options, or nil if resource is not (yet) defined
|
132
|
+
requests = resources.zip(resource_apis).map{ |resource, api_client|
|
133
|
+
next nil unless api_client.api_resources?
|
134
|
+
|
135
|
+
resource_client = api_client.client_for_resource(resource)
|
124
136
|
|
125
|
-
resource_clients = resources.map{|resource| client_for_resource(resource) }
|
126
|
-
requests = resources.zip(resource_clients).map{|resource, resource_client|
|
127
137
|
{
|
128
138
|
method: 'GET',
|
129
139
|
path: resource_client.path(resource.metadata.name, namespace: resource.metadata.namespace),
|
130
140
|
response_class: resource_client.resource_class,
|
131
141
|
}
|
132
142
|
}
|
133
|
-
|
143
|
+
|
144
|
+
# map non-nil requests to response objects, or nil for nil request options
|
145
|
+
Util.compact_map(requests) { |reqs|
|
146
|
+
@transport.requests(*reqs, skip_missing: true)
|
147
|
+
}
|
134
148
|
end
|
135
149
|
|
136
150
|
# @param resource [K8s::Resource]
|
data/lib/k8s/client/version.rb
CHANGED
data/lib/k8s/stack.rb
CHANGED
@@ -124,6 +124,7 @@ module K8s
|
|
124
124
|
client.delete_resource(resource)
|
125
125
|
rescue K8s::Error::NotFound
|
126
126
|
# assume aliased objects in multiple API groups, like for Deployments
|
127
|
+
# alternatively, a custom resource whose definition was already deleted earlier
|
127
128
|
end
|
128
129
|
end
|
129
130
|
end
|
data/lib/k8s/transport.rb
CHANGED
@@ -201,19 +201,21 @@ module K8s
|
|
201
201
|
# @param skip_missing [Boolean] return nil for HTTP 404 responses
|
202
202
|
# @param retry_errors [Boolean] retry with non-pipelined request for HTTP 503 responses
|
203
203
|
# @return [Array<response_class, Hash, nil>]
|
204
|
-
def requests(*options,
|
204
|
+
def requests(*options, skip_missing: false, retry_errors: true, **common_options)
|
205
205
|
return [] if options.empty? # excon chokes
|
206
206
|
|
207
207
|
start = Time.now
|
208
208
|
responses = excon.requests(
|
209
|
-
options.map{|options| request_options(**options)}
|
209
|
+
options.map{|options| request_options(**common_options.merge(options))}
|
210
210
|
)
|
211
211
|
t = Time.now - start
|
212
212
|
|
213
213
|
objects = responses.zip(options).map{|response, request_options|
|
214
|
+
response_class = request_options[:response_class] || common_options[:response_class]
|
215
|
+
|
214
216
|
begin
|
215
217
|
parse_response(response, request_options,
|
216
|
-
response_class:
|
218
|
+
response_class: response_class,
|
217
219
|
)
|
218
220
|
rescue K8s::Error::NotFound
|
219
221
|
if skip_missing
|
@@ -226,7 +228,7 @@ module K8s
|
|
226
228
|
logger.warn { "Retry #{format_request(request_options)} => HTTP #{exc.code} #{exc.reason} in #{'%.3f' % t}s" }
|
227
229
|
|
228
230
|
# only retry the failed request, not the entire pipeline
|
229
|
-
request(response_class: response_class, **request_options)
|
231
|
+
request(response_class: response_class, **common_options.merge(request_options))
|
230
232
|
else
|
231
233
|
raise
|
232
234
|
end
|
@@ -250,13 +252,12 @@ module K8s
|
|
250
252
|
end
|
251
253
|
|
252
254
|
# @param *paths [String]
|
253
|
-
def gets(*paths,
|
255
|
+
def gets(*paths, **options)
|
254
256
|
requests(*paths.map{|path| {
|
255
257
|
method: 'GET',
|
256
258
|
path: self.path(path),
|
257
|
-
**options,
|
258
259
|
} },
|
259
|
-
|
260
|
+
**options
|
260
261
|
)
|
261
262
|
end
|
262
263
|
end
|
data/lib/k8s/util.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module K8s
|
2
|
+
module Util
|
3
|
+
# Yield with all non-nil args, returning matching array with corresponding return values or nils.
|
4
|
+
#
|
5
|
+
# Args must be usable as hash keys. Duplicate args will all map to the same return value.
|
6
|
+
#
|
7
|
+
# @param args [Array<nil, Object>]
|
8
|
+
# @yield args
|
9
|
+
# @yieldparam args [Array<Object>] omitting any nil values
|
10
|
+
# @return [Array<nil, Object>] matching args array 1:1, containing yielded values for non-nil args
|
11
|
+
def self.compact_map(args)
|
12
|
+
func_args = args.compact
|
13
|
+
|
14
|
+
values = yield func_args
|
15
|
+
|
16
|
+
# Hash{arg => value}
|
17
|
+
value_map = Hash[func_args.zip(values)]
|
18
|
+
|
19
|
+
args.map{|arg| value_map[arg] }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: k8s-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kontena, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: excon
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- lib/k8s/resource_client.rb
|
160
160
|
- lib/k8s/stack.rb
|
161
161
|
- lib/k8s/transport.rb
|
162
|
+
- lib/k8s/util.rb
|
162
163
|
homepage: https://github.com/kontena/k8s-client
|
163
164
|
licenses:
|
164
165
|
- Apache-2.0
|