forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OperationContext.java
552 lines (466 loc) · 17.2 KB
/
OperationContext.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* 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.dsl.internal;
import io.fabric8.kubernetes.api.builder.VisitableBuilder;
import io.fabric8.kubernetes.api.model.DeletionPropagation;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.Client;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.dsl.FieldValidateable;
import io.fabric8.kubernetes.client.dsl.FieldValidateable.Validation;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.impl.BaseClient;
import io.fabric8.kubernetes.client.impl.ResourceHandler;
import io.fabric8.kubernetes.client.utils.ApiVersionUtil;
import io.fabric8.kubernetes.client.utils.Utils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
public class OperationContext {
protected Object item;
protected String resourceVersion;
protected String plural;
protected String apiGroupName;
protected String apiGroupVersion;
protected String namespace;
protected boolean defaultNamespace = true;
protected String name;
protected boolean reloadingFromServer;
protected boolean dryRun;
protected FieldValidateable.Validation fieldValidation;
protected String fieldManager;
protected Boolean forceConflicts;
// Default to -1 to respect the value set in the resource or the Kubernetes default (30 seconds)
protected long gracePeriodSeconds = -1L;
protected DeletionPropagation propagationPolicy;
protected Map<String, String> labels;
protected Map<String, String[]> labelsNot;
protected Map<String, String[]> labelsIn;
protected Map<String, String[]> labelsNotIn;
protected Map<String, String> fields;
protected Map<String, String[]> fieldsNot;
protected String selectorAsString;
protected Client client;
private long timeout;
private TimeUnit timeoutUnit = TimeUnit.MILLISECONDS;
public OperationContext() {
}
public OperationContext(OperationContext other) {
this(other.client, other.plural, other.namespace, other.name, other.apiGroupName, other.apiGroupVersion,
other.item, other.labels, other.labelsNot, other.labelsIn, other.labelsNotIn, other.fields,
other.fieldsNot, other.resourceVersion, other.reloadingFromServer, other.gracePeriodSeconds, other.propagationPolicy,
other.dryRun, other.selectorAsString, other.defaultNamespace, other.fieldValidation, other.fieldManager,
other.forceConflicts, other.timeout, other.timeoutUnit);
}
public OperationContext(Client client, String plural, String namespace, String name,
String apiGroupName, String apiGroupVersion, Object item, Map<String, String> labels,
Map<String, String[]> labelsNot, Map<String, String[]> labelsIn, Map<String, String[]> labelsNotIn,
Map<String, String> fields, Map<String, String[]> fieldsNot, String resourceVersion, boolean reloadingFromServer,
long gracePeriodSeconds, DeletionPropagation propagationPolicy,
boolean dryRun, String selectorAsString, boolean defaultNamespace, FieldValidateable.Validation fieldValidation,
String fieldManager, Boolean forceConflicts, long timeout, TimeUnit timeoutUnit) {
this.client = client;
this.item = item;
this.plural = plural;
setNamespace(namespace, defaultNamespace);
this.name = name;
setApiGroupName(apiGroupName);
setApiGroupVersion(apiGroupVersion);
setLabels(labels);
setLabelsNot(labelsNot);
setLabelsIn(labelsIn);
setLabelsNotIn(labelsNotIn);
setFields(fields);
setFieldsNot(fieldsNot);
this.resourceVersion = resourceVersion;
this.reloadingFromServer = reloadingFromServer;
this.gracePeriodSeconds = gracePeriodSeconds;
this.propagationPolicy = propagationPolicy;
this.dryRun = dryRun;
this.selectorAsString = selectorAsString;
this.fieldValidation = fieldValidation;
this.fieldManager = fieldManager;
this.forceConflicts = forceConflicts;
this.timeout = timeout;
this.timeoutUnit = timeoutUnit;
}
private void setFieldsNot(Map<String, String[]> fieldsNot) {
this.fieldsNot = Utils.getNonNullOrElse(fieldsNot, new HashMap<>());
}
private void setFields(Map<String, String> fields) {
this.fields = Utils.getNonNullOrElse(fields, new HashMap<>());
}
private void setLabelsNotIn(Map<String, String[]> labelsNotIn) {
this.labelsNotIn = Utils.getNonNullOrElse(labelsNotIn, new HashMap<>());
}
private void setLabelsIn(Map<String, String[]> labelsIn) {
this.labelsIn = Utils.getNonNullOrElse(labelsIn, new HashMap<>());
}
private void setLabelsNot(Map<String, String[]> labelsNot) {
this.labelsNot = Utils.getNonNullOrElse(labelsNot, new HashMap<>());
}
private void setLabels(Map<String, String> labels) {
this.labels = Utils.getNonNullOrElse(labels, new HashMap<>());
}
private void setApiGroupVersion(String apiGroupVersion) {
this.apiGroupVersion = ApiVersionUtil.apiVersion(item, apiGroupVersion);
}
private void setApiGroupName(String apiGroupName) {
this.apiGroupName = ApiVersionUtil.apiGroup(item, apiGroupName);
}
private void setNamespace(String namespace, boolean defaultNamespace) {
Config config = getConfig();
if (!defaultNamespace || Utils.isNotNullOrEmpty(namespace)) {
this.namespace = namespace;
this.defaultNamespace = defaultNamespace;
} else if (config != null) {
this.namespace = config.getNamespace();
this.defaultNamespace = config.isDefaultNamespace();
}
}
public Client getClient() {
return client;
}
public HttpClient getHttpClient() {
if (client == null) {
return null;
}
return client.getHttpClient();
}
public Config getConfig() {
if (client == null) {
return null;
}
return client.getConfiguration();
}
public String getPlural() {
return plural;
}
public String getNamespace() {
return namespace;
}
public boolean isDefaultNamespace() {
return defaultNamespace;
}
public String getName() {
return name;
}
public String getApiGroupName() {
return apiGroupName;
}
public String getApiGroupVersion() {
return apiGroupVersion;
}
public Object getItem() {
return item;
}
public Map<String, String> getLabels() {
return labels;
}
public Map<String, String[]> getLabelsNot() {
return labelsNot;
}
public Map<String, String[]> getLabelsIn() {
return labelsIn;
}
public Map<String, String[]> getLabelsNotIn() {
return labelsNotIn;
}
public Map<String, String> getFields() {
return fields;
}
public Map<String, String[]> getFieldsNot() {
return fieldsNot;
}
public String getResourceVersion() {
return resourceVersion;
}
public boolean isReloadingFromServer() {
return reloadingFromServer;
}
public long getGracePeriodSeconds() {
return gracePeriodSeconds;
}
public DeletionPropagation getPropagationPolicy() {
return propagationPolicy;
}
public boolean getDryRun() {
return dryRun;
}
public long getTimeout() {
return timeout;
}
public TimeUnit getTimeoutUnit() {
return timeoutUnit;
}
public String getLabelQueryParam() {
if (Utils.isNotNullOrEmpty(selectorAsString)) {
return selectorAsString;
}
StringBuilder sb = new StringBuilder(101);
appendEntriesAsParam(sb, getLabels(), asKeyValuePair);
appendEntriesAsParam(sb, getLabelsNot(), asNotEqualToValues);
appendEntriesAsParam(sb, getLabelsIn(), asInSet);
appendEntriesAsParam(sb, getLabelsNotIn(), asNotInSet);
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
private static BiFunction<String, String[], String> getEntryProcessorFor(String operator) {
return (key, values) -> key + " " + operator + " (" + String.join(",", values) + ")";
}
private final static BiFunction<String, String, String> asKeyValuePair = (key, value) -> value != null ? key + "=" + value
: key;
private final static BiFunction<String, String[], String> asNotEqualToValues = (key, values) -> Utils.isNotNull(values)
? Arrays.stream(values).map(v -> key + "!=" + v).collect(Collectors.joining(","))
: "!" + key;
private final static BiFunction<String, String[], String> asInSet = getEntryProcessorFor("in");
private final static BiFunction<String, String[], String> asNotInSet = getEntryProcessorFor("notin");
private <T> void appendEntriesAsParam(StringBuilder sb, Map<String, T> entries,
BiFunction<String, T, String> entryProcessor) {
if (entries != null && !entries.isEmpty()) {
if (sb.length() > 0) {
sb.append(",");
}
sb.append(entries.entrySet().stream()
.map(entry -> entryProcessor.apply(entry.getKey(), entry.getValue()))
.collect(Collectors.joining(",")));
}
}
public String getFieldQueryParam() {
StringBuilder sb = new StringBuilder(101);
if (Utils.isNotNullOrEmpty(getName())) {
sb.append("metadata.name").append("=").append(getName());
}
appendEntriesAsParam(sb, getFields(), asKeyValuePair);
appendEntriesAsParam(sb, getFieldsNot(), asNotEqualToValues);
if (sb.length() > 0) {
return sb.toString();
}
return null;
}
public OperationContext copy() {
return new OperationContext(this);
}
public OperationContext withClient(Client client) {
if (this.client == client) {
return this;
}
final OperationContext context = new OperationContext(this);
context.client = client;
return context;
}
public OperationContext withPlural(String plural) {
if (Objects.equals(this.plural, plural)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.plural = plural;
return context;
}
public OperationContext withNamespace(String namespace) {
if (Objects.equals(this.namespace, namespace) && !this.defaultNamespace) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setNamespace(namespace, false);
return context;
}
public OperationContext withName(String name) {
if (Objects.equals(this.name, name)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.name = name;
return context;
}
public OperationContext withApiGroupName(String apiGroupName) {
if (Objects.equals(this.apiGroupName, apiGroupName)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setApiGroupName(apiGroupName);
return context;
}
public OperationContext withApiGroupVersion(String apiGroupVersion) {
if (Objects.equals(this.apiGroupVersion, apiGroupVersion)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setApiGroupVersion(apiGroupVersion);
return context;
}
public OperationContext withItem(Object item) {
if (this.item == item) {
return this;
}
final OperationContext context = new OperationContext(this);
context.item = item;
return context;
}
public OperationContext withLabels(Map<String, String> labels) {
if (Objects.equals(this.labels, labels)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setLabels(labels);
return context;
}
public OperationContext withLabelsIn(Map<String, String[]> labelsIn) {
if (Objects.equals(this.labelsIn, labelsIn)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setLabelsIn(labelsIn);
return context;
}
public OperationContext withLabelsNot(Map<String, String[]> labelsNot) {
if (Objects.equals(this.labelsNot, labelsNot)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setLabelsNot(labelsNot);
return context;
}
public OperationContext withLabelsNotIn(Map<String, String[]> labelsNotIn) {
if (Objects.equals(this.labelsNotIn, labelsNotIn)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setLabelsNotIn(labelsNotIn);
return context;
}
public OperationContext withFields(Map<String, String> fields) {
if (Objects.equals(this.fields, fields)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setFields(fields);
return context;
}
public OperationContext withFieldsNot(Map<String, String[]> fieldsNot) {
if (Objects.equals(this.fieldsNot, fieldsNot)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.setFieldsNot(fieldsNot);
return context;
}
public OperationContext withResourceVersion(String resourceVersion) {
if (Objects.equals(this.resourceVersion, resourceVersion)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.resourceVersion = resourceVersion;
return context;
}
public OperationContext withReloadingFromServer(boolean reloadingFromServer) {
if (this.reloadingFromServer == reloadingFromServer) {
return this;
}
final OperationContext context = new OperationContext(this);
context.reloadingFromServer = reloadingFromServer;
return context;
}
public OperationContext withGracePeriodSeconds(long gracePeriodSeconds) {
if (this.gracePeriodSeconds == gracePeriodSeconds) {
return this;
}
final OperationContext context = new OperationContext(this);
context.gracePeriodSeconds = gracePeriodSeconds;
return context;
}
public OperationContext withPropagationPolicy(DeletionPropagation propagationPolicy) {
if (this.propagationPolicy == propagationPolicy) {
return this;
}
final OperationContext context = new OperationContext(this);
context.propagationPolicy = propagationPolicy;
return context;
}
public OperationContext withDryRun(boolean dryRun) {
if (this.dryRun == dryRun) {
return this;
}
final OperationContext context = new OperationContext(this);
context.dryRun = dryRun;
return context;
}
public OperationContext withLabelSelector(String selectorAsString) {
if (Objects.equals(this.selectorAsString, selectorAsString)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.selectorAsString = selectorAsString;
return context;
}
<T extends HasMetadata, V extends VisitableBuilder<T, V>> ResourceHandler<T, V> getHandler(T item) {
return getClient().adapt(BaseClient.class).getHandlers().get(item, getClient());
}
public <C extends Client> C clientInWriteContext(Class<C> clazz) {
// other than namespace these do not have values on the config, so we need to associate a default
// operationcontext
OperationContext newContext = HasMetadataOperationsImpl.defaultContext(client).withDryRun(getDryRun())
.withGracePeriodSeconds(getGracePeriodSeconds()).withPropagationPolicy(getPropagationPolicy())
.withReloadingFromServer(isReloadingFromServer()).withFieldValidation(this.fieldValidation)
.withTimeout(this.timeout, this.timeoutUnit);
// check before setting to prevent flipping the default flag
if (!Objects.equals(getNamespace(), newContext.getNamespace())
|| isDefaultNamespace() ^ newContext.isDefaultNamespace()) {
newContext = newContext.withNamespace(getNamespace());
}
return getClient().adapt(BaseClient.class).newClient(newContext).adapt(clazz);
}
public Executor getExecutor() {
return getClient().adapt(BaseClient.class).getExecutor();
}
public OperationContext withFieldValidation(Validation fieldValidation) {
if (this.fieldValidation == fieldValidation) {
return this;
}
final OperationContext context = new OperationContext(this);
context.fieldValidation = fieldValidation;
return context;
}
public OperationContext withFieldManager(String fieldManager) {
if (Objects.equals(fieldManager, this.fieldManager)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.fieldManager = fieldManager;
return context;
}
public OperationContext withForceConflicts() {
if (Boolean.TRUE.equals(this.forceConflicts)) {
return this;
}
final OperationContext context = new OperationContext(this);
context.forceConflicts = true;
return context;
}
public OperationContext withTimeout(long timeout, TimeUnit timeUnit) {
final OperationContext context = new OperationContext(this);
context.timeout = timeout;
context.timeoutUnit = timeUnit == null ? TimeUnit.MILLISECONDS : timeUnit;
return context;
}
}