io.fabric8.kubernetes.api.model.apiextensions.DoneableCustomResourceDefinition Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.apiextensions.DoneableCustomResourceDefinition. 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: PatchService.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private static EntityPatcher<CustomResourceDefinition> crdPatcher() {
    return (KubernetesClient client, String namespace, CustomResourceDefinition newObj, CustomResourceDefinition oldObj) -> {
        if (UserConfigurationCompare.configEqual(newObj, oldObj)) {
            return oldObj;
        }

        DoneableCustomResourceDefinition entity =
                client.customResourceDefinitions()
                        .withName(oldObj.getMetadata().getName())
                        .edit();

        if (!UserConfigurationCompare.configEqual(newObj.getMetadata(), oldObj.getMetadata())) {
            entity.withMetadata(newObj.getMetadata());
        }

        if (!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
            entity.withSpec(newObj.getSpec());
        }
        return entity.done();
    };
}
 
Example #2
Source File: KubernetesConnection.java    From vault-crd with Apache License 2.0 6 votes vote down vote up
@Bean
public MixedOperation<Vault, VaultList, DoneableVault, Resource<Vault, DoneableVault>> customResource(
        KubernetesClient client, @Value("${kubernetes.crd.name}") String crdName) {
    Resource<CustomResourceDefinition, DoneableCustomResourceDefinition> crdResource
            = client.customResourceDefinitions().withName(crdName);

    // Hack for bug in Kubernetes-Client for CRDs https://github.com/fabric8io/kubernetes-client/issues/1099
    String kind = StringUtils.substringAfter(crdName, ".") + "/v1#Vault";
    KubernetesDeserializer.registerCustomKind(kind, Vault.class);

    CustomResourceDefinition customResourceDefinition = crdResource.get();
    if (customResourceDefinition == null) {
        log.error("Please first apply custom resource definition and then restart vault-crd");
        System.exit(1);
    }

    return client.customResources(customResourceDefinition, Vault.class, VaultList.class, DoneableVault.class);
}
 
Example #3
Source File: CustomResourceDefinitionOperationsImpl.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public CustomResourceDefinitionOperationsImpl(OperationContext context) {
  super(context.withApiGroupName("apiextensions.k8s.io")
    .withApiGroupVersion("v1beta1")
    .withPlural("customresourcedefinitions"));
  this.type = CustomResourceDefinition.class;
  this.listType = CustomResourceDefinitionList.class;
  this.doneableType = DoneableCustomResourceDefinition.class;
}
 
Example #4
Source File: KafkaMetaDataRetrievalTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldFetchPropertiesFromKafkaCustomResources() {
    final KubernetesClient client = mock(KubernetesClient.class);

    final KafkaMetaDataRetrieval metaDataRetrieval = new KafkaMetaDataRetrieval() {
        @Override
        KubernetesClient createKubernetesClient() {
            return client;
        }
    };

    @SuppressWarnings("unchecked")
    final NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> definitions = mock(
        NonNamespaceOperation.class);
    when(client.customResourceDefinitions()).thenReturn(definitions);
    final CustomResourceDefinitionList values = new CustomResourceDefinitionList();
    values.setItems(Collections.singletonList(STRIMZI_KAFKA_CRD));
    when(definitions.list()).thenReturn(values);

    @SuppressWarnings("unchecked")
    final MixedOperation<Kafka, KafkaResourceList, KafkaResourceDoneable, Resource<Kafka, KafkaResourceDoneable>> operation = mock(
        MixedOperation.class);
    when(client.customResources(STRIMZI_KAFKA_CRD,
        Kafka.class,
        KafkaResourceList.class,
        KafkaResourceDoneable.class)).thenReturn(operation);
    when(operation.inAnyNamespace()).thenReturn(operation);

    when(operation.list()).thenReturn(KAFKAS);

    final SyndesisMetadataProperties properties = metaDataRetrieval.fetchProperties(null, null, null);

    final Map<String, List<PropertyPair>> expected = new HashMap<>();
    expected.put("brokers", Arrays.asList(
        new PropertyPair("my-cluster-kafka-bootstrap.zregvart.svc:9092", "zregvart::my-cluster (plain)"),
        new PropertyPair("my-cluster-kafka-bootstrap.zregvart.svc:9093", "zregvart::my-cluster (tls)"),
        new PropertyPair("zorans-cluster-kafka-bootstrap.zregvart.svc:9092", "zregvart::zorans-cluster (plain)"),
        new PropertyPair("zorans-cluster-kafka-bootstrap.zregvart.svc:9093", "zregvart::zorans-cluster (tls)")
        ));

    assertThat(properties.getProperties()).containsExactlyEntriesOf(expected);
}
 
