Skip to content

Commit

Permalink
Move AbstractModel static methods to ModelUtils (strimzi#2429)
Browse files Browse the repository at this point in the history
* Move createVolumeMount, createPersistentVolumeClaimTemplate, createEmptyDirVolume, createSecretVolume from AbstractModel to ModelUtils

Signed-off-by: Abhinav Sonkar <[email protected]>

* Remove unnecessary initialization of AbstractModel in ModelUtilsTest

Signed-off-by: Abhinav Sonkar <[email protected]>

* Move all Volume related helper methods into VolumeUtils.

Signed-off-by: Abhinav Sonkar <[email protected]>

* Move getVolumePrefix() as well

Signed-off-by: Abhinav Sonkar <[email protected]>

* Add missing license header in VolumeUtilsTest

Signed-off-by: Abhinav Sonkar <[email protected]>

* Add @return in javadocs.

Signed-off-by: Abhinav Sonkar <[email protected]>
  • Loading branch information
xsreality authored Jan 29, 2020
1 parent c145408 commit f033271
Show file tree
Hide file tree
Showing 19 changed files with 371 additions and 326 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerPort;
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
import io.fabric8.kubernetes.api.model.EmptyDirVolumeSource;
import io.fabric8.kubernetes.api.model.EmptyDirVolumeSourceBuilder;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
import io.fabric8.kubernetes.api.model.EnvVarSource;
import io.fabric8.kubernetes.api.model.EnvVarSourceBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.KeyToPath;
import io.fabric8.kubernetes.api.model.KeyToPathBuilder;
import io.fabric8.kubernetes.api.model.LabelSelector;
import io.fabric8.kubernetes.api.model.LabelSelectorBuilder;
import io.fabric8.kubernetes.api.model.LocalObjectReference;
Expand All @@ -33,8 +29,6 @@
import io.fabric8.kubernetes.api.model.Quantity;
import io.fabric8.kubernetes.api.model.ResourceRequirements;
import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretVolumeSource;
import io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.ServiceAccountBuilder;
Expand All @@ -44,8 +38,6 @@
import io.fabric8.kubernetes.api.model.Toleration;
import io.fabric8.kubernetes.api.model.Volume;
import io.fabric8.kubernetes.api.model.VolumeBuilder;
import io.fabric8.kubernetes.api.model.VolumeMount;
import io.fabric8.kubernetes.api.model.VolumeMountBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.kubernetes.api.model.apps.DeploymentStrategy;
Expand All @@ -72,7 +64,6 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -564,15 +555,6 @@ protected List<Container> getInitContainers(ImagePullPolicy imagePullPolicy) {
*/
protected abstract List<Container> getContainers(ImagePullPolicy imagePullPolicy);

protected static VolumeMount createVolumeMount(String name, String path) {
VolumeMount volumeMount = new VolumeMountBuilder()
.withName(name)
.withMountPath(path)
.build();
log.trace("Created volume mount {}", volumeMount);
return volumeMount;
}

protected ContainerPort createContainerPort(String name, int port, String protocol) {
ContainerPort containerPort = new ContainerPortBuilder()
.withName(name)
Expand Down Expand Up @@ -603,32 +585,6 @@ protected ServicePort createServicePort(String name, int port, int targetPort, I
return servicePort;
}

protected static PersistentVolumeClaim createPersistentVolumeClaimTemplate(String name, PersistentClaimStorage storage) {
Map<String, Quantity> requests = new HashMap<>();
requests.put("storage", new Quantity(storage.getSize(), null));

LabelSelector selector = null;
if (storage.getSelector() != null && !storage.getSelector().isEmpty()) {
selector = new LabelSelector(null, storage.getSelector());
}

PersistentVolumeClaim pvc = new PersistentVolumeClaimBuilder()
.withNewMetadata()
.withName(name)
.endMetadata()
.withNewSpec()
.withAccessModes("ReadWriteOnce")
.withNewResources()
.withRequests(requests)
.endResources()
.withStorageClassName(storage.getStorageClass())
.withSelector(selector)
.endSpec()
.build();

return pvc;
}

protected PersistentVolumeClaim createPersistentVolumeClaim(int podNumber, String name, PersistentClaimStorage storage) {
Map<String, Quantity> requests = new HashMap<>();
requests.put("storage", new Quantity(storage.getSize(), null));
Expand Down Expand Up @@ -670,20 +626,6 @@ protected PersistentVolumeClaim createPersistentVolumeClaim(int podNumber, Strin
return pvc;
}

protected static Volume createEmptyDirVolume(String name, String sizeLimit) {
EmptyDirVolumeSource emptyDirVolumeSource = new EmptyDirVolumeSourceBuilder().build();
if (sizeLimit != null && !sizeLimit.isEmpty()) {
emptyDirVolumeSource.setSizeLimit(new Quantity(sizeLimit));
}

Volume volume = new VolumeBuilder()
.withName(name)
.withEmptyDir(emptyDirVolumeSource)
.build();
log.trace("Created emptyDir Volume named '{}' with sizeLimit '{}'", name, sizeLimit);
return volume;
}

protected Volume createConfigMapVolume(String name, String configMapName) {

ConfigMapVolumeSource configMapVolumeSource = new ConfigMapVolumeSourceBuilder()
Expand Down Expand Up @@ -711,56 +653,6 @@ protected ConfigMap createConfigMap(String name, Map<String, String> data) {
.build();
}

protected static Volume createSecretVolume(String name, String secretName, boolean isOpenshift) {
int mode = 0444;
if (isOpenshift) {
mode = 0440;
}

SecretVolumeSource secretVolumeSource = new SecretVolumeSourceBuilder()
.withDefaultMode(mode)
.withSecretName(secretName)
.build();

Volume volume = new VolumeBuilder()
.withName(name)
.withSecret(secretVolumeSource)
.build();
log.trace("Created secret Volume named '{}' with source secret '{}'", name, secretName);
return volume;
}

protected static Volume createSecretVolume(String name, String secretName, Map<String, String> items, boolean isOpenshift) {
int mode = 0444;
if (isOpenshift) {
mode = 0440;
}

List<KeyToPath> keysPaths = new ArrayList<>();

for (Map.Entry<String, String> item : items.entrySet()) {
KeyToPath keyPath = new KeyToPathBuilder()
.withNewKey(item.getKey())
.withNewPath(item.getValue())
.build();

keysPaths.add(keyPath);
}

SecretVolumeSource secretVolumeSource = new SecretVolumeSourceBuilder()
.withDefaultMode(mode)
.withSecretName(secretName)
.withItems(keysPaths)
.build();

Volume volume = new VolumeBuilder()
.withName(name)
.withSecret(secretVolumeSource)
.build();
log.trace("Created secret Volume named '{}' with source secret '{}'", name, secretName);
return volume;
}

protected Secret createSecret(String name, Map<String, String> data) {
return ModelUtils.createSecret(name, namespace, labels, createOwnerReference(), data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public static void configureClientAuthenticationVolumes(KafkaClientAuthenticatio

// skipping if a volume with same Secret name was already added
if (!volumeList.stream().anyMatch(v -> v.getName().equals(tlsAuth.getCertificateAndKey().getSecretName()))) {
volumeList.add(AbstractModel.createSecretVolume(tlsAuth.getCertificateAndKey().getSecretName(), tlsAuth.getCertificateAndKey().getSecretName(), isOpenShift));
volumeList.add(VolumeUtils.createSecretVolume(tlsAuth.getCertificateAndKey().getSecretName(), tlsAuth.getCertificateAndKey().getSecretName(), isOpenShift));
}
} else if (authentication instanceof KafkaClientAuthenticationPlain) {
KafkaClientAuthenticationPlain passwordAuth = (KafkaClientAuthenticationPlain) authentication;
volumeList.add(AbstractModel.createSecretVolume(passwordAuth.getPasswordSecret().getSecretName(), passwordAuth.getPasswordSecret().getSecretName(), isOpenShift));
volumeList.add(VolumeUtils.createSecretVolume(passwordAuth.getPasswordSecret().getSecretName(), passwordAuth.getPasswordSecret().getSecretName(), isOpenShift));
} else if (authentication instanceof KafkaClientAuthenticationScramSha512) {
KafkaClientAuthenticationScramSha512 passwordAuth = (KafkaClientAuthenticationScramSha512) authentication;
volumeList.add(AbstractModel.createSecretVolume(passwordAuth.getPasswordSecret().getSecretName(), passwordAuth.getPasswordSecret().getSecretName(), isOpenShift));
volumeList.add(VolumeUtils.createSecretVolume(passwordAuth.getPasswordSecret().getSecretName(), passwordAuth.getPasswordSecret().getSecretName(), isOpenShift));
} else if (authentication instanceof KafkaClientAuthenticationOAuth) {
KafkaClientAuthenticationOAuth oauth = (KafkaClientAuthenticationOAuth) authentication;
volumeList.addAll(configureOauthCertificateVolumes(oauthVolumeNamePrefix, oauth.getTlsTrustedCertificates(), isOpenShift));
Expand All @@ -123,15 +123,15 @@ public static void configureClientAuthenticationVolumeMounts(KafkaClientAuthenti

// skipping if a volume mount with same Secret name was already added
if (!volumeMountList.stream().anyMatch(vm -> vm.getName().equals(tlsAuth.getCertificateAndKey().getSecretName()))) {
volumeMountList.add(AbstractModel.createVolumeMount(tlsAuth.getCertificateAndKey().getSecretName(),
volumeMountList.add(VolumeUtils.createVolumeMount(tlsAuth.getCertificateAndKey().getSecretName(),
tlsVolumeMount + tlsAuth.getCertificateAndKey().getSecretName()));
}
} else if (authentication instanceof KafkaClientAuthenticationPlain) {
KafkaClientAuthenticationPlain passwordAuth = (KafkaClientAuthenticationPlain) authentication;
volumeMountList.add(AbstractModel.createVolumeMount(passwordAuth.getPasswordSecret().getSecretName(), passwordVolumeMount + passwordAuth.getPasswordSecret().getSecretName()));
volumeMountList.add(VolumeUtils.createVolumeMount(passwordAuth.getPasswordSecret().getSecretName(), passwordVolumeMount + passwordAuth.getPasswordSecret().getSecretName()));
} else if (authentication instanceof KafkaClientAuthenticationScramSha512) {
KafkaClientAuthenticationScramSha512 passwordAuth = (KafkaClientAuthenticationScramSha512) authentication;
volumeMountList.add(AbstractModel.createVolumeMount(passwordAuth.getPasswordSecret().getSecretName(), passwordVolumeMount + passwordAuth.getPasswordSecret().getSecretName()));
volumeMountList.add(VolumeUtils.createVolumeMount(passwordAuth.getPasswordSecret().getSecretName(), passwordVolumeMount + passwordAuth.getPasswordSecret().getSecretName()));
} else if (authentication instanceof KafkaClientAuthenticationOAuth) {
KafkaClientAuthenticationOAuth oauth = (KafkaClientAuthenticationOAuth) authentication;
volumeMountList.addAll(configureOauthCertificateVolumeMounts(oauthVolumeNamePrefix, oauth.getTlsTrustedCertificates(), oauthVolumeMount));
Expand Down Expand Up @@ -211,7 +211,7 @@ public static List<Volume> configureOauthCertificateVolumes(String volumeNamePre
Map<String, String> items = Collections.singletonMap(certSecretSource.getCertificate(), "tls.crt");
String volumeName = String.format("%s-%d", volumeNamePrefix, i);

Volume vol = AbstractModel.createSecretVolume(volumeName, certSecretSource.getSecretName(), items, isOpenShift);
Volume vol = VolumeUtils.createSecretVolume(volumeName, certSecretSource.getSecretName(), items, isOpenShift);

newVolumes.add(vol);
i++;
Expand Down Expand Up @@ -239,7 +239,7 @@ public static List<VolumeMount> configureOauthCertificateVolumeMounts(String vol

for (CertSecretSource certSecretSource : trustedCertificates) {
String volumeName = String.format("%s-%d", volumeNamePrefix, i);
newVolumeMounts.add(AbstractModel.createVolumeMount(volumeName, String.format("%s/%s-%d", baseVolumeMount, certSecretSource.getSecretName(), i)));
newVolumeMounts.add(VolumeUtils.createVolumeMount(volumeName, String.format("%s/%s-%d", baseVolumeMount, certSecretSource.getSecretName(), i)));
i++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ protected List<Container> getContainers(ImagePullPolicy imagePullPolicy) {
.withReadinessProbe(ModelUtils.tlsSidecarReadinessProbe(tlsSidecar))
.withResources(tlsSidecar != null ? tlsSidecar.getResources() : null)
.withEnv(getTlsSidecarEnvVars())
.withVolumeMounts(createVolumeMount(TLS_SIDECAR_EO_CERTS_VOLUME_NAME, TLS_SIDECAR_EO_CERTS_VOLUME_MOUNT),
createVolumeMount(TLS_SIDECAR_CA_CERTS_VOLUME_NAME, TLS_SIDECAR_CA_CERTS_VOLUME_MOUNT))
.withVolumeMounts(VolumeUtils.createVolumeMount(TLS_SIDECAR_EO_CERTS_VOLUME_NAME, TLS_SIDECAR_EO_CERTS_VOLUME_MOUNT),
VolumeUtils.createVolumeMount(TLS_SIDECAR_CA_CERTS_VOLUME_NAME, TLS_SIDECAR_CA_CERTS_VOLUME_MOUNT))
.withLifecycle(new LifecycleBuilder().withNewPreStop().withNewExec()
.withCommand("/opt/stunnel/entity_operator_stunnel_pre_stop.sh")
.endExec().endPreStop().build())
Expand Down Expand Up @@ -291,8 +291,8 @@ private List<Volume> getVolumes(boolean isOpenShift) {
if (userOperator != null) {
volumeList.addAll(userOperator.getVolumes());
}
volumeList.add(createSecretVolume(TLS_SIDECAR_EO_CERTS_VOLUME_NAME, EntityOperator.secretName(cluster), isOpenShift));
volumeList.add(createSecretVolume(TLS_SIDECAR_CA_CERTS_VOLUME_NAME, AbstractModel.clusterCaCertSecretName(cluster), isOpenShift));
volumeList.add(VolumeUtils.createSecretVolume(TLS_SIDECAR_EO_CERTS_VOLUME_NAME, EntityOperator.secretName(cluster), isOpenShift));
volumeList.add(VolumeUtils.createSecretVolume(TLS_SIDECAR_CA_CERTS_VOLUME_NAME, AbstractModel.clusterCaCertSecretName(cluster), isOpenShift));
return volumeList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ public List<Volume> getVolumes() {
}

private List<VolumeMount> getVolumeMounts() {
return asList(createVolumeMount(logAndMetricsConfigVolumeName, logAndMetricsConfigMountPath),
createVolumeMount(EntityOperator.TLS_SIDECAR_EO_CERTS_VOLUME_NAME, EntityOperator.TLS_SIDECAR_EO_CERTS_VOLUME_MOUNT),
createVolumeMount(EntityOperator.TLS_SIDECAR_CA_CERTS_VOLUME_NAME, EntityOperator.TLS_SIDECAR_CA_CERTS_VOLUME_MOUNT));
return asList(VolumeUtils.createVolumeMount(logAndMetricsConfigVolumeName, logAndMetricsConfigMountPath),
VolumeUtils.createVolumeMount(EntityOperator.TLS_SIDECAR_EO_CERTS_VOLUME_NAME, EntityOperator.TLS_SIDECAR_EO_CERTS_VOLUME_MOUNT),
VolumeUtils.createVolumeMount(EntityOperator.TLS_SIDECAR_CA_CERTS_VOLUME_NAME, EntityOperator.TLS_SIDECAR_CA_CERTS_VOLUME_MOUNT));
}

public RoleBinding generateRoleBinding(String namespace, String watchedNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public List<Volume> getVolumes() {
}

private List<VolumeMount> getVolumeMounts() {
return singletonList(createVolumeMount(logAndMetricsConfigVolumeName, logAndMetricsConfigMountPath));
return singletonList(VolumeUtils.createVolumeMount(logAndMetricsConfigVolumeName, logAndMetricsConfigMountPath));
}

public RoleBinding generateRoleBinding(String namespace, String watchedNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ protected List<Volume> getVolumes(boolean isOpenShift) {
for (CertSecretSource certSecretSource : trustedCertificates) {
// skipping if a volume with same Secret name was already added
if (!volumeList.stream().anyMatch(v -> v.getName().equals(certSecretSource.getSecretName()))) {
volumeList.add(createSecretVolume(certSecretSource.getSecretName(), certSecretSource.getSecretName(), isOpenShift));
volumeList.add(VolumeUtils.createSecretVolume(certSecretSource.getSecretName(), certSecretSource.getSecretName(), isOpenShift));
}
}
}
Expand All @@ -283,7 +283,7 @@ protected List<Volume> getVolumes(boolean isOpenShift) {

protected List<VolumeMount> getVolumeMounts() {
List<VolumeMount> volumeMountList = new ArrayList<>(1);
volumeMountList.add(createVolumeMount(logAndMetricsConfigVolumeName, logAndMetricsConfigMountPath));
volumeMountList.add(VolumeUtils.createVolumeMount(logAndMetricsConfigVolumeName, logAndMetricsConfigMountPath));

if (tls != null) {
List<CertSecretSource> trustedCertificates = tls.getTrustedCertificates();
Expand All @@ -292,7 +292,7 @@ protected List<VolumeMount> getVolumeMounts() {
for (CertSecretSource certSecretSource : trustedCertificates) {
// skipping if a volume mount with same Secret name was already added
if (!volumeMountList.stream().anyMatch(vm -> vm.getName().equals(certSecretSource.getSecretName()))) {
volumeMountList.add(createVolumeMount(certSecretSource.getSecretName(),
volumeMountList.add(VolumeUtils.createVolumeMount(certSecretSource.getSecretName(),
TLS_CERTS_BASE_VOLUME_MOUNT + certSecretSource.getSecretName()));
}
}
Expand Down
Loading

0 comments on commit f033271

Please sign in to comment.