Search in sources :

Example 21 with StatefulSet

use of io.fabric8.kubernetes.api.model.apps.StatefulSet in project kubernetes-client by fabric8io.

the class StatefulSetTest method testDeleteLoadedResource.

@Test
public void testDeleteLoadedResource() {
    StatefulSet response = client.apps().statefulSets().load(getClass().getResourceAsStream("/test-statefulset.yml")).get();
    server.expect().delete().withPath("/apis/apps/v1beta1/namespaces/test/statefulsets/example").andReturn(200, response).once();
    Deletable items = client.load(getClass().getResourceAsStream("/test-statefulset.yml"));
    assertTrue(items.delete());
}
Also used : Deletable(io.fabric8.kubernetes.client.dsl.Deletable) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Test(org.junit.jupiter.api.Test)

Example 22 with StatefulSet

use of io.fabric8.kubernetes.api.model.apps.StatefulSet in project quarkus by quarkusio.

the class OpenshiftWithStatefulSetResourceTest method assertGeneratedResources.

@Test
public void assertGeneratedResources() throws IOException {
    final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir).isDirectoryContaining(p -> p.getFileName().endsWith("openshift.json")).isDirectoryContaining(p -> p.getFileName().endsWith("openshift.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesDir.resolve("openshift.yml"));
    assertThat(kubernetesList).filteredOn(i -> i instanceof StatefulSet).singleElement().satisfies(i -> {
        assertThat(i).isInstanceOfSatisfying(StatefulSet.class, s -> {
            assertThat(s.getMetadata()).satisfies(m -> {
                assertThat(m.getName()).isEqualTo(APP_NAME);
            });
            assertThat(s.getSpec()).satisfies(statefulSetSpec -> {
                assertThat(statefulSetSpec.getServiceName()).isEqualTo(APP_NAME);
                assertThat(statefulSetSpec.getReplicas()).isEqualTo(5);
                assertThat(statefulSetSpec.getTemplate()).satisfies(t -> {
                    assertThat(t.getSpec()).satisfies(podSpec -> {
                        assertThat(podSpec.getTerminationGracePeriodSeconds()).isEqualTo(10);
                        assertThat(podSpec.getContainers()).allMatch(c -> APP_NAME.equals(c.getName()));
                    });
                });
                assertThat(statefulSetSpec.getSelector()).satisfies(ls -> {
                    assertThat(ls.getMatchLabels()).containsEntry("app.kubernetes.io/name", APP_NAME);
                });
            });
        });
    });
}
Also used : Path(java.nio.file.Path) AppArtifact(io.quarkus.bootstrap.model.AppArtifact) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) Version(io.quarkus.builder.Version) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Test(org.junit.jupiter.api.Test) List(java.util.List) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ProdBuildResults(io.quarkus.test.ProdBuildResults) ProdModeTestResults(io.quarkus.test.ProdModeTestResults) Path(java.nio.file.Path) Collections(java.util.Collections) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) Test(org.junit.jupiter.api.Test)

Example 23 with StatefulSet

use of io.fabric8.kubernetes.api.model.apps.StatefulSet in project quarkus by quarkusio.

the class KubernetesWithStatefulSetResourceTest method assertGeneratedResources.

