io.fabric8.kubernetes.api.model.apps.StatefulSet Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.apps.StatefulSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaAssemblyOperatorCustomCertTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testPodToRestartFalseWhenCustomCertAnnotationsHaveMatchingThumbprints(VertxTestContext context) {
    Checkpoint async = context.checkpoint();
    operator.createOrUpdate(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName), kafka)
        .onComplete(context.succeeding(v -> context.verify(() -> {
            assertThat(reconcileStsCaptor.getAllValues(), hasSize(1));

            StatefulSet reconcileSts = reconcileStsCaptor.getValue();
            assertThat(reconcileSts.getSpec().getTemplate().getMetadata().getAnnotations(),
                    hasEntry(KafkaCluster.ANNO_STRIMZI_CUSTOM_CERT_THUMBPRINT_TLS_LISTENER, getTlsThumbprint()));
            assertThat(reconcileSts.getSpec().getTemplate().getMetadata().getAnnotations(),
                    hasEntry(KafkaCluster.ANNO_STRIMZI_CUSTOM_CERT_THUMBPRINT_EXTERNAL_LISTENER, getExternalThumbprint()));

            assertThat(isPodToRestartFunctionCaptor.getAllValues(), hasSize(1));
            Function<Pod, String> isPodToRestart = isPodToRestartFunctionCaptor.getValue();
            assertThat(isPodToRestart.apply(getPod(reconcileSts)), is(nullValue()));

            async.flag();
        })));
}
 
Example #2
Source File: StatefulSetOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Override
protected StatefulSet resource() {
    return new StatefulSetBuilder()
            .withNewMetadata()
                .withNamespace(AbstractResourceOperatorTest.NAMESPACE)
                .withName(AbstractResourceOperatorTest.RESOURCE_NAME)
            .endMetadata()
            .withNewSpec()
                .withReplicas(3)
                .withNewTemplate()
                    .withNewMetadata()
                    .endMetadata()
                .endTemplate()
            .endSpec()
            .build();
}
 
Example #3
Source File: StatefulSetTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("Should restart rollout")
void testRolloutRestart() throws InterruptedException {
  // Given
  server.expect().get().withPath("/apis/apps/v1/namespaces/ns1/statefulsets/statefulset1")
    .andReturn(HttpURLConnection.HTTP_OK, getStatefulSetBuilder().build()).times(3);
  server.expect().patch().withPath("/apis/apps/v1/namespaces/ns1/statefulsets/statefulset1")
    .andReturn(HttpURLConnection.HTTP_OK, getStatefulSetBuilder().build()).once();
  KubernetesClient client = server.getClient();

  // When
  StatefulSet deployment = client.apps().statefulSets().inNamespace("ns1").withName("statefulset1")
    .rolling().restart();

  // Then
  RecordedRequest recordedRequest = server.getLastRequest();
  assertNotNull(deployment);
  assertEquals("PATCH", recordedRequest.getMethod());
  assertTrue(recordedRequest.getBody().readUtf8().contains("kubectl.kubernetes.io/restartedAt"));
}
 
Example #4
Source File: ZookeeperClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullSecretsFromBoth() {
    LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret");
    LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret");

    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCmJson, configurationJson, emptyMap()))
            .editSpec()
                .editZookeeper()
                    .withNewTemplate()
                        .withNewPod()
                            .withImagePullSecrets(secret2)
                        .endPod()
                    .endTemplate()
                .endZookeeper()
            .endSpec()
            .build();
    ZookeeperCluster zc = ZookeeperCluster.fromCrd(kafkaAssembly, VERSIONS);

    StatefulSet sts = zc.generateStatefulSet(true, null, singletonList(secret1));
    assertThat(sts.getSpec().getTemplate().getSpec().getImagePullSecrets().size(), is(1));
    assertThat(sts.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret1), is(false));
    assertThat(sts.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret2), is(true));
}
 