Example #5
Source File: ClusterOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that Cluster Operator starts and then stops a verticle in each namespace
 * @param context test context passed in for assertions
 * @param namespaces namespaces the operator should be watching and operating on
 */
private void startStop(VertxTestContext context, String namespaces, boolean openShift) throws InterruptedException {
    AtomicInteger numWatchers = new AtomicInteger(0);

    KubernetesClient client;
    if (openShift) {
        client = mock(OpenShiftClient.class);
        when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(true);
        when(client.adapt(eq(OpenShiftClient.class))).thenReturn((OpenShiftClient) client);
    } else {
        client = mock(KubernetesClient.class);
        when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(false);
    }
    when(client.isAdaptable(eq(OkHttpClient.class))).thenReturn(true);

    try {
        when(client.getMasterUrl()).thenReturn(new URL("http://localhost"));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
    MixedOperation mockCms = mock(MixedOperation.class);
    NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> mockCrds = mock(NonNamespaceOperation.class);
    Resource<CustomResourceDefinition, DoneableCustomResourceDefinition> mockResource = mock(Resource.class);
    if (openShift) {
        when(mockResource.get()).thenReturn(Crds.kafkaConnectS2I());
    } else {
        when(mockResource.get()).thenReturn(null);
    }
    when(mockCrds.withName(KafkaConnectS2I.CRD_NAME)).thenReturn(mockResource);
    when(client.customResourceDefinitions()).thenReturn(mockCrds);
    when(client.customResources(any(), any(), any(), any())).thenReturn(mockCms);

    List<String> namespaceList = asList(namespaces.split(" *,+ *"));
    for (String namespace: namespaceList) {

        MixedOperation mockNamespacedCms = mock(MixedOperation.class);
        when(mockNamespacedCms.watch(any())).thenAnswer(invo -> {
            numWatchers.incrementAndGet();
            Watch mockWatch = mock(Watch.class);
            doAnswer(invo2 -> {
                ((Watcher) invo.getArgument(0)).onClose(null);
                return null;
            }).when(mockWatch).close();
            return mockWatch;
        });

        when(mockNamespacedCms.withLabels(any())).thenReturn(mockNamespacedCms);
        when(mockCms.inNamespace(namespace)).thenReturn(mockNamespacedCms);
    }

    Map<String, String> env = buildEnv(namespaces);

    CountDownLatch latch = new CountDownLatch(namespaceList.size() + 1);

    Main.run(vertx, client, new PlatformFeaturesAvailability(openShift, KubernetesVersion.V1_9),
                ClusterOperatorConfig.fromMap(env, KafkaVersionTestUtils.getKafkaVersionLookup()))
        .onComplete(context.succeeding(v -> context.verify(() -> {
            assertThat("A verticle per namespace", vertx.deploymentIDs(), hasSize(namespaceList.size()));
            for (String deploymentId: vertx.deploymentIDs()) {
                vertx.undeploy(deploymentId, asyncResult -> {
                    if (asyncResult.failed()) {
                        log.error("Failed to undeploy {}", deploymentId);
                        context.failNow(asyncResult.cause());
                    }
                    latch.countDown();
                });
            }

            int maximumExpectedNumberOfWatchers = (openShift ? 9 : 7) * namespaceList.size(); // we do not have connectS2I on k8s
            assertThat("Looks like there were more watchers than namespaces",
                    numWatchers.get(), lessThanOrEqualTo(maximumExpectedNumberOfWatchers));
            latch.countDown();
        })));
    latch.await(10, TimeUnit.SECONDS);
    context.completeNow();
}
 
Example #6
Source File: ClusterOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts that Cluster Operator starts and then stops a verticle in every namespace using the namespace wildcard (*)
 * @param context test context passed in for assertions
 * @param namespaces namespaces the operator should be watching and operating on
 */
private void startStopAllNamespaces(VertxTestContext context, String namespaces, boolean openShift) throws InterruptedException {
    AtomicInteger numWatchers = new AtomicInteger(0);
    KubernetesClient client;
    if (openShift) {
        client = mock(OpenShiftClient.class);
        when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(true);
        when(client.adapt(eq(OpenShiftClient.class))).thenReturn((OpenShiftClient) client);
    } else {
        client = mock(KubernetesClient.class);
        when(client.isAdaptable(eq(OpenShiftClient.class))).thenReturn(false);
    }
    when(client.isAdaptable(eq(OkHttpClient.class))).thenReturn(true);

    try {
        when(client.getMasterUrl()).thenReturn(new URL("http://localhost"));
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    MixedOperation mockCms = mock(MixedOperation.class);
    NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition,
            Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> mockCrds = mock(NonNamespaceOperation.class);
    Resource<CustomResourceDefinition, DoneableCustomResourceDefinition> mockResource = mock(Resource.class);
    if (openShift) {
        when(mockResource.get()).thenReturn(Crds.kafkaConnectS2I());
    } else {
        when(mockResource.get()).thenReturn(null);
    }
    when(mockCrds.withName(KafkaConnectS2I.CRD_NAME)).thenReturn(mockResource);
    when(client.customResourceDefinitions()).thenReturn(mockCrds);
    when(client.customResources(any(), any(), any(), any())).thenReturn(mockCms);

    FilterWatchListMultiDeletable mockFilteredCms = mock(FilterWatchListMultiDeletable.class);
    when(mockFilteredCms.withLabels(any())).thenReturn(mockFilteredCms);
    when(mockFilteredCms.watch(any())).thenAnswer(invo -> {
        numWatchers.incrementAndGet();
        Watch mockWatch = mock(Watch.class);
        doAnswer(invo2 -> {
            ((Watcher) invo.getArgument(0)).onClose(null);
            return null;
        }).when(mockWatch).close();
        return mockWatch;
    });
    when(mockCms.inAnyNamespace()).thenReturn(mockFilteredCms);

    Map<String, String> env = buildEnv(namespaces);

    CountDownLatch latch = new CountDownLatch(2);
    Main.run(vertx, client, new PlatformFeaturesAvailability(openShift, KubernetesVersion.V1_9),
            ClusterOperatorConfig.fromMap(env, KafkaVersionTestUtils.getKafkaVersionLookup()))
        .onComplete(context.succeeding(v -> context.verify(() -> {
            assertThat("A verticle per namespace", vertx.deploymentIDs(), hasSize(1));
            for (String deploymentId: vertx.deploymentIDs()) {
                vertx.undeploy(deploymentId, asyncResult -> {
                    if (asyncResult.failed()) {
                        log.error("Failed to undeploy {}", deploymentId);
                        context.failNow(asyncResult.cause());
                    }
                    latch.countDown();
                });
            }

            int maximumExpectedNumberOfWatchers = openShift ? 9 : 7; // we do not have connectS2I on k8s
            assertThat("Looks like there were more watchers than namespaces", numWatchers.get(), lessThanOrEqualTo(maximumExpectedNumberOfWatchers));
            latch.countDown();
        })));
    latch.await(10, TimeUnit.SECONDS);
    context.completeNow();
}
 
Example #7
Source File: DefaultKubernetesClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> customResourceDefinitions() {
  return new CustomResourceDefinitionOperationsImpl(httpClient, getConfiguration());
}
 
Example #8
Source File: ManagedKubernetesClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> customResourceDefinitions() {
  return delegate.customResourceDefinitions();
}
 
Example #9
Source File: ManagedOpenShiftClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> customResourceDefinitions() {
  return delegate.customResourceDefinitions();
}
 
Example #10
Source File: DefaultOpenShiftClient.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Override
public NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> customResourceDefinitions() {
  return new CustomResourceDefinitionOperationsImpl(httpClient, getConfiguration());
}
 
Example #11
Source File: KubernetesClient.java    From kubernetes-client with Apache License 2.0 2 votes vote down vote up
/**
 * API entrypoint for CustomResourcedefinition(CRDs). This offers basic operations like
 * load, get, create, update, delete and watch for APIGroup apiextensions/v1beta1
 *
 * @return NonNamespaceOperation object for CustomResourceDefinition
 */
NonNamespaceOperation<CustomResourceDefinition, CustomResourceDefinitionList, DoneableCustomResourceDefinition, Resource<CustomResourceDefinition, DoneableCustomResourceDefinition>> customResourceDefinitions();