Skip to content

Commit

Permalink
refactor: reviewed YAML generation for multiple environments
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa committed May 24, 2022
1 parent 8a0b216 commit 8c369a9
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void run() {
kubernetesExtension.helm).build();
jKubeServiceHub.getHelmService().generateHelmCharts(helm);
} catch (IOException exception) {
throw new IllegalStateException(exception.getMessage());
throw new IllegalStateException(exception.getMessage(), exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.MockedConstruction;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -58,7 +59,9 @@ public void runTask_withNoTemplateDir_shouldThrowException() {

// Then
assertThat(illegalStateException)
.hasMessageContaining("META-INF/jkube/kubernetes (No such file or directory)");
.hasMessageContaining("META-INF/jkube/kubernetes")
.getCause()
.isInstanceOf(NoSuchFileException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
*/
package org.eclipse.jkube.gradle.plugin.task;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.gradle.plugin.TestKubernetesExtension;
import org.eclipse.jkube.kit.resource.helm.HelmService;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.MockedConstruction;

import java.io.IOException;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -57,7 +59,9 @@ public void runTask_withNoTemplateDir_shouldThrowException() {

// Then
assertThat(illegalStateException)
.hasMessageContaining("META-INF/jkube/kubernetes (No such file or directory)");
.hasMessageContaining("META-INF/jkube/kubernetes")
.getCause()
.isInstanceOf(NoSuchFileException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.mockito.MockedConstruction;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -57,7 +58,9 @@ public void runTask_withNoTemplateDir_shouldThrowException() {

// Then
assertThat(illegalStateException)
.hasMessageContaining("META-INF/jkube/openshift (No such file or directory)");
.hasMessageContaining("META-INF/jkube/openshift")
.getCause()
.isInstanceOf(NoSuchFileException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.mockito.MockedConstruction;

import java.io.IOException;
import java.nio.file.NoSuchFileException;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertThrows;
Expand Down Expand Up @@ -56,7 +57,9 @@ public void runTask_withNoTemplateDir_shouldThrowException() {

// Then
assertThat(illegalStateException)
.hasMessageContaining("META-INF/jkube/openshift (No such file or directory)");
.hasMessageContaining("META-INF/jkube/openshift")
.getCause()
.isInstanceOf(NoSuchFileException.class);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import io.fabric8.openshift.api.model.Build;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.DeploymentConfigSpec;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;


Expand Down Expand Up @@ -672,13 +671,9 @@ public static boolean removeEnvVar(List<EnvVar> envVarList, String name) {
* @return file if present or null
*/
public static File getResourceFragmentFromSource(File resourceDirFinal, List<String> remotes, String resourceNameSuffix, KitLogger log) {
File[] resourceFiles = listResourceFragments(resourceDirFinal, remotes, log);

if (resourceFiles != null) {
for (File file : resourceFiles) {
if (file.getName().endsWith(resourceNameSuffix)) {
return file;
}
for (File file : listResourceFragments(remotes, log, resourceDirFinal)) {
if (file.getName().endsWith(resourceNameSuffix)) {
return file;
}
}
return null;
Expand All @@ -700,16 +695,26 @@ public static Map<String, Quantity> getQuantityFromString(Map<String, String> qu
return stringQuantityMap;
}

public static File[] listResourceFragments(File localResourceDir, List<String> remotes, KitLogger log) {
File[] resourceFiles = listResourceFragments(localResourceDir);
public static File[] listResourceFragments(List<String> remotes, KitLogger log, List<File> resourceDirs) {
return listResourceFragments(remotes, log, resourceDirs.stream().filter(Objects::nonNull).toArray(File[]::new));
}

public static File[] listResourceFragments(List<String> remotes, KitLogger log, File... resourceDirs) {
final List<File> resourceFiles = new ArrayList<>();
for (File resourceDir : resourceDirs) {
final File[] resourceFragments = listResourceFragments(resourceDir);
if (resourceFragments != null) {
Collections.addAll(resourceFiles, resourceFragments);
}
}

if(remotes != null) {
File[] remoteResourceFiles = listRemoteResourceFragments(remotes, log);
if (remoteResourceFiles.length > 0) {
resourceFiles = ArrayUtils.addAll(resourceFiles, remoteResourceFiles);
Collections.addAll(resourceFiles, remoteResourceFiles);
}
}
return resourceFiles;
return resourceFiles.toArray(new File[0]);
}

public static File[] listResourceFragments(File resourceDir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
package org.eclipse.jkube.kit.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -77,7 +77,7 @@ public static List<HasMetadata> deserializeKubernetesListOrTemplate(File manifes
return Collections.emptyList();
}
final List<HasMetadata> kubernetesResources = new ArrayList<>();
try (InputStream fis = new FileInputStream(manifest)) {
try (InputStream fis = Files.newInputStream(manifest.toPath())) {
kubernetesResources.addAll(split(Serialization.unmarshal(fis, Collections.emptyMap())));
}
return kubernetesResources;
Expand All @@ -104,7 +104,7 @@ private static List<HasMetadata> split(Object resource) throws IOException {
}

public static <T extends KubernetesResource> T load(File file, Class<T> clazz) throws IOException {
return Serialization.unmarshal(new FileInputStream(file), clazz);
return Serialization.unmarshal(Files.newInputStream(file.toPath()), clazz);
}

public static File save(File file, Object data) throws IOException {
Expand Down Expand Up @@ -137,7 +137,7 @@ private static ObjectMapper getObjectMapper(ResourceFileType resourceFileType) {
public static List<File> getFinalResourceDirs(File resourceDir, String environmentAsCommaSeparateStr) {
List<File> resourceDirs = new ArrayList<>();

if (resourceDir != null && StringUtils.isNotEmpty(environmentAsCommaSeparateStr)) {
if (resourceDir != null && StringUtils.isNotBlank(environmentAsCommaSeparateStr)) {
String[] environments = environmentAsCommaSeparateStr.split(",");
for (String environment : environments) {
resourceDirs.add(new File(resourceDir, environment.trim()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void testListResourceFragments() {
File localResourceDir = new File(getClass().getResource("/util/fragments").getPath());

// When & Then
assertLocalFragments(KubernetesHelper.listResourceFragments(localResourceDir, null, logger), 2);
assertLocalFragments(KubernetesHelper.listResourceFragments(null, logger, localResourceDir), 2);
}

@Test
Expand All @@ -91,7 +91,7 @@ public void testResourceFragmentsWithRemotes() {
File localResourceDir = new File(getClass().getResource("/util/fragments").getPath());

// When
File[] fragments = KubernetesHelper.listResourceFragments(localResourceDir, remoteStrList, logger);
File[] fragments = KubernetesHelper.listResourceFragments(remoteStrList, logger, localResourceDir);

// Then
assertLocalFragments(fragments, 4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -48,55 +48,40 @@ private ProfileUtil() {}
// Default profile which will be always there
public static final String DEFAULT_PROFILE = "default";

public static Profile findProfile(String profileArg, List<File> resourceDirs) throws IOException {
return findProfile(profileArg, resourceDirs == null ? new File[0] : resourceDirs.toArray(new File[0]));
}

/**
* Find a profile. Profiles are looked up at various locations:
*
* <ul>
* <li>A given directory with the name profiles.yml (and variations, {@link #findProfile(String, List)}</li>
* <li>A given directory with the name profiles.yml (and variations, {@link #findProfile(String, File[])}</li>
* </ul>
* @param profileArg the profile's name
* @param resourceDirs a directory to check for profiles.
* @param resourceDirs directories to check for profiles.
* @return the profile found or the default profile if none of this name is given
* @throws IOException
* @throws IOException if there's a problem while performing IO operations.
*/
public static Profile findProfile(String profileArg, List<File> resourceDirs) throws IOException {
if (resourceDirs != null) {
public static Profile findProfile(String profileArg, File... resourceDirs) throws IOException {
try {
final String profile = profileArg == null ? DEFAULT_PROFILE : profileArg;
for (File resourceDir : resourceDirs) {
Profile profile = findProfile(profileArg, resourceDir);
if (profile != null) {
return profile;
Profile profileFound = lookup(profile, resourceDir);
if (profileFound != null) {
if (profileFound.getParentProfile() != null) {
profileFound = inheritFromParentProfile(profileFound, resourceDir);
log.info("{} inheriting resources from {}", profileFound, profileFound.getParentProfile());
}
return profileFound;
}
}
}
String profile = profileArg == null ? DEFAULT_PROFILE : profileArg;
Profile defaultLookupProfile = lookup(profile, null);
defaultLookupProfile = checkParentProfileAndInherit(defaultLookupProfile, null);
if (defaultLookupProfile != null) {
return defaultLookupProfile;
}

throw new IllegalArgumentException("No profile '" + profile + "' defined");
}

public static Profile findProfile(String profileArg, File resourceDir) throws IOException {
try {
String profile = profileArg == null ? DEFAULT_PROFILE : profileArg;
Profile profileFound = lookup(profile, resourceDir);
profileFound = checkParentProfileAndInherit(profileFound, resourceDir);
return profileFound;
throw new IllegalArgumentException("No profile '" + profile + "' defined");
} catch (IOException e) {
throw new IOException("Error while looking up profile " + profileArg + ": " + e.getMessage(),e);
}
}

private static Profile checkParentProfileAndInherit(Profile profile, File resourceDir) throws IOException {
if (profile != null && profile.getParentProfile() != null) {
profile = inheritFromParentProfile(profile, resourceDir);
log.info("%s inheriting resources from %s", profile, profile.getParentProfile());
}
return profile;
}

private static Profile inheritFromParentProfile(Profile aProfile, File resourceDir) throws IOException {
Profile aParentProfile = lookup(aProfile.getParentProfile(), resourceDir);
if(aParentProfile != null) {
Expand Down Expand Up @@ -145,7 +130,7 @@ public static Profile lookup(String name, File directory) throws IOException {

File profileFile = findProfileYaml(directory);
if (profileFile != null) {
List<Profile> fileProfiles = fromYaml(new FileInputStream(profileFile));
List<Profile> fileProfiles = fromYaml(Files.newInputStream(profileFile.toPath()));
for (Profile profile : fileProfiles) {
if (profile.getName().equals(name)) {
profiles.add(profile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ public File getProfileDir() throws URISyntaxException {

@Test
public void findProfile_whenValidProfileArg_returnsValidProfile() throws URISyntaxException, IOException {

assertNotNull(ProfileUtil.findProfile("simple", getProfileDir()));

}

@Test
Expand All @@ -100,7 +98,16 @@ public void findProfile_whenNonExistentProfileArg_throwsException () throws URIS
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> ProfileUtil.findProfile("not-there", profileDirs));

assertTrue(illegalArgumentException.getMessage().contains("not-there"));
}

@Test
public void findProfile_whenProfileUsedWithInvalidParent_thenThrowsException() throws URISyntaxException {
// Given
File profileDir = getProfileDir();
// When
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> ProfileUtil.findProfile("invalid-parent", profileDir));
// Then
assertEquals("No parent profile 'i-dont-exist' defined", illegalArgumentException.getMessage());
}

@Test
Expand Down Expand Up @@ -144,16 +151,4 @@ public void shouldExtend() throws Exception {
assertTrue(aProfile.getGeneratorConfig().use("spring.swarm"));
assertFalse(aProfile.getGeneratorConfig().use("java.app"));
}

@Test
public void findProfile_whenProfileUsedWithInvalidParent_thenThrowsException() throws URISyntaxException {
// Given
File profileDir = getProfileDir();

// When
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> ProfileUtil.findProfile("invalid-parent", profileDir));

// Then
assertEquals("No parent profile 'i-dont-exist' defined", illegalArgumentException.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.validation.ConstraintViolationException;
Expand Down Expand Up @@ -83,7 +82,10 @@ private KubernetesListBuilder generateAppResources(PlatformMode platformMode, En

final ResourceConfig resourceConfig = resourceServiceConfig.getResourceConfig();
try {
File[] resourceFiles = aggregateResourceFragments(resourceServiceConfig.getResourceDirs(), resourceConfig, log);
File[] resourceFiles = listResourceFragments(
resourceConfig != null ? resourceConfig.getRemotes() : null, log, resourceServiceConfig.getResourceDirs());
resourceServiceConfig.getResourceDirs()
.forEach(resourceDir -> log.info("Using resource templates from %s", resourceDir));
final File[] processedResource = processResourceFiles(resourceFiles);
KubernetesListBuilder builder = processResourceFragments(platformMode, processedResource);

Expand Down Expand Up @@ -150,16 +152,4 @@ private File[] processResourceFiles(File[] resourceFiles) throws IOException {
return resourceFiles;
}

private File[] aggregateResourceFragments(List<File> resourceDirs, ResourceConfig resourceConfig, KitLogger log) {
List<File> fragments = new ArrayList<>();
for (File resourceDir : resourceDirs) {
log.info("Using resource templates from %s", resourceDir);
File[] resourceFiles = listResourceFragments(resourceDir, resourceConfig !=null ? resourceConfig.getRemotes() : null, log);
if (resourceFiles != null && resourceFiles.length > 0) {
fragments.addAll(Arrays.asList(resourceFiles));
}
}
return fragments.toArray(new File[0]);
}

}
Loading

0 comments on commit 8c369a9

Please sign in to comment.