Search in sources :

Example 6 with StatefulSetBuilder

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

the class StatefulSetDiffTest method testCpuResources.

public StatefulSetDiff testCpuResources(ResourceRequirements requirements1, ResourceRequirements requirements2) {
    StatefulSet ss1 = new StatefulSetBuilder().withNewMetadata().withNamespace("test").withName("foo").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withResources(requirements1).endContainer().endSpec().endTemplate().endSpec().build();
    StatefulSet ss2 = new StatefulSetBuilder().withNewMetadata().withNamespace("test").withName("foo").endMetadata().withNewSpec().withNewTemplate().withNewSpec().addNewContainer().withResources(requirements2).endContainer().endSpec().endTemplate().endSpec().build();
    return new StatefulSetDiff(Reconciliation.DUMMY_RECONCILIATION, ss1, ss2);
}
Also used : StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)

Example 7 with StatefulSetBuilder

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

the class StatefulSetDiffTest method testPvcSizeChangeIgnored.

@Test
public void testPvcSizeChangeIgnored() {
    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("100Gi"))).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("110Gi"))).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(true));
}
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 8 with StatefulSetBuilder

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

the class AbstractModel method createStatefulSet.

protected StatefulSet createStatefulSet(Map<String, String> stsAnnotations, Map<String, String> podAnnotations, List<Volume> volumes, List<PersistentVolumeClaim> volumeClaims, Affinity affinity, List<Container> initContainers, List<Container> containers, List<LocalObjectReference> imagePullSecrets, boolean isOpenShift) {
    PodSecurityContext securityContext = templateSecurityContext;
    // This is to give each pod write permissions under a specific group so that if a pod changes users it does not have permission issues.
    if (ModelUtils.containsPersistentStorage(storage) && !isOpenShift && securityContext == null) {
        securityContext = new PodSecurityContextBuilder().withFsGroup(AbstractModel.DEFAULT_FS_GROUPID).build();
    }
    StatefulSet statefulSet = new StatefulSetBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithStrimziName(name, templateStatefulSetLabels).toMap()).withNamespace(namespace).withAnnotations(Util.mergeLabelsOrAnnotations(stsAnnotations, templateStatefulSetAnnotations)).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withPodManagementPolicy(templatePodManagementPolicy.toValue()).withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withSelector(new LabelSelectorBuilder().withMatchLabels(getSelectorLabels().toMap()).build()).withServiceName(headlessServiceName).withReplicas(replicas).withNewTemplate().withNewMetadata().withName(name).withLabels(getLabelsWithStrimziName(name, templatePodLabels).toMap()).withAnnotations(Util.mergeLabelsOrAnnotations(podAnnotations, templatePodAnnotations)).endMetadata().withNewSpec().withServiceAccountName(getServiceAccountName()).withEnableServiceLinks(templatePodEnableServiceLinks).withAffinity(affinity).withInitContainers(initContainers).withContainers(containers).withVolumes(volumes).withTolerations(getTolerations()).withTerminationGracePeriodSeconds(Long.valueOf(templateTerminationGracePeriodSeconds)).withImagePullSecrets(templateImagePullSecrets != null ? templateImagePullSecrets : imagePullSecrets).withSecurityContext(securityContext).withPriorityClassName(templatePodPriorityClassName).withSchedulerName(templatePodSchedulerName != null ? templatePodSchedulerName : "default-scheduler").withHostAliases(templatePodHostAliases).withTopologySpreadConstraints(templatePodTopologySpreadConstraints).endSpec().endTemplate().withVolumeClaimTemplates(volumeClaims).endSpec().build();
    return statefulSet;
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) PodSecurityContextBuilder(io.fabric8.kubernetes.api.model.PodSecurityContextBuilder) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)

Example 9 with StatefulSetBuilder

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

the class AbstractModel method createStatefulSet.

