io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder. 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: TektonHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public PersistentVolumeClaim createSourceWorkspacePvc(TektonConfig config) {
  Map<String, Quantity> requests = new HashMap<String, Quantity>() {{
      put("storage", new QuantityBuilder().withAmount(String.valueOf(config.getSourceWorkspaceClaim().getSize())).withFormat(config.getSourceWorkspaceClaim().getUnit()).build());
  }};
  LabelSelector selector = null;


  if (config.getSourceWorkspaceClaim().getMatchLabels().length != 0) {
    selector = new LabelSelectorBuilder()
      .withMatchLabels(Arrays.stream(config.getSourceWorkspaceClaim().getMatchLabels()).collect(Collectors.toMap(l -> l.getKey(), l -> l.getValue())))
      .build();
  }
  return new PersistentVolumeClaimBuilder()
    .withNewMetadata()
    .withName(sourceWorkspaceClaimName(config))
    .endMetadata()
    .withNewSpec()
    .withAccessModes(config.getSourceWorkspaceClaim().getAccessMode().name())
    .withStorageClassName(config.getSourceWorkspaceClaim().getStorageClass())
    .withNewResources().withRequests(requests).endResources()
    .withSelector(selector)
    .endSpec()
    .build();
}
 
Example #2
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
void testBuild() {
  PersistentVolumeClaim persistentVolumeClaim = new PersistentVolumeClaimBuilder()
    .withNewMetadata().withName("test-pv-claim").withNamespace("test").endMetadata()
    .withNewSpec()
    .withStorageClassName("my-local-storage")
    .withAccessModes("ReadWriteOnce")
    .withNewResources()
    .addToRequests("storage", new Quantity("500Gi"))
    .endResources()
    .endSpec()
    .build();

  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/test-pv-claim").andReturn(200, persistentVolumeClaim).once();

  KubernetesClient client = server.getClient();
  persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("test").withName("test-pv-claim").get();
  assertNotNull(persistentVolumeClaim);
}
 
Example #3
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
void testDeleteMulti() {
  PersistentVolumeClaim persistentVolumeClaim1 = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentvolumeclaim1").withNamespace("test").endMetadata().build();
  PersistentVolumeClaim persistentVolumeClaim2 = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentvolumeclaim2").withNamespace("ns1").endMetadata().build();
  PersistentVolumeClaim persistentVolumeClaim3 = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentvolumeclaim3").withNamespace("any").endMetadata().build();

  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1").andReturn(200, persistentVolumeClaim1).once();
  server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2").andReturn(200, persistentVolumeClaim2).once();

  KubernetesClient client = server.getClient();
  Boolean deleted = client.persistentVolumeClaims().inAnyNamespace().delete(persistentVolumeClaim1, persistentVolumeClaim2);
  assertTrue(deleted);

  deleted = client.persistentVolumeClaims().inAnyNamespace().delete(persistentVolumeClaim3);
  assertFalse(deleted);
}
 
Example #4
Source File: KubernetesEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldCreateK8sEnvironmentWithPVCsFromRecipe() throws Exception {
  // given
  PersistentVolumeClaim pvc1 =
      new PersistentVolumeClaimBuilder().withNewMetadata().withName("pvc1").endMetadata().build();
  PersistentVolumeClaim pvc2 =
      new PersistentVolumeClaimBuilder().withNewMetadata().withName("pvc2").endMetadata().build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(pvc1, pvc2));

  // when
  KubernetesEnvironment k8sEnv = k8sEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 2);
  assertEquals(k8sEnv.getPersistentVolumeClaims().get("pvc1"), pvc1);
  assertEquals(k8sEnv.getPersistentVolumeClaims().get("pvc2"), pvc2);
}
 
Example #5
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns new instance of {@link PersistentVolumeClaim} with specified name, accessMode, quantity
 * and storageClassName.
 */
public static PersistentVolumeClaim newPVC(
    String name, String accessMode, String quantity, String storageClassName) {
  SpecNested<PersistentVolumeClaimBuilder> specs =
      new PersistentVolumeClaimBuilder()
          .withNewMetadata()
          .withName(name)
          .endMetadata()
          .withNewSpec()
          .withAccessModes(accessMode);
  if (!isNullOrEmpty(storageClassName)) {
    specs.withStorageClassName(storageClassName);
  }
  return specs
      .withNewResources()
      .withRequests(ImmutableMap.of(STORAGE_PARAM, new Quantity(quantity)))
      .endResources()
      .endSpec()
      .build();
}
 