Example #5
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testExternalNodePortsWithAddressType() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
                .editKafka()
                    .withNewListeners()
                        .withNewKafkaListenerExternalNodePort()
                            .withNewConfiguration()
                                .withPreferredAddressType(NodeAddressType.INTERNAL_DNS)
                            .endConfiguration()
                        .endKafkaListenerExternalNodePort()
                    .endListeners()
                .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check StatefulSet changes
    StatefulSet sts = kc.generateStatefulSet(true, null, null);
    Container initCont = sts.getSpec().getTemplate().getSpec().getInitContainers().get(0);

    assertThat(initCont.getEnv().stream().filter(env -> KafkaCluster.ENV_VAR_KAFKA_INIT_EXTERNAL_ADDRESS_TYPE.equals(env.getName()))
                    .map(EnvVar::getValue).findFirst().orElse("").equals(NodeAddressType.INTERNAL_DNS.toValue()), is(true));
}
 
Example #6
Source File: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a StatefulSet according to configured defaults
 *
 * @param isOpenShift      True iff this operator is operating within OpenShift.
 * @param imagePullPolicy  The image pull policy.
 * @param imagePullSecrets The image pull secrets.
 * @return The generated StatefulSet.
 */
public StatefulSet generateStatefulSet(boolean isOpenShift,
                                       ImagePullPolicy imagePullPolicy,
                                       List<LocalObjectReference> imagePullSecrets) {
    Map<String, String> stsAnnotations = new HashMap<>(2);
    stsAnnotations.put(ANNO_STRIMZI_IO_KAFKA_VERSION, kafkaVersion.version());
    stsAnnotations.put(ANNO_STRIMZI_IO_STORAGE, ModelUtils.encodeStorageToJson(storage));

    return createStatefulSet(
            stsAnnotations,
            emptyMap(),
            getVolumes(isOpenShift),
            getVolumeClaims(),
            getMergedAffinity(),
            getInitContainers(imagePullPolicy),
            getContainers(imagePullPolicy),
            imagePullSecrets,
            isOpenShift);
}
 
Example #7
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testImagePullSecrets() {
    LocalObjectReference secret1 = new LocalObjectReference("some-pull-secret");
    LocalObjectReference secret2 = new LocalObjectReference("some-other-pull-secret");

    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
                .editKafka()
                    .withNewTemplate()
                        .withNewPod()
                            .withImagePullSecrets(secret1, secret2)
                        .endPod()
                    .endTemplate()
                .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    StatefulSet sts = kc.generateStatefulSet(true, null, null);
    assertThat(sts.getSpec().getTemplate().getSpec().getImagePullSecrets().size(), is(2));
    assertThat(sts.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret1), is(true));
    assertThat(sts.getSpec().getTemplate().getSpec().getImagePullSecrets().contains(secret2), is(true));
}
 
Example #8
Source File: KafkaRollerTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testControllerNotInitiallyRollable(VertxTestContext testContext) {
    PodOperator podOps = mockPodOps(podId -> succeededFuture());
    StatefulSet sts = buildStatefulSet();
    AtomicInteger count = new AtomicInteger(2);
    TestingKafkaRoller kafkaRoller = new TestingKafkaRoller(sts, null, null, podOps,
        null, null, null,
        brokerId -> {
            if (brokerId == 2) {
                boolean b = count.getAndDecrement() == 0;
                log.info("Can broker {} be rolled now ? {}", brokerId, b);
                return succeededFuture(b);
            } else {
                return succeededFuture(true);
            }
        },
        2);
    doSuccessfulRollingRestart(testContext, kafkaRoller,
            asList(0, 1, 2, 3, 4),
            asList(0, 1, 3, 4, 2));
}
 
Example #9
Source File: Readiness.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
public static boolean isReady(HasMetadata item) {
  if (item instanceof Deployment) {
    return isDeploymentReady((Deployment) item);
  } else if (item instanceof ReplicaSet) {
    return isReplicaSetReady((ReplicaSet) item);
  } else if (item instanceof Pod) {
    return isPodReady((Pod) item);
  } else if (item instanceof DeploymentConfig) {
    return isDeploymentConfigReady((DeploymentConfig) item);
  } else if (item instanceof ReplicationController) {
    return isReplicationControllerReady((ReplicationController) item);
  } else if (item instanceof Endpoints) {
    return isEndpointsReady((Endpoints) item);
  } else if (item instanceof Node) {
    return isNodeReady((Node) item);
  } else if (item instanceof StatefulSet) {
    return isStatefulSetReady((StatefulSet) item);
  } else {
    throw new IllegalArgumentException("Item needs to be one of [Node, Deployment, ReplicaSet, StatefulSet, Pod, DeploymentConfig, ReplicationController], but was: [" + (item != null ? item.getKind() : "Unknown (null)") + "]");
  }
}
 
