io.fabric8.kubernetes.client.dsl.Resource Java Examples
The following examples show how to use
io.fabric8.kubernetes.client.dsl.Resource.
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: AbstractNonNamespacedResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
@Test public void testDeletionWhenResourceDoesNotExistIsANop(VertxTestContext context) { T resource = resource(); Resource mockResource = mock(resourceType()); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.reconcile(resource.getMetadata().getName(), null).onComplete(context.succeeding(rrDeleted -> { verify(mockResource).get(); verify(mockResource, never()).delete(); async.flag(); })); }
Example #2
Source File: AbstractNonNamespacedResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
@Test public void testSuccessfulCreation(VertxTestContext context) { T resource = resource(); Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(null); when(mockResource.create(any())).thenReturn(resource); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperationsWithMockedReadiness( vertx, mockClient); Checkpoint async = context.checkpoint(); op.createOrUpdate(resource).onComplete(context.succeeding(rr -> { verify(mockResource).get(); verify(mockResource).create(eq(resource)); async.flag(); })); }
Example #3
Source File: ApplyService.java From jkube with Eclipse Public License 2.0 | 6 votes |
public void applyImageStream(ImageStream entity, String sourceName) { OpenShiftClient openShiftClient = getOpenShiftClient(); if (openShiftClient != null) { String kind = getKind(entity); String name = getName(entity); String namespace = getNamespace(); try { Resource<ImageStream, DoneableImageStream> resource = openShiftClient.imageStreams().inNamespace(namespace).withName(name); ImageStream old = resource.get(); if (old == null) { log.info("Creating " + kind + " " + name + " from " + sourceName); resource.create(entity); } else { log.info("Updating " + kind + " " + name + " from " + sourceName); copyAllImageStreamTags(entity, old); entity = patchService.compareAndPatchEntity(namespace, entity, old); openShiftClient.resource(entity).inNamespace(namespace).createOrReplace(); } } catch (Exception e) { onApplyError("Failed to create " + kind + " from " + sourceName + ". " + e, e); } } }
Example #4
Source File: AbstractResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
@Test public void testCreateOrUpdateThrowsWhenCreateThrows(VertxTestContext context) { T resource = resource(); RuntimeException ex = new RuntimeException("Testing this exception is handled correctly"); Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(null); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.inNamespace(matches(resource.getMetadata().getNamespace()))).thenReturn(mockNameable); when(mockResource.create(any())).thenThrow(ex); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.createOrUpdate(resource).onComplete(context.failing(e -> { context.verify(() -> assertThat(e, is(ex))); async.flag(); })); }
Example #5
Source File: KubernetesNamespaceTest.java From che with Eclipse Public License 2.0 | 6 votes |
@Test public void testKubernetesNamespacePreparingCreationWhenNamespaceDoesNotExist() throws Exception { // given MetadataNested namespaceMeta = prepareCreateNamespaceRequest(); Resource resource = prepareNamespaceResource(NAMESPACE); doThrow(new KubernetesClientException("error", 403, null)).when(resource).get(); KubernetesNamespace namespace = new KubernetesNamespace(clientFactory, executor, NAMESPACE, WORKSPACE_ID); // when namespace.prepare(true); // then verify(namespaceMeta).withName(NAMESPACE); }
Example #6
Source File: OpenShiftProjectTest.java From che with Eclipse Public License 2.0 | 6 votes |
@Test public void testOpenShiftProjectPreparingWhenProjectDoesNotExist() throws Exception { // given MetadataNested projectMetadata = prepareProjectRequest(); Resource resource = prepareProjectResource(PROJECT_NAME); doThrow(new KubernetesClientException("error", 403, null)).when(resource).get(); OpenShiftProject project = new OpenShiftProject(clientFactory, executor, PROJECT_NAME, WORKSPACE_ID); // when openShiftProject.prepare(true); // then verify(projectMetadata).withName(PROJECT_NAME); }
Example #7
Source File: AbstractNonNamespacedResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
@Test public void testReconcileDeletionThrowsWhenWatchMethodThrows(VertxTestContext context) { T resource = resource(); RuntimeException ex = new RuntimeException("Testing this exception is handled correctly"); Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(resource); when(mockResource.delete()).thenThrow(ex); when(mockResource.watch(any())).thenThrow(ex); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.reconcile(resource.getMetadata().getName(), null).onComplete(context.failing(e -> { context.verify(() -> assertThat(e, is(ex))); async.flag(); })); }
Example #8
Source File: RabbitMQResourceController.java From rabbitmq-operator with Apache License 2.0 | 5 votes |
@Override protected MixedOperation<RabbitMQCustomResource, RabbitMQCustomResourceList, DoneableRabbitMQCustomResource, Resource<RabbitMQCustomResource, DoneableRabbitMQCustomResource>> operation() { final CustomResourceDefinition rabbitCrd = getClient().customResourceDefinitions().withName(RABBITMQ_CRD_NAME).get(); if (rabbitCrd == null) { throw new RuntimeException(String.format("CustomResourceDefinition %s has not been defined", RABBITMQ_CRD_NAME)); } return getClient().customResources(rabbitCrd, RabbitMQCustomResource.class, RabbitMQCustomResourceList.class, DoneableRabbitMQCustomResource.class); }
Example #9
Source File: ExtensionsAPIGroupClient.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Override @Deprecated /** * @deprecated Replaced by {@link PolicyAPIGroupClient#podSecurityPolicies()} */ public MixedOperation<PodSecurityPolicy, PodSecurityPolicyList, DoneablePodSecurityPolicy, Resource<PodSecurityPolicy, DoneablePodSecurityPolicy>> podSecurityPolicies() { return new PodSecurityPolicyOperationsImpl(httpClient, getConfiguration()); }
Example #10
Source File: ServiceAccountOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
@Override public void createWhenExistsIsAPatch(VertxTestContext context, boolean cascade) { // This is overridden because SA patch is coded as a no op to avoid needless token creation. ServiceAccount resource = resource(); Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(resource); when(mockResource.cascading(cascade)).thenReturn(mockResource); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.inNamespace(matches(resource.getMetadata().getNamespace()))).thenReturn(mockNameable); KubernetesClient mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractResourceOperator<KubernetesClient, ServiceAccount, ServiceAccountList, DoneableServiceAccount, Resource<ServiceAccount, DoneableServiceAccount>> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.createOrUpdate(resource) .onComplete(context.succeeding(rr -> { context.verify(() -> assertThat(rr, instanceOf(ReconcileResult.Noop.class))); verify(mockResource).get(); //verify(mockResource).patch(any()); verify(mockResource, never()).create(any()); verify(mockResource, never()).createNew(); verify(mockResource, never()).createOrReplace(any()); verify(mockCms, never()).createOrReplace(any()); async.flag(); })); }
Example #11
Source File: KubeDiscovery.java From vxms with Apache License 2.0 | 5 votes |
private static ServiceList getServicesByLabel(Map<String, String> labels, KubernetesClient client) throws KubernetesClientException { Objects.requireNonNull(client, "no client available"); final MixedOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> services = client.services(); final FilterWatchListDeletable<Service, ServiceList, Boolean, Watch, Watcher<Service>> listable = createLabelFilterQuery(labels, services); return listable.list(); }
Example #12
Source File: PodSetController.java From podsetoperatorinjava with Apache License 2.0 | 5 votes |
public PodSetController(KubernetesClient kubernetesClient, MixedOperation<PodSet, PodSetList, DoneablePodSet, Resource<PodSet, DoneablePodSet>> podSetClient, SharedIndexInformer<Pod> podInformer, SharedIndexInformer<PodSet> podSetInformer, String namespace) { this.kubernetesClient = kubernetesClient; this.podSetClient = podSetClient; this.podSetLister = new Lister<>(podSetInformer.getIndexer(), namespace); this.podSetInformer = podSetInformer; this.podLister = new Lister<>(podInformer.getIndexer(), namespace); this.podInformer = podInformer; this.workqueue = new ArrayBlockingQueue<>(1024); }
Example #13
Source File: PersistentVolumeClaimTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test void testEditMissing() { server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim").andReturn(404, "error message from kubernetes").always(); KubernetesClient client = server.getClient(); Resource<PersistentVolumeClaim, DoneablePersistentVolumeClaim> pvcResource = client.persistentVolumeClaims().inNamespace("test").withName("persistentvolumeclaim"); Assertions.assertThrows(KubernetesClientException.class, pvcResource::edit); }
Example #14
Source File: UserHandlerTest.java From syndesis with Apache License 2.0 | 5 votes |
@Test public void successfulWhoAmIWithoutFullName() { SecurityContext sec = mock(SecurityContext.class); Principal principal = mock(Principal.class); when(principal.getName()).thenReturn("testuser"); when(sec.getUserPrincipal()).thenReturn(principal); NamespacedOpenShiftClient client = mock(NamespacedOpenShiftClient.class); @SuppressWarnings("unchecked") Resource<User,DoneableUser> user = mock(Resource.class); when(user.get()).thenReturn(new UserBuilder().withNewMetadata().withName("testuser").and().build()); @SuppressWarnings("unchecked") NonNamespaceOperation<User, UserList, DoneableUser, Resource<User, DoneableUser>> users = mock(NonNamespaceOperation.class); when(users.withName("testuser")).thenReturn(user); when(client.users()).thenReturn(users); SecurityContextHolder.getContext().setAuthentication(new PreAuthenticatedAuthenticationToken("testuser", "doesn'tmatter")); UserConfigurationProperties properties = new UserConfigurationProperties(); properties.setMaxDeploymentsPerUser(1); UserHandler userHandler = new UserHandler(null, new OpenShiftServiceImpl(client, null), properties); io.syndesis.common.model.user.User whoAmI = userHandler.whoAmI(sec); Assertions.assertThat(whoAmI).isNotNull(); Assertions.assertThat(whoAmI.getUsername()).isEqualTo("testuser"); Assertions.assertThat(whoAmI.getFullName()).isEmpty(); }
Example #15
Source File: AbstractResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public void createWhenExistsIsAPatch(VertxTestContext context, boolean cascade) { T resource = resource(); Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(resource); when(mockResource.cascading(cascade)).thenReturn(mockResource); when(mockResource.patch(any())).thenReturn(resource); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.inNamespace(matches(resource.getMetadata().getNamespace()))).thenReturn(mockNameable); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.createOrUpdate(resource()).onComplete(context.succeeding(rr -> context.verify(() -> { verify(mockResource).get(); verify(mockResource).patch(any()); verify(mockResource, never()).create(any()); verify(mockResource, never()).createNew(); verify(mockResource, never()).createOrReplace(any()); verify(mockCms, never()).createOrReplace(any()); async.flag(); }))); }
Example #16
Source File: OpenShiftProjectTest.java From che with Eclipse Public License 2.0 | 5 votes |
@Test(expectedExceptions = InfrastructureException.class) public void throwsExceptionIfNamespaceDoesntExistAndNotAllowedToCreateIt() throws Exception { // given Resource resource = prepareProjectResource(PROJECT_NAME); doThrow(new KubernetesClientException("error", 403, null)).when(resource).get(); OpenShiftProject project = new OpenShiftProject(clientFactory, executor, PROJECT_NAME, WORKSPACE_ID); // when project.prepare(false); // then // exception is thrown }
Example #17
Source File: ConnectorMockTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
public void waitForConnectorNotReady(String connectorName, String reason, String message) { Resource<KafkaConnector, DoneableKafkaConnector> resource = Crds.kafkaConnectorOperation(client) .inNamespace(NAMESPACE) .withName(connectorName); waitForStatus(resource, connectorName, ConnectorMockTest.<KafkaConnector>statusIsForCurrentGeneration().and(notReady(reason, message))); }
Example #18
Source File: OpenShiftProjectFactoryTest.java From che with Eclipse Public License 2.0 | 5 votes |
private void throwOnTryToGetProjectByName(String name, KubernetesClientException e) throws Exception { @SuppressWarnings("unchecked") Resource<Project, DoneableProject> getProjectByNameOperation = mock(Resource.class); when(projectOperation.withName(name)).thenReturn(getProjectByNameOperation); when(getProjectByNameOperation.get()).thenThrow(e); }
Example #19
Source File: AbtractReadyResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
@Test public void testWaitUntilReadyUnsuccessful(VertxTestContext context) { T resource = resource(); // This test does not apply to the resource types without the Ready field if (!Readiness.isReadinessApplicable(resource.getClass())) { context.completeNow(); } Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(resource); when(mockResource.isReady()).thenReturn(Boolean.FALSE); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(resource.getMetadata().getName()))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.inNamespace(matches(resource.getMetadata().getNamespace()))).thenReturn(mockNameable); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractReadyResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.readiness(NAMESPACE, RESOURCE_NAME, 20, 100) .onComplete(context.failing(e -> context.verify(() -> { assertThat(e, instanceOf(TimeoutException.class)); verify(mockResource, atLeastOnce()).get(); verify(mockResource, atLeastOnce()).isReady(); async.flag(); }))); }
Example #20
Source File: MockKube.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
private MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> buildStatefulSets(MockBuilder<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> podMockBuilder, MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods, MixedOperation<PersistentVolumeClaim, PersistentVolumeClaimList, DoneablePersistentVolumeClaim, Resource<PersistentVolumeClaim, DoneablePersistentVolumeClaim>> mockPvcs) { MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> result = new StatefulSetMockBuilder(podMockBuilder, ssDb, podDb, mockPods, mockPvcs).build(); return result; }
Example #21
Source File: Kubernetes.java From enmasse with Apache License 2.0 | 5 votes |
public MixedOperation<IoTProject, IoTProjectList, DoneableIoTProject, Resource<IoTProject, DoneableIoTProject>> getIoTProjectClient(String namespace) { if (namespace == null) { return client.customResources(IoTCrd.project(), IoTProject.class, IoTProjectList.class, DoneableIoTProject.class); } else { return (MixedOperation<IoTProject, IoTProjectList, DoneableIoTProject, Resource<IoTProject, DoneableIoTProject>>) client.customResources(IoTCrd.project(), IoTProject.class, IoTProjectList.class, DoneableIoTProject.class).inNamespace(namespace); } }
Example #22
Source File: AbstractNonNamespacedResourceOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("unchecked") public void testDeletionWhenResourceExistsStillDeletes(VertxTestContext context) { T resource = resource(); AtomicBoolean watchWasClosed = new AtomicBoolean(false); Resource mockResource = mock(resourceType()); when(mockResource.get()).thenReturn(resource); when(mockResource.withGracePeriod(anyLong())).thenReturn(mockResource); when(mockResource.delete()).thenReturn(true); when(mockResource.watch(any())).thenAnswer(invocation -> { Watcher<T> watcher = invocation.getArgument(0); watcher.eventReceived(Watcher.Action.DELETED, null); return (Watch) () -> { watchWasClosed.set(true); }; }); NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class); when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); MixedOperation mockCms = mock(MixedOperation.class); when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource); C mockClient = mock(clientType()); mocker(mockClient, mockCms); AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient); Checkpoint async = context.checkpoint(); op.reconcile(resource.getMetadata().getName(), null).onComplete(context.succeeding(rrDeleted -> { verify(mockResource).delete(); context.verify(() -> assertThat("Watch was not closed", watchWasClosed.get(), is(true))); async.flag(); })); }
Example #23
Source File: KubernetesNamespaceFactoryTest.java From che with Eclipse Public License 2.0 | 5 votes |
private void prepareNamespaceToBeFoundByName(String name, Namespace namespace) throws Exception { @SuppressWarnings("unchecked") Resource<Namespace, DoneableNamespace> getNamespaceByNameOperation = mock(Resource.class); when(namespaceOperation.withName(name)).thenReturn(getNamespaceByNameOperation); when(getNamespaceByNameOperation.get()).thenReturn(namespace); }
Example #24
Source File: KubernetesNamespaceTest.java From che with Eclipse Public License 2.0 | 5 votes |
@Test public void testDeletesExistingNamespace() throws Exception { // given KubernetesNamespace namespace = new KubernetesNamespace(clientFactory, executor, NAMESPACE, WORKSPACE_ID); Resource resource = prepareNamespaceResource(NAMESPACE); // when namespace.delete(); // then verify(resource).delete(); }
Example #25
Source File: ManagedKubernetesClient.java From kubernetes-client with Apache License 2.0 | 4 votes |
public MixedOperation<Endpoints, EndpointsList, DoneableEndpoints, Resource<Endpoints, DoneableEndpoints>> endpoints() { return delegate.endpoints(); }
Example #26
Source File: KafkaCrdOperatorTest.java From strimzi-kafka-operator with Apache License 2.0 | 4 votes |
@Override protected Class<Resource> resourceType() { return Resource.class; }
Example #27
Source File: PodDisruptionBudgetController.java From rabbitmq-operator with Apache License 2.0 | 4 votes |
@Override protected MixedOperation<PodDisruptionBudget, PodDisruptionBudgetList, DoneablePodDisruptionBudget, Resource<PodDisruptionBudget, DoneablePodDisruptionBudget>> operation() { return getClient().policy().podDisruptionBudget(); }
Example #28
Source File: K8SCoreRuntime.java From jhipster-operator with Apache License 2.0 | 4 votes |
public <T extends HasMetadata, L extends KubernetesResourceList, D extends Doneable<T>> MixedOperation<T, L, D, Resource<T, D>> customResourcesClient(CustomResourceDefinition crd, Class<T> resourceType, Class<L> listClass, Class<D> doneClass) { return kubernetesClient.customResources(crd, resourceType, listClass, doneClass); }
Example #29
Source File: V1alpha1APIGroupClient.java From kubernetes-client with Apache License 2.0 | 4 votes |
@Override public MixedOperation<Task, TaskList, DoneableTask, Resource<Task, DoneableTask>> tasks() { return new TaskOperationsImpl(this.getHttpClient(), this.getConfiguration()); }
Example #30
Source File: IoTProjects.java From enmasse with Apache License 2.0 | 4 votes |
public static MixedOperation<IoTProject, IoTProjectList, DoneableIoTProject, Resource<IoTProject, DoneableIoTProject>> clientForProject(final KubernetesClient client) { return client .customResources(project(), IoTProject.class, IoTProjectList.class, DoneableIoTProject.class); }