Search in sources :

Example 26 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project fabric8-maven-plugin by fabric8io.

the class ImageEnricherTest method checkEnrichStatefulSet.

@Test
public void checkEnrichStatefulSet() throws Exception {
    KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new StatefulSetBuilder().build());
    imageEnricher.create(PlatformMode.kubernetes, builder);
    assertCorrectlyGeneratedResources(builder.build(), "StatefulSet");
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Test(org.junit.Test)

Example 27 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project fabric8-maven-plugin by fabric8io.

the class ControllerViaPluginConfigurationEnricher method create.

@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
    final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId()));
    ResourceConfig xmlResourceConfig = getConfiguration().getResource().orElse(null);
    final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getImagePullPolicy(xmlResourceConfig, getConfig(Config.pullPolicy))).withReplicas(getReplicaCount(builder, xmlResourceConfig, Configs.asInt(getConfig(Config.replicaCount)))).build();
    final List<ImageConfiguration> images = getImages().orElse(Collections.emptyList());
    // Check if at least a replica set is added. If not add a default one
    if (KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
        // At least one image must be present, otherwise the resulting config will be invalid
        if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
            final StatefulSetSpec spec = statefulSetHandler.getStatefulSet(config, images).getSpec();
            if (spec != null) {
                builder.accept(new TypedVisitor<StatefulSetBuilder>() {

                    @Override
                    public void visit(StatefulSetBuilder statefulSetBuilder) {
                        statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                        mergeStatefulSetSpec(statefulSetBuilder, spec);
                    }
                });
                if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                    final PodSpec podSpec = spec.getTemplate().getSpec();
                    builder.accept(new TypedVisitor<PodSpecBuilder>() {

                        @Override
                        public void visit(PodSpecBuilder builder) {
                            String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getValueFromConfig(SIDECAR, false));
                            if (defaultApplicationContainerName != null) {
                                setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
                            }
                        }
                    });
                }
            }
        } else {
            final DeploymentSpec spec = deployHandler.getDeployment(config, images).getSpec();
            if (spec != null) {
                builder.accept(new TypedVisitor<DeploymentBuilder>() {

                    @Override
                    public void visit(DeploymentBuilder deploymentBuilder) {
                        deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                        mergeDeploymentSpec(deploymentBuilder, spec);
                    }
                });
                if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                    final PodSpec podSpec = spec.getTemplate().getSpec();
                    builder.accept(new TypedVisitor<PodSpecBuilder>() {

                        @Override
                        public void visit(PodSpecBuilder builder) {
                            String defaultApplicationContainerName = KubernetesResourceUtil.mergePodSpec(builder, podSpec, name, getValueFromConfig(SIDECAR, false));
                            if (defaultApplicationContainerName != null) {
                                setProcessingInstruction(NEED_IMAGECHANGE_TRIGGERS, Collections.singletonList(defaultApplicationContainerName));
                            }
                        }
                    });
                }
            }
        }
    }
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.apps.StatefulSetSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 28 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class StatefulSetDiffTest method testPvcSizeUnitChangeIgnored.

@Test
public void testPvcSizeUnitChangeIgnored() {
    StatefulSet ss1 = new StatefulSetBuilder().withNewMetadata().withNamespace("test").withName("foo").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addToVolumes(0, new VolumeBuilder().withConfigMap(new ConfigMapVolumeSourceBuilder().withDefaultMode(1).build()).build()).endSpec().endTemplate().withVolumeClaimTemplates(new PersistentVolumeClaimBuilder().withNewSpec().withNewResources().withRequests(singletonMap("storage", new Quantity("3072Gi"))).endResources().endSpec().build()).endSpec().build();
    StatefulSet ss2 = new StatefulSetBuilder().withNewMetadata().withNamespace("test").withName("foo").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addToVolumes(0, new VolumeBuilder().withConfigMap(new ConfigMapVolumeSourceBuilder().withDefaultMode(2).build()).build()).endSpec().endTemplate().withVolumeClaimTemplates(new PersistentVolumeClaimBuilder().withNewSpec().withNewResources().withRequests(singletonMap("storage", new Quantity("3Ti"))).endResources().endSpec().build()).endSpec().build();
    assertThat(new StatefulSetDiff(Reconciliation.DUMMY_RECONCILIATION, ss1, ss2).changesVolumeClaimTemplates(), is(false));
    assertThat(new StatefulSetDiff(Reconciliation.DUMMY_RECONCILIATION, ss1, ss2).changesVolumeSize(), is(false));
}
Also used : ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) Quantity(io.fabric8.kubernetes.api.model.Quantity) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Test(org.junit.jupiter.api.Test)

Example 29 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class MockKubeRegressionTest method testStatefulSetCreationAndDeletion.

