Skip to content

Commit

Permalink
refactor (jkube-kit/config/resource) : Deprecate jkube.io annotatio…
Browse files Browse the repository at this point in the history
…n prefix in favor of `jkube.eclipse.org` for JKubeAnnotations (#1273)

+ Deprecate `jkube.io` annotation prefix in favor of `jkube.eclipse.org`
+ Add `jkube.useOldJKubePrefix` flag to switch to `jkube.io`
  annotation prefix in case any user is interested in this.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed Feb 23, 2023
1 parent 8db1fe1 commit ba95fb2
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ Usage:
./scripts/extract-changelog-for-version.sh 1.3.37 5
```
### 1.12-SNAPSHOT
* Fix #1273: Deprecate `jkube.io` annotation prefix in favor of `jkube.eclipse.org` for JKubeAnnotations

### 1.11.0 (2023-02-16)
* Fix #1316: Add support for adding InitContainers via plugin configuration
Expand Down
Expand Up @@ -349,11 +349,11 @@ public static void handleKubernetesClientException(KubernetesClientException e,
Throwable cause = e.getCause();
if (cause instanceof UnknownHostException) {
logger.error( "Could not connect to kubernetes cluster!");
logger.error("Have you started a local cluster via `mvn jkube:cluster-start` or connected to a remote cluster via `kubectl`?");
logger.info("For more help see: http://jkube.io/guide/getStarted/");
logger.error("Have you started a local cluster or connected to a remote cluster via `kubectl`?");
logger.info("For more help see: https://www.eclipse.org/jkube/docs/#getting-started");
logger.error( "Connection error: %s", cause);

String message = "Could not connect to kubernetes cluster. Have you started a cluster via `minikube start` or connected to a remote cluster via `kubectl`? Error: " + cause;
String message = "Could not connect to kubernetes cluster. Have you started a local cluster or connected to a remote cluster via `kubectl`? Error: " + cause;
throw new IllegalStateException(message, e);
} else {
throw new IllegalStateException(e.getMessage(), e);
Expand Down
Expand Up @@ -36,21 +36,35 @@ public enum JKubeAnnotations {
SCM_TAG("scm-tag"),
SCM_URL("scm-url"),

TARGET_PLATFORM("target-platform");

TARGET_PLATFORM("target-platform"),
ICON_URL("iconUrl");
private static final String JKUBE_ANNOTATION_PREFIX = "jkube.eclipse.org";
/**
* @deprecated in favor of <code>jkube.eclipse.org</code>
*/
@Deprecated
private static final String DEPRECATED_JKUBE_ANNOTATION_PREFIX = "jkube.io";
private final String annotation;

JKubeAnnotations(String anno) {
this.annotation = "jkube.io/" + anno;
this.annotation = "/" + anno;
}

public String value() {
return annotation;
return value(false);
}

public String value(boolean useDeprecatedPrefix) {
if (useDeprecatedPrefix) {
return DEPRECATED_JKUBE_ANNOTATION_PREFIX + annotation;
}
return JKUBE_ANNOTATION_PREFIX + annotation;
}

@Override
public String toString() {
return value();
}

}

Expand Up @@ -113,6 +113,7 @@ public class ResourceConfig {
@Deprecated
private String restartPolicy;
private ControllerResourceConfig controller;
private boolean useLegacyJKubePrefix;

public final ControllerResourceConfig getController() {
if (controller != null && isAnyControllerLegacyConfigFieldSet()) {
Expand Down
@@ -0,0 +1,44 @@
/**
* 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.kit.config.resource;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class JKubeAnnotationsTest {
@Test
void value_withUseDeprecatedPrefixFalse_shouldReturnJKubeEclipseOrgPrefix() {
// Given + When
String result = JKubeAnnotations.GIT_COMMIT.value();
// Then
assertThat(result).isEqualTo("jkube.eclipse.org/git-commit");
}

@Test
void value_withUseDeprecatedPrefixTrue_shouldReturnJKubeIoPrefix() {
// Given + When
String result = JKubeAnnotations.GIT_COMMIT.value(true);
// Then
assertThat(result).isEqualTo("jkube.io/git-commit");
}

@Test
void toString_whenInvoked_shouldUseJkubeEclipseOrgPrefix() {
// Given + When
String result = JKubeAnnotations.GIT_BRANCH.toString();
// Then
assertThat(result).isEqualTo("jkube.eclipse.org/git-branch");
}
}
Expand Up @@ -55,6 +55,7 @@ public class BaseEnricher implements Enricher {
public static final String JKUBE_ENFORCED_REPLICAS = "jkube.replicas";
public static final String JKUBE_DEFAULT_IMAGE_PULL_POLICY = "IfNotPresent";
public static final String JKUBE_ENFORCED_IMAGE_PULL_POLICY = "jkube.imagePullPolicy";
private static final String JKUBE_USE_OLD_PREFIX = "jkube.useLegacyJKubePrefix";

private final EnricherConfig config;
private final String name;
Expand Down Expand Up @@ -239,6 +240,18 @@ protected void setProcessingInstruction(String key, List<String> containerNames)
enricherContext.setProcessingInstructions(processingInstructionsMap);
}

protected boolean shouldUseLegacyJKubePrefix() {
String valueFromProperties = getValueFromConfig(JKUBE_USE_OLD_PREFIX, null);
if (StringUtils.isNotBlank(valueFromProperties)) {
return Boolean.parseBoolean(valueFromProperties);
}
ResourceConfig resourceConfig = getConfiguration().getResource();
if (resourceConfig != null) {
return resourceConfig.isUseLegacyJKubePrefix();
}
return false;
}

/**
* Getting a property value from configuration
*
Expand Down
Expand Up @@ -36,10 +36,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.jkube.kit.enricher.api.BaseEnricher.getNamespace;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class BaseEnricherTest {
private BaseEnricher baseEnricher;
Expand Down Expand Up @@ -436,4 +435,36 @@ void getControllerResourceConfig_whenNullResourceProvided_thenReturnsEmptyContro
// Then
assertThat(config).isNotNull();
}

@Test
void shouldUseLegacyJKubePrefix_whenPropertyProvided_thenReturnTrue() {
// Given
when(context.getProperty("jkube.useLegacyJKubePrefix")).thenReturn("true");
// When
boolean value = baseEnricher.shouldUseLegacyJKubePrefix();
// Then
assertThat(value).isTrue();
}

@Test
void shouldUseLegacyJKubePrefix_whenResourceConfigProvided_thenReturnTrue() {
// Given
baseEnricher = new BaseEnricher(context.toBuilder()
.resources(ResourceConfig.builder()
.useLegacyJKubePrefix(true)
.build())
.build(), "test-enricher");
// When
boolean value = baseEnricher.shouldUseLegacyJKubePrefix();
// Then
assertThat(value).isTrue();
}

@Test
void shouldUseLegacyJKubePrefix_whenNothingProvided_thenReturnFalse() {
// Given + When
boolean value = baseEnricher.shouldUseLegacyJKubePrefix();
// Then
assertThat(value).isFalse();
}
}
Expand Up @@ -55,6 +55,7 @@ public GitEnricher(JKubeEnricherContext buildContext) {

private Map<String, String> getAnnotations(PlatformMode platformMode) {
final Map<String, String> annotations = new HashMap<>();
boolean useDeprecatedAnnotationPrefix = shouldUseLegacyJKubePrefix();
if (GitUtil.findGitFolder(getContext().getProjectDirectory()) != null) {
try (Repository repository = GitUtil.getGitRepository(getContext().getProjectDirectory())) {
// Git annotations (if git is used as SCM)
Expand All @@ -64,7 +65,7 @@ private Map<String, String> getAnnotations(PlatformMode platformMode) {
log.warn("Could not detect any git remote");
}

annotations.putAll(getAnnotations(platformMode, gitRemoteUrl, repository.getBranch(), GitUtil.getGitCommitId(repository)));
annotations.putAll(getAnnotations(platformMode, gitRemoteUrl, repository.getBranch(), GitUtil.getGitCommitId(repository), useDeprecatedAnnotationPrefix));
}
return annotations;
} catch (IOException | GitAPIException e) {
Expand Down Expand Up @@ -135,11 +136,11 @@ public void visit(JobBuilder builder) {
});
}

protected static Map<String, String> getAnnotations(PlatformMode platformMode, String gitRemoteUrl, String branch, String commitId) {
protected static Map<String, String> getAnnotations(PlatformMode platformMode, String gitRemoteUrl, String branch, String commitId, boolean useDeprecatedAnnotationPrefix) {
Map<String, String> annotationsToBeAdded = new HashMap<>();
annotationsToBeAdded.putAll(addAnnotation(JKubeAnnotations.GIT_BRANCH.value(), branch));
annotationsToBeAdded.putAll(addAnnotation(JKubeAnnotations.GIT_COMMIT.value(), commitId));
annotationsToBeAdded.putAll(addAnnotation(JKubeAnnotations.GIT_URL.value(), gitRemoteUrl));
annotationsToBeAdded.putAll(addAnnotation(JKubeAnnotations.GIT_BRANCH.value(useDeprecatedAnnotationPrefix), branch));
annotationsToBeAdded.putAll(addAnnotation(JKubeAnnotations.GIT_COMMIT.value(useDeprecatedAnnotationPrefix), commitId));
annotationsToBeAdded.putAll(addAnnotation(JKubeAnnotations.GIT_URL.value(useDeprecatedAnnotationPrefix), gitRemoteUrl));
if (platformMode.equals(PlatformMode.openshift)) {
annotationsToBeAdded.putAll(addAnnotation(OpenShiftAnnotations.VCS_URI.value(), gitRemoteUrl));
annotationsToBeAdded.putAll(addAnnotation(OpenShiftAnnotations.VCS_REF.value(), branch));
Expand Down
Expand Up @@ -111,16 +111,17 @@ public void visit(JobBuilder builder) {

private Map<String, String> getAnnotations() {
Map<String, String> annotations = new HashMap<>();

boolean useDeprecatedAnnotationPrefix = shouldUseLegacyJKubePrefix();

if (getContext() instanceof JKubeEnricherContext) {
JKubeEnricherContext jkubeEnricherContext = (JKubeEnricherContext) getContext();
JavaProject rootProject = jkubeEnricherContext.getProject();
if (hasIssueManagement(rootProject)) {
String system = rootProject.getIssueManagementSystem();
String url = rootProject.getIssueManagementUrl();
if (StringUtils.isNotEmpty(system) && StringUtils.isNotEmpty(url)) {
annotations.put(JKubeAnnotations.ISSUE_SYSTEM.value(), system);
annotations.put(JKubeAnnotations.ISSUE_TRACKER_URL.value(), url);
annotations.put(JKubeAnnotations.ISSUE_SYSTEM.value(useDeprecatedAnnotationPrefix), system);
annotations.put(JKubeAnnotations.ISSUE_TRACKER_URL.value(useDeprecatedAnnotationPrefix), url);
}
}
}
Expand Down
Expand Up @@ -54,6 +54,7 @@ public MavenScmEnricher(JKubeEnricherContext buildContext) {

private Map<String, String> getAnnotations() {
Map<String, String> annotations = new HashMap<>();
boolean useDeprecatedAnnotationPrefix = shouldUseLegacyJKubePrefix();

if (getContext() instanceof JKubeEnricherContext) {
JKubeEnricherContext jkubeEnricherContext = (JKubeEnricherContext) getContext();
Expand All @@ -63,10 +64,10 @@ private Map<String, String> getAnnotations() {
String tag = rootProject.getScmTag();

if (StringUtils.isNotEmpty(tag)) {
annotations.put(JKubeAnnotations.SCM_TAG.value(), tag);
annotations.put(JKubeAnnotations.SCM_TAG.value(useDeprecatedAnnotationPrefix), tag);
}
if (StringUtils.isNotEmpty(url)) {
annotations.put(JKubeAnnotations.SCM_URL.value(), url);
annotations.put(JKubeAnnotations.SCM_URL.value(useDeprecatedAnnotationPrefix), url);
}
}
}
Expand Down
Expand Up @@ -192,3 +192,4 @@ public void visit(ServiceBuilder service) {
}

}

Expand Up @@ -34,7 +34,7 @@ void getAnnotations_addedInKubernetesPlatformMode() {
Map<String, String> annotations;

// When
annotations = GitEnricher.getAnnotations(PlatformMode.kubernetes, GIT_REMOTE_URL, GIT_BRANCH, GIT_COMMIT_ID);
annotations = GitEnricher.getAnnotations(PlatformMode.kubernetes, GIT_REMOTE_URL, GIT_BRANCH, GIT_COMMIT_ID, true);

// Then
assertJkubeAnnotations(annotations);
Expand All @@ -46,7 +46,7 @@ void getAnnotations_addedInOpenShiftPlatformMode() {
Map<String, String> annotations;

// When
annotations = GitEnricher.getAnnotations(PlatformMode.openshift, GIT_REMOTE_URL, GIT_BRANCH, GIT_COMMIT_ID);
annotations = GitEnricher.getAnnotations(PlatformMode.openshift, GIT_REMOTE_URL, GIT_BRANCH, GIT_COMMIT_ID, true);

// Then
assertJkubeAnnotations(annotations);
Expand All @@ -60,7 +60,7 @@ void getAnnotations_addedWithAllNullValues() {
Map<String, String> annotations;

// When
annotations = GitEnricher.getAnnotations(PlatformMode.kubernetes, null, null, null);
annotations = GitEnricher.getAnnotations(PlatformMode.kubernetes, null, null, null, true);

// Then
assertThat(annotations).isEmpty();
Expand All @@ -72,15 +72,15 @@ void getAnnotations_addedWithNullCommitValues() {
Map<String, String> annotations;

// When
annotations = GitEnricher.getAnnotations(PlatformMode.kubernetes, GIT_REMOTE_URL, GIT_BRANCH, null);
annotations = GitEnricher.getAnnotations(PlatformMode.kubernetes, GIT_REMOTE_URL, GIT_BRANCH, null, true);

// Then
assertJkubeAnnotationsRemoteUrlAndBranch(annotations);
}

private void assertJkubeAnnotations(Map<String, String> annotations) {
assertJkubeAnnotationsRemoteUrlAndBranch(annotations);
assertThat(annotations).containsEntry(JKubeAnnotations.GIT_COMMIT.value(),GIT_COMMIT_ID);
assertThat(annotations).containsEntry(JKubeAnnotations.GIT_COMMIT.value(true), GIT_COMMIT_ID);
}

private void assertJkubeAnnotationsRemoteUrlAndBranch(Map<String, String> annotations) {
Expand Down
Expand Up @@ -57,8 +57,8 @@ void mavenScmAll() {
assertThat(scmAnnotations)
.isNotNull()
.hasSize(2)
.containsEntry("jkube.io/scm-tag", "HEAD")
.containsEntry("jkube.io/scm-url", "git://github.com/jkubeio/kubernetes-maven-plugin.git");
.containsEntry("jkube.eclipse.org/scm-tag", "HEAD")
.containsEntry("jkube.eclipse.org/scm-url", "git://github.com/jkubeio/kubernetes-maven-plugin.git");

}

Expand All @@ -78,8 +78,8 @@ void mavenScmOnlyDevConnection() {
Map<String, String> scmAnnotations = builder.buildFirstItem().getMetadata().getAnnotations();
assertThat(scmAnnotations).isNotNull()
.hasSize(1)
.containsEntry("jkube.io/scm-url", "git://github.com/jkubeio/kubernetes-maven-plugin.git")
.doesNotContainKey("jkube.io/scm-tag");
.containsEntry("jkube.eclipse.org/scm-url", "git://github.com/jkubeio/kubernetes-maven-plugin.git")
.doesNotContainKey("jkube.eclipse.org/scm-tag");
}

@Test
Expand All @@ -99,8 +99,8 @@ void mavenScmOnlyUrl() {
Map<String, String> scmAnnotations = builder.buildFirstItem().getMetadata().getAnnotations();
assertThat(scmAnnotations).isNotNull()
.hasSize(1)
.containsEntry("jkube.io/scm-url", "scm:git:git://github.com/jkubeio/kubernetes-maven-plugin.git")
.doesNotContainKey("jkube.io/scm-tag");
.containsEntry("jkube.eclipse.org/scm-url", "scm:git:git://github.com/jkubeio/kubernetes-maven-plugin.git")
.doesNotContainKey("jkube.eclipse.org/scm-tag");
}

@Test
Expand Down
Expand Up @@ -236,3 +236,4 @@ private void assertPodTemplateSpecContainsInitContainerAndVolumeMounts(Kubernete
}
}


0 comments on commit ba95fb2

Please sign in to comment.