protected StatefulSet createStatefulSet(Map<String, String> stsAnnotations, Map<String, String> podAnnotations, List<Volume> volumes, List<PersistentVolumeClaim> volumeClaims, Affinity affinity, List<Container> initContainers, List<Container> containers, List<LocalObjectReference> imagePullSecrets, boolean isOpenShift) {
    PodSecurityContext securityContext = templateSecurityContext;
    // This is to give each pod write permissions under a specific group so that if a pod changes users it does not have permission issues.
    if (ModelUtils.containsPersistentStorage(storage) && !isOpenShift && securityContext == null) {
        securityContext = new PodSecurityContextBuilder().withFsGroup(AbstractModel.DEFAULT_FS_GROUPID).build();
    }
    StatefulSet statefulSet = new StatefulSetBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithStrimziName(name, templateStatefulSetLabels).toMap()).withNamespace(namespace).withAnnotations(Util.mergeLabelsOrAnnotations(stsAnnotations, templateStatefulSetAnnotations)).withOwnerReferences(createOwnerReference()).endMetadata().withNewSpec().withPodManagementPolicy(templatePodManagementPolicy.toValue()).withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withSelector(new LabelSelectorBuilder().withMatchLabels(getSelectorLabels().toMap()).build()).withServiceName(headlessServiceName).withReplicas(replicas).withNewTemplate().withNewMetadata().withName(name).withLabels(getLabelsWithStrimziName(name, templatePodLabels).toMap()).withAnnotations(Util.mergeLabelsOrAnnotations(podAnnotations, templatePodAnnotations)).endMetadata().withNewSpec().withServiceAccountName(getServiceAccountName()).withEnableServiceLinks(templatePodEnableServiceLinks).withAffinity(affinity).withInitContainers(initContainers).withContainers(containers).withVolumes(volumes).withTolerations(getTolerations()).withTerminationGracePeriodSeconds(Long.valueOf(templateTerminationGracePeriodSeconds)).withImagePullSecrets(templateImagePullSecrets != null ? templateImagePullSecrets : imagePullSecrets).withSecurityContext(securityContext).withPriorityClassName(templatePodPriorityClassName).withSchedulerName(templatePodSchedulerName != null ? templatePodSchedulerName : "default-scheduler").withHostAliases(templatePodHostAliases).withTopologySpreadConstraints(templatePodTopologySpreadConstraints).endSpec().endTemplate().withVolumeClaimTemplates(volumeClaims).endSpec().build();
    return statefulSet;
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) PodSecurityContextBuilder(io.fabric8.kubernetes.api.model.PodSecurityContextBuilder) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetUpdateStrategyBuilder) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder)

Example 10 with StatefulSetBuilder

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

the class KafkaUpgradeDowngradeMockTest method testUpgradeRecoveryWithMessageAndProtocolVersions.

