forked from kubernetes-client/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscovery.java
More file actions
284 lines (253 loc) · 9.33 KB
/
Discovery.java
File metadata and controls
284 lines (253 loc) · 9.33 KB
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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.ApiResponse;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.models.V1APIGroup;
import io.kubernetes.client.openapi.models.V1APIGroupList;
import io.kubernetes.client.openapi.models.V1APIResourceList;
import io.kubernetes.client.openapi.models.V1APIVersions;
import io.kubernetes.client.util.exception.IncompleteDiscoveryException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import okhttp3.Call;
public class Discovery {
private final ApiClient apiClient;
public Discovery() {
this(Configuration.getDefaultApiClient());
}
public Discovery(ApiClient apiClient) {
this.apiClient = apiClient;
}
public Set<APIResource> findAll() throws ApiException {
Set<APIResource> allResources = new HashSet<>();
for (String version : legacyCoreApi().getVersions()) {
allResources.addAll(findAll("", Arrays.asList(version), version, "/api/" + version));
}
IncompleteDiscoveryException incompleteDiscoveryException = null;
for (V1APIGroup group : groupDiscovery("/apis").getGroups()) {
try {
allResources.addAll(
findAll(
group.getName(),
group.getVersions().stream().map(v -> v.getVersion()).collect(Collectors.toList()),
group.getPreferredVersion().getVersion()));
} catch (ApiException e){
IncompleteDiscoveryException resourceDiscoveryException = new IncompleteDiscoveryException(
String.format("Unable to retrieve the complete list of server APIs: %s/%s : %s",
group.getName(), group.getPreferredVersion().getVersion(), e.getResponseBody()),
e, allResources);
if (incompleteDiscoveryException == null) {
incompleteDiscoveryException = resourceDiscoveryException;
}else {
incompleteDiscoveryException.addSuppressed(resourceDiscoveryException);
}
}
}
if (incompleteDiscoveryException != null) {
throw incompleteDiscoveryException;
}
return allResources;
}
public Set<APIResource> findAll(String group, List<String> versions, String preferredVersion)
throws ApiException {
return findAll(group, versions, preferredVersion, "/apis/" + group + "/" + preferredVersion);
}
public Set<APIResource> findAll(
String group, List<String> versions, String preferredVersion, String path)
throws ApiException {
V1APIResourceList resourceList = resourceDiscovery(path);
return groupResourcesByName(group, versions, preferredVersion, resourceList);
}
public Set<APIResource> groupResourcesByName(
String group,
List<String> versions,
String preferredVersion,
V1APIResourceList resourceList) {
// parse raw discovery responses to APIResource for better readability
Set<APIResource> resources =
resourceList.getResources().stream()
.filter(r -> !getSubResourceNameIfPossible(r.getName()).isPresent())
.map(
r ->
new APIResource(
group,
versions,
preferredVersion,
r.getKind(),
r.getNamespaced(),
r.getName(),
r.getSingularName()))
.collect(Collectors.toSet());
// wiring up connections between major-resource and sub-resources
Map<String, Set<String>> subResources = manageRelationFromResourceToSubResources(resourceList);
resources.stream()
.forEach(
r -> {
if (subResources.containsKey(r.getResourcePlural())) {
r.subResources.addAll(subResources.get(r.getResourcePlural()));
}
});
return resources;
}
private Map<String, Set<String>> manageRelationFromResourceToSubResources(
V1APIResourceList resourceList) {
Map<String, Set<String>> subResources = new HashMap<>();
resourceList.getResources().stream()
.forEach(r -> subResources.put(r.getName(), new HashSet<>()));
resourceList.getResources().stream()
.forEach(
r -> {
getSubResourceNameIfPossible(r.getName())
.ifPresent(
subResourceName -> {
subResources
.computeIfAbsent(
getMajorResourceName(r.getName()),
majorResourceName -> new HashSet<>())
.add(subResourceName);
});
});
return subResources;
}
private String getMajorResourceName(String discoveredResourceName) {
String[] parts = discoveredResourceName.split("/", 2);
return parts[0];
}
private Optional<String> getSubResourceNameIfPossible(String discoveredResourceName) {
boolean isSubResource = discoveredResourceName.contains("/");
if (!isSubResource) {
return Optional.empty();
}
String[] parts = discoveredResourceName.split("/", 2);
return Optional.of(parts[1]);
}
public V1APIVersions legacyCoreApi() throws ApiException {
return versionDiscovery("/api");
}
public V1APIGroupList groupDiscovery(String path) throws ApiException {
return get(V1APIGroupList.class, path);
}
public V1APIVersions versionDiscovery(String path) throws ApiException {
return get(V1APIVersions.class, path);
}
public V1APIResourceList resourceDiscovery(String path) throws ApiException {
return get(V1APIResourceList.class, path);
}
private <T> T get(Class<T> returnTypeClass, String urlPath) throws ApiException {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
Call call =
apiClient.buildCall(
urlPath,
"GET",
null,
null,
null,
headers,
Collections.emptyMap(),
Collections.emptyMap(),
new String[] {"BearerToken"},
null);
ApiResponse<T> resourceList = apiClient.execute(call, returnTypeClass);
return resourceList.getData();
}
public static class APIResource {
private final String group;
private final String kind;
private final List<String> versions;
private final String preferredVersion;
private final Boolean isNamespaced;
private final String resourcePlural;
private final String resourceSingular;
private final List<String> subResources;
public APIResource(
String group,
List<String> versions,
String preferredVersion,
String kind,
Boolean isNamespaced,
String resourcePlural,
String resourceSingular) {
this.group = group;
this.versions = versions;
this.preferredVersion = preferredVersion;
this.kind = kind;
this.isNamespaced = isNamespaced;
this.resourcePlural = resourcePlural;
this.resourceSingular = resourceSingular;
this.subResources = new ArrayList<>();
}
public String getGroup() {
return group;
}
public List<String> getVersions() {
return versions;
}
public String getPreferredVersion() {
return preferredVersion;
}
public String getKind() {
return kind;
}
public Boolean getNamespaced() {
return isNamespaced;
}
public String getResourcePlural() {
return resourcePlural;
}
public String getResourceSingular() {
return resourceSingular;
}
public List<String> getSubResources() {
return subResources;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
APIResource that = (APIResource) o;
return Objects.equals(group, that.group)
&& Objects.equals(kind, that.kind)
&& Objects.equals(versions, that.versions)
&& Objects.equals(preferredVersion, that.preferredVersion)
&& Objects.equals(isNamespaced, that.isNamespaced)
&& Objects.equals(resourcePlural, that.resourcePlural)
&& Objects.equals(resourceSingular, that.resourceSingular)
&& Objects.equals(subResources, that.subResources);
}
@Override
public int hashCode() {
return Objects.hash(
group,
kind,
versions,
preferredVersion,
isNamespaced,
resourcePlural,
resourceSingular,
subResources);
}
}
}