Example #6
Source File: OpenShiftEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldCreateOpenShiftEnvironmentWithPVCsFromRecipe() throws Exception {
  // given
  PersistentVolumeClaim pvc1 =
      new PersistentVolumeClaimBuilder().withNewMetadata().withName("pvc1").endMetadata().build();
  PersistentVolumeClaim pvc2 =
      new PersistentVolumeClaimBuilder().withNewMetadata().withName("pvc2").endMetadata().build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(pvc1, pvc2));

  // when
  OpenShiftEnvironment osEnv = osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  assertEquals(osEnv.getPersistentVolumeClaims().size(), 2);
  assertEquals(osEnv.getPersistentVolumeClaims().get("pvc1"), pvc1);
  assertEquals(osEnv.getPersistentVolumeClaims().get("pvc2"), pvc2);
}
 
Example #7
Source File: KafkaAssemblyOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
private Map<String, PersistentVolumeClaim> createPvcs(String namespace, Storage storage, int replicas,
                                               BiFunction<Integer, Integer, String> pvcNameFunction) {

    Map<String, PersistentVolumeClaim> pvcs = new HashMap<>();
    if (storage instanceof PersistentClaimStorage) {

        for (int i = 0; i < replicas; i++) {
            Integer storageId = ((PersistentClaimStorage) storage).getId();
            String pvcName = pvcNameFunction.apply(i, storageId);
            PersistentVolumeClaim pvc =
                    new PersistentVolumeClaimBuilder()
                            .withNewMetadata()
                            .withNamespace(namespace)
                            .withName(pvcName)
                            .endMetadata()
                            .build();
            pvcs.put(pvcName, pvc);
        }

    }
    return pvcs;
}
 
Example #8
Source File: TektonHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public PersistentVolumeClaim createM2WorkspacePvc(TektonConfig config) {
  Map<String, Quantity> requests = new HashMap<String, Quantity>() {{
      put("storage", new QuantityBuilder().withAmount(String.valueOf(config.getM2WorkspaceClaim().getSize())).withFormat(config.getM2WorkspaceClaim().getUnit()).build());
  }};
  LabelSelector selector = null;
  if (config.getM2WorkspaceClaim().getMatchLabels().length != 0) {
    selector = new LabelSelectorBuilder()
      .withMatchLabels(Arrays.stream(config.getM2WorkspaceClaim().getMatchLabels()).collect(Collectors.toMap(l -> l.getKey(), l -> l.getValue())))
      .build();
  }

  return new PersistentVolumeClaimBuilder()
    .withNewMetadata()
    .withName(m2WorkspaceClaimName(config))
    .endMetadata()
    .withNewSpec()
    .withAccessModes(config.getM2WorkspaceClaim().getAccessMode().name())
    .withStorageClassName(config.getM2WorkspaceClaim().getStorageClass())
    .withNewResources().withRequests(requests).endResources()
    .withSelector(selector)
    .endSpec()
    .build();
}
 
Example #9
Source File: PvcOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRevertingImmutableFields()   {
    PersistentVolumeClaim desired = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
                .withName("my-pvc")
                .withNamespace("my-namespace")
            .endMetadata()
            .withNewSpec()
                .withNewResources()
                    .withRequests(Collections.singletonMap("storage", new Quantity("100", null)))
                .endResources()
            .endSpec()
            .build();

    PersistentVolumeClaim current = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
                .withName("my-pvc")
                .withNamespace("my-namespace")
            .endMetadata()
            .withNewSpec()
                .withAccessModes("ReadWriteOnce")
                .withNewResources()
                    .withRequests(Collections.singletonMap("storage", new Quantity("10", null)))
                .endResources()
                .withStorageClassName("my-storage-class")
                .withSelector(new LabelSelector(null, Collections.singletonMap("key", "label")))
                .withVolumeName("pvc-ce9ebf52-435a-11e9-8fbc-06b5ff7c7748")
            .endSpec()
            .build();

    PvcOperator op = createResourceOperations(vertx, mock(KubernetesClient.class));
    op.revertImmutableChanges(current, desired);

    assertThat(current.getSpec().getStorageClassName(), is(desired.getSpec().getStorageClassName()));
    assertThat(current.getSpec().getAccessModes(), is(desired.getSpec().getAccessModes()));
    assertThat(current.getSpec().getSelector(), is(desired.getSpec().getSelector()));
    assertThat(current.getSpec().getVolumeName(), is(desired.getSpec().getVolumeName()));
}
 
