Skip to content

Commit 252b562

Browse files
shawkinsmanusa
authored andcommitted
switching to nested, rather than a builder
and adding a changelog
1 parent 8d0f823 commit 252b562

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Fix #3225: Pod metric does not have corresponding label selector variant
1717
* Fix #3243: pipes provided to exec command are no longer closed on connection close, so that client can fully read the buffer after the command finishes.
1818
* Fix #3272: prevent index npe after informer sees an empty list
19+
* Fix #3275: filter related dsl methods withLabel, withField, etc. should not modify the current context. If you need similar behavior to the previous use `Filterable.withNewFilter`.
1920

2021
#### Improvements
2122
* Fix #3078: adding javadocs to further clarify patch, edit, replace, etc. and note the possibility of items being modified.

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/FilterBuilder.java renamed to kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/FilterNested.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

1717
package io.fabric8.kubernetes.client.dsl;
1818

19-
import io.fabric8.kubernetes.api.builder.Builder;
19+
import io.fabric8.kubernetes.api.builder.Nested;
2020

21-
public interface FilterBuilder<T> extends Filterable<FilterBuilder<T>>, Builder<T> {
21+
public interface FilterNested<T> extends Filterable<FilterNested<T>>, Nested<T> {
22+
23+
default T endFilter() {
24+
return and();
25+
}
2226

2327
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/FilterWatchListDeletable.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
public interface FilterWatchListDeletable<T, L> extends Filterable<FilterWatchListDeletable<T, L>>, WatchListDeletable<T, L> {
1919

2020
/**
21-
* Accumulate a filter on the context, when done {@link FilterBuilder#build()} must be called
22-
* @return a filter builder
21+
* Accumulate a filter on the context, when done {@link FilterNested#endFilter()} or and must be called
22+
* @return a {@link FilterNested}
2323
*/
24-
FilterBuilder<FilterWatchListDeletable<T, L>> withFilter();
24+
FilterNested<FilterWatchListDeletable<T, L>> withNewFilter();
2525

2626
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/BaseOperation.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import io.fabric8.kubernetes.client.Watch;
4343
import io.fabric8.kubernetes.client.Watcher;
4444
import io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable;
45-
import io.fabric8.kubernetes.client.dsl.FilterBuilder;
45+
import io.fabric8.kubernetes.client.dsl.FilterNested;
4646
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
4747
import io.fabric8.kubernetes.client.dsl.Gettable;
4848
import io.fabric8.kubernetes.client.dsl.Informable;
@@ -406,70 +406,70 @@ public final T createOrReplace(T... items) {
406406

407407
@Override
408408
public FilterWatchListDeletable<T, L> withLabels(Map<String, String> labels) {
409-
return withFilter().withLabels(labels).build();
409+
return withNewFilter().withLabels(labels).endFilter();
410410
}
411411

412412
@Override
413413
public FilterWatchListDeletable<T, L> withLabelSelector(LabelSelector selector) {
414-
return withFilter().withLabelSelector(selector).build();
414+
return withNewFilter().withLabelSelector(selector).endFilter();
415415
}
416416

417417
@Override
418418
public FilterWatchListDeletable<T, L> withoutLabels(Map<String, String> labels) {
419-
return withFilter().withoutLabels(labels).build();
419+
return withNewFilter().withoutLabels(labels).endFilter();
420420
}
421421

422422
@Override
423423
public FilterWatchListDeletable<T, L> withLabelIn(String key, String... values) {
424-
return withFilter().withLabelIn(key, values).build();
424+
return withNewFilter().withLabelIn(key, values).endFilter();
425425
}
426426

427427
@Override
428428
public FilterWatchListDeletable<T, L> withLabelNotIn(String key, String... values) {
429-
return withFilter().withLabelNotIn(key, values).build();
429+
return withNewFilter().withLabelNotIn(key, values).endFilter();
430430
}
431431

432432
@Override
433433
public FilterWatchListDeletable<T, L> withLabel(String key, String value) {
434-
return withFilter().withLabel(key, value).build();
434+
return withNewFilter().withLabel(key, value).endFilter();
435435
}
436436

437437
@Override
438438
public FilterWatchListDeletable<T, L> withoutLabel(String key, String value) {
439-
return withFilter().withoutLabel(key, value).build();
439+
return withNewFilter().withoutLabel(key, value).endFilter();
440440
}
441441

442442
@Override
443443
public FilterWatchListDeletable<T, L> withFields(Map<String, String> fields) {
444-
return withFilter().withFields(fields).build();
444+
return withNewFilter().withFields(fields).endFilter();
445445
}
446446

447447
@Override
448448
public FilterWatchListDeletable<T, L> withField(String key, String value) {
449-
return withFilter().withField(key, value).build();
449+
return withNewFilter().withField(key, value).endFilter();
450450
}
451451

452452
@Override
453453
public FilterWatchListDeletable<T, L> withInvolvedObject(ObjectReference objectReference) {
454454
if (objectReference != null) {
455-
return withFilter().withInvolvedObject(objectReference).build();
455+
return withNewFilter().withInvolvedObject(objectReference).endFilter();
456456
}
457457
return this;
458458
}
459459

460460
@Override
461-
public FilterBuilder<FilterWatchListDeletable<T, L>> withFilter() {
462-
return new FilterBuilderImpl<>(this);
461+
public FilterNested<FilterWatchListDeletable<T, L>> withNewFilter() {
462+
return new FilterNestedImpl<>(this);
463463
}
464464

465465
@Override
466466
public FilterWatchListDeletable<T, L> withoutFields(Map<String, String> fields) {
467-
return withFilter().withoutFields(fields).build();
467+
return withNewFilter().withoutFields(fields).endFilter();
468468
}
469469

470470
@Override
471471
public FilterWatchListDeletable<T, L> withoutField(String key, String value) {
472-
return withFilter().withoutField(key, value).build();
472+
return withNewFilter().withoutField(key, value).endFilter();
473473
}
474474

475475
public String getLabelQueryParam() {

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/FilterBuilderImpl.java renamed to kubernetes-client/src/main/java/io/fabric8/kubernetes/client/dsl/base/FilterNestedImpl.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.fabric8.kubernetes.api.model.LabelSelector;
2222
import io.fabric8.kubernetes.api.model.LabelSelectorRequirement;
2323
import io.fabric8.kubernetes.api.model.ObjectReference;
24-
import io.fabric8.kubernetes.client.dsl.FilterBuilder;
24+
import io.fabric8.kubernetes.client.dsl.FilterNested;
2525
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
2626
import io.fabric8.kubernetes.client.dsl.Resource;
2727
import io.fabric8.kubernetes.client.utils.Utils;
@@ -31,7 +31,7 @@
3131
import java.util.List;
3232
import java.util.Map;
3333

34-
final class FilterBuilderImpl<T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>> implements FilterBuilder<FilterWatchListDeletable<T, L>> {
34+
final class FilterNestedImpl<T extends HasMetadata, L extends KubernetesResourceList<T>, R extends Resource<T>> implements FilterNested<FilterWatchListDeletable<T, L>> {
3535

3636
private static final String INVOLVED_OBJECT_NAME = "involvedObject.name";
3737
private static final String INVOLVED_OBJECT_NAMESPACE = "involvedObject.namespace";
@@ -44,10 +44,7 @@ final class FilterBuilderImpl<T extends HasMetadata, L extends KubernetesResourc
4444
private final BaseOperation<T, L, R> baseOperation;
4545
private OperationContext context;
4646

47-
/**
48-
* @param baseOperation
49-
*/
50-
FilterBuilderImpl(BaseOperation<T, L, R> baseOperation) {
47+
FilterNestedImpl(BaseOperation<T, L, R> baseOperation) {
5148
this.baseOperation = baseOperation;
5249
// create a context copy
5350
context = this.baseOperation.context;
@@ -63,38 +60,38 @@ final class FilterBuilderImpl<T extends HasMetadata, L extends KubernetesResourc
6360
}
6461

6562
@Override
66-
public FilterBuilder<FilterWatchListDeletable<T, L>> withLabels(Map<String, String> labels) {
63+
public FilterNested<FilterWatchListDeletable<T, L>> withLabels(Map<String, String> labels) {
6764
this.context.labels.putAll(labels);
6865
return this;
6966
}
7067

7168
@Override
72-
public FilterBuilder<FilterWatchListDeletable<T, L>> withoutLabels(Map<String, String> labels) {
69+
public FilterNested<FilterWatchListDeletable<T, L>> withoutLabels(Map<String, String> labels) {
7370
// Re-use "withoutLabel" to convert values from String to String[]
7471
labels.forEach(this::withoutLabel);
7572
return this;
7673
}
7774

7875
@Override
79-
public FilterBuilder<FilterWatchListDeletable<T, L>> withLabelIn(String key, String... values) {
76+
public FilterNested<FilterWatchListDeletable<T, L>> withLabelIn(String key, String... values) {
8077
context.labelsIn.put(key, values);
8178
return this;
8279
}
8380

8481
@Override
85-
public FilterBuilder<FilterWatchListDeletable<T, L>> withLabelNotIn(String key, String... values) {
82+
public FilterNested<FilterWatchListDeletable<T, L>> withLabelNotIn(String key, String... values) {
8683
context.labelsNotIn.put(key, values);
8784
return this;
8885
}
8986

9087
@Override
91-
public FilterBuilder<FilterWatchListDeletable<T, L>> withLabel(String key, String value) {
88+
public FilterNested<FilterWatchListDeletable<T, L>> withLabel(String key, String value) {
9289
context.labels.put(key, value);
9390
return this;
9491
}
9592

9693
@Override
97-
public FilterBuilder<FilterWatchListDeletable<T, L>> withoutLabel(String key, String value) {
94+
public FilterNested<FilterWatchListDeletable<T, L>> withoutLabel(String key, String value) {
9895
context.labelsNot.merge(key, new String[]{value}, (oldList, newList) -> {
9996
final String[] concatList = (String[]) Array.newInstance(String.class, oldList.length + newList.length);
10097
System.arraycopy(oldList, 0, concatList, 0, oldList.length);
@@ -105,26 +102,26 @@ public FilterBuilder<FilterWatchListDeletable<T, L>> withoutLabel(String key, St
105102
}
106103

107104
@Override
108-
public FilterBuilder<FilterWatchListDeletable<T, L>> withFields(Map<String, String> fields) {
105+
public FilterNested<FilterWatchListDeletable<T, L>> withFields(Map<String, String> fields) {
109106
this.context.fields.putAll(fields);
110107
return this;
111108
}
112109

113110
@Override
114-
public FilterBuilder<FilterWatchListDeletable<T, L>> withField(String key, String value) {
111+
public FilterNested<FilterWatchListDeletable<T, L>> withField(String key, String value) {
115112
this.context.fields.put(key, value);
116113
return this;
117114
}
118115

119116
@Override
120-
public FilterBuilder<FilterWatchListDeletable<T, L>> withoutFields(Map<String, String> fields) {
117+
public FilterNested<FilterWatchListDeletable<T, L>> withoutFields(Map<String, String> fields) {
121118
// Re-use "withoutField" to convert values from String to String[]
122119
fields.forEach(this::withoutField);
123120
return this;
124121
}
125122

126123
@Override
127-
public FilterBuilder<FilterWatchListDeletable<T, L>> withoutField(String key, String value) {
124+
public FilterNested<FilterWatchListDeletable<T, L>> withoutField(String key, String value) {
128125
context.fieldsNot.merge(key, new String[]{value}, (oldList, newList) -> {
129126
if (Utils.isNotNullOrEmpty(newList[0])) { // Only add new values when not null
130127
final String[] concatList = (String[]) Array.newInstance(String.class, oldList.length + newList.length);
@@ -139,7 +136,7 @@ public FilterBuilder<FilterWatchListDeletable<T, L>> withoutField(String key, St
139136
}
140137

141138
@Override
142-
public FilterBuilder<FilterWatchListDeletable<T, L>> withLabelSelector(LabelSelector selector) {
139+
public FilterNested<FilterWatchListDeletable<T, L>> withLabelSelector(LabelSelector selector) {
143140
Map<String, String> matchLabels = selector.getMatchLabels();
144141
if (matchLabels != null) {
145142
withLabels(matchLabels);
@@ -171,7 +168,7 @@ public FilterBuilder<FilterWatchListDeletable<T, L>> withLabelSelector(LabelSele
171168
}
172169

173170
@Override
174-
public FilterBuilder<FilterWatchListDeletable<T, L>> withInvolvedObject(ObjectReference objectReference) {
171+
public FilterNested<FilterWatchListDeletable<T, L>> withInvolvedObject(ObjectReference objectReference) {
175172
if (objectReference.getName() != null) {
176173
context.fields.put(INVOLVED_OBJECT_NAME, objectReference.getName());
177174
}
@@ -197,7 +194,7 @@ public FilterBuilder<FilterWatchListDeletable<T, L>> withInvolvedObject(ObjectRe
197194
}
198195

199196
@Override
200-
public FilterWatchListDeletable<T, L> build() {
197+
public FilterWatchListDeletable<T, L> and() {
201198
return this.baseOperation.newInstance(context);
202199
}
203200
}

0 commit comments

Comments
 (0)