Search in sources :

Example 16 with StatefulSet

use of io.fabric8.kubernetes.api.model.extensions.StatefulSet in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method testReconcile.

@Test
public void testReconcile(TestContext context) {
    Async async = context.async(3);
    // create CM, Service, headless service, statefulset
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    ZookeeperSetOperator mockZsOps = mock(ZookeeperSetOperator.class);
    KafkaSetOperator mockKsOps = mock(KafkaSetOperator.class);
    PvcOperator mockPvcOps = mock(PvcOperator.class);
    DeploymentOperator mockDepOps = mock(DeploymentOperator.class);
    String clusterCmNamespace = "myNamespace";
    ConfigMap foo = getConfigMap("foo");
    ConfigMap bar = getConfigMap("bar");
    ConfigMap baz = getConfigMap("baz");
    when(mockCmOps.list(eq(clusterCmNamespace), any())).thenReturn(asList(foo, bar));
    // when requested ConfigMap for a specific Kafka cluster
    when(mockCmOps.get(eq(clusterCmNamespace), eq("foo"))).thenReturn(foo);
    when(mockCmOps.get(eq(clusterCmNamespace), eq("bar"))).thenReturn(bar);
    // providing the list of ALL StatefulSets for all the Kafka clusters
    Labels newLabels = Labels.forType(AssemblyType.KAFKA);
    when(mockKsOps.list(eq(clusterCmNamespace), eq(newLabels))).thenReturn(asList(KafkaCluster.fromConfigMap(bar).generateStatefulSet(openShift), KafkaCluster.fromConfigMap(baz).generateStatefulSet(openShift)));
    // providing the list StatefulSets for already "existing" Kafka clusters
    Labels barLabels = Labels.forCluster("bar");
    when(mockKsOps.list(eq(clusterCmNamespace), eq(barLabels))).thenReturn(asList(KafkaCluster.fromConfigMap(bar).generateStatefulSet(openShift)));
    Labels bazLabels = Labels.forCluster("baz");
    when(mockKsOps.list(eq(clusterCmNamespace), eq(bazLabels))).thenReturn(asList(KafkaCluster.fromConfigMap(baz).generateStatefulSet(openShift)));
    Set<String> createdOrUpdated = new HashSet<>();
    Set<String> deleted = new HashSet<>();
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps) {

        @Override
        public void createOrUpdate(Reconciliation reconciliation, ConfigMap assemblyCm, Handler h) {
            createdOrUpdated.add(assemblyCm.getMetadata().getName());
            async.countDown();
            h.handle(Future.succeededFuture());
        }

        @Override
        public void delete(Reconciliation reconciliation, Handler h) {
            deleted.add(reconciliation.assemblyName());
            async.countDown();
            h.handle(Future.succeededFuture());
        }
    };
    // Now try to reconcile all the Kafka clusters
    ops.reconcileAll("test", clusterCmNamespace, Labels.EMPTY);
    async.await();
    context.assertEquals(new HashSet(asList("foo", "bar")), createdOrUpdated);
    context.assertEquals(singleton("baz"), deleted);
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ZookeeperSetOperator(io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator) Handler(io.vertx.core.Handler) Labels(io.strimzi.controller.cluster.model.Labels) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.controller.cluster.operator.resource.PvcOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Async(io.vertx.ext.unit.Async) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with StatefulSet

use of io.fabric8.kubernetes.api.model.extensions.StatefulSet in project strimzi by strimzi.

the class AbstractModel method createStatefulSet.

protected StatefulSet createStatefulSet(List<ContainerPort> ports, List<Volume> volumes, List<PersistentVolumeClaim> volumeClaims, List<VolumeMount> volumeMounts, Probe livenessProbe, Probe readinessProbe, boolean isOpenShift) {
    Map<String, String> annotations = new HashMap<>();
    annotations.put(String.format("%s/%s", ClusterController.STRIMZI_CLUSTER_CONTROLLER_DOMAIN, Storage.DELETE_CLAIM_FIELD), String.valueOf(storage.isDeleteClaim()));
    Container container = new ContainerBuilder().withName(name).withImage(getImage()).withEnv(getEnvVars()).withVolumeMounts(volumeMounts).withPorts(ports).withLivenessProbe(livenessProbe).withReadinessProbe(readinessProbe).build();
    List<Container> initContainers = new ArrayList<>();
    PodSecurityContext securityContext = null;
    // there is an hack on volume mounting which needs an "init-container"
    if ((this.storage.type() == Storage.StorageType.PERSISTENT_CLAIM) && !isOpenShift) {
        String chown = String.format("chown -R %d:%d %s", AbstractModel.VOLUME_MOUNT_HACK_GROUPID, AbstractModel.VOLUME_MOUNT_HACK_GROUPID, volumeMounts.get(0).getMountPath());
        Container initContainer = new ContainerBuilder().withName(AbstractModel.VOLUME_MOUNT_HACK_NAME).withImage(AbstractModel.VOLUME_MOUNT_HACK_IMAGE).withVolumeMounts(volumeMounts.get(0)).withCommand("sh", "-c", chown).build();
        initContainers.add(initContainer);
        securityContext = new PodSecurityContextBuilder().withFsGroup(AbstractModel.VOLUME_MOUNT_HACK_GROUPID).build();
    }
    StatefulSet statefulSet = new StatefulSetBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).withAnnotations(annotations).endMetadata().withNewSpec().withPodManagementPolicy("Parallel").withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withSelector(new LabelSelectorBuilder().withMatchLabels(getLabelsWithName()).build()).withServiceName(headlessName).withReplicas(replicas).withNewTemplate().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withAnnotations(getPrometheusAnnotations()).endMetadata().withNewSpec().withSecurityContext(securityContext).withInitContainers(initContainers).withContainers(container).withVolumes(volumes).endSpec().endTemplate().withVolumeClaimTemplates(volumeClaims).endSpec().build();
    return statefulSet;
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) HashMap(java.util.HashMap) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetUpdateStrategyBuilder) ArrayList(java.util.ArrayList) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodSecurityContextBuilder(io.fabric8.kubernetes.api.model.PodSecurityContextBuilder) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 18 with StatefulSet

