Skip to content

Commit

Permalink
Fix #624: Unable to override Image Name in Simple Dockerfile Mode wit…
Browse files Browse the repository at this point in the history
…h `jkube.generator.name`

Modify Mojos logic to consider image name configured from properties
  • Loading branch information
rohanKanojia authored and manusa committed Mar 31, 2021
1 parent d12f920 commit 0a1b6b1
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Usage:
* Fix #450: Quarkus port is inferred from application.properties/yaml (considers profile too)
* Fix #471: Remove the declaration of thrown runtime exceptions across javadoc
* Fix #620: Added k8s support for NetworkPolicy
* Fix #624: Unable to override Image Name in Simple Dockerfile Mode with `jkube.generator.name`

### 1.1.1 (2021-02-23)
* Fix #570: Disable namespace creation during k8s:resource with `jkube.namespace` flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
* @author roland
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package org.eclipse.jkube.kit.common.util;

import io.fabric8.kubernetes.client.utils.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -39,4 +41,23 @@ public static Properties getPropertiesFromResource(URL resource) {
}
return ret;
}

/**
* Return first Non Null set property from a set of provided properties
*
* @param properties {@link Properties} in a given project
* @param keys an array of property key values to find
* @return a string which is first non null value found for the provided list
*/
public static String getValueFromProperties(Properties properties, String... keys) {
for (String property : keys) {
if (properties.containsKey(property)) {
String value = properties.get(property).toString();
if (Utils.isNotNullOrEmpty(value)) {
return value;
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getValueFromProperties;

public class PropertiesUtilTest {

Expand All @@ -47,4 +46,31 @@ public void testNonExistentPropertiesParsing() {
// Then
assertThat(result).isEmpty();
}

@Test
public void testGetValueFromProperties() {
// Given
Properties properties = new Properties();
String[] keys = new String[] {"property1", "property2"};

// When
String imageName = getValueFromProperties(properties, keys);

// Then
assertThat(imageName).isNull();
}

@Test
public void testGetValueFromPropertiesReturnsValidValue() {
// Given
Properties properties = new Properties();
properties.put("property1", "value1");
String[] keys = new String[] {"property1", "property2"};

// When
String imageName = getValueFromProperties(properties, keys);

// Then
assertThat(imageName).isNotNull().isEqualTo("value1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.kit.build.core.GavLabel;
import org.eclipse.jkube.kit.build.api.helper.DockerFileUtil;
import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy;
import org.eclipse.jkube.kit.config.image.build.JKubeConfiguration;
import org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory;
Expand Down Expand Up @@ -94,7 +93,12 @@
import org.fusesource.jansi.Ansi;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;

import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.addSimpleDockerfileConfig;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.createSimpleDockerfileConfig;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.getTopLevelDockerfile;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.isSimpleDockerFileMode;
import static org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory.DockerAccessContext.DEFAULT_MAX_CONNECTIONS;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getValueFromProperties;
import static org.eclipse.jkube.maven.plugin.mojo.build.AbstractJKubeMojo.DEFAULT_LOG_PREFIX;

public abstract class AbstractDockerMojo extends AbstractMojo
Expand Down Expand Up @@ -789,6 +793,7 @@ protected void executeBuildPlugins() {

// Resolve and customize image configuration
protected String initImageConfiguration(Date buildTimeStamp) throws DependencyResolutionRequiredException {
ImageNameFormatter imageNameFormatter = new ImageNameFormatter(MavenUtil.convertMavenProjectToJKubeProject(project, session), buildTimeStamp);
// Resolve images
resolvedImages = ConfigHelper.resolveImages(
log,
Expand All @@ -805,17 +810,19 @@ protected String initImageConfiguration(Date buildTimeStamp) throws DependencyRe
this); // customizer (can be overwritten by a subclass)

// Check for simple Dockerfile mode
if (DockerFileUtil.isSimpleDockerFileMode(project.getBasedir())) {
File topDockerfile = DockerFileUtil.getTopLevelDockerfile(project.getBasedir());
if (isSimpleDockerFileMode(project.getBasedir())) {
File topDockerfile = getTopLevelDockerfile(project.getBasedir());
String defaultImageName = imageNameFormatter.format(getValueFromProperties(project.getProperties(),
"jkube.image.name", "jkube.generator.name"));
if (resolvedImages.isEmpty()) {
resolvedImages.add(DockerFileUtil.createSimpleDockerfileConfig(topDockerfile, MavenUtil.getPropertiesWithSystemOverrides(project).getProperty("jkube.image.name")));
resolvedImages.add(createSimpleDockerfileConfig(topDockerfile, defaultImageName));
} else if (resolvedImages.size() == 1 && resolvedImages.get(0).getBuildConfiguration() == null) {
resolvedImages.set(0, DockerFileUtil.addSimpleDockerfileConfig(resolvedImages.get(0), topDockerfile));
resolvedImages.set(0, addSimpleDockerfileConfig(resolvedImages.get(0), topDockerfile));
}
}

// Initialize configuration and detect minimal API version
return ConfigHelper.initAndValidate(resolvedImages, apiVersion, new ImageNameFormatter(MavenUtil.convertMavenProjectToJKubeProject(project, session), buildTimeStamp));
return ConfigHelper.initAndValidate(resolvedImages, apiVersion, imageNameFormatter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import javax.validation.ConstraintViolationException;

import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.kit.build.api.helper.DockerFileUtil;
import org.eclipse.jkube.kit.build.service.docker.config.handler.ImageConfigResolver;
import org.eclipse.jkube.kit.build.service.docker.helper.ConfigHelper;
import org.eclipse.jkube.kit.build.service.docker.helper.ImageNameFormatter;
Expand Down Expand Up @@ -67,7 +66,12 @@
import org.apache.maven.shared.filtering.MavenFileFilter;
import org.apache.maven.shared.filtering.MavenFilteringException;

import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.addSimpleDockerfileConfig;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.createSimpleDockerfileConfig;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.getTopLevelDockerfile;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.isSimpleDockerFileMode;
import static org.eclipse.jkube.kit.common.ResourceFileType.yaml;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getValueFromProperties;
import static org.eclipse.jkube.kit.common.util.ResourceMojoUtil.DEFAULT_RESOURCE_LOCATION;
import static org.eclipse.jkube.kit.common.util.ResourceMojoUtil.useDekorate;
import static org.eclipse.jkube.maven.plugin.mojo.build.BuildMojo.CONTEXT_KEY_BUILD_TIMESTAMP;
Expand Down Expand Up @@ -387,18 +391,21 @@ private List<ImageConfiguration> getResolvedImages(List<ImageConfiguration> imag
});

Date now = getBuildReferenceDate();
ImageNameFormatter imageNameFormatter = new ImageNameFormatter(MavenUtil.convertMavenProjectToJKubeProject(project, session), now);
storeReferenceDateInPluginContext(now);
// Check for simple Dockerfile mode
if (DockerFileUtil.isSimpleDockerFileMode(project.getBasedir())) {
File topDockerfile = DockerFileUtil.getTopLevelDockerfile(project.getBasedir());
if (isSimpleDockerFileMode(project.getBasedir())) {
File topDockerfile = getTopLevelDockerfile(project.getBasedir());
String defaultImageName = imageNameFormatter.format(getValueFromProperties(project.getProperties(),
"jkube.image.name", "jkube.generator.name"));
if (ret.isEmpty()) {
ret.add(DockerFileUtil.createSimpleDockerfileConfig(topDockerfile, MavenUtil.getPropertiesWithSystemOverrides(project).getProperty("jkube.image.name")));
ret.add(createSimpleDockerfileConfig(topDockerfile, defaultImageName));
} else if (ret.size() == 1 && ret.get(0).getBuildConfiguration() == null) {
ret.set(0, DockerFileUtil.addSimpleDockerfileConfig(resolvedImages.get(0), topDockerfile));
ret.set(0, addSimpleDockerfileConfig(resolvedImages.get(0), topDockerfile));
}
}
String minimalApiVersion = ConfigHelper.initAndValidate(ret, null /* no minimal api version */,
new ImageNameFormatter(MavenUtil.convertMavenProjectToJKubeProject(project, session), now));
imageNameFormatter);
return ret;
}

Expand Down

0 comments on commit 0a1b6b1

Please sign in to comment.