Skip to content

Commit

Permalink
fix (jkube-kit) : Generate yaml files for different environments (ecl…
Browse files Browse the repository at this point in the history
…ipse-jkube#1218

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed May 2, 2022
1 parent 81a8b28 commit 53a4429
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ protected ClusterConfiguration initClusterConfiguration() {
System.getProperties(), kubernetesExtension.javaProject.getProperties()).build();
}

protected final File resolveResourceSourceDirectory() {
return ResourceUtil.getFinalResourceDir(kubernetesExtension.getResourceSourceDirectoryOrDefault(),
protected final List<File> resolveResourceSourceDirectory() {
return ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(),
kubernetesExtension.getResourceEnvironmentOrNull());
}


protected ProcessorConfig extractGeneratorConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, kubernetesExtension.getProfileOrNull(), kubernetesExtension.getResourceTargetDirectoryOrDefault(), kubernetesExtension.generator);
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.generator);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() {
}
final ResourceServiceConfig resourceServiceConfig = ResourceServiceConfig.builder()
.project(kubernetesExtension.javaProject)
.resourceDir(resolveResourceSourceDirectory())
.resourceDirs(resolveResourceSourceDirectory())
.targetDir(kubernetesExtension.getResourceTargetDirectoryOrDefault())
.resourceFileType(kubernetesExtension.getResourceFileTypeOrDefault())
.resourceConfig(resourceConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jkube.gradle.plugin.task;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.common.util.ResourceUtil;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;

import javax.inject.Inject;
Expand All @@ -41,10 +40,9 @@ public void run() {
.filter(s -> !s.isEmpty())
.orElse(null))
.build();
final File environmentResourceDir = ResourceUtil.getFinalResourceDir(resolveResourceSourceDirectory(),
kubernetesExtension.getResourceEnvironmentOrNull());
final List<File> environmentResourceDirs = resolveResourceSourceDirectory();
jKubeServiceHub.getUndeployService()
.undeploy(environmentResourceDir, resources, findManifestsToUndeploy().toArray(new File[0]));
.undeploy(environmentResourceDirs, resources, findManifestsToUndeploy().toArray(new File[0]));
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private WatcherContext createWatcherContext() throws IOException {

private ProcessorConfig extractWatcherConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.WATCHER_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDir(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.watcher);
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.WATCHER_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.watcher);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract watcher config: " + e, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Collections;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.gradle.plugin.TestKubernetesExtension;
Expand Down Expand Up @@ -91,8 +92,8 @@ public void runTask_withOfflineTrue_shouldUndeployResources() throws IOException
assertThat(kubernetesUndeployServiceMockedConstruction.constructed()).hasSize(1);
verify(kubernetesUndeployServiceMockedConstruction.constructed().iterator().next(), times(1))
.undeploy(
taskEnvironment.getRoot().toPath().resolve(Paths.get("src", "main", "jkube"))
.toFile(),
Collections.singletonList(taskEnvironment.getRoot().toPath().resolve(Paths.get("src", "main", "jkube"))
.toFile()),
ResourceConfig.builder().build(), taskEnvironment.getRoot().toPath()
.resolve(Paths.get("build", "classes", "java", "main", "META-INF", "jkube", "kubernetes.yml")).toFile()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;

import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension;
Expand Down Expand Up @@ -91,8 +92,8 @@ public void runTask_withOfflineTrue_shouldUndeployResources() throws IOException
assertThat(openshiftUndeployServiceMockedConstruction.constructed()).hasSize(1);
verify(openshiftUndeployServiceMockedConstruction.constructed().iterator().next(), times(1))
.undeploy(
taskEnvironment.getRoot().toPath().resolve(Paths.get("src", "main", "jkube"))
.toFile(),
Collections.singletonList(taskEnvironment.getRoot().toPath().resolve(Paths.get("src", "main", "jkube"))
.toFile()),
ResourceConfig.builder().build(), taskEnvironment.getRoot().toPath()
.resolve(Paths.get("build", "classes", "java", "main", "META-INF", "jkube", "openshift.yml")).toFile(),
taskEnvironment.getRoot().toPath().resolve(Paths.get("build", "test-project-is.yml")).toFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,18 @@ private static ObjectMapper getObjectMapper(ResourceFileType resourceFileType) {
.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
}

public static File getFinalResourceDir(File resourceDir, String environment) {
if (resourceDir != null && StringUtils.isNotEmpty(environment)) {
return new File(resourceDir, environment);
public static List<File> getFinalResourceDirs(File resourceDir, String environmentAsCommaSeparateStr) {
List<File> resourceDirs = new ArrayList<>();

if (resourceDir != null && StringUtils.isNotEmpty(environmentAsCommaSeparateStr)) {
String[] environments = environmentAsCommaSeparateStr.split(",");
for (String environment : environments) {
resourceDirs.add(new File(resourceDir, environment.trim()));
}
} else if (StringUtils.isEmpty(environmentAsCommaSeparateStr)) {
resourceDirs.add(resourceDir);
}
return resourceDir;
return resourceDirs;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.kit.config.resource;

import java.io.File;
import java.util.List;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.ResourceFileType;
Expand All @@ -35,7 +36,7 @@
public class ResourceServiceConfig {

private JavaProject project;
private File resourceDir;
private List<File> resourceDirs;
private File targetDir;
private ResourceFileType resourceFileType;
private ResourceConfig resourceConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

public interface UndeployService {

void undeploy(File resourceDir, ResourceConfig resourceConfig, File... manifestFiles) throws IOException;
void undeploy(List<File> resourceDir, ResourceConfig resourceConfig, File... manifestFiles) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public KubernetesUndeployService(JKubeServiceHub jKubeServiceHub, KitLogger logg
}

@Override
public void undeploy(File resourceDir, ResourceConfig resourceConfig, File... manifestFiles) throws IOException {
public void undeploy(List<File> resourceDirs, ResourceConfig resourceConfig, File... manifestFiles) throws IOException {
final String fallbackNamespace = KubernetesClientUtil.resolveFallbackNamespace(resourceConfig, jKubeServiceHub.getClusterAccess());
final List<File> manifests = Stream.of(manifestFiles)
.filter(Objects::nonNull).filter(File::exists).filter(File::isFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,51 @@ private ProfileUtil() {}
* 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, File)}</li>
* <li>A given directory with the name profiles.yml (and variations, {@link #findProfile(String, List)}</li>
* </ul>
* @param profileArg the profile's name
* @param resourceDir a directory to check for profiles.
* @param resourceDirs a directory to check for profiles.
* @return the profile found or the default profile if none of this name is given
* @throws IOException
*/
public static Profile findProfile(String profileArg, List<File> resourceDirs) throws IOException {
if (resourceDirs != null) {
for (File resourceDir : resourceDirs) {
Profile profile = findProfile(profileArg, resourceDir);
if (profile != null) {
return profile;
}
}
}
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);
if (profileFound != null) {
if(profileFound.getParentProfile() != null) {
profileFound = inheritFromParentProfile(profileFound, resourceDir);
log.info(profileFound + " inheriting resources from " + profileFound.getParentProfile());
}
return profileFound;
} else {
throw new IllegalArgumentException("No profile '" + profile + "' defined");
}
profileFound = checkParentProfileAndInherit(profileFound, resourceDir);
return profileFound;
} 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 All @@ -94,17 +114,17 @@ private static Profile inheritFromParentProfile(Profile aProfile, File resourceD
*
* @param configExtractor how to extract the config from a profile when found
* @param profile the profile name (can be null, then no profile is used)
* @param resourceDir resource directory where to lookup the profile (in addition to a classpath lookup)
* @param resourceDirs resource directory where to lookup the profile (in addition to a classpath lookup)
* @return the merged configuration which can be empty if no profile is given
* @param config the provided configuration
* @throws IOException
*/
public static ProcessorConfig blendProfileWithConfiguration(ProcessorConfigurationExtractor configExtractor,
String profile,
File resourceDir,
List<File> resourceDirs,
ProcessorConfig config) throws IOException {
// Get specified profile or the default profile
ProcessorConfig profileConfig = extractProcesssorConfiguration(configExtractor, profile, resourceDir);
ProcessorConfig profileConfig = extractProcesssorConfiguration(configExtractor, profile, resourceDirs);

return ProcessorConfig.mergeProcessorConfigs(config, profileConfig);
}
Expand Down Expand Up @@ -140,8 +160,8 @@ public static Profile lookup(String name, File directory) throws IOException {

private static ProcessorConfig extractProcesssorConfiguration(ProcessorConfigurationExtractor extractor,
String profile,
File resourceDir) throws IOException {
Profile profileFound = findProfile(profile, resourceDir);
List<File> resourceDirs) throws IOException {
Profile profileFound = findProfile(profile, resourceDirs);
return extractor.extract(profileFound);
}

Expand Down Expand Up @@ -193,6 +213,16 @@ public static List<Profile> readAllFromClasspath(String name, String ext) throws

// ================================================================================

private static File findProfileYaml(List<File> directories) {
for (File directory : directories) {
File ret = findProfileYaml(directory);
if (ret != null && ret.exists()) {
return ret;
}
}
return null;
}

// check for various variations of profile files
private static File findProfileYaml(File directory) {
for (String profileFile : PROFILE_FILENAMES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public void findProfile_whenValidProfileArg_returnsValidProfile() throws URISynt
@Test
public void findProfile_whenNonExistentProfileArg_throwsException () throws URISyntaxException {

File profileDir = getProfileDir();
List<File> profileDirs = Collections.singletonList(getProfileDir());

IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> ProfileUtil.findProfile("not-there", profileDir));
IllegalArgumentException illegalArgumentException = assertThrows(IllegalArgumentException.class, () -> ProfileUtil.findProfile("not-there", profileDirs));

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

Expand All @@ -115,18 +115,18 @@ public void blendProfiles() throws Exception {

ProcessorConfig origConfig = new ProcessorConfig(Arrays.asList("i1", "i2"), Collections.singleton("spring.swarm"), null);
ProcessorConfig mergeConfig = ProfileUtil.blendProfileWithConfiguration(ProfileUtil.ENRICHER_CONFIG,
"simple",
getProfileDir(),
origConfig);
"simple",
Collections.singletonList(getProfileDir()),
origConfig);
assertTrue(mergeConfig.use("base"));
assertTrue(mergeConfig.use("i1"));
assertEquals("http://jolokia.org", mergeConfig.getConfig().get("base").get("url"));


mergeConfig = ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG,
"simple",
getProfileDir(),
origConfig);
"simple",
Collections.singletonList(getProfileDir()),
origConfig);
assertTrue(mergeConfig.use("i2"));
assertFalse(mergeConfig.use("spring.swarm"));

Expand Down

0 comments on commit 53a4429

Please sign in to comment.