use of io.fabric8.kubernetes.api.model.extensions.StatefulSet in project strimzi by strimzi.

the class KafkaAssemblyOperator method deleteKafka.

private final Future<CompositeFuture> deleteKafka(Reconciliation reconciliation) {
    String namespace = reconciliation.namespace();
    String name = reconciliation.assemblyName();
    log.info("{}: delete kafka {}", reconciliation, name);
    StatefulSet ss = kafkaSetOperations.get(namespace, KafkaCluster.kafkaClusterName(name));
    final KafkaCluster kafka = ss == null ? null : KafkaCluster.fromAssembly(ss, namespace, name);
    boolean deleteClaims = kafka != null && kafka.getStorage().type() == Storage.StorageType.PERSISTENT_CLAIM && kafka.getStorage().isDeleteClaim();
    List<Future> result = new ArrayList<>(4 + (deleteClaims ? kafka.getReplicas() : 0));
    result.add(configMapOperations.reconcile(namespace, KafkaCluster.metricConfigsName(name), null));
    result.add(serviceOperations.reconcile(namespace, KafkaCluster.kafkaClusterName(name), null));
    result.add(serviceOperations.reconcile(namespace, KafkaCluster.headlessName(name), null));
    result.add(kafkaSetOperations.reconcile(namespace, KafkaCluster.kafkaClusterName(name), null));
    if (deleteClaims) {
        for (int i = 0; i < kafka.getReplicas(); i++) {
            result.add(pvcOperations.reconcile(namespace, kafka.getPersistentVolumeClaimName(i), null));
        }
    }
    return CompositeFuture.join(result);
}
Also used : KafkaCluster(io.strimzi.controller.cluster.model.KafkaCluster) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 19 with StatefulSet

use of io.fabric8.kubernetes.api.model.extensions.StatefulSet in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method getPodLabelSelector.

public static LabelSelector getPodLabelSelector(HasMetadata entity) {
    LabelSelector selector = null;
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof DaemonSet) {
        DaemonSet resource = (DaemonSet) entity;
        DaemonSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof StatefulSet) {
        StatefulSet resource = (StatefulSet) entity;
        StatefulSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof Job) {
        Job resource = (Job) entity;
        JobSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    }
    return selector;
}
Also used : StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DaemonSetSpec(io.fabric8.kubernetes.api.model.extensions.DaemonSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) JobSpec(io.fabric8.kubernetes.api.model.JobSpec) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Job(io.fabric8.kubernetes.api.model.Job) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 20 with StatefulSet

use of io.fabric8.kubernetes.api.model.extensions.StatefulSet in project fabric8-maven-plugin by fabric8io.

the class ImageEnricherTest method checkEnrichStatefulSet.

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

Aggregations

StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)15 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)8 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)8 Service (io.fabric8.kubernetes.api.model.Service)6 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)5 Reconciliation (io.strimzi.controller.cluster.Reconciliation)5 KafkaCluster (io.strimzi.controller.cluster.model.KafkaCluster)5 ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)5 DeploymentOperator (io.strimzi.controller.cluster.operator.resource.DeploymentOperator)5 KafkaSetOperator (io.strimzi.controller.cluster.operator.resource.KafkaSetOperator)5 PvcOperator (io.strimzi.controller.cluster.operator.resource.PvcOperator)5 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)5 ZookeeperSetOperator (io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator)5 ArrayList (java.util.ArrayList)5 Pod (io.fabric8.kubernetes.api.model.Pod)4 TopicController (io.strimzi.controller.cluster.model.TopicController)4 ZookeeperCluster (io.strimzi.controller.cluster.model.ZookeeperCluster)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)3 DaemonSet (io.fabric8.kubernetes.api.model.extensions.DaemonSet)3