Skip to content

Commit c0df24d

Browse files
committed
lining up existing mock expectations and adding a streamingList method
1 parent a00e020 commit c0df24d

File tree

14 files changed

+350
-55
lines changed

14 files changed

+350
-55
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public class Config {
170170
private AuthProviderConfig authProvider;
171171
private String username;
172172
private String password;
173+
173174
private volatile String oauthToken;
174175
@JsonIgnore
175176
private volatile String autoOAuthToken;
@@ -223,6 +224,7 @@ public class Config {
223224
private TlsVersion[] tlsVersions;
224225

225226
private Boolean onlyHttpWatches;
227+
private Boolean watchList;
226228

227229
/**
228230
* custom headers
@@ -320,13 +322,13 @@ protected Config(boolean autoConfigure) {
320322
null,
321323
null, null, null, null, null,
322324
null, null, null,
323-
null, null, null,
325+
null, null, null, null,
324326
null, null, null, null,
325327
null, autoConfigure, true);
326328
}
327329

328330
@JsonCreator
329-
public Config(
331+
public static Config newConfig(
330332
@JsonProperty("masterUrl") String masterUrl,
331333
@JsonProperty("apiVersion") String apiVersion,
332334
@JsonProperty("namespace") String namespace,
@@ -374,17 +376,18 @@ public Config(
374376
@JsonProperty("requestRetryBackoffInterval") Integer requestRetryBackoffInterval,
375377
@JsonProperty("uploadRequestTimeout") Integer uploadRequestTimeout,
376378
@JsonProperty("onlyHttpWatches") Boolean onlyHttpWatches,
379+
@JsonProperty("watchList") Boolean watchList,
377380
@JsonProperty("currentContext") NamedContext currentContext,
378381
@JsonProperty("contexts") List<NamedContext> contexts,
379382
@JsonProperty("autoConfigure") Boolean autoConfigure) {
380-
this(masterUrl, apiVersion, namespace, trustCerts, disableHostnameVerification, caCertFile, caCertData,
383+
return new Config(masterUrl, apiVersion, namespace, trustCerts, disableHostnameVerification, caCertFile, caCertData,
381384
clientCertFile, clientCertData, clientKeyFile, clientKeyData, clientKeyAlgo, clientKeyPassphrase, username,
382385
password, oauthToken, autoOAuthToken, watchReconnectInterval, watchReconnectLimit, connectionTimeout, requestTimeout,
383386
scaleTimeout, loggingInterval, maxConcurrentRequests, maxConcurrentRequestsPerHost, http2Disable,
384387
httpProxy, httpsProxy, noProxy, userAgent, tlsVersions, websocketPingInterval, proxyUsername, proxyPassword,
385388
trustStoreFile, trustStorePassphrase, keyStoreFile, keyStorePassphrase, impersonateUsername, impersonateGroups,
386389
impersonateExtras, oauthTokenProvider, customHeaders, requestRetryBackoffLimit, requestRetryBackoffInterval,
387-
uploadRequestTimeout, onlyHttpWatches, currentContext, contexts, autoConfigure, true);
390+
uploadRequestTimeout, onlyHttpWatches, watchList, currentContext, contexts, autoConfigure, true);
388391
}
389392

390393
/*
@@ -402,15 +405,15 @@ public Config(
402405
String proxyPassword, String trustStoreFile, String trustStorePassphrase, String keyStoreFile, String keyStorePassphrase,
403406
String impersonateUsername, String[] impersonateGroups, Map<String, List<String>> impersonateExtras,
404407
OAuthTokenProvider oauthTokenProvider, Map<String, String> customHeaders, Integer requestRetryBackoffLimit,
405-
Integer requestRetryBackoffInterval, Integer uploadRequestTimeout, Boolean onlyHttpWatches, NamedContext currentContext,
408+
Integer requestRetryBackoffInterval, Integer uploadRequestTimeout, Boolean onlyHttpWatches, Boolean watchList,
409+
NamedContext currentContext,
406410
List<NamedContext> contexts, Boolean autoConfigure, Boolean shouldSetDefaultValues) {
407411
if (Boolean.TRUE.equals(shouldSetDefaultValues)) {
408412
this.masterUrl = DEFAULT_MASTER_URL;
409413
this.apiVersion = "v1";
410414
this.defaultNamespace = true;
411415
this.trustCerts = false;
412416
this.disableHostnameVerification = false;
413-
this.onlyHttpWatches = false;
414417
this.http2Disable = false;
415418
this.clientKeyAlgo = "RSA";
416419
this.clientKeyPassphrase = DEFAULT_CLIENT_KEY_PASSPHRASE;
@@ -582,6 +585,7 @@ public Config(
582585
this.oauthTokenProvider = oauthTokenProvider;
583586
this.customHeaders = customHeaders;
584587
this.onlyHttpWatches = onlyHttpWatches;
588+
this.watchList = watchList;
585589
}
586590

587591
public static void configFromSysPropsOrEnvVars(Config config) {
@@ -970,9 +974,9 @@ public static String getKeyAlgorithm(InputStream inputStream) throws IOException
970974
String line, algorithm = null;
971975

972976
while ((line = bufferedReader.readLine()) != null) {
973-
if (line.contains("BEGIN EC PRIVATE KEY"))
977+
if (line.contains("BEGIN EC PRIVATE KEY")) {
974978
algorithm = "EC";
975-
else if (line.contains("BEGIN RSA PRIVATE KEY")) {
979+
} else if (line.contains("BEGIN RSA PRIVATE KEY")) {
976980
algorithm = "RSA";
977981
}
978982
}
@@ -1538,4 +1542,16 @@ public void setOnlyHttpWatches(boolean onlyHttpWatches) {
15381542
this.onlyHttpWatches = onlyHttpWatches;
15391543
}
15401544

1545+
public Boolean getWatchList() {
1546+
return watchList;
1547+
}
1548+
1549+
public boolean isWatchList() {
1550+
return Optional.ofNullable(watchList).orElse(false);
1551+
}
1552+
1553+
public void setWatchList(boolean watchList) {
1554+
this.watchList = watchList;
1555+
}
1556+
15411557
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/ConfigBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public ConfigBuilder(Config instance) {
4242

4343
ConfigFluent<?> fluent;
4444

45+
@Override
4546
public Config build() {
4647
Config buildable = new Config(fluent.getMasterUrl(), fluent.getApiVersion(), fluent.getNamespace(), fluent.getTrustCerts(),
4748
fluent.getDisableHostnameVerification(), fluent.getCaCertFile(), fluent.getCaCertData(), fluent.getClientCertFile(),
@@ -56,7 +57,7 @@ public Config build() {
5657
fluent.getImpersonateUsername(), fluent.getImpersonateGroups(), fluent.getImpersonateExtras(),
5758
fluent.getOauthTokenProvider(), fluent.getCustomHeaders(), fluent.getRequestRetryBackoffLimit(),
5859
fluent.getRequestRetryBackoffInterval(), fluent.getUploadRequestTimeout(), fluent.getOnlyHttpWatches(),
59-
fluent.getCurrentContext(), fluent.getContexts(),
60+
fluent.getWatchList(), fluent.getCurrentContext(), fluent.getContexts(),
6061
Optional.ofNullable(fluent.getAutoConfigure()).orElse(!disableAutoConfig()), true);
6162
buildable.setAuthProvider(fluent.getAuthProvider());
6263
return buildable;

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/ConfigFluent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public void copyInstance(Config instance) {
7373
this.withRequestRetryBackoffLimit(instance.getRequestRetryBackoffLimit());
7474
this.withRequestRetryBackoffInterval(instance.getRequestRetryBackoffInterval());
7575
this.withUploadRequestTimeout(instance.getUploadRequestTimeout());
76-
this.withOnlyHttpWatches(instance.isOnlyHttpWatches());
76+
this.withOnlyHttpWatches(instance.getAutoConfigure());
77+
this.withWatchList(instance.getWatchList());
7778
this.withCurrentContext(instance.getCurrentContext());
7879
this.withContexts(instance.getContexts());
7980
this.withAutoConfigure(instance.getAutoConfigure());
@@ -143,6 +144,10 @@ public A withOnlyHttpWatches(boolean onlyHttpWatches) {
143144
return this.withOnlyHttpWatches(Boolean.valueOf(onlyHttpWatches));
144145
}
145146

147+
public A withWatchList(boolean watchList) {
148+
return this.withWatchList(Boolean.valueOf(watchList));
149+
}
150+
146151
public A withAutoConfigure(boolean autoConfigure) {
147152
return this.withAutoConfigure(Boolean.valueOf(autoConfigure));
148153
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/SundrioConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public SundrioConfig(String masterUrl, String apiVersion, String namespace, Bool
4949
String proxyPassword, String trustStoreFile, String trustStorePassphrase, String keyStoreFile, String keyStorePassphrase,
5050
String impersonateUsername, String[] impersonateGroups, Map<String, List<String>> impersonateExtras,
5151
OAuthTokenProvider oauthTokenProvider, Map<String, String> customHeaders, Integer requestRetryBackoffLimit,
52-
Integer requestRetryBackoffInterval, Integer uploadRequestTimeout, Boolean onlyHttpWatches, NamedContext currentContext,
52+
Integer requestRetryBackoffInterval, Integer uploadRequestTimeout, Boolean onlyHttpWatches, Boolean watchList,
53+
NamedContext currentContext,
5354
List<NamedContext> contexts, Boolean autoConfigure) {
5455
super(masterUrl, apiVersion, namespace, trustCerts, disableHostnameVerification, caCertFile, caCertData,
5556
clientCertFile, clientCertData, clientKeyFile, clientKeyData, clientKeyAlgo, clientKeyPassphrase, username,
@@ -58,6 +59,6 @@ public SundrioConfig(String masterUrl, String apiVersion, String namespace, Bool
5859
httpProxy, httpsProxy, noProxy, userAgent, tlsVersions, websocketPingInterval, proxyUsername, proxyPassword,
5960
trustStoreFile, trustStorePassphrase, keyStoreFile, keyStorePassphrase, impersonateUsername, impersonateGroups,
6061
impersonateExtras, oauthTokenProvider, customHeaders, requestRetryBackoffLimit, requestRetryBackoffInterval,
61-
uploadRequestTimeout, onlyHttpWatches, currentContext, contexts, autoConfigure, true);
62+
uploadRequestTimeout, onlyHttpWatches, watchList, currentContext, contexts, autoConfigure, true);
6263
}
6364
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/dsl/Watchable.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,19 @@
1515
*/
1616
package io.fabric8.kubernetes.client.dsl;
1717

18+
import io.fabric8.kubernetes.api.model.HasMetadata;
1819
import io.fabric8.kubernetes.api.model.ListOptions;
20+
import io.fabric8.kubernetes.api.model.ListOptionsBuilder;
21+
import io.fabric8.kubernetes.api.model.ObjectMeta;
22+
import io.fabric8.kubernetes.api.model.Status;
23+
import io.fabric8.kubernetes.client.KubernetesClientException;
1924
import io.fabric8.kubernetes.client.Watch;
2025
import io.fabric8.kubernetes.client.Watcher;
26+
import io.fabric8.kubernetes.client.WatcherException;
27+
28+
import java.util.Optional;
29+
import java.util.concurrent.CompletableFuture;
30+
import java.util.function.Consumer;
2131

2232
public interface Watchable<T> {
2333

@@ -55,4 +65,59 @@ public interface Watchable<T> {
5565
@Deprecated
5666
Watch watch(String resourceVersion, Watcher<T> watcher);
5767

68+
/**
69+
* Helper method to use the WatchList feature to list resources.
70+
* A watch is used under the covers, but will be terminated after the initial events.
71+
* <br>
72+
* Not specifying a resourceVersion on the context or using 0 will perform a "consistent read"
73+
* from the time at which the request started processing.
74+
*
75+
* @param onItem a consumer to be called for each item
76+
* @return a CompletableFuture that provides the terminal resourceVersion, or any underlying exception during processing. It
77+
* may be
78+
* cancelled to terminate the streamingList operation early
79+
*/
80+
default CompletableFuture<String> streamingList(Consumer<T> onItem) {
81+
CompletableFuture<String> future = new CompletableFuture<>();
82+
Watch watch = this.watch(new ListOptionsBuilder().withSendInitialEvents(true)
83+
.withResourceVersionMatch("NotOlderThan")
84+
.withAllowWatchBookmarks(true)
85+
.build(), new Watcher<T>() {
86+
87+
@Override
88+
public void eventReceived(Action action, T resource) {
89+
switch (action) {
90+
case ADDED:
91+
onItem.accept(resource);
92+
break;
93+
case BOOKMARK:
94+
if (resource instanceof HasMetadata) {
95+
future.complete(Optional.ofNullable(((HasMetadata) resource).getMetadata())
96+
.map(ObjectMeta::getResourceVersion).orElse(null));
97+
} else {
98+
future.complete(null);
99+
}
100+
break;
101+
default:
102+
if (action == Action.ERROR && resource instanceof Status) {
103+
Status status = (Status) resource;
104+
future.completeExceptionally(new KubernetesClientException(status));
105+
} else {
106+
future.completeExceptionally(
107+
new KubernetesClientException("Unexpected event before list ending bookmark: " + action));
108+
}
109+
break;
110+
}
111+
}
112+
113+
@Override
114+
public void onClose(WatcherException cause) {
115+
future.completeExceptionally(cause);
116+
}
117+
118+
});
119+
future.whenComplete((v, t) -> watch.close());
120+
return future;
121+
}
122+
58123
}

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/ConfigConstructorTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ static Stream<Arguments> blankConfigurationProviders() {
9393
null,
9494
null, null, null, null, null,
9595
null, null, null,
96-
null, null, null,
96+
null, null, null, null,
9797
null, null, null, null,
9898
null, null, false)));
9999
}
@@ -197,7 +197,7 @@ void whenAutoConfigureEnabled_thenUseBothDefaultAndAutoConfiguredValues() {
197197
null,
198198
null, null, null, null, null,
199199
null, null, null,
200-
null, null, null,
200+
null, null, null, null,
201201
null, null, null, null,
202202
null, true, true);
203203

@@ -264,7 +264,7 @@ void whenAutoConfigureDisabled_thenOnlyUseDefaultValues() {
264264
null,
265265
null, null, null, null, null,
266266
null, null, null,
267-
null, null, null,
267+
null, null, null, null,
268268
null, null, null, null,
269269
null, false, true);
270270

@@ -373,7 +373,7 @@ void configLoadedViaSystemProperties() {
373373
null,
374374
null, null, null, null, null,
375375
null, null, null,
376-
null, null, null,
376+
null, null, null, null,
377377
null, null, null, null,
378378
null, true, true);
379379

@@ -481,7 +481,7 @@ void configLoadedViaKubeConfig() {
481481
null,
482482
null, null, null, null, null,
483483
null, null, null,
484-
null, null, null,
484+
null, null, null, null,
485485
null, null, null, null,
486486
null, true, true);
487487

@@ -522,7 +522,7 @@ void configLoadedViaServiceAccount() {
522522
null,
523523
null, null, null, null, null,
524524
null, null, null,
525-
null, null, null,
525+
null, null, null, null,
526526
null, null, null, null,
527527
null, true, true);
528528

@@ -572,7 +572,7 @@ void throwsException() {
572572
null,
573573
null, null, null, null, null,
574574
null, null, null,
575-
null, null, null,
575+
null, null, null, null,
576576
null, null, null, null,
577577
null, true, false));
578578
} finally {

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/SundrioConfigBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ void hasExpectedNumberOfFields() {
3131
.collect(Collectors.toList()))
3232
.withFailMessage("You've probably modified Config and SundrioConfig constructor annotated with @Buildable," +
3333
"please update the ConfigFluent.copyInstance method too")
34-
.hasSize(51);
34+
.hasSize(52);
3535
}
3636
}

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/impl/DefaultSharedIndexInformer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public DefaultSharedIndexInformer(Class<T> apiTypeClass, ListerWatcher<T, L> lis
8989

9090
processorStore = new ProcessorStore<>(this.indexer, this.processor);
9191
this.reflector = new Reflector<>(listerWatcher, processorStore, informerExecutor);
92+
this.reflector.setWatchList(listerWatcher.getConfig().isWatchList());
9293
}
9394

9495
/**

kubernetes-client/src/main/java/io/fabric8/kubernetes/client/informers/impl/ListerWatcher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.fabric8.kubernetes.api.model.HasMetadata;
1919
import io.fabric8.kubernetes.api.model.ListOptions;
20+
import io.fabric8.kubernetes.client.Config;
2021
import io.fabric8.kubernetes.client.Watcher;
2122
import io.fabric8.kubernetes.client.dsl.internal.AbstractWatchManager;
2223

@@ -39,4 +40,6 @@ public interface ListerWatcher<T extends HasMetadata, L> {
3940
int getWatchReconnectInterval();
4041

4142
String getApiEndpointPath();
43+
44+
Config getConfig();
4245
}

0 commit comments

Comments
 (0)