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);
}
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;
}
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);
}
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;
}
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");
}
Aggregations