@Test
public void testStatefulSetCreationAndDeletion() {
    StatefulSet sts = new StatefulSetBuilder().withNewMetadata().withName("foo").withNamespace("ns").endMetadata().withNewSpec().withReplicas(3).withNewTemplate().withNewMetadata().endMetadata().withNewSpec().endSpec().endTemplate().endSpec().build();
    client.apps().statefulSets().inNamespace("ns").withName("foo").create(sts);
    List<Pod> ns = client.pods().inNamespace("ns").list().getItems();
    assertThat(ns, hasSize(3));
    AtomicBoolean deleted = new AtomicBoolean(false);
    AtomicBoolean recreated = new AtomicBoolean(false);
    Watch watch = client.pods().inNamespace("ns").withName(ns.get(0).getMetadata().getName()).watch(new Watcher<Pod>() {

        @Override
        public void eventReceived(Action action, Pod resource) {
            if (action == Action.DELETED) {
                if (deleted.getAndSet(true)) {
                    fail("Deleted twice");
                }
            } else if (action == Action.ADDED) {
                if (!deleted.get()) {
                    fail("Created before deleted");
                }
                if (recreated.getAndSet(true)) {
                    fail("Recreated twice");
                }
            }
        }

        @Override
        public void onClose(WatcherException cause) {
        }
    });
    client.pods().inNamespace("ns").withName(ns.get(0).getMetadata().getName()).withPropagationPolicy(DeletionPropagation.FOREGROUND).delete();
    assertThat(deleted.get(), is(true));
    assertThat(recreated.get(), is(true));
    watch.close();
    ns = client.pods().inNamespace("ns").list().getItems();
    assertThat(ns, hasSize(3));
    client.apps().statefulSets().inNamespace("ns").withName("foo").withPropagationPolicy(DeletionPropagation.FOREGROUND).delete();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) WatcherException(io.fabric8.kubernetes.client.WatcherException) Test(org.junit.jupiter.api.Test)

Example 30 with StatefulSetBuilder

use of io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder in project strimzi by strimzi.

the class TolerationsIT method testEmptyStringValueIntoleration.

@Test
public void testEmptyStringValueIntoleration(VertxTestContext context) {
    Toleration t1 = new TolerationBuilder().withEffect("NoSchedule").withValue("").build();
    List<Toleration> tolerationList = new ArrayList<>();
    tolerationList.add(t1);
    // CO does this over the generated STS
    tolerationList = ModelUtils.removeEmptyValuesFromTolerations(tolerationList);
    StatefulSet ss = new StatefulSetBuilder().withNewMetadata().withNamespace(namespace).withName("foo").endMetadata().withNewSpec().withSelector(new LabelSelectorBuilder().withMatchLabels(Collections.singletonMap("app", "test")).build()).withNewTemplate().withNewMetadata().withLabels(Collections.singletonMap("app", "test")).endMetadata().withNewSpec().withTolerations(tolerationList).withDnsPolicy("ClusterFirst").withRestartPolicy("Always").withSchedulerName("default-scheduler").withSecurityContext(null).withTerminationGracePeriodSeconds(30L).endSpec().endTemplate().endSpec().build();
    KubernetesClient client = new DefaultKubernetesClient();
    client.apps().statefulSets().inNamespace(namespace).create(ss);
    StatefulSet stsk8s = client.apps().statefulSets().inNamespace(namespace).withName("foo").get();
    StatefulSetDiff diff = new StatefulSetDiff(Reconciliation.DUMMY_RECONCILIATION, ss, stsk8s);
    Checkpoint checkpoint = context.checkpoint();
    context.verify(() -> {
        assertThat(diff.changesSpecTemplate(), is(false));
        assertThat(stsk8s.getSpec().getTemplate().getSpec().getTolerations().get(0).getValue(), is(nullValue()));
        assertThat(ss.getSpec().getTemplate().getSpec().getTolerations().get(0).getValue(), is(nullValue()));
        checkpoint.flag();
    });
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) TolerationBuilder(io.fabric8.kubernetes.api.model.TolerationBuilder) StatefulSetDiff(io.strimzi.operator.cluster.operator.resource.StatefulSetDiff) Toleration(io.fabric8.kubernetes.api.model.Toleration) ArrayList(java.util.ArrayList) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

StatefulSetBuilder (io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)48 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)43 Test (org.junit.jupiter.api.Test)34 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)18 Checkpoint (io.vertx.junit5.Checkpoint)18 Pod (io.fabric8.kubernetes.api.model.Pod)16 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)16 Reconciliation (io.strimzi.operator.common.Reconciliation)16 Future (io.vertx.core.Future)16 Vertx (io.vertx.core.Vertx)16 VertxTestContext (io.vertx.junit5.VertxTestContext)16 Map (java.util.Map)16 CoreMatchers.is (org.hamcrest.CoreMatchers.is)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 StrimziPodSetList (io.strimzi.api.kafka.StrimziPodSetList)14 Kafka (io.strimzi.api.kafka.model.Kafka)14 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)14 StrimziPodSet (io.strimzi.api.kafka.model.StrimziPodSet)14 GenericKafkaListenerBuilder (io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder)14 KafkaListenerType (io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType)14