Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #273: Enabling custom naming of the app label in deployment descriptor #325

Merged
merged 5 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Usage:
```
### 1.0.0-SNAPSHOT
* Fix #290: Bump Fabric8 Kubernetes Client to v4.10.3
* Fix #273: Added new parameter to allow for custom _app_ name

### 1.0.0-rc-1 (2020-07-23)
* Fix #252: Replace Quarkus Native Base image with ubi-minimal (same as in `Dockerfile.native`)
Expand All @@ -47,7 +48,7 @@ Usage:
* Fix #306: Template resolution and helm work in OpenShift-Maven-Plugin

### 1.0.0-alpha-4 (2020-06-08)
* Fix #173: Use OpenShift compliant git/vcs annotations
* Fix #173: Use OpenShift compliant git/vcs annotations
* Fix #182: Assembly is never null
* Fix #184: IngressEnricher seems to be broken, Port of fabric8io/fabric8-maven-plugin#1730
* Fix #198: Wildfly works in OpenShift with S2I binary build (Docker)
Expand Down Expand Up @@ -92,7 +93,7 @@ Usage:
* Fix #97: Port of fabric8io/fabric8-maven-plugin#1794 to fix ImageChange triggers not being set in DeploymentConfig when resource fragments are used
* Ported PR fabric8io/fabric8-maven-plugin#1802, Labels are missing for some objects
* Ported PR fabric8io/fabric8-maven-plugin#1805, NullPointerException in ConfigMapEnricher
* Ported PR fabric8io/fabric8-maven-plugin#1772, Support for setting BuildConfig memory/cpu request and limits
* Ported PR fabric8io/fabric8-maven-plugin#1772, Support for setting BuildConfig memory/cpu request and limits
* Fix #112: Fix windows specific path error while splitting file path
* Fix #102: HelmMojo works again
* Fix #120: Critical bugs reported by Sonar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public class ProjectLabelEnricher extends BaseEnricher {

@AllArgsConstructor
private enum Config implements Configs.Config {
USE_PROJECT_LABEL("useProjectLabel", "false");
USE_PROJECT_LABEL("useProjectLabel", "false"),
APP("app", null);

@Getter
protected String key;
Expand Down Expand Up @@ -160,8 +161,7 @@ private Map<String, String> createLabels(boolean withoutVersion) {
if (enableProjectLabel) {
ret.put("project", groupArtifactVersion.getArtifactId());
} else {
// default label is app
ret.put("app", groupArtifactVersion.getArtifactId());
ret.put("app", getConfig(Config.APP, groupArtifactVersion.getArtifactId()));
}

ret.put("group", groupArtifactVersion.getGroupId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.enricher.generic;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.util.Map;
import java.util.Properties;

import org.eclipse.jkube.kit.config.resource.GroupArtifactVersion;
import org.eclipse.jkube.kit.config.resource.PlatformMode;
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext;
import org.junit.Before;
import org.junit.Test;

import io.fabric8.kubernetes.api.model.KubernetesList;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder;
import io.fabric8.openshift.api.model.DeploymentConfigBuilder;
import mockit.Expectations;
import mockit.Mocked;

/**
* Test label generation.
*
* @author Tue Dissing
*/
public class ProjectLabelEnricherTest {

@Mocked
private JKubeEnricherContext context;

@Before
public void setupExpectations() {
new Expectations() {{
context.getGav();
result = new GroupArtifactVersion("groupId", "artifactId", "version");
}};
}

@Test
public void testCustomAppName() {

// Setup
final String APP = "my-custom-app-name";
final Properties properties = new Properties();
properties.setProperty("jkube.enricher.jkube-project-label.app", APP);
// @formatter:off
new Expectations() {{
context.getProperties(); result = properties;
}};
// @formatter:on

ProjectLabelEnricher projectEnricher = new ProjectLabelEnricher(context);

KubernetesListBuilder builder = createListWithDeploymentConfig();
projectEnricher.enrich(PlatformMode.kubernetes, builder);
KubernetesList list = builder.build();

Map<String, String> labels = list.getItems().get(0).getMetadata().getLabels();

assertNotNull(labels);
assertEquals("groupId", labels.get("group"));
assertEquals(APP, labels.get("app"));
assertEquals("version", labels.get("version"));
assertNull(labels.get("project"));

builder = new KubernetesListBuilder().withItems(new DeploymentBuilder().build());
projectEnricher.create(PlatformMode.kubernetes, builder);

Deployment deployment = (Deployment)builder.buildFirstItem();
Map<String, String> selectors = deployment.getSpec().getSelector().getMatchLabels();
assertEquals("groupId", selectors.get("group"));
assertEquals(APP, selectors.get("app"));
assertNull(selectors.get("version"));
assertNull(selectors.get("project"));
}

@Test
public void testEmptyCustomAppName() {

// Setup
final String APP = "";
final Properties properties = new Properties();
properties.setProperty("jkube.enricher.jkube-project-label.app", APP);
// @formatter:off
new Expectations() {{
context.getProperties(); result = properties;
}};
// @formatter:on

ProjectLabelEnricher projectEnricher = new ProjectLabelEnricher(context);

KubernetesListBuilder builder = createListWithDeploymentConfig();
projectEnricher.enrich(PlatformMode.kubernetes, builder);
KubernetesList list = builder.build();

Map<String, String> labels = list.getItems().get(0).getMetadata().getLabels();

assertNotNull(labels);
assertEquals("groupId", labels.get("group"));
assertEquals(APP, labels.get("app"));
assertEquals("version", labels.get("version"));
assertNull(labels.get("project"));

builder = new KubernetesListBuilder().withItems(new DeploymentBuilder().build());
projectEnricher.create(PlatformMode.kubernetes, builder);

Deployment deployment = (Deployment)builder.buildFirstItem();
Map<String, String> selectors = deployment.getSpec().getSelector().getMatchLabels();
assertEquals("groupId", selectors.get("group"));
assertEquals(APP, selectors.get("app"));
assertNull(selectors.get("version"));
assertNull(selectors.get("project"));
}

@Test
public void testDefaultAppName() {
ProjectLabelEnricher projectEnricher = new ProjectLabelEnricher(context);

KubernetesListBuilder builder = createListWithDeploymentConfig();
projectEnricher.enrich(PlatformMode.kubernetes, builder);
KubernetesList list = builder.build();

Map<String, String> labels = list.getItems().get(0).getMetadata().getLabels();

assertNotNull(labels);
assertEquals("groupId", labels.get("group"));
assertEquals("artifactId", labels.get("app"));
assertEquals("version", labels.get("version"));
assertNull(labels.get("project"));

builder = new KubernetesListBuilder().withItems(new DeploymentBuilder().build());
projectEnricher.create(PlatformMode.kubernetes, builder);

Deployment deployment = (Deployment)builder.buildFirstItem();
Map<String, String> selectors = deployment.getSpec().getSelector().getMatchLabels();
assertEquals("groupId", selectors.get("group"));
assertEquals("artifactId", selectors.get("app"));
assertNull(selectors.get("version"));
assertNull(selectors.get("project"));
}

private KubernetesListBuilder createListWithDeploymentConfig() {
return new KubernetesListBuilder().addToItems(new DeploymentConfigBuilder()
.withNewMetadata().endMetadata()
.withNewSpec().endSpec()
.build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ been replaced by the `app` label in newer versions of the plugin.

Defaults to `false`.
| `jkube.enricher.jkube-project-label.useProjectLabel`
| *app*
| Makes it possible to define a custom `app` label used in the generated resource files used for deployment.

Defaults to the Maven `project.artifactId` property.
| `jkube.enricher.jkube-project-label.app`
|===

The project labels which are already specified in the input fragments are not overridden by the enricher.