forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KubernetesResourceUtil.java
480 lines (441 loc) · 15.9 KB
/
KubernetesResourceUtil.java
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* 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.fabric8.kubernetes.client.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.DefaultKubernetesResourceList;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.Event;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.fabric8.kubernetes.client.readiness.Readiness;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public class KubernetesResourceUtil {
private KubernetesResourceUtil() {
}
public static final Pattern KUBERNETES_DNS1123_LABEL_REGEX = Pattern.compile("[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?");
private static final Pattern INVALID_LABEL_CHARS_PATTERN = Pattern.compile("[^-A-Za-z0-9]+");
private static final String DEFAULT_CONTAINER_IMAGE_REGISTRY_SECRET_NAME = "container-image-registry-secret";
/**
* Returns the resource version for the entity or null if it does not have one
*
* @param entity entity provided
* @return returns resource version of provided entity
*/
public static String getResourceVersion(HasMetadata entity) {
if (entity != null) {
ObjectMeta metadata = entity.getMetadata();
if (metadata != null) {
String resourceVersion = metadata.getResourceVersion();
if (!Utils.isNullOrEmpty(resourceVersion)) {
return resourceVersion;
}
}
}
return null;
}
/**
* Set resource version of a kubernetes resource
*
* @param entity entity provided
* @param resourceVersion updated resource version
*/
public static void setResourceVersion(HasMetadata entity, String resourceVersion) {
if (entity != null) {
ObjectMeta metadata = entity.getMetadata();
if (metadata != null) {
metadata.setResourceVersion(resourceVersion);
}
}
}
/**
* Set namespace of a kubernetes resource if possible
*
* @param entity entity provided
* @param namespace the new namesapce
*/
public static void setNamespace(HasMetadata entity, String namespace) {
if (entity != null) {
ObjectMeta metadata = entity.getMetadata();
if (metadata != null) {
metadata.setNamespace(namespace);
}
}
}
/**
* Returns the kind of the entity
*
* @param entity provided entity
* @return returns kind of entity provided
*/
public static String getKind(HasMetadata entity) {
if (entity != null) {
// TODO use reflection to find the kind?
if (entity instanceof KubernetesList) {
return "List";
} else {
return entity.getClass().getSimpleName();
}
} else {
return null;
}
}
/**
* Returns Qualified name for the specified Kubernetes Resource
*
* @param entity Kubernetes resource
* @return returns qualified name
*/
public static String getQualifiedName(HasMetadata entity) {
if (entity != null) {
return "" + getNamespace(entity) + "/" + getName(entity);
} else {
return null;
}
}
/**
* Returns name of the resource from it's Metadata
*
* @param entity Kubernetes resource
* @return returns name of resource
*/
public static String getName(HasMetadata entity) {
if (entity != null) {
return getName(entity.getMetadata());
} else {
return null;
}
}
/**
* Returns true if this entity has a valid non blank resourceVersion in its metadata
*
* @param entity entity provided
* @return returns a boolean value indicating whether it has a valid non blank resourceVersion
*/
public static boolean hasResourceVersion(HasMetadata entity) {
return getResourceVersion(entity) != null;
}
/**
* Returns name of the resource from it's Metadata
*
* @param entity MetaData of kubernetes resource
* @return returns name of resource
*/
public static String getName(ObjectMeta entity) {
if (entity != null) {
return Utils.coalesce(entity.getName(),
getAdditionalPropertyText(entity.getAdditionalProperties(), "id"),
entity.getUid());
} else {
return null;
}
}
/**
* Used to get additional properties from Object's metadata
*
* @param additionalProperties additional properties
* @param name name of resource
* @return returns additional property text
*/
protected static String getAdditionalPropertyText(Map<String, Object> additionalProperties, String name) {
if (additionalProperties != null) {
Object value = additionalProperties.get(name);
if (value != null) {
return value.toString();
}
}
return null;
}
/**
* Null safe get operation for getting namespace from Kubernetes Resource's MetaData
*
* @param entity Kubernetes Resource
* @return returns namespace as plain string
*/
public static String getNamespace(ObjectMeta entity) {
if (entity != null) {
return entity.getNamespace();
} else {
return null;
}
}
/**
* Getting namespace from Kubernetes Resource
*
* @param entity Kubernetes Resource
* @return returns namespace as plain string
*/
public static String getNamespace(HasMetadata entity) {
if (entity != null) {
return getNamespace(entity.getMetadata());
} else {
return null;
}
}
/**
* Null safe get for fetching annotations from MetaData of Kubernetes Resource
*
* @param entity Kubernetes resource
* @return returns a hashmap containing annotations
*/
public static Map<String, String> getOrCreateAnnotations(HasMetadata entity) {
ObjectMeta metadata = getOrCreateMetadata(entity);
Map<String, String> answer = metadata.getAnnotations();
if (answer == null) {
// use linked so the annotations can be in the FIFO order
answer = new LinkedHashMap<>();
metadata.setAnnotations(answer);
}
return answer;
}
/**
* Returns an identifier from the given string that can be used as resource name.
*
* @param name which needs to be sanitized
* @return sanitized name
*/
public static String sanitizeName(String name) {
if (name != null) {
name = INVALID_LABEL_CHARS_PATTERN.matcher(name).replaceAll("-");
if (!Character.isLetterOrDigit(name.charAt(0))) {
name = "a" + name;
}
if (name.length() > 63) {
name = name.substring(0, 63);
}
name = name.toLowerCase();
final int lastChar = name.length() - 1;
if (!Character.isLetterOrDigit(name.charAt(lastChar))) {
name = name.substring(0, lastChar - 1) + "a";
}
return name;
}
return null;
}
/**
* Null safe get method for getting Labels of a Kubernetes Resource
*
* @param entity Kubernetes Resource
* @return returns a hashmap containing labels
*/
public static Map<String, String> getOrCreateLabels(HasMetadata entity) {
ObjectMeta metadata = getOrCreateMetadata(entity);
Map<String, String> answer = metadata.getLabels();
if (answer == null) {
// use linked so the annotations can be in the FIFO order
answer = new LinkedHashMap<>();
metadata.setLabels(answer);
}
return answer;
}
/**
* Returns the labels of the given metadata object or an empty map if the metadata or labels are null
*
* @param metadata ObjectMeta for resource's metadata
* @return returns labels as a hashmap
*/
@SuppressWarnings("unchecked")
public static Map<String, String> getLabels(ObjectMeta metadata) {
if (metadata != null) {
Map<String, String> labels = metadata.getLabels();
if (labels != null) {
return labels;
}
}
return Collections.emptyMap();
}
/**
* Null safe operation for getting Metadata of a Kubernetes resource
*
* @param entity Kubernetes Resource
* @return returns ObjectMeta as metadata
*/
public static ObjectMeta getOrCreateMetadata(HasMetadata entity) {
ObjectMeta metadata = entity.getMetadata();
if (metadata == null) {
metadata = new ObjectMeta();
entity.setMetadata(metadata);
}
return metadata;
}
/**
* Validates name of Kubernetes Resource name, label or annotation based on Kubernetes regex
*
* @param name Name of resource/label/annotation
* @return returns a boolean value indicating whether it's valid or not
*/
public static boolean isValidName(String name) {
return Utils.isNotNullOrEmpty(name) && KUBERNETES_DNS1123_LABEL_REGEX.matcher(name).matches();
}
/**
* Validates labels/annotations of Kubernetes resources
*
* @param map Label/Annotation of resource
* @return returns a boolean value indicating whether it's valid or not
*/
public static boolean isValidLabelOrAnnotation(Map<String, String> map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
if (!(isValidName(entry.getKey()) && isValidName(entry.getValue()))) {
return false;
}
}
return true;
}
/**
* Checks whether the resource has some controller(parent) or not.
*
* @param resource resource
* @return boolean value indicating whether it's a child or not.
*/
public static boolean hasController(HasMetadata resource) {
return getControllerUid(resource) != null;
}
public static OwnerReference getControllerUid(HasMetadata resource) {
if (resource.getMetadata() != null) {
List<OwnerReference> ownerReferenceList = resource.getMetadata().getOwnerReferences();
for (OwnerReference ownerReference : ownerReferenceList) {
if (Boolean.TRUE.equals(ownerReference.getController())) {
return ownerReference;
}
}
}
return null;
}
public static void sortEventListBasedOnTimestamp(List<Event> eventList) {
if (eventList != null) {
// Sort to get latest events in beginning, putting events without lastTimestamp first
eventList.sort(Comparator.comparing(Event::getLastTimestamp,
Comparator.nullsFirst(Comparator.comparing(Instant::parse).reversed())));
}
}
public static List<EnvVar> convertMapToEnvVarList(Map<String, String> envVarMap) {
List<EnvVar> envVars = new ArrayList<>();
for (Map.Entry<String, String> entry : envVarMap.entrySet()) {
if (entry.getKey() != null && entry.getValue() != null) {
envVars.add(new EnvVarBuilder()
.withName(entry.getKey())
.withValue(entry.getValue())
.build());
}
}
return envVars;
}
/**
* Check whether a Kubernetes resource is Ready or not. Applicable only to
* Deployment, ReplicaSet, Pod, ReplicationController, Endpoints, Node and
* StatefulSet
*
* @param item item which needs to be checked
* @return boolean value indicating it's status
*/
public static boolean isResourceReady(HasMetadata item) {
return Readiness.getInstance().isReady(item);
}
/**
* Calculates age of a kubernetes resource
*
* @param kubernetesResource
* @return a positive duration indicating age of the kubernetes resource
*/
public static Duration getAge(HasMetadata kubernetesResource) {
Instant instant = Instant.parse(kubernetesResource.getMetadata().getCreationTimestamp());
return Duration.between(instant, Instant.now()).abs();
}
public static <T extends HasMetadata> Class<? extends KubernetesResourceList> inferListType(Class<T> type) {
return (Class<? extends KubernetesResourceList>) loadRelated(type, "List", DefaultKubernetesResourceList.class);
}
public static <T extends HasMetadata, V extends VisitableBuilder<T, V>> Class<V> inferBuilderType(Class<T> type) {
return (Class<V>) loadRelated(type, "Builder", null);
}
private static Class<?> loadRelated(Class<?> type, String suffix, Class<?> defaultClass) {
try {
return Thread.currentThread().getContextClassLoader().loadClass(type.getName() + suffix);
} catch (ClassNotFoundException | ClassCastException | NullPointerException e) {
try {
return type.getClassLoader().loadClass(type.getName() + suffix);
} catch (ClassNotFoundException | ClassCastException | NullPointerException ex) {
return defaultClass;
}
}
}
/**
* Create Secret by using username and password.
*
* @param dockerServer User to store key value pair for auths map
* @param username username that needs to be used during secret creation
* @param password password that needs to be used during secret creation
*
* secret's default name : "container-image-registry-secret" is the default name for secret
* @return an object of Secret
*/
public static Secret createDockerRegistrySecret(String dockerServer, String username, String password)
throws JsonProcessingException {
Map<String, Object> dockerConfigMap = createDockerRegistryConfigMap(dockerServer, username, password);
String dockerConfigAsStr = Serialization.asJson(dockerConfigMap);
return createDockerSecret(DEFAULT_CONTAINER_IMAGE_REGISTRY_SECRET_NAME, dockerConfigAsStr);
}
/**
* Create Secret by using username,password and secretName.
*
* @param dockerServer User to store key value pair for auths map
* @param username username that needs to be used during secret creation
* @param password password that needs to be used during secret creation
* @param secretName secretName that needs to be used during secret creation
* @return an object of Secret
*/
public static Secret createDockerRegistrySecret(String dockerServer, String username, String password, String secretName)
throws JsonProcessingException {
Map<String, Object> dockerConfigMap = createDockerRegistryConfigMap(dockerServer, username, password);
String dockerConfigAsStr = Serialization.asJson(dockerConfigMap);
return createDockerSecret(secretName, dockerConfigAsStr);
}
private static Map<String, Object> createDockerRegistryConfigMap(String dockerServer, String username, String password) {
Map<String, Object> dockerConfigMap = new HashMap<>();
Map<String, Object> auths = new HashMap<>();
Map<String, Object> credentials = new HashMap<>();
credentials.put("username", username);
credentials.put("password", password);
String usernameAndPasswordAuth = username + ":" + password;
credentials.put("auth", Base64.getEncoder().encodeToString(usernameAndPasswordAuth.getBytes(StandardCharsets.UTF_8)));
auths.put(dockerServer, credentials);
dockerConfigMap.put("auths", auths);
return dockerConfigMap;
}
private static Secret createDockerSecret(String secretName, String dockerConfig) {
return new SecretBuilder()
.withNewMetadata().withName(secretName).endMetadata()
.withType("kubernetes.io/dockerconfigjson")
.addToData(".dockerconfigjson", Base64.getEncoder().encodeToString(dockerConfig.getBytes(StandardCharsets.UTF_8)))
.build();
}
}