Example #10
Source File: StatefulSetRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(StatefulSet obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #11
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * If the STS exists, complete any pending rolls
 *
 * @return A Future which completes with the current state of the STS, or with null if the STS never existed.
 */
public Future<Void> waitForQuiescence(StatefulSet sts) {
    if (sts != null) {
        return kafkaSetOperations.maybeRollingUpdate(sts,
            pod -> {
                boolean notUpToDate = !isPodUpToDate(sts, pod);
                String reason = null;
                if (notUpToDate) {
                    log.debug("Rolling pod {} prior to upgrade", pod.getMetadata().getName());
                    reason = "upgrade quiescence";
                }
                return reason;
            });
    } else {
        return Future.succeededFuture();
    }
}
 
Example #12
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
Future<ReconciliationState> kafkaManualRollingUpdate() {
    Future<StatefulSet> futsts = kafkaSetOperations.getAsync(namespace, KafkaCluster.kafkaClusterName(name));
    if (futsts != null) {
        return futsts.compose(sts -> {
            if (sts != null) {
                if (Annotations.booleanAnnotation(sts, Annotations.ANNO_STRIMZI_IO_MANUAL_ROLLING_UPDATE,
                        false, Annotations.ANNO_OP_STRIMZI_IO_MANUAL_ROLLING_UPDATE)) {
                    return kafkaSetOperations.maybeRollingUpdate(sts, pod -> {
                        if (pod == null) {
                            throw new ConcurrentDeletionException("Unexpectedly pod no longer exists during roll of StatefulSet.");
                        }
                        log.debug("{}: Rolling Kafka pod {} due to manual rolling update",
                                reconciliation, pod.getMetadata().getName());
                        return "manual rolling update";
                    });
                }
            }
            return Future.succeededFuture();
        }).map(i -> this);
    }
    return Future.succeededFuture(this);
}
 
Example #13
Source File: ZookeeperClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGenerateStatefulSetWithPodManagementPolicy() {
    Kafka editZooAssembly = new KafkaBuilder(ka)
                    .editSpec()
                        .editZookeeper()
                            .withNewTemplate()
                                .withNewStatefulset()
                                    .withPodManagementPolicy(PodManagementPolicy.ORDERED_READY)
                                .endStatefulset()
                            .endTemplate()
                        .endZookeeper()
                    .endSpec().build();
    ZookeeperCluster zc = ZookeeperCluster.fromCrd(editZooAssembly, VERSIONS);
    StatefulSet sts = zc.generateStatefulSet(false, null, null);
    assertThat(sts.getSpec().getPodManagementPolicy(), is(PodManagementPolicy.ORDERED_READY.toValue()));
}
 
Example #14
Source File: StatefulSetOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Asynchronously perform a rolling update of all the pods in the StatefulSet identified by the given
 * {@code namespace} and {@code name}, returning a Future that will complete when the rolling update
 * is complete. Starting with pod 0, each pod will be deleted and re-created automatically by the ReplicaSet,
 * once the pod has been recreated then given {@code isReady} function will be polled until it returns true,
 * before the process proceeds with the pod with the next higher number.
 * @param sts The StatefulSet
 * @param podNeedsRestart Predicate for deciding whether the pod needs to be restarted.
 * @return A future that completes when any necessary rolling has been completed.
 */
public Future<Void> maybeRollingUpdate(StatefulSet sts, Function<Pod, String> podNeedsRestart) {
    String cluster = sts.getMetadata().getLabels().get(Labels.STRIMZI_CLUSTER_LABEL);
    String namespace = sts.getMetadata().getNamespace();
    Future<Secret> clusterCaKeySecretFuture = secretOperations.getAsync(
            namespace, KafkaResources.clusterCaCertificateSecretName(cluster));
    Future<Secret> coKeySecretFuture = secretOperations.getAsync(
            namespace, ClusterOperator.secretName(cluster));
    return CompositeFuture.join(clusterCaKeySecretFuture, coKeySecretFuture).compose(compositeFuture -> {
        Secret clusterCaKeySecret = compositeFuture.resultAt(0);
        if (clusterCaKeySecret == null) {
            return Future.failedFuture(Util.missingSecretException(namespace, KafkaCluster.clusterCaKeySecretName(cluster)));
        }
        Secret coKeySecret = compositeFuture.resultAt(1);
        if (coKeySecret == null) {
            return Future.failedFuture(Util.missingSecretException(namespace, ClusterOperator.secretName(cluster)));
        }
        return maybeRollingUpdate(sts, podNeedsRestart, clusterCaKeySecret, coKeySecret);
    });
}
 
Example #15
Source File: ZookeeperClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultGracePeriod() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCmJson, configurationJson, emptyMap()))
            .build();
    ZookeeperCluster zc = ZookeeperCluster.fromCrd(kafkaAssembly, VERSIONS);

    StatefulSet sts = zc.generateStatefulSet(true, null, null);
    assertThat(sts.getSpec().getTemplate().getSpec().getTerminationGracePeriodSeconds(), is(Long.valueOf(30)));
}
 
Example #16
Source File: StatefulSetTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeleteLoadedResource() {
  StatefulSet response = server.getClient().apps().statefulSets().load(getClass().getResourceAsStream("/test-statefulset.yml")).get();
  server.expect().delete().withPath("/apis/apps/v1beta1/namespaces/test/statefulsets/example").andReturn(200, response).once();

  KubernetesClient client = server.getClient();

  Deletable<Boolean> items = client.load(getClass().getResourceAsStream("/test-statefulset.yml"));
  assertTrue(items.delete());
}
 
Example #17
Source File: StatefulSetDiffTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testSpecVolumesIgnored() {
    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()
        .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()
        .endSpec()
        .build();
    assertThat(new StatefulSetDiff(ss1, ss2).changesSpecTemplate(), is(false));
}
 
Example #18
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateStatefulSetWithSetStorageSelector() {
    Map<String, String> selector = TestUtils.map("foo", "bar");
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
            .editKafka()
            .withNewPersistentClaimStorage().withSelector(selector).withSize("100Gi").endPersistentClaimStorage()
            .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);
    StatefulSet sts = kc.generateStatefulSet(false, null, null);
    assertThat(sts.getSpec().getVolumeClaimTemplates().get(0).getSpec().getSelector().getMatchLabels(), is(selector));
}
 
Example #19
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private Storage getOldStorage(StatefulSet sts)  {
    Storage storage = null;

    if (sts != null)    {
        String jsonStorage = Annotations.stringAnnotation(sts, ANNO_STRIMZI_IO_STORAGE, null);

        if (jsonStorage != null)    {
            storage = ModelUtils.decodeStorageFromJson(jsonStorage);
        }
    }

    return storage;
}
 
Example #20
Source File: KafkaRollerTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollWithPod2AsController(VertxTestContext testContext) {
    PodOperator podOps = mockPodOps(podId -> succeededFuture());
    StatefulSet sts = buildStatefulSet();
    TestingKafkaRoller kafkaRoller = rollerWithControllers(sts, podOps, 2);
    doSuccessfulRollingRestart(testContext, kafkaRoller,
            asList(0, 1, 2, 3, 4),
            asList(0, 1, 3, 4, 2));
}
 
Example #21
Source File: StatefulSetIT.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void update() {
  ReadyEntity<StatefulSet> statefulSetReady = new ReadyEntity<>(StatefulSet.class, client, "ss1", currentNamespace);
  ss1 = client.apps().statefulSets().inNamespace(currentNamespace).withName("ss1").scale(5);
  await().atMost(30, TimeUnit.SECONDS).until(statefulSetReady);
  assertEquals(5, ss1.getSpec().getReplicas().intValue());
}
 
Example #22
Source File: StatefulSetMockBuilder.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Override
protected void mockCreate(String resourceName, RollableScalableResource<StatefulSet, DoneableStatefulSet> resource) {
    when(resource.create(any())).thenAnswer(cinvocation -> {
        checkNotExists(resourceName);
        StatefulSet argument = cinvocation.getArgument(0);
        LOGGER.debug("create {} {} -> {}", resourceType, resourceName, argument);
        StatefulSet value = copyResource(argument);
        value.setStatus(new StatefulSetStatus());
        db.put(resourceName, value);
        for (int i = 0; i < argument.getSpec().getReplicas(); i++) {
            final int podNum = i;
            String podName = argument.getMetadata().getName() + "-" + podNum;
            LOGGER.debug("create Pod {} because it's in StatefulSet {}", podName, resourceName);
            mockPods.inNamespace(argument.getMetadata().getNamespace()).createOrReplace(doCreatePod(argument, podName));

            if (value.getSpec().getVolumeClaimTemplates().size() > 0) {

                for (PersistentVolumeClaim pvcTemplate: value.getSpec().getVolumeClaimTemplates()) {

                    String pvcName = pvcTemplate.getMetadata().getName() + "-" + podName;
                    if (mockPvcs.inNamespace(argument.getMetadata().getNamespace()).withName(pvcName).get() == null) {

                        LOGGER.debug("create Pvc {} because it's in VolumeClaimTemplate of StatefulSet {}", pvcName, resourceName);
                        PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder()
                                .withNewMetadata()
                                    .withLabels(argument.getSpec().getSelector().getMatchLabels())
                                    .withNamespace(argument.getMetadata().getNamespace())
                                    .withName(pvcName)
                                .endMetadata()
                                .build();
                        mockPvcs.inNamespace(argument.getMetadata().getNamespace()).withName(pvcName).create(pvc);
                    }
                }

            }
        }
        return argument;
    });
}
 
Example #23
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateStatefulSetWithEmptyStorageSelector() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
            .editKafka()
            .withNewPersistentClaimStorage().withSelector(emptyMap()).withSize("100Gi").endPersistentClaimStorage()
            .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);
    StatefulSet sts = kc.generateStatefulSet(false, null, null);
    assertThat(sts.getSpec().getVolumeClaimTemplates().get(0).getSpec().getSelector(), is(nullValue()));
}
 
Example #24
Source File: StatefulSetOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Overridden to not cascade to dependent resources (e.g. pods).
 *
 * {@inheritDoc}
 */
@Override
protected Future<ReconcileResult<StatefulSet>> internalPatch(String namespace, String name, StatefulSet current, StatefulSet desired) {
    StatefulSetDiff diff = new StatefulSetDiff(current, desired);

    if (shouldIncrementGeneration(diff)) {
        incrementGeneration(current, desired);
    } else {
        setGeneration(desired, getStsGeneration(current));
    }

    // Don't scale via patch
    desired.getSpec().setReplicas(current.getSpec().getReplicas());
    if (log.isTraceEnabled()) {
        log.trace("Patching {} {}/{} to match desired state {}", resourceKind, namespace, name, desired);
    } else {
        log.debug("Patching {} {}/{}", resourceKind, namespace, name);
    }

    if (diff.changesVolumeClaimTemplates() || diff.changesVolumeSize()) {
        // When volume claim templates change, we need to delete the STS and re-create it
        return internalReplace(namespace, name, current, desired, false);
    } else {
        return super.internalPatch(namespace, name, current, desired, false);
    }

}
 
Example #25
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testTlsSidecarContainerSecurityContext() {

    SecurityContext securityContext = new SecurityContextBuilder()
            .withPrivileged(false)
            .withNewReadOnlyRootFilesystem(false)
            .withAllowPrivilegeEscalation(false)
            .withRunAsNonRoot(true)
            .withNewCapabilities()
                .addNewDrop("ALL")
            .endCapabilities()
            .build();

    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
                .editKafka()
                    .withNewTemplate()
                        .withNewTlsSidecarContainer()
                            .withSecurityContext(securityContext)
                        .endTlsSidecarContainer()
                    .endTemplate()
                .endKafka()
            .endSpec()
            .build();

    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);
    StatefulSet sts = kc.generateStatefulSet(false, null, null);

    assertThat(sts.getSpec().getTemplate().getSpec().getContainers(),
            hasItem(allOf(
                    hasProperty("name", equalTo(KafkaCluster.TLS_SIDECAR_NAME)),
                    hasProperty("securityContext", equalTo(securityContext))
            )));
}
 
Example #26
Source File: ZookeeperClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultSecurityContext() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCmJson, configurationJson, emptyMap()))
            .build();
    ZookeeperCluster zc = ZookeeperCluster.fromCrd(kafkaAssembly, VERSIONS);

    StatefulSet sts = zc.generateStatefulSet(true, null, null);
    assertThat(sts.getSpec().getTemplate().getSpec().getSecurityContext(), is(nullValue()));
}
 
Example #27
Source File: StatefulSetOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the {@code strimzi.io/generation} of the given StatefulSet.
 * @param resource the StatefulSet.
 * @return The {@code strimzi.io/generation} of the given StatefulSet.
 */
@SuppressWarnings("deprecation")
public static int getStsGeneration(StatefulSet resource) {
    if (resource == null) {
        return NO_GENERATION;
    }
    return Annotations.intAnnotation(resource.getSpec().getTemplate(), ANNO_STRIMZI_IO_GENERATION,
            NO_GENERATION, ANNO_OP_STRIMZI_IO_GENERATION);
}
 
Example #28
Source File: KafkaRollerTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonControllerNeverRollable(VertxTestContext testContext) throws InterruptedException {
    PodOperator podOps = mockPodOps(podId -> succeededFuture());
    StatefulSet sts = buildStatefulSet();
    TestingKafkaRoller kafkaRoller = new TestingKafkaRoller(sts, null, null, podOps,
        null, null, null,
        brokerId ->
                brokerId == 1 ? succeededFuture(false)
                        : succeededFuture(true),
        2);
    doFailingRollingRestart(testContext, kafkaRoller,
            asList(0, 1, 2, 3, 4),
            KafkaRoller.UnforceableProblem.class, "Pod c-kafka-1 is currently not rollable",
            // Controller last, broker 1 never restarted
            asList(0, 3, 4, 2));
    // TODO assert subsequent rolls
    kafkaRoller = new TestingKafkaRoller(sts, null, null, podOps,
        null, null, null,
        brokerId -> succeededFuture(brokerId != 1),
        2);
    clearRestarted();
    doFailingRollingRestart(testContext, kafkaRoller,
            singletonList(1),
            KafkaRoller.UnforceableProblem.class, "Pod c-kafka-1 is currently not rollable",
            // Controller last, broker 1 never restarted
            emptyList());
}
 
Example #29
Source File: StatefulSetRollingUpdater.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
protected StatefulSet createClone(StatefulSet obj, String newName, String newDeploymentHash) {
  return new StatefulSetBuilder(obj)
    .editMetadata()
    .withResourceVersion(null)
    .withName(newName)
    .endMetadata()
    .editSpec()
    .withReplicas(0)
    .editSelector().addToMatchLabels(DEPLOYMENT_KEY, newDeploymentHash).endSelector()
    .editTemplate().editMetadata().addToLabels(DEPLOYMENT_KEY, newDeploymentHash).endMetadata().endTemplate()
    .endSpec()
    .build();
}
 
Example #30
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private KafkaVersionChange getKafkaVersionChange(StatefulSet kafkaSts) {
    // Get the current version of the cluster
    KafkaVersion currentVersion = versions.version(Annotations.annotations(kafkaSts).get(ANNO_STRIMZI_IO_KAFKA_VERSION));

    log.debug("STS {} currently has Kafka version {}", kafkaSts.getMetadata().getName(), currentVersion);

    String fromVersionAnno = Annotations.annotations(kafkaSts).get(ANNO_STRIMZI_IO_FROM_VERSION);
    KafkaVersion fromVersion;
    if (fromVersionAnno != null) { // We're mid version change
        fromVersion = versions.version(fromVersionAnno);
    } else {
        fromVersion = currentVersion;
    }

    log.debug("STS {} is moving from Kafka version {}", kafkaSts.getMetadata().getName(), fromVersion);

    String toVersionAnno = Annotations.annotations(kafkaSts).get(ANNO_STRIMZI_IO_TO_VERSION);
    KafkaVersion toVersion;
    if (toVersionAnno != null) { // We're mid version change
        toVersion = versions.version(toVersionAnno);
    } else {
        toVersion = versions.version(kafkaAssembly.getSpec().getKafka().getVersion());
    }

    log.debug("STS {} is moving to Kafka version {}", kafkaSts.getMetadata().getName(), toVersion);

    KafkaVersionChange versionChange = new KafkaVersionChange(fromVersion, toVersion);

    log.debug("Kafka version change: {}", versionChange);

    return versionChange;
}