-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor (jkube-kit/enricher) : Move storageClass related functionali…
…ty out of VolumePermissionEnricher (#1179) Related to #1179 + Move setting PersistentVolumeClaim's storageClass related logic into a new enricher : PersistentVolumeClaimStorageClassEnricher . This way user can simply exclude VolumePermissionEnricher to opt out of volume permission initContainer. + Deprecate VolumePermissionEnricher's `defaultStorageClass` and `useStorageClassAnnotation` fields in favor of PersistentVolumeClaimStorageClassEnricher equivalents. Signed-off-by: Rohan Kumar <rohaan@redhat.com>
- Loading branch information
1 parent
41df93e
commit 48ebbeb
Showing
13 changed files
with
245 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...rsistentvolumeclaim-storageclass/_jkube_persistentvolumeclaim_storageclass.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[[jkube-persistentvolumeclaim-storageclass]] | ||
==== jkube-persistentvolumeclaim-storageclass | ||
|
||
Enricher which fixes adds name of StorageClass required by PersistentVolumeClaim either in metadata or in spec. | ||
|
||
.Supported properties | ||
[cols="1,6,1"] | ||
|=== | ||
| Option | Description | Property | ||
|
||
| *defaultStorageClass* | ||
| PersistentVolume storage class. | ||
| `jkube.enricher.jkube-volume-permission.defaultStorageClass` | ||
|
||
| *useStorageClassAnnotation* | ||
| If enabled, storage class would be added to PersistentVolumeClaim metadata as `volume.beta.kubernetes.io/storage-class=<storageClassName>` annotation rather than `.spec.storageClassName` | ||
|
||
Defaults to `false` | ||
| `jkube.enricher.jkube-volume-permission.useStorageClassAnnotation` | ||
|=== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...in/java/org/eclipse/jkube/enricher/generic/PersistentVolumeClaimStorageClassEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.enricher.generic; | ||
|
||
import io.fabric8.kubernetes.api.builder.TypedVisitor; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.eclipse.jkube.kit.common.Configs; | ||
import org.eclipse.jkube.kit.config.resource.PlatformMode; | ||
import org.eclipse.jkube.kit.enricher.api.BaseEnricher; | ||
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; | ||
|
||
public class PersistentVolumeClaimStorageClassEnricher extends BaseEnricher { | ||
|
||
public static final String ENRICHER_NAME = "jkube-persistentvolumeclaim-storageclass"; | ||
static final String VOLUME_STORAGE_CLASS_ANNOTATION = "volume.beta.kubernetes.io/storage-class"; | ||
|
||
@AllArgsConstructor | ||
enum Config implements Configs.Config { | ||
DEFAULT_STORAGE_CLASS("defaultStorageClass", null), | ||
USE_ANNOTATION("useStorageClassAnnotation", "false"); | ||
|
||
@Getter | ||
protected String key; | ||
@Getter | ||
protected String defaultValue; | ||
} | ||
|
||
public PersistentVolumeClaimStorageClassEnricher(JKubeEnricherContext buildContext) { | ||
super(buildContext, ENRICHER_NAME); | ||
} | ||
|
||
@Override | ||
public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) { | ||
builder.accept(new TypedVisitor<PersistentVolumeClaimBuilder>() { | ||
@Override | ||
public void visit(PersistentVolumeClaimBuilder pvcBuilder) { | ||
// lets ensure we have a default storage class so that PVs will get dynamically created OOTB | ||
if (pvcBuilder.buildMetadata() == null) { | ||
pvcBuilder.withNewMetadata().endMetadata(); | ||
} | ||
String storageClass = getStorageClass(); | ||
if (StringUtils.isNotBlank(storageClass)) { | ||
if (shouldUseAnnotation()) { | ||
pvcBuilder.editMetadata().addToAnnotations(VOLUME_STORAGE_CLASS_ANNOTATION, storageClass).endMetadata(); | ||
} else { | ||
pvcBuilder.editSpec().withStorageClassName(storageClass).endSpec(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private boolean shouldUseAnnotation() { | ||
if (Boolean.TRUE.equals(Boolean.parseBoolean(getConfig(Config.USE_ANNOTATION)))) { | ||
return true; | ||
} | ||
VolumePermissionEnricher volumePermissionEnricher = new VolumePermissionEnricher((JKubeEnricherContext) getContext()); | ||
return Boolean.TRUE.equals(volumePermissionEnricher.shouldUseAnnotation()); | ||
} | ||
|
||
private String getStorageClass() { | ||
String storageClassConfig = getConfig(Config.DEFAULT_STORAGE_CLASS); | ||
if (StringUtils.isNotBlank(storageClassConfig)) { | ||
return storageClassConfig; | ||
} | ||
VolumePermissionEnricher volumePermissionEnricher = new VolumePermissionEnricher((JKubeEnricherContext) getContext()); | ||
return volumePermissionEnricher.getDefaultStorageClass(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ava/org/eclipse/jkube/enricher/generic/PersistentVolumeClaimStorageClassEnricherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.enricher.generic; | ||
|
||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.PersistentVolumeClaim; | ||
import io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.eclipse.jkube.kit.config.resource.PlatformMode; | ||
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.util.Collections; | ||
import java.util.Properties; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
class PersistentVolumeClaimStorageClassEnricherTest { | ||
private JKubeEnricherContext context; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
context = mock(JKubeEnricherContext.class, RETURNS_DEEP_STUBS); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"jkube-persistentvolumeclaim-storageclass", "jkube-volume-permission"}) | ||
void enrich_withPersistentVolumeClaim_shouldAddStorageClassToSpec(String enricher) { | ||
// Given | ||
Properties properties = new Properties(); | ||
properties.put("jkube.enricher." + enricher + ".defaultStorageClass", "standard"); | ||
when(context.getProperties()).thenReturn(properties); | ||
PersistentVolumeClaimStorageClassEnricher volumePermissionEnricher = new PersistentVolumeClaimStorageClassEnricher(context); | ||
KubernetesListBuilder klb = new KubernetesListBuilder(); | ||
klb.addToItems(createNewPersistentVolumeClaim()); | ||
|
||
// When | ||
volumePermissionEnricher.enrich(PlatformMode.kubernetes, klb); | ||
|
||
// Then | ||
assertThat(klb.buildItems()) | ||
.singleElement(InstanceOfAssertFactories.type(PersistentVolumeClaim.class)) | ||
.hasFieldOrPropertyWithValue("spec.storageClassName", "standard") | ||
.extracting("metadata.annotations") | ||
.asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class)) | ||
.isNullOrEmpty(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"jkube-persistentvolumeclaim-storageclass", "jkube-volume-permission"}) | ||
void enrich_withPersistentVolumeClaimAndUseAnnotationEnabled_shouldAddStorageClassAnnotation(String enricher) { | ||
// Given | ||
Properties properties = new Properties(); | ||
properties.put("jkube.enricher." + enricher + ".defaultStorageClass", "standard"); | ||
properties.put("jkube.enricher." + enricher + ".useStorageClassAnnotation", "true"); | ||
when(context.getProperties()).thenReturn(properties); | ||
PersistentVolumeClaimStorageClassEnricher volumePermissionEnricher = new PersistentVolumeClaimStorageClassEnricher(context); | ||
KubernetesListBuilder klb = new KubernetesListBuilder(); | ||
klb.addToItems(createNewPersistentVolumeClaim()); | ||
|
||
// When | ||
volumePermissionEnricher.enrich(PlatformMode.kubernetes, klb); | ||
|
||
// Then | ||
assertThat(klb.buildItems()) | ||
.singleElement(InstanceOfAssertFactories.type(PersistentVolumeClaim.class)) | ||
.hasFieldOrPropertyWithValue("metadata.annotations", Collections.singletonMap("volume.beta.kubernetes.io/storage-class", "standard")) | ||
.hasFieldOrPropertyWithValue("spec.storageClassName", null); | ||
} | ||
|
||
private PersistentVolumeClaim createNewPersistentVolumeClaim() { | ||
return new PersistentVolumeClaimBuilder() | ||
.withNewMetadata().withName("pv1").endMetadata() | ||
.withNewSpec().endSpec() | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.