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

The following examples show how to use io.fabric8.kubernetes.api.model.apps.StatefulSetSpecBuilder. 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: StatefulSetHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private StatefulSetSpec createStatefulSetSpec(ResourceConfig config, List<ImageConfiguration> images) {
    return new StatefulSetSpecBuilder()
            .withReplicas(config.getReplicas())
            .withServiceName(config.getControllerName())
            .withTemplate(podTemplateHandler.getPodTemplate(config,images))
            .build();
}
 
Example #2
Source File: TestRabbitMQClusterReconciler.java    From rabbitmq-operator with Apache License 2.0 4 votes vote down vote up
@Test
void scalingDownDeletesOrphanPVCs() throws InterruptedException, RabbitClusterConfigurationException {
    final Reconciliation rec = new Reconciliation(NAME, NAME, NAMESPACE, "type");

    final StatefulSet originalStatefulSet = new StatefulSet(
            "apps/v1",
            "StatefulSet",
            new ObjectMetaBuilder().build(),
            new StatefulSetSpecBuilder().withReplicas(4).build(),
            null
    );

    final RabbitMQCustomResource scaledResource = new RabbitMQCustomResourceBuilder()
            .withMetadata(
                    new ObjectMetaBuilder()
                            .withName(NAME)
                            .withNamespace(NAMESPACE)
                            .build()
            )
            .withSpec(
                    new RabbitMQCustomResourceSpecBuilder()
                            .withReplicas(3)
                            .build()
            )
            .build();

    when(controller.get(rec.getResourceName(), rec.getNamespace())).thenReturn(scaledResource);

    when(clusterFactory.fromCustomResource(scaledResource)).thenReturn(
            RabbitMQCluster.newBuilder()
                    .withName(NAME)
                    .withNamespace(NAMESPACE)
                    .withAdminSecret(null)
                    .withErlangCookieSecret(null)
                    .withMainService(null)
                    .withDiscoveryService(null)
                    .withLoadBalancerService(Optional.empty())
                    .withNodePortService(Optional.empty())
                    .withStatefulSet(originalStatefulSet)
                    .withPodDisruptionBudget(null)
                    .withShovels(Lists.newArrayList())
                    .withUsers(Lists.newArrayList())
                    .withPolicies(Lists.newArrayList())
                    .withOperatorPolicies(Lists.newArrayList())
                    .build()
    );

    // This call will happen twice.  In both cases it will occur before the StatefulSet has been patched, hence it
    // will reflect the origin unscaled replica count.
    when(statefulSetController.get(NAME, NAMESPACE)).thenReturn(originalStatefulSet);

    reconciler.reconcile(rec);

    verify(persistentVolumeClaimController).delete(RABBITMQ_STORAGE_NAME + "-" + NAME + "-3", NAMESPACE);
    verifyNoMoreInteractions(persistentVolumeClaimController);
}
 
Example #3
Source File: TestRabbitMQClusterReconciler.java    From rabbitmq-operator with Apache License 2.0 4 votes vote down vote up
@Test
void scalingDownPreservesOrphanPVCs() throws InterruptedException, RabbitClusterConfigurationException {
    final Reconciliation rec = new Reconciliation(NAME, NAME, NAMESPACE, "type");

    final StatefulSet originalStatefulSet = new StatefulSet(
            "apps/v1",
            "StatefulSet",
            new ObjectMetaBuilder().build(),
            new StatefulSetSpecBuilder().withReplicas(4).build(),
            null
    );

    final RabbitMQCustomResource scaledResource = new RabbitMQCustomResourceBuilder()
            .withMetadata(
                    new ObjectMetaBuilder()
                            .withName(NAME)
                            .withNamespace(NAMESPACE)
                            .build()
            )
            .withSpec(
                    new RabbitMQCustomResourceSpecBuilder()
                            .withReplicas(3)
                            .withPreserveOrphanPVCs(true)
                            .build()
            )
            .build();

    when(controller.get(rec.getResourceName(), rec.getNamespace())).thenReturn(scaledResource);

    when(clusterFactory.fromCustomResource(scaledResource)).thenReturn(
            RabbitMQCluster.newBuilder()
                    .withName(NAME)
                    .withNamespace(NAMESPACE)
                    .withAdminSecret(null)
                    .withErlangCookieSecret(null)
                    .withMainService(null)
                    .withDiscoveryService(null)
                    .withLoadBalancerService(Optional.empty())
                    .withNodePortService(Optional.empty())
                    .withStatefulSet(originalStatefulSet)
                    .withPodDisruptionBudget(null)
                    .withShovels(Lists.newArrayList())
                    .withUsers(Lists.newArrayList())
                    .withPolicies(Lists.newArrayList())
                    .withOperatorPolicies(Lists.newArrayList())
                    .build()
    );

    // This call will happen twice.  In both cases it will occur before the StatefulSet has been patched, hence it
    // will reflect the origin unscaled replica count.
    when(statefulSetController.get(NAME, NAMESPACE)).thenReturn(originalStatefulSet);

    reconciler.reconcile(rec);

    verifyZeroInteractions(persistentVolumeClaimController);
    verifyNoMoreInteractions(persistentVolumeClaimController);
}
 
Example #4
Source File: KubernetesAppDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 2 votes vote down vote up
/**
 * Create a StatefulSet
 *
 * @param request the {@link AppDeploymentRequest}
 */
protected void createStatefulSet(AppDeploymentRequest request) {

	String appId = createDeploymentId(request);

	int externalPort = getExternalPort(request);

	Map<String, String> idMap = createIdMap(appId, request);

	int replicas = getCountFromRequest(request);

	Map<String, String> kubernetesDeployerProperties = request.getDeploymentProperties();

	logger.debug(String.format("Creating StatefulSet: %s on %d with %d replicas", appId, externalPort, replicas));

	Map<String, Quantity> storageResource = Collections.singletonMap("storage",
			new Quantity(this.deploymentPropertiesResolver.getStatefulSetStorage(kubernetesDeployerProperties)));

	String storageClassName = this.deploymentPropertiesResolver.getStatefulSetStorageClassName(kubernetesDeployerProperties);

	PersistentVolumeClaimBuilder persistentVolumeClaimBuilder = new PersistentVolumeClaimBuilder().withNewSpec().
			withStorageClassName(storageClassName).withAccessModes(Collections.singletonList("ReadWriteOnce"))
			.withNewResources().addToLimits(storageResource).addToRequests(storageResource).endResources()
			.endSpec().withNewMetadata().withName(appId).withLabels(idMap)
			.addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).endMetadata();

	PodSpec podSpec = createPodSpec(request);

	podSpec.getVolumes().add(new VolumeBuilder().withName("config").withNewEmptyDir().endEmptyDir().build());

	podSpec.getContainers().get(0).getVolumeMounts()
		.add(new VolumeMountBuilder().withName("config").withMountPath("/config").build());

	String statefulSetInitContainerImageName = this.deploymentPropertiesResolver.getStatefulSetInitContainerImageName(kubernetesDeployerProperties);

	podSpec.getInitContainers().add(createStatefulSetInitContainer(statefulSetInitContainerImageName));
	
	Map<String, String> deploymentLabels=  this.deploymentPropertiesResolver.getDeploymentLabels(request.getDeploymentProperties());
	
	StatefulSetSpec spec = new StatefulSetSpecBuilder().withNewSelector().addToMatchLabels(idMap)
			.addToMatchLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).endSelector()
			.withVolumeClaimTemplates(persistentVolumeClaimBuilder.build()).withServiceName(appId)
			.withPodManagementPolicy("Parallel").withReplicas(replicas).withNewTemplate().withNewMetadata()
			.withLabels(idMap).addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).addToLabels(deploymentLabels)
			.endMetadata().withSpec(podSpec).endTemplate().build();

	StatefulSet statefulSet = new StatefulSetBuilder().withNewMetadata().withName(appId).withLabels(idMap)
			.addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE).addToLabels(deploymentLabels).endMetadata().withSpec(spec).build();

	client.apps().statefulSets().create(statefulSet);
}