Skip to content

Commit

Permalink
Fix fabric8io#1312 : Container name should not be generated from mave…
Browse files Browse the repository at this point in the history
…n group Id
  • Loading branch information
rohanKanojia committed Jul 14, 2018
1 parent a8bf934 commit 2f93b35
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ We use semantic versioning in some slight variation until our feature set has st

After this we will switch probably to real [Semantic Versioning 2.0.0](http://semver.org/)

###3.5.41
* Feature 1312: Container name should not be generated from maven group id

###3.5.40
* Feature 1264: Added `osio` profile, with enricher to apply OpenShift.io space labels to resources
* Feature 1291: Added ImageStream triggers for StatefulSets, ReplicaSets and DaemonSets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public class KubernetesResourceUtil {

public static final HashSet<Class<?>> SIMPLE_FIELD_TYPES = new HashSet<>();

public static final String CONTAINER_NAME_REGEX = "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$";

protected static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";


Expand Down Expand Up @@ -402,7 +404,17 @@ public static String extractContainerName(MavenProject project, ImageConfigurati
private static String extractImageUser(String image, MavenProject project) {
ImageName name = new ImageName(image);
String imageUser = name.getUser();
return imageUser != null ? imageUser : project.getGroupId();
String projectGroupId = project.getGroupId();
if(imageUser != null) {
return imageUser;
} else {
if(projectGroupId == null || projectGroupId.matches(CONTAINER_NAME_REGEX)) {
return projectGroupId;
}
else {
return projectGroupId.replaceAll("[^a-zA-Z0-9]", "");
}
}
}

public static Map<String, String> removeVersionSelector(Map<String, String> selector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;

import io.fabric8.maven.docker.config.ImageConfiguration;
import mockit.Expectations;
import mockit.Mocked;
import org.apache.maven.project.MavenProject;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -45,6 +49,9 @@ public class KubernetesResourceUtilTest {

private static File fabric8Dir;

@Mocked
final MavenProject project = new MavenProject();

@BeforeClass
public static void initPath() throws UnsupportedEncodingException {
ClassLoader classLoader = KubernetesResourceUtil.class.getClassLoader();
Expand Down Expand Up @@ -143,6 +150,25 @@ public void invalidExtension() throws IOException {
}
}

@Test
public void containerName() {
new Expectations() {{
project.getGroupId();
result = "io.fabric8";

project.getArtifactId();
result = "fabric8-maven-plugin-dummy";
}};

ImageConfiguration imageConfiguration = new ImageConfiguration.Builder()
.name("dummy-image")
.registry("example.com/someregistry")
.name("test")
.build();
String containerName = KubernetesResourceUtil.extractContainerName(project, imageConfiguration);
assertTrue(containerName.matches(KubernetesResourceUtil.CONTAINER_NAME_REGEX));
}

@Test
public void readWholeDir() throws IOException {
ResourceVersioning v = new ResourceVersioning()
Expand Down

0 comments on commit 2f93b35

Please sign in to comment.