// Tests recovery from failed upgrade with the message format and protocol versions configured to the same Kafka
// version as we are upgrading from.
@Test
public void testUpgradeRecoveryWithMessageAndProtocolVersions(VertxTestContext context) {
    Kafka initialKafka = kafkaWithVersions(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Kafka updatedKafka = kafkaWithVersions(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION);
    Checkpoint reconciliation = context.checkpoint();
    initialize(context, initialKafka).onComplete(context.succeeding(v -> {
        context.verify(() -> {
            assertVersionsInStatefulSet(KafkaVersionTestUtils.PREVIOUS_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION, KafkaVersionTestUtils.PREVIOUS_KAFKA_IMAGE);
        });
    })).compose(v -> {
        StatefulSet sts = client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").get();
        StatefulSet modifiedSts = new StatefulSetBuilder(sts).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_VERSION).endMetadata().editSpec().editTemplate().editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_VERSION).addToAnnotations(StatefulSetOperator.ANNO_STRIMZI_IO_GENERATION, "1").endMetadata().editSpec().editContainer(0).withImage(KafkaVersionTestUtils.LATEST_KAFKA_IMAGE).endContainer().endSpec().endTemplate().endSpec().build();
        client.apps().statefulSets().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka").createOrReplace(modifiedSts);
        Pod pod = client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + 1).get();
        Pod modifiedPod = new PodBuilder(pod).editMetadata().addToAnnotations(KafkaCluster.ANNO_STRIMZI_IO_KAFKA_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_VERSION).addToAnnotations(StatefulSetOperator.ANNO_STRIMZI_IO_GENERATION, "1").endMetadata().editSpec().editContainer(0).withImage(KafkaVersionTestUtils.LATEST_KAFKA_IMAGE).endContainer().endSpec().build();
        client.pods().inNamespace(NAMESPACE).withName(CLUSTER_NAME + "-kafka-" + 1).createOrReplace(modifiedPod);
        return Future.succeededFuture();
    }).compose(v -> operator.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, CLUSTER_NAME), updatedKafka)).onComplete(context.succeeding(v -> context.verify(() -> {
        assertVersionsInStatefulSet(KafkaVersionTestUtils.LATEST_KAFKA_VERSION, KafkaVersionTestUtils.PREVIOUS_FORMAT_VERSION, KafkaVersionTestUtils.PREVIOUS_PROTOCOL_VERSION, KafkaVersionTestUtils.LATEST_KAFKA_IMAGE);
        reconciliation.flag();
    })));
}
Also used : StatefulSetBuilder(io.fabric8.kubernetes.api.model.apps.StatefulSetBuilder) VertxTestContext(io.vertx.junit5.VertxTestContext) CoreMatchers.is(org.hamcrest.CoreMatchers.is) KafkaList(io.strimzi.api.kafka.KafkaList) Crds(io.strimzi.api.kafka.Crds) KafkaBuilder(io.strimzi.api.kafka.model.KafkaBuilder) FeatureGates(io.strimzi.operator.cluster.FeatureGates) MockKube(io.strimzi.test.mockkube.MockKube) AfterAll(org.junit.jupiter.api.AfterAll) KafkaCluster(io.strimzi.operator.cluster.model.KafkaCluster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KafkaVersionTestUtils(io.strimzi.operator.cluster.KafkaVersionTestUtils) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) ResourceOperatorSupplier(io.strimzi.operator.cluster.operator.resource.ResourceOperatorSupplier) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) ResourceUtils(io.strimzi.operator.cluster.ResourceUtils) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) StatefulSetOperator(io.strimzi.operator.cluster.operator.resource.StatefulSetOperator) StrimziPodSetList(io.strimzi.api.kafka.StrimziPodSetList) KubernetesVersion(io.strimzi.operator.KubernetesVersion) KafkaVersion(io.strimzi.operator.cluster.model.KafkaVersion) Vertx(io.vertx.core.Vertx) Pod(io.fabric8.kubernetes.api.model.Pod) GenericKafkaListenerBuilder(io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder) VertxExtension(io.vertx.junit5.VertxExtension) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) Reconciliation(io.strimzi.operator.common.Reconciliation) KafkaConfiguration(io.strimzi.operator.cluster.model.KafkaConfiguration) Logger(org.apache.logging.log4j.Logger) StrimziPodSet(io.strimzi.api.kafka.model.StrimziPodSet) KafkaListenerType(io.strimzi.api.kafka.model.listener.arraylistener.KafkaListenerType) PasswordGenerator(io.strimzi.operator.common.PasswordGenerator) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Checkpoint(io.vertx.junit5.Checkpoint) Matchers.stringContainsInOrder(org.hamcrest.Matchers.stringContainsInOrder) Kafka(io.strimzi.api.kafka.model.Kafka) PlatformFeaturesAvailability(io.strimzi.operator.PlatformFeaturesAvailability) LogManager(org.apache.logging.log4j.LogManager) Collections(java.util.Collections) ClusterOperatorConfig(io.strimzi.operator.cluster.ClusterOperatorConfig) MockCertManager(io.strimzi.operator.common.operator.MockCertManager) Checkpoint(io.vertx.junit5.Checkpoint) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Reconciliation(io.strimzi.operator.common.Reconciliation) Kafka(io.strimzi.api.kafka.model.Kafka) 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