io.fabric8.kubernetes.client.CustomResource Java Examples
The following examples show how to use
io.fabric8.kubernetes.client.CustomResource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceTester.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
public void beforeEach() { // Parse resource into CM try { resourceName = CustomResource.class.isAssignableFrom(cls) ? prefix + "-" + cls.newInstance().getKind() + ".yaml" : prefix + "-" + cls.getSimpleName() + ".yaml"; } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } URL resource = getClass().getResource(resourceName); if (resource == null) { model = null; } else { R cm = fromYaml(resource, cls); // Construct the desired resources from the CM model = fromK8sResource.apply(cm, lookup); } }
Example #2
Source File: Operator.java From java-operator-sdk with Apache License 2.0 | 6 votes |
private <R extends CustomResource> void registerWatches(ResourceController<R> controller, MixedOperation client, Class<R> resClass, boolean watchAllNamespaces, String[] targetNamespaces, EventScheduler eventScheduler) { CustomResourceOperationsImpl crClient = (CustomResourceOperationsImpl) client; if (watchAllNamespaces) { crClient.inAnyNamespace().watch(eventScheduler); } else if (targetNamespaces.length == 0) { client.watch(eventScheduler); } else { for (String targetNamespace : targetNamespaces) { crClient.inNamespace(targetNamespace).watch(eventScheduler); log.debug("Registered controller for namespace: {}", targetNamespace); } } customResourceClients.put(resClass, (CustomResourceOperationsImpl) client); log.info("Registered Controller: '{}' for CRD: '{}' for namespaces: {}", controller.getClass().getSimpleName(), resClass, targetNamespaces.length == 0 ? "[all/client namespace]" : Arrays.toString(targetNamespaces)); }
Example #3
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 6 votes |
@Test public void eventsAreNotExecutedConcurrentlyForSameResource() throws InterruptedException { normalDispatcherExecution(); CustomResource resource1 = sampleResource(); CustomResource resource2 = sampleResource(); resource2.getMetadata().setResourceVersion("2"); resource2.getMetadata().setGeneration(2l); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2); waitTimeForExecution(2); log.info("Event processing details 1.: {}. 2: {}", eventProcessingList.get(0), eventProcessingList.get(1)); assertThat(eventProcessingList).hasSize(2) .matches(list -> eventProcessingList.get(0).getCustomResource().getMetadata().getResourceVersion().equals("1") && eventProcessingList.get(1).getCustomResource().getMetadata().getResourceVersion().equals("2"), "Events processed in correct order") .matches(list -> eventProcessingList.get(0).getEndTime().isBefore(eventProcessingList.get(1).startTime), "Start time of event 2 is after end time of event 1"); }
Example #4
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 6 votes |
@Test public void generationAwareSchedulingSkipsEventsWithoutIncreasedGeneration() { normalDispatcherExecution(); CustomResource resource1 = sampleResource(); CustomResource resource2 = sampleResource(); resource2.getMetadata().setResourceVersion("2"); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2); waitTimeForExecution(2); assertThat(eventProcessingList).hasSize(1) .matches(list -> eventProcessingList.get(0).getCustomResource().getMetadata().getResourceVersion().equals("1")); }
Example #5
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 6 votes |
@Test public void notGenerationAwareSchedulingProcessesAllEventsRegardlessOfGeneration() { generationUnAwareScheduler(); normalDispatcherExecution(); CustomResource resource1 = sampleResource(); CustomResource resource2 = sampleResource(); resource2.getMetadata().setResourceVersion("2"); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2); waitTimeForExecution(2); log.info("Event processing details 1.: {}. 2: {}", eventProcessingList.get(0), eventProcessingList.get(1)); assertThat(eventProcessingList).hasSize(2) .matches(list -> eventProcessingList.get(0).getCustomResource().getMetadata().getResourceVersion().equals("1") && eventProcessingList.get(1).getCustomResource().getMetadata().getResourceVersion().equals("2"), "Events processed in correct order") .matches(list -> eventExecutedBefore(0, 1), "Start time of event 2 is after end time of event 1"); }
Example #6
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 6 votes |
@Test public void onlyLastEventIsScheduledIfMoreReceivedDuringAndExecution() { normalDispatcherExecution(); CustomResource resource1 = sampleResource(); CustomResource resource2 = sampleResource(); resource2.getMetadata().setResourceVersion("2"); resource2.getMetadata().setGeneration(2l); CustomResource resource3 = sampleResource(); resource3.getMetadata().setResourceVersion("3"); resource3.getMetadata().setGeneration(3l); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource3); waitTimeForExecution(3); log.info("Event processing details 1.: {}. 2: {}", eventProcessingList.get(0), eventProcessingList.get(1)); assertThat(eventProcessingList).hasSize(2) .matches(list -> eventProcessingList.get(0).getCustomResource().getMetadata().getResourceVersion().equals("1") && eventProcessingList.get(1).getCustomResource().getMetadata().getResourceVersion().equals("3"), "Events processed in correct order") .matches(list -> eventExecutedBefore(0, 1), "Start time of event 2 is after end time of event 1"); }
Example #7
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 6 votes |
@Test public void retriesEventsWithErrors() { doAnswer(this::exceptionInExecution) .doAnswer(this::normalExecution) .when(eventDispatcher) .handleEvent(any(Watcher.Action.class), any(CustomResource.class)); CustomResource resource = sampleResource(); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource); waitTimeForExecution(2, 1); assertThat(eventProcessingList) .hasSize(2) .has(new Condition<>(e -> e.getException() != null, ""), atIndex(0)) .has(new Condition<>(e -> e.getException() == null, ""), atIndex(1)); }
Example #8
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 6 votes |
@Test public void processesNewEventIfItIsReceivedAfterExecutionInError() { CustomResource resource1 = sampleResource(); CustomResource resource2 = sampleResource(); resource2.getMetadata().setResourceVersion("2"); resource2.getMetadata().setGeneration(2l); doAnswer(this::exceptionInExecution).when(eventDispatcher).handleEvent(any(Watcher.Action.class), eq(resource1)); doAnswer(this::normalExecution).when(eventDispatcher).handleEvent(any(Watcher.Action.class), eq(resource2)); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource1); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource2); waitTimeForExecution(2); assertThat(eventProcessingList).hasSize(2) .matches(list -> eventProcessingList.get(0).getCustomResource().getMetadata().getResourceVersion().equals("1") && eventProcessingList.get(1).getCustomResource().getMetadata().getResourceVersion().equals("2"), "Events processed in correct order") .matches(list -> eventExecutedBefore(0, 1), "Start time of event 2 is after end time of event 1"); assertThat(eventProcessingList.get(0).getException()).isNotNull(); assertThat(eventProcessingList.get(1).getException()).isNull(); }
Example #9
Source File: ControllerUtils.java From java-operator-sdk with Apache License 2.0 | 5 votes |
public static <T extends CustomResource> Class<? extends CustomResourceDoneable<T>> getCustomResourceDoneableClass(ResourceController<T> controller) { try { Class<? extends CustomResource> customResourceClass = getAnnotation(controller).customResourceClass(); String className = customResourceClass.getPackage().getName() + "." + customResourceClass.getSimpleName() + "CustomResourceDoneable"; if (doneableClassCache.containsKey(customResourceClass)) { return (Class<? extends CustomResourceDoneable<T>>) doneableClassCache.get(customResourceClass); } ClassPool pool = ClassPool.getDefault(); pool.appendClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader())); CtClass superClass = pool.get(CustomResourceDoneable.class.getName()); CtClass function = pool.get(Function.class.getName()); CtClass customResource = pool.get(customResourceClass.getName()); CtClass[] argTypes = {customResource, function}; CtClass customDoneable = pool.makeClass(className, superClass); CtConstructor ctConstructor = CtNewConstructor.make(argTypes, null, "super($1, $2);", customDoneable); customDoneable.addConstructor(ctConstructor); Class<? extends CustomResourceDoneable<T>> doneableClass; if (JAVA_VERSION >= 9) { doneableClass = (Class<? extends CustomResourceDoneable<T>>) customDoneable.toClass(customResourceClass); } else { doneableClass = (Class<? extends CustomResourceDoneable<T>>) customDoneable.toClass(); } doneableClassCache.put(customResourceClass, doneableClass); return doneableClass; } catch (CannotCompileException | NotFoundException e) { throw new IllegalStateException(e); } }
Example #10
Source File: Crds.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public static <T extends CustomResource> String kind(Class<T> cls) { try { return cls.newInstance().getKind(); } catch (ReflectiveOperationException e) { throw new RuntimeException(e); } }
Example #11
Source File: AbstractCrdIT.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
protected <T extends CustomResource> void createScaleDelete(Class<T> resourceClass, String resource) { T model = loadResource(resourceClass, resource); String modelKind = model.getKind(); String modelName = model.getMetadata().getName(); String modelStr = TestUtils.toYamlString(model); createScaleDelete(modelKind, modelName, modelStr); }
Example #12
Source File: MockKube.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public <T extends CustomResource, L extends KubernetesResourceList<T>, D extends Doneable<T>, S> MockedCrd<T, L, D, S> withCustomResourceDefinition(CustomResourceDefinition crd, Class<T> instanceClass, Class<L> instanceListClass, Class<D> doneableInstanceClass, Function<T, S> getStatus, BiConsumer<T, S> setStatus) { MockedCrd<T, L, D, S> mockedCrd = new MockedCrd<>(crd, instanceClass, instanceListClass, doneableInstanceClass, getStatus, setStatus); this.mockedCrds.add(mockedCrd); return mockedCrd; }
Example #13
Source File: CrdGenerator.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
void generate(Class<? extends CustomResource> crdClass, Writer out) throws IOException { ObjectNode node = nf.objectNode(); Crd crd = crdClass.getAnnotation(Crd.class); if (crd == null) { err(crdClass + " is not annotated with @Crd"); } else { String apiVersion = crd.apiVersion(); if (!apiVersion.startsWith("apiextensions.k8s.io")) { warn("@ Crd.apiVersion is the API version of the CustomResourceDefinition," + "not the version of instances of the custom resource. " + "It should almost certainly be apiextensions.k8s.io/${some-version}."); } node.put("apiVersion", apiVersion) .put("kind", "CustomResourceDefinition") .putObject("metadata") .put("name", crd.spec().names().plural() + "." + crd.spec().group()); if (!labels.isEmpty()) { ((ObjectNode) node.get("metadata")) .putObject("labels") .setAll(labels.entrySet().stream() .collect(Collectors.<Map.Entry<String, String>, String, JsonNode, LinkedHashMap<String, JsonNode>>toMap( Map.Entry::getKey, e -> new TextNode( e.getValue() .replace("%group%", crd.spec().group()) .replace("%plural%", crd.spec().names().plural()) .replace("%singular%", crd.spec().names().singular())), (x, y) -> x, LinkedHashMap::new))); } node.set("spec", buildSpec(crd.spec(), crdClass)); } mapper.writeValue(out, node); }
Example #14
Source File: EventDispatcher.java From java-operator-sdk with Apache License 2.0 | 5 votes |
private void addFinalizerIfNotPresent(CustomResource resource) { if (!hasDefaultFinalizer(resource) && !markedForDeletion(resource)) { log.info("Adding default finalizer to {}", resource.getMetadata()); if (resource.getMetadata().getFinalizers() == null) { resource.getMetadata().setFinalizers(new ArrayList<>(1)); } resource.getMetadata().getFinalizers().add(resourceDefaultFinalizer); } }
Example #15
Source File: DocGenerator.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public DocGenerator(int headerDepth, Iterable<Class<? extends CustomResource>> crdClasses, Appendable out, Linker linker) { this.out = out; this.headerDepth = headerDepth; this.linker = linker; this.usedIn = new HashMap<>(); for (Class<? extends CustomResource> crdClass: crdClasses) { usedIn(crdClass, usedIn); } }
Example #16
Source File: EventDispatcher.java From java-operator-sdk with Apache License 2.0 | 5 votes |
public void handleEvent(Watcher.Action action, CustomResource resource) { log.info("Handling event {} for resource {}", action, resource.getMetadata()); if (Watcher.Action.ERROR == action) { log.error("Received error for resource: {}", resource.getMetadata().getName()); return; } // Its interesting problem if we should call delete if received event after object is marked for deletion // but there is not our finalizer. Since it can happen that there are multiple finalizers, also other events after // we called delete and remove finalizers already. But also it can happen that we did not manage to put // finalizer into the resource before marked for delete. So for now we will call delete every time, since delete // operation should be idempotent too, and this way we cover the corner case. if (markedForDeletion(resource) || action == Watcher.Action.DELETED) { boolean removeFinalizer = controller.deleteResource(resource); if (removeFinalizer && hasDefaultFinalizer(resource)) { log.debug("Removing finalizer on {}: {}", resource.getMetadata().getName(), resource.getMetadata()); removeDefaultFinalizer(resource); } } else { Optional<CustomResource> updateResult = controller.createOrUpdateResource(resource); if (updateResult.isPresent()) { log.debug("Updating resource: {} with version: {}", resource.getMetadata().getName(), resource.getMetadata().getResourceVersion()); log.trace("Resource before update: {}", resource); CustomResource updatedResource = updateResult.get(); addFinalizerIfNotPresent(updatedResource); replace(updatedResource); log.trace("Resource after update: {}", resource); // We always add the default finalizer if missing and not marked for deletion. } else if (!hasDefaultFinalizer(resource) && !markedForDeletion(resource)) { log.debug("Adding finalizer for resource: {} version: {}", resource.getMetadata().getName(), resource.getMetadata().getResourceVersion()); addFinalizerIfNotPresent(resource); replace(resource); } } }
Example #17
Source File: EventScheduler.java From java-operator-sdk with Apache License 2.0 | 5 votes |
@Override public void eventReceived(Watcher.Action action, CustomResource resource) { log.debug("Event received for action: {}, {}: {}", action.toString().toLowerCase(), resource.getClass().getSimpleName(), resource.getMetadata().getName()); CustomResourceEvent event = new CustomResourceEvent(action, resource, retry); scheduleEventFromApi(event); }
Example #18
Source File: EventConsumer.java From java-operator-sdk with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") private boolean processEvent() { Watcher.Action action = event.getAction(); CustomResource resource = event.getResource(); try { eventDispatcher.handleEvent(action, resource); } catch (RuntimeException e) { log.error("Processing event {} failed.", event, e); return false; } return true; }
Example #19
Source File: Crds.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public static <T extends CustomResource, L extends CustomResourceList<T>, D extends Doneable<T>> MixedOperation<T, L, D, Resource<T, D>> operation(KubernetesClient client, Class<T> cls, Class<L> listCls, Class<D> doneableCls) { return client.customResources(crd(cls), cls, listCls, doneableCls); }
Example #20
Source File: Crds.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
/** * Register custom resource kinds with {@link KubernetesDeserializer} so Fabric8 knows how to deserialize them. */ public static void registerCustomKinds() { for (Class<? extends CustomResource> crdClass : CRDS) { for (String version : apiVersions(crdClass)) { KubernetesDeserializer.registerCustomKind(version, kind(crdClass), crdClass); } } }
Example #21
Source File: EventDispatcherTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
CustomResource getResource() { TestCustomResource resource = new TestCustomResource(); resource.setMetadata(new ObjectMetaBuilder() .withClusterName("clusterName") .withCreationTimestamp("creationTimestamp") .withDeletionGracePeriodSeconds(10L) .withGeneration(10L) .withName("name") .withNamespace("namespace") .withResourceVersion("resourceVersion") .withSelfLink("selfLink") .withUid("uid").build()); return resource; }
Example #22
Source File: Crds.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
private static CustomResourceDefinition crd(Class<? extends CustomResource> cls) { String version = null; if (cls.equals(Kafka.class)) { version = Kafka.VERSIONS.get(0); } else if (cls.equals(KafkaConnect.class)) { version = KafkaConnect.VERSIONS.get(0); } else if (cls.equals(KafkaConnectS2I.class)) { version = KafkaConnectS2I.VERSIONS.get(0); } else if (cls.equals(KafkaTopic.class)) { version = Kafka.VERSIONS.get(0); } else if (cls.equals(KafkaUser.class)) { version = Kafka.VERSIONS.get(0); } else if (cls.equals(KafkaMirrorMaker.class)) { version = KafkaMirrorMaker.VERSIONS.get(0); } else if (cls.equals(KafkaBridge.class)) { version = KafkaBridge.VERSIONS.get(0); } else if (cls.equals(KafkaConnector.class)) { version = KafkaConnector.VERSIONS.get(0); } else if (cls.equals(KafkaMirrorMaker2.class)) { version = KafkaMirrorMaker2.VERSIONS.get(0); } else if (cls.equals(KafkaRebalance.class)) { version = KafkaRebalance.VERSIONS.get(0); } else { throw new RuntimeException(); } return crd(cls, version); }
Example #23
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
@Test public void schedulesEvent() { normalDispatcherExecution(); CustomResource resource = sampleResource(); eventScheduler.eventReceived(Watcher.Action.MODIFIED, resource); waitMinimalTimeForExecution(); verify(eventDispatcher, times(1)).handleEvent(Watcher.Action.MODIFIED, resource); assertThat(eventProcessingList).hasSize(1); }
Example #24
Source File: StatusUtils.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public static <R extends CustomResource, S extends Status> void setStatusConditionAndObservedGeneration(R resource, S status, String type, String conditionStatus, Throwable error) { if (resource.getMetadata().getGeneration() != null) { status.setObservedGeneration(resource.getMetadata().getGeneration()); } Condition readyCondition = StatusUtils.buildConditionFromException(type, conditionStatus, error); status.setConditions(Collections.singletonList(readyCondition)); }
Example #25
Source File: StatusUtils.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public static <R extends CustomResource, S extends Status> void setStatusConditionAndObservedGeneration(R resource, S status, String type, String conditionStatus) { if (resource.getMetadata().getGeneration() != null) { status.setObservedGeneration(resource.getMetadata().getGeneration()); } Condition condition = StatusUtils.buildCondition(type, conditionStatus, null); status.setConditions(Collections.singletonList(condition)); }
Example #26
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
public EventProcessingDetail(Watcher.Action action, LocalDateTime startTime, LocalDateTime endTime, CustomResource customResource, Exception exception) { this.action = action; this.startTime = startTime; this.endTime = endTime; this.customResource = customResource; this.exception = exception; }
Example #27
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
CustomResource sampleResource() { TestCustomResource resource = new TestCustomResource(); resource.setMetadata(new ObjectMetaBuilder() .withCreationTimestamp("creationTimestamp") .withDeletionGracePeriodSeconds(10L) .withGeneration(1L) .withName("name") .withNamespace("namespace") .withResourceVersion("1") .withSelfLink("selfLink") .withUid("uid").build()); return resource; }
Example #28
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
private Object exceptionInExecution(InvocationOnMock invocation) { try { Object[] args = invocation.getArguments(); LocalDateTime start = LocalDateTime.now(); Thread.sleep(INVOCATION_DURATION); LocalDateTime end = LocalDateTime.now(); IllegalStateException exception = new IllegalStateException("Exception thrown for testing purposes"); eventProcessingList.add(new EventProcessingDetail((Watcher.Action) args[0], start, end, (CustomResource) args[1], exception)); throw exception; } catch (InterruptedException e) { throw new IllegalStateException(e); } }
Example #29
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
private Object normalExecution(InvocationOnMock invocation) { try { Object[] args = invocation.getArguments(); LocalDateTime start = LocalDateTime.now(); Thread.sleep(INVOCATION_DURATION); LocalDateTime end = LocalDateTime.now(); eventProcessingList.add(new EventProcessingDetail((Watcher.Action) args[0], start, end, (CustomResource) args[1])); return null; } catch (InterruptedException e) { throw new IllegalStateException(e); } }
Example #30
Source File: EventSchedulerTest.java From java-operator-sdk with Apache License 2.0 | 5 votes |
/** * Tests scenario when controller execution always fails (throws exception), but since the number of retries is limited * it will end eventually with maximal number of processing attempts. */ @Test public void numberOfRetriesCanBeLimited() { doAnswer(this::exceptionInExecution).when(eventDispatcher).handleEvent(any(Watcher.Action.class), any(CustomResource.class)); eventScheduler.eventReceived(Watcher.Action.MODIFIED, sampleResource()); waitTimeForExecution(1, MAX_RETRY_ATTEMPTS + 2); assertThat(eventProcessingList).hasSize(MAX_RETRY_ATTEMPTS); }