@Test
public void assertGeneratedResources() throws IOException {
    final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList).filteredOn(i -> i instanceof StatefulSet).singleElement().satisfies(i -> {
        assertThat(i).isInstanceOfSatisfying(StatefulSet.class, s -> {
            assertThat(s.getMetadata()).satisfies(m -> {
                assertThat(m.getName()).isEqualTo(APP_NAME);
            });
            assertThat(s.getSpec()).satisfies(statefulSetSpec -> {
                assertThat(statefulSetSpec.getServiceName()).isEqualTo(APP_NAME);
                assertThat(statefulSetSpec.getReplicas()).isEqualTo(42);
                assertThat(statefulSetSpec.getTemplate()).satisfies(t -> {
                    assertThat(t.getSpec()).satisfies(podSpec -> {
                        assertThat(podSpec.getTerminationGracePeriodSeconds()).isEqualTo(10);
                        assertThat(podSpec.getContainers()).allMatch(c -> APP_NAME.equals(c.getName()));
                    });
                });
                assertThat(statefulSetSpec.getSelector()).satisfies(ls -> {
                    assertThat(ls.getMatchLabels()).containsEntry("app.kubernetes.io/name", APP_NAME);
                });
            });
        });
    });
}
Also used : Path(java.nio.file.Path) AppArtifact(io.quarkus.bootstrap.model.AppArtifact) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) Version(io.quarkus.builder.Version) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Test(org.junit.jupiter.api.Test) List(java.util.List) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ProdBuildResults(io.quarkus.test.ProdBuildResults) ProdModeTestResults(io.quarkus.test.ProdModeTestResults) Path(java.nio.file.Path) Collections(java.util.Collections) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) Test(org.junit.jupiter.api.Test)

Example 24 with StatefulSet

use of io.fabric8.kubernetes.api.model.apps.StatefulSet in project quarkus by quarkusio.

the class KubernetesWithInputStatefulSetResourcesTest method assertGeneratedResources.

@Test
public void assertGeneratedResources() throws IOException {
    final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes");
    assertThat(kubernetesDir).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")).isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml"));
    List<HasMetadata> kubernetesList = DeserializationUtil.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));
    assertThat(kubernetesList).filteredOn(i -> i instanceof StatefulSet).singleElement().satisfies(i -> {
        assertThat(i).isInstanceOfSatisfying(StatefulSet.class, s -> {
            assertThat(s.getMetadata()).satisfies(m -> {
                assertThat(m.getName()).isEqualTo(APP_NAME);
            });
            assertThat(s.getSpec()).satisfies(statefulSetSpec -> {
                assertThat(statefulSetSpec.getServiceName()).isEqualTo(APP_NAME);
                assertThat(statefulSetSpec.getReplicas()).isEqualTo(42);
                assertThat(statefulSetSpec.getTemplate()).satisfies(t -> {
                    assertThat(t.getSpec()).satisfies(podSpec -> {
                        assertThat(podSpec.getTerminationGracePeriodSeconds()).isEqualTo(10);
                        assertThat(podSpec.getContainers()).allMatch(c -> APP_NAME.equals(c.getName()));
                    });
                });
                assertThat(statefulSetSpec.getSelector()).satisfies(ls -> {
                    assertThat(ls.getMatchLabels()).containsEntry("app.kubernetes.io/name", APP_NAME);
                    assertThat(ls.getMatchLabels()).containsEntry("custom-label", "my-label");
                });
            });
        });
    });
}
Also used : Path(java.nio.file.Path) AppArtifact(io.quarkus.bootstrap.model.AppArtifact) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) Version(io.quarkus.builder.Version) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IOException(java.io.IOException) CustomProjectRootBuildItem(io.quarkus.kubernetes.spi.CustomProjectRootBuildItem) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Test(org.junit.jupiter.api.Test) List(java.util.List) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) ProdBuildResults(io.quarkus.test.ProdBuildResults) ProdModeTestResults(io.quarkus.test.ProdModeTestResults) Path(java.nio.file.Path) Collections(java.util.Collections) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) QuarkusProdModeTest(io.quarkus.test.QuarkusProdModeTest) Test(org.junit.jupiter.api.Test)

Example 25 with StatefulSet

use of io.fabric8.kubernetes.api.model.apps.StatefulSet in project couchbase-elasticsearch-connector by couchbase.

the class ReplicaChangeWatcher method getReplicasAndPanicOnChange.

/**
 * Starts watching the replica count and panics if the count changes
 * or can't be determined.
 *
 * @return initial number of replicas
 */
public static int getReplicasAndPanicOnChange(KubernetesClient client, PanicButton panicButton) {
    try {
        StatefulSetInfo info = StatefulSetInfo.fromHostname();
        String k8sName = info.name;
        log.info("Kubernetes API version = {}", client.discovery().v1().getApiVersion());
        String namespace = client.discovery().v1().getNamespace();
        log.info("Kubernetes namespace = {}", namespace);
        StatefulSet statefulSet = client.apps().statefulSets().inNamespace(namespace).withName(k8sName).get();
        requireNonNull(statefulSet, "Failed to get StatefulSet in namespace '" + namespace + "' with name '" + k8sName + "'");
        int initialReplicas = statefulSet.getSpec().getReplicas();
        log.info("StatefulSet replicas = {}", initialReplicas);
        log.info("Kubernetes API resync period = {} ; kill switch deadline = {}", resyncPeriod, killSwitchDeadline);
        KillSwitch killSwitch = KillSwitch.start(killSwitchDeadline, () -> panicButton.panic("The connector contacts the Kubernetes API server every " + resyncPeriod + " to verify the StatefulSet's replica count has not changed," + " but " + killSwitchDeadline + " has elapsed without a successful response." + " Terminating to ensure multiple pods don't process the same Couchbase partitions."));
        SharedIndexInformer<StatefulSet> informer = client.apps().statefulSets().inNamespace(namespace).withName(k8sName).inform(new ResourceEventHandler<>() {

            // When the K8S API server is reachable, an update happens once per resync period
            // and also immediately when the informer detects a change to the resource.
            @Override
            public void onUpdate(StatefulSet oldSet, StatefulSet newSet) {
                killSwitch.reset();
                int newReplicas = newSet.getSpec().getReplicas();
                if (newReplicas != 0 && newReplicas != initialReplicas) {
                    // Panic to terminate the connector and let Kubernetes restart it.
                    // This is simpler than trying to stream from different vbuckets on the fly.
                    panicButton.mildPanic("The connector is automatically restarting because" + " it detected a change to the number of replicas in its StatefulSet." + " This is the intended behavior, and not a cause for alarm." + " Upon restart, the connector will wait for a quiet period to elapse, giving all pods" + " a chance to notice the change and shut down. This prevents multiple pods" + " from processing the same Couchbase partitions." + " There is a [very small] chance this strategy could fail, in which case" + " stale versions of some documents could be written to Elasticsearch and remain" + " until the documents are modified again in Couchbase." + " To be absolutely sure this never happens, we recommend first scaling" + " the StatefulSet down to zero (or deleting it) and waiting for pods" + " to terminate before scaling back up to the desired number of replicas.");
                }
            }

            @Override
            public void onAdd(StatefulSet it) {
            }

            @Override
            public void onDelete(StatefulSet it, boolean deletedFinalStateUnknown) {
            }
        }, resyncPeriod.toMillis());
        return initialReplicas;
    } catch (Throwable t) {
        panicButton.panic("Failed to get/watch StatefulSet replica count.", t);
        // unreachable
        throw t;
    }
}
Also used : KillSwitch(com.couchbase.connector.cluster.KillSwitch) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet)

Aggregations

StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)316 Kafka (io.strimzi.api.kafka.model.Kafka)142 Test (org.junit.jupiter.api.Test)137 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)134 ParallelTest (io.strimzi.test.annotations.ParallelTest)102 Pod (io.fabric8.kubernetes.api.model.Pod)96 Reconciliation (io.strimzi.operator.common.Reconciliation)82 ArrayList (java.util.ArrayList)82 Map (java.util.Map)81 List (java.util.List)80 GenericKafkaListenerBuilder (io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder)76 StatefulSetBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)73 Checkpoint (io.vertx.junit5.Checkpoint)70 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)70 KafkaVersionTestUtils (io.strimzi.operator.cluster.KafkaVersionTestUtils)68 ResourceUtils (io.strimzi.operator.cluster.ResourceUtils)64 PasswordGenerator (io.strimzi.operator.common.PasswordGenerator)62 PodOperator (io.strimzi.operator.common.operator.resource.PodOperator)60 Future (io.vertx.core.Future)59 KafkaListenerType (io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType)58