Example #10
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 #11
Source File: SystemtestsKubernetesApps.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim getPostgresPVC() {
    return new PersistentVolumeClaimBuilder()
            .withNewMetadata()
            .withName(POSTGRES_APP)
            .addToLabels("app", POSTGRES_APP)
            .endMetadata()
            .withNewSpec()
            .withAccessModes("ReadWriteOnce")
            .withNewResources()
            .addToRequests("storage", new Quantity("5Gi"))
            .endResources()
            .endSpec()
            .build();
}
 
Example #12
Source File: PersistentVolumeClaimHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private void generate(PersistentVolumeClaimModel volumeClaimModel) throws KubernetesPluginException {
    Quantity quantity = new QuantityBuilder()
            .withAmount(volumeClaimModel.getVolumeClaimSizeAmount())
            .withFormat(volumeClaimModel.getVolumeClaimSizeFormat())
            .build();
    
    Map<String, Quantity> requests = new HashMap<>();
    requests.put("storage", quantity);
    PersistentVolumeClaim claim = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
            .withName(volumeClaimModel.getName())
            .withNamespace(dataHolder.getNamespace())
            .withAnnotations(volumeClaimModel.getAnnotations())
            .endMetadata()
            .withNewSpec()
            .withAccessModes(volumeClaimModel.getAccessMode())
            .withNewResources()
            .withRequests(requests)
            .endResources()
            .endSpec()
            .build();
    try {
        String claimContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(claim);
        KubernetesUtils.writeToFile(claimContent,
                VOLUME_CLAIM_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for volume claim: " + volumeClaimModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
 
Example #13
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void testDelete() {
  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1").andReturn(200, new PersistentVolumeClaimBuilder().build()).once();
  server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2").andReturn(200, new PersistentVolumeClaimBuilder().build()).once();

  KubernetesClient client = server.getClient();
  Boolean deleted = client.persistentVolumeClaims().inNamespace("test").withName("persistentvolumeclaim1").delete();
  assertTrue(deleted);

  deleted = client.persistentVolumeClaims().withName("persistentvolumeclaim2").delete();
  assertFalse(deleted);

  deleted = client.persistentVolumeClaims().inNamespace("ns1").withName("persistentvolumeclaim2").delete();
  assertTrue(deleted);
}
 
Example #14
Source File: UniqueWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
static PersistentVolumeClaim newPVC(String name, Map<String, String> labels) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .withLabels(labels)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #15
Source File: CommonPVCStrategyTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim newPVC(String name) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #16
Source File: PerWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim newPVC(String name) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #17
Source File: SubPathPrefixesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim newPVC(String name, Map<String, String> labels) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .withLabels(labels)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #18
Source File: PVCProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim newPVC(String name, Map<String, String> labels) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .withLabels(labels)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #19
Source File: DynamicPVCWorkspaceVolume.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public PersistentVolumeClaim createVolume(KubernetesClient client, ObjectMeta podMetaData){
    String namespace = podMetaData.getNamespace();
    String podId = podMetaData.getName();
    LOGGER.log(Level.FINE, "Adding workspace volume from pod: {0}/{1}", new Object[] { namespace, podId });
    OwnerReference ownerReference = new OwnerReferenceBuilder().
            withApiVersion("v1").
            withKind("Pod").
            withBlockOwnerDeletion(true).
            withController(true).
            withName(podMetaData.getName()).
            withUid(podMetaData.getUid()).build();

     PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
            .withName("pvc-" + podMetaData.getName())
            .withOwnerReferences(ownerReference)
            .withLabels(DEFAULT_POD_LABELS)
            .endMetadata()
            .withNewSpec()
            .withAccessModes(getAccessModesOrDefault())
            .withNewResources()
            .withRequests(getResourceMap())
            .endResources()
            .withStorageClassName(getStorageClassNameOrDefault())
            .endSpec()
            .build();
     pvc = client.persistentVolumeClaims().inNamespace(podMetaData.getNamespace()).create(pvc);
     LOGGER.log(INFO, "Created PVC: {0}/{1}", new Object[] { namespace, pvc.getMetadata().getName() });
     return pvc;
}
 
Example #20
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void testGet() {
  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1").andReturn(200, new PersistentVolumeClaimBuilder().build()).once();
  server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2").andReturn(200, new PersistentVolumeClaimBuilder().build()).once();

  KubernetesClient client = server.getClient();
  PersistentVolumeClaim persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("test").withName("persistentvolumeclaim1").get();
  assertNotNull(persistentVolumeClaim);

  persistentVolumeClaim = client.persistentVolumeClaims().withName("persistentvolumeclaim2").get();
  assertNull(persistentVolumeClaim);

  persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("ns1").withName("persistentvolumeclaim2").get();
  assertNotNull(persistentVolumeClaim);
}
 
Example #21
Source File: PersistentVolumeClaimExample.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
  String master = "https://localhost:8443";
  String namespace = "default";
  String storageClassName = "my-local-storage";
  if (args.length == 1) {
    master = args[0];
  }

  log("Using master with url ", master);
  Config config = new ConfigBuilder().withMasterUrl(master).build();
  try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
    try {

      StorageClass storageClass = client.storage().storageClasses().load(PersistentVolumeClaimExample.class.getResourceAsStream("/test-storage.yml")).get();
      client.storage().storageClasses().create(storageClass);

      log("Creating PersistentVolume object");
      PersistentVolume pv = new PersistentVolumeBuilder()
        .withNewMetadata().withName("test-local-pv").endMetadata()
        .withNewSpec()
        .addToCapacity(Collections.singletonMap("storage", new Quantity("500Gi")))
        .withAccessModes("ReadWriteOnce")
        .withPersistentVolumeReclaimPolicy("Retain")
        .withStorageClassName(storageClassName)
        .withNewLocal()
        .withPath("/mnt/disks/vol1")
        .endLocal()
        .withNewNodeAffinity()
        .withNewRequired()
        .addNewNodeSelectorTerm()
        .withMatchExpressions(Arrays.asList(new NodeSelectorRequirementBuilder()
          .withKey("kubernetes.io/hostname")
          .withOperator("In")
          .withValues("my-node")
          .build()
        ))
        .endNodeSelectorTerm()
        .endRequired()
        .endNodeAffinity()
        .endSpec()
        .build();

      client.persistentVolumes().create(pv);

      log("Successfully created PersistentVolume object");

      log("Creating PersistentVolumeClaim object");
      PersistentVolumeClaim persistentVolumeClaim = new PersistentVolumeClaimBuilder()
        .withNewMetadata().withName("test-pv-claim").withNamespace(namespace).endMetadata()
        .withNewSpec()
        .withStorageClassName(storageClassName)
        .withAccessModes("ReadWriteOnce")
        .withNewResources()
        .addToRequests("storage", new Quantity("500Gi"))
        .endResources()
        .endSpec()
        .build();

      client.persistentVolumeClaims().create(persistentVolumeClaim);
      log("Successfully created PersistentVolumeClaim object");

      log("Creating pod");
      Pod pod = client.pods().inNamespace(namespace).load(PersistentVolumeClaimExample.class.getResourceAsStream("/test-pv-pod.yml")).get();
      client.pods().inNamespace(namespace).create(pod);

      log("Successfully created pod");
    } finally {
      client.persistentVolumeClaims().inNamespace(namespace).withName("test-pv-claim").delete();
      client.persistentVolumes().withName("test-local-pv").delete(); //fixed the name
      client.pods().inNamespace("default").withName("test-pv-pod").delete(); // you forgot to remove the pod
      client.storage().storageClasses().withName(storageClassName).delete();
    }
  } catch (KubernetesClientException e) {
    log("Could not create resource", e.getMessage());
  }
}
 
Example #22
Source File: StatefulSetDiffTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
public void testNewPvcNotIgnored() {
    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("100Gi")))
                        .endResources()
                        .endSpec()
                        .build(),
                        new PersistentVolumeClaimBuilder()
                                .withNewSpec()
                                .withNewResources()
                                .withRequests(singletonMap("storage", new Quantity("110Gi")))
                                .endResources()
                                .endSpec()
                                .build())
            .endSpec()
            .build();
    assertThat(new StatefulSetDiff(ss1, ss2).changesVolumeClaimTemplates(), is(true));
    assertThat(new StatefulSetDiff(ss1, ss2).changesVolumeSize(), is(false));
}
 
Example #23
Source File: StatefulSetDiffTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@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(ss1, ss2).changesVolumeClaimTemplates(), is(false));
    assertThat(new StatefulSetDiff(ss1, ss2).changesVolumeSize(), is(true));
}
 
Example #24
Source File: StatefulSetDiffTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Test
public void testPvcSizeUnitChangeIgnored() {
    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("3072Gi")))
                    .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("3Ti")))
                    .endResources()
                    .endSpec()
                    .build())
            .endSpec()
            .build();
    assertThat(new StatefulSetDiff(ss1, ss2).changesVolumeClaimTemplates(), is(false));
    assertThat(new StatefulSetDiff(ss1, ss2).changesVolumeSize(), is(false));
}
 
Example #25
Source File: PvcOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Override
protected PersistentVolumeClaim resource() {
    return new PersistentVolumeClaimBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(RESOURCE_NAME).endMetadata().build();
}
 
Example #26
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);
}