forked from fabric8io/kubernetes-client
/
KubernetesResourceUtil.java
331 lines (303 loc) · 9.6 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
/**
* 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 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.ObjectMeta;
import io.fabric8.kubernetes.api.model.OwnerReference;
import java.time.Instant;
import java.util.Collections;
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]*[a-z0-9])?");
public static final int KUBERNETES_DNS1123_LABEL_MAX_LENGTH = 63;
/**
* 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);
}
}
}
/**
* 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) {
return name.replaceAll("[^A-Za-z0-9]+", "-");
}
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) &&
name.length() < KUBERNETES_DNS1123_LABEL_MAX_LENGTH &&
KUBERNETES_DNS1123_LABEL_REGEX.matcher(name).matches();
}
/**
* Validates labels/annotations of Kubernetes resources
*
* @param map Label/Annotation of resource
* @return returns a boolean value inidicating 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 begining
eventList.sort((o1, o2) -> {
Instant d1 = Instant.parse(o1.getLastTimestamp());
Instant d2 = Instant.parse(o2.getLastTimestamp());
return (int) (d2.getEpochSecond() - d1.getEpochSecond());
});
}
}
}