Java源码示例:io.kubernetes.client.util.CallGeneratorParams
示例1
@Test
public void shutdownInformerFactoryInstantlyAfterStarting() {
CoreV1Api api = new CoreV1Api();
SharedInformerFactory factory = new SharedInformerFactory();
factory.sharedIndexInformerFor(
(CallGeneratorParams params) -> {
return api.listNamespaceCall(
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null);
},
V1Namespace.class,
V1NamespaceList.class);
factory.startAllRegisteredInformers();
factory.stopAllRegisteredInformers();
}
示例2
private SharedIndexInformer<V1Node> createNodeInformer(SharedInformerFactory sharedInformerFactory) {
return sharedInformerFactory.sharedIndexInformerFor(
(CallGeneratorParams params) -> coreV1Api.listNodeCall(
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null
),
V1Node.class,
V1NodeList.class,
configuration.getKubeApiServerIntegratorRefreshIntervalMs()
);
}
示例3
private SharedIndexInformer<V1Pod> createPodInformer(SharedInformerFactory sharedInformerFactory) {
return sharedInformerFactory.sharedIndexInformerFor(
(CallGeneratorParams params) -> coreV1Api.listNamespacedPodCall(
KUBERNETES_NAMESPACE,
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null
),
V1Pod.class,
V1PodList.class,
configuration.getKubeApiServerIntegratorRefreshIntervalMs()
);
}
示例4
private Call listOpportunisticResourcesCall(CallGeneratorParams params) {
try {
return customObjectsApi.listNamespacedCustomObjectCall(
OPPORTUNISTIC_RESOURCE_GROUP,
OPPORTUNISTIC_RESOURCE_VERSION,
OPPORTUNISTIC_RESOURCE_NAMESPACE,
OPPORTUNISTIC_RESOURCE_PLURAL,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null
);
} catch (ApiException e) {
throw new IllegalStateException("listNamespacedCustomObjectCall error", e);
}
}
示例5
@Override
public ApiListType list(CallGeneratorParams params) throws ApiException {
if (!listExecuted) {
listExecuted = true;
return list;
}
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
}
return null;
}
示例6
@Override
public Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException {
if (!watchExecuted) {
watchExecuted = true;
return new MockWatch<>(events);
}
try {
Thread.sleep(100000);
} catch (InterruptedException e) {
}
return new MockWatch<>();
}
示例7
@Test
public void testBuildWatchShouldWorkIfInformerPresent() {
CoreV1Api api = new CoreV1Api();
informerFactory.sharedIndexInformerFor(
(CallGeneratorParams params) -> {
return api.listPodForAllNamespacesCall(
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null);
},
V1Pod.class,
V1PodList.class);
ControllerBuilder.defaultBuilder(informerFactory)
.watch(
(workQueue) -> ControllerBuilder.controllerWatchBuilder(V1Pod.class, workQueue).build())
.withReconciler(
new Reconciler() {
@Override
public Result reconcile(Request request) {
return new Result(false);
}
})
.build();
}
示例8
public static void main(String[] args) throws IOException {
CoreV1Api coreV1Api = new CoreV1Api();
ApiClient apiClient = coreV1Api.getApiClient();
OkHttpClient httpClient =
apiClient.getHttpClient().newBuilder().readTimeout(0, TimeUnit.SECONDS).build();
apiClient.setHttpClient(httpClient);
// instantiating an informer-factory, and there should be only one informer-factory globally.
SharedInformerFactory informerFactory = new SharedInformerFactory();
// registering node-informer into the informer-factory.
SharedIndexInformer<V1Node> nodeInformer =
informerFactory.sharedIndexInformerFor(
(CallGeneratorParams params) -> {
return coreV1Api.listNodeCall(
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null);
},
V1Node.class,
V1NodeList.class);
informerFactory.startAllRegisteredInformers();
EventBroadcaster eventBroadcaster = new LegacyEventBroadcaster(coreV1Api);
// nodeReconciler prints node information on events
NodePrintingReconciler nodeReconciler =
new NodePrintingReconciler(
nodeInformer,
eventBroadcaster.newRecorder(
new V1EventSource().host("localhost").component("node-printer")));
// Use builder library to construct a default controller.
Controller controller =
ControllerBuilder.defaultBuilder(informerFactory)
.watch(
(workQueue) ->
ControllerBuilder.controllerWatchBuilder(V1Node.class, workQueue)
.withWorkQueueKeyFunc(
(V1Node node) ->
new Request(node.getMetadata().getName())) // optional, default to
.withOnAddFilter(
(V1Node createdNode) ->
createdNode
.getMetadata()
.getName()
.startsWith("docker-")) // optional, set onAdd filter
.withOnUpdateFilter(
(V1Node oldNode, V1Node newNode) ->
newNode
.getMetadata()
.getName()
.startsWith("docker-")) // optional, set onUpdate filter
.withOnDeleteFilter(
(V1Node deletedNode, Boolean stateUnknown) ->
deletedNode
.getMetadata()
.getName()
.startsWith("docker-")) // optional, set onDelete filter
.build())
.withReconciler(nodeReconciler) // required, set the actual reconciler
.withName("node-printing-controller") // optional, set name for controller
.withWorkerCount(4) // optional, set worker thread count
.withReadyFunc(
nodeInformer
::hasSynced) // optional, only starts controller when the cache has synced up
.build();
// Use builder library to manage one or multiple controllers.
ControllerManager controllerManager =
ControllerBuilder.controllerManagerBuilder(informerFactory)
.addController(controller)
.build();
LeaderElectingController leaderElectingController =
new LeaderElectingController(
new LeaderElector(
new LeaderElectionConfig(
new EndpointsLock("kube-system", "leader-election", "foo"),
Duration.ofMillis(10000),
Duration.ofMillis(8000),
Duration.ofMillis(5000))),
controllerManager);
leaderElectingController.run();
}
示例9
@Test
public void testInformerReListWatchOnWatchConflict() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
String startRV = "1000";
V1PodList podList =
new V1PodList().metadata(new V1ListMeta().resourceVersion(startRV)).items(Arrays.asList());
stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
.withQueryParam("watch", equalTo("false"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(new JSON().serialize(podList))));
Watch.Response<V1Pod> watchResponse =
new Watch.Response<>(
EventType.ERROR.name(), new V1Status().apiVersion("v1").kind("Status").code(409));
stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
.withQueryParam("watch", equalTo("true"))
.withQueryParam("resourceVersion", equalTo(startRV))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody(new JSON().serialize(watchResponse))));
SharedInformerFactory factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> podInformer =
factory.sharedIndexInformerFor(
(CallGeneratorParams params) -> {
try {
return coreV1Api.listNamespacedPodCall(
namespace,
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
},
V1Pod.class,
V1PodList.class);
factory.startAllRegisteredInformers();
// Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
Thread.sleep(3000);
verify(
moreThan(1),
getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
.withQueryParam("watch", equalTo("false")));
verify(
moreThan(1),
getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
.withQueryParam("watch", equalTo("true")));
factory.stopAllRegisteredInformers();
}
示例10
@Test
public void testInformerReListingOnListForbidden() throws InterruptedException {
CoreV1Api coreV1Api = new CoreV1Api(client);
stubFor(
get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
.withQueryParam("watch", equalTo("false"))
.willReturn(
aResponse()
.withStatus(403)
.withHeader("Content-Type", "application/json")
.withBody(
new JSON()
.serialize(
new V1Status()
.apiVersion("v1")
.kind("Status")
.code(403)
.reason("RBAC forbidden")))));
SharedInformerFactory factory = new SharedInformerFactory();
SharedIndexInformer<V1Pod> podInformer =
factory.sharedIndexInformerFor(
(CallGeneratorParams params) -> {
try {
return coreV1Api.listNamespacedPodCall(
namespace,
null,
null,
null,
null,
null,
null,
params.resourceVersion,
params.timeoutSeconds,
params.watch,
null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
},
V1Pod.class,
V1PodList.class);
factory.startAllRegisteredInformers();
// Sleep mroe than 1s so that informer can perform multiple rounds of list-watch
Thread.sleep(3000);
verify(
moreThan(1),
getRequestedFor(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods"))
.withQueryParam("watch", equalTo("false")));
factory.stopAllRegisteredInformers();
}
示例11
ApiListType list(CallGeneratorParams params) throws ApiException;
示例12
Watchable<ApiType> watch(CallGeneratorParams params) throws ApiException;