Skip to content

Commit

Permalink
chore: Deprecate maven.jkube.io annotation prefix from enrichers
Browse files Browse the repository at this point in the history
`maven.jkube.io` seems to be a side effect of search-replace during
migration from FMP to JKube. Deprecate all annotation properties
starting with this prefix.

Introduce use of `jkube.eclipse.org` prefix instead.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Jan 31, 2022
1 parent be8514f commit beabba8
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Usage:
* Fix #1213: SnakeYaml dependency from Kubernetes Client + uses SafeConstructor
* Fix #1142: Log informative message whenever a goal/task is skipped
* Fix #1197: Kubernetes Gradle Build tasks don't account for image model configuration skip field
* Fix #1245: Deprecate `maven.jkube.io` annotation prefix used in enrichers in favor of `jkube.eclipse.org`

### 1.5.1 (2021-10-28)
* Fix #1084: Gradle dependencies should be test or provided scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @since 01/04/16
*/
public interface Enricher extends Named {
String INTERNAL_ANNOTATION_PREFIX = "jkube.eclipse.org";

/**
* Add default resources when they are missing. Each enricher should be responsible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
// TODO-F8SPEC : Should be move the to AppCatalog mojo and must not be in the general available util package
// Also consider whether the Constants class pattern makes (should probably change to real enums ???)
public class Constants {
/**
* @deprecated Use annotations with <code>jkube.eclipse.org</code> prefix instead
*/
@Deprecated
public static final String RESOURCE_SOURCE_URL_ANNOTATION = "maven.jkube.io/source-url";
/**
* @deprecated Use annotations with <code>jkube.eclipse.org</code> prefix instead
*/
@Deprecated
public static final String RESOURCE_APP_CATALOG_ANNOTATION = "maven.jkube.io/app-catalog";

private Constants() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@

public class ConfigMapEnricher extends BaseEnricher {

/**
* @deprecated Use <code>jkube.eclipse.org/cm/</code> prefix instead
*/
@Deprecated
protected static final String PREFIX_ANNOTATION = "maven.jkube.io/cm/";

protected static final String CONFIGMAP_PREFIX_ANNOTATION = INTERNAL_ANNOTATION_PREFIX + "/cm/";

public ConfigMapEnricher(JKubeEnricherContext enricherContext) {
super(enricherContext, "jkube-configmap-file");
}
Expand Down Expand Up @@ -78,7 +84,7 @@ private void addConfigMapFromAnnotations(final Map<String, String> annotations,
Map.Entry<String, String> entry = it.next();
final String key = entry.getKey();

if (key.startsWith(PREFIX_ANNOTATION)) {
if (key.startsWith(PREFIX_ANNOTATION) || key.startsWith(CONFIGMAP_PREFIX_ANNOTATION)) {
addConfigMapEntryFromDirOrFile(configMapBuilder, getOutput(key), entry.getValue());
it.remove();
}
Expand Down Expand Up @@ -121,7 +127,10 @@ private void addConfigMapEntryFromFile(final ConfigMapBuilder configMapBuilder,
}

private String getOutput(String key) {
return key.substring(PREFIX_ANNOTATION.length());
if (key.startsWith(PREFIX_ANNOTATION)) {
return key.substring(PREFIX_ANNOTATION.length());
}
return key.substring(CONFIGMAP_PREFIX_ANNOTATION.length());
}

private void addConfigMapFromXmlConfigurations(KubernetesListBuilder builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
import java.util.Optional;

public class DockerRegistrySecretEnricher extends SecretEnricher {
/**
* @deprecated Use <code>jkube.eclipse.org/dockerServerId</code> instead.
*/
@Deprecated
private static final String ANNOTATION_KEY = "maven.jkube.io/dockerServerId";
private static final String DOCKER_SERVER_ID_ANNOTATION_KEY = INTERNAL_ANNOTATION_PREFIX + "/dockerServerId";
private static final String ENRICHER_NAME = "jkube-docker-registry-secret";


Expand All @@ -34,6 +39,11 @@ public DockerRegistrySecretEnricher(JKubeEnricherContext buildContext) {

@Override
protected String getAnnotationKey() {
return DOCKER_SERVER_ID_ANNOTATION_KEY;
}

@Override
protected String getDeprecatedAnnotationKey() {
return ANNOTATION_KEY;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@

public class FileDataSecretEnricher extends BaseEnricher {

/**
* @deprecated Use <code>jkube.eclipse.org/secret/</code> instead
*/
@Deprecated
protected static final String PREFIX_ANNOTATION = "maven.jkube.io/secret/";
protected static final String FILEDATASECRET_PREFIX_ANNOTATION = INTERNAL_ANNOTATION_PREFIX + "/secret/";

public FileDataSecretEnricher(JKubeEnricherContext buildContext) {
super(buildContext, "jkube-secret-file");
Expand Down Expand Up @@ -68,7 +73,8 @@ private Map<String, String> createSecretFromAnnotations(final Map<String, String
Map.Entry<String, String> entry = it.next();
final String key = entry.getKey();

if(key.startsWith(PREFIX_ANNOTATION)) {
String secretFileLocationKey = getOutput(key);
if(secretFileLocationKey != null) {
byte[] bytes = readContent(entry.getValue());
secretFileLocations.put(getOutput(key), Base64Util.encodeToString(bytes));
it.remove();
Expand All @@ -83,6 +89,11 @@ private byte[] readContent(String location) throws IOException {
}

private String getOutput(String key) {
return key.substring(PREFIX_ANNOTATION.length());
if (key.startsWith(PREFIX_ANNOTATION)) {
return key.substring(PREFIX_ANNOTATION.length());
} else if (key.startsWith(FILEDATASECRET_PREFIX_ANNOTATION)) {
return key.substring(FILEDATASECRET_PREFIX_ANNOTATION.length());
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
public void visit(SecretBuilder secretBuilder) {
Map<String, String> annotation = secretBuilder.buildMetadata().getAnnotations();
if (annotation != null) {
if (!annotation.containsKey(getAnnotationKey())) {
String dockerId = getDockerIdFromAnnotation(annotation);
if (dockerId == null) {
return;
}
String dockerId = annotation.get(getAnnotationKey());
Map<String, String> data = generateData(dockerId);
if (data == null) {
return;
Expand Down Expand Up @@ -135,7 +135,18 @@ private List<SecretConfig> getSecretsFromXmlConfig() {
return null;
}

private String getDockerIdFromAnnotation(Map<String, String> annotation) {
if (annotation.containsKey(getDeprecatedAnnotationKey())) {
return annotation.get(getDeprecatedAnnotationKey());
} else if (annotation.containsKey(getAnnotationKey())) {
return annotation.get(getAnnotationKey());
}
return null;
}

protected abstract String getAnnotationKey();

protected abstract String getDeprecatedAnnotationKey();

protected abstract Map<String, String> generateData(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public class ConfigMapEnricherTest {
private JKubeEnricherContext context;

@Test
public void should_materialize_file_content_from_annotation() throws Exception {
public void should_materialize_file_content_from_deprecated_annotation() throws Exception {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test-application.properties",
"src/test/resources/test-application.properties");
"src/test/resources/test-application.properties", "maven.jkube.io/cm/");
final KubernetesListBuilder builder = new KubernetesListBuilder().addToConfigMapItems(baseConfigMap);
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

Expand All @@ -60,8 +60,8 @@ public void should_materialize_file_content_from_annotation() throws Exception {
}

@Test
public void should_materialize_dir_content_from_annotation() throws Exception {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test-dir", "src/test/resources/test-dir");
public void should_materialize_dir_content_from_deprecated_annotation() throws Exception {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test-dir", "src/test/resources/test-dir", "maven.jkube.io/cm/");
final KubernetesListBuilder builder = new KubernetesListBuilder().addToConfigMapItems(baseConfigMap);
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

Expand All @@ -78,9 +78,63 @@ public void should_materialize_dir_content_from_annotation() throws Exception {
assertThat(annotations).isEmpty();
}

@Test
public void should_materialize_binary_file_content_from_deprecated_annotation() {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test.bin", "src/test/resources/test.bin", "maven.jkube.io/cm/");
final KubernetesListBuilder builder = new KubernetesListBuilder().addToConfigMapItems(baseConfigMap);
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

final ConfigMap configMap = (ConfigMap) builder.buildFirstItem();

final Map<String, String> data = configMap.getData();
assertThat(data).isEmpty();

final Map<String, String> binaryData = configMap.getBinaryData();
assertThat(binaryData).containsEntry("test.bin", "wA==");

final Map<String, String> annotations = configMap.getMetadata().getAnnotations();
assertThat(annotations).isEmpty();
}

@Test
public void should_materialize_file_content_from_annotation() throws Exception {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test-application.properties",
"src/test/resources/test-application.properties", "jkube.eclipse.org/cm/");
final KubernetesListBuilder builder = new KubernetesListBuilder().addToConfigMapItems(baseConfigMap);
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

final ConfigMap configMap = (ConfigMap) builder.buildFirstItem();

final Map<String, String> data = configMap.getData();
assertThat(data).containsEntry("test-application.properties",
readFileContentsAsString("src/test/resources/test-application.properties"));

final Map<String, String> annotations = configMap.getMetadata().getAnnotations();
assertThat(annotations).isEmpty();
}

@Test
public void should_materialize_dir_content_from_annotation() throws Exception {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test-dir", "src/test/resources/test-dir", "jkube.eclipse.org/cm/");
final KubernetesListBuilder builder = new KubernetesListBuilder().addToConfigMapItems(baseConfigMap);
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

final ConfigMap configMap = (ConfigMap) builder.buildFirstItem();

final Map<String, String> data = configMap.getData();
assertThat(data).containsEntry("test-application.properties",
readFileContentsAsString("src/test/resources/test-dir/test-application.properties"))
.doesNotContainKey("test-dir-empty");
final Map<String, String> binaryData = configMap.getBinaryData();
assertThat(binaryData).containsEntry("test.bin", "wA==");

final Map<String, String> annotations = configMap.getMetadata().getAnnotations();
assertThat(annotations).isEmpty();
}

@Test
public void should_materialize_binary_file_content_from_annotation() {
final ConfigMap baseConfigMap = createAnnotationConfigMap("test.bin", "src/test/resources/test.bin");
final ConfigMap baseConfigMap = createAnnotationConfigMap("test.bin", "src/test/resources/test.bin", "jkube.eclipse.org/cm/");
final KubernetesListBuilder builder = new KubernetesListBuilder().addToConfigMapItems(baseConfigMap);
new ConfigMapEnricher(context).create(PlatformMode.kubernetes, builder);

Expand Down Expand Up @@ -148,11 +202,11 @@ private org.eclipse.jkube.kit.config.resource.ConfigMap createXmlConfigMap(final
return configMap;
}

private ConfigMap createAnnotationConfigMap(final String key, final String file) {
private ConfigMap createAnnotationConfigMap(final String key, final String file, final String annotationPrefix) {
ObjectMetaBuilder metaBuilder = new ObjectMetaBuilder().withName("some-config-map").withNamespace("default");

Map<String, String> annotations = new HashMap<>();
annotations.put(ConfigMapEnricher.PREFIX_ANNOTATION + key, file);
annotations.put(annotationPrefix + key, file);
metaBuilder = metaBuilder.withAnnotations(annotations);

Map<String, String> data = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class DockerRegistrySecretEnricherTest {
private JKubeEnricherContext context;

private String dockerUrl = "docker.io";
private String annotation = "maven.jkube.io/dockerServerId";
private String annotation = "jkube.eclipse.org/dockerServerId";

private void setupExpectations() {
new Expectations() {
Expand All @@ -70,7 +70,30 @@ public void testDockerRegistry() {
setupExpectations();
DockerRegistrySecretEnricher enricher = new DockerRegistrySecretEnricher(context);
KubernetesListBuilder builder = new KubernetesListBuilder();
Secret secretEnriched = createBaseSecret(true);
Secret secretEnriched = createBaseSecret(true, annotation);
builder.addToSecretItems(secretEnriched);
enricher.create(PlatformMode.kubernetes, builder);

secretEnriched = (Secret) builder.buildItem(0);
Map<String, String> enrichedData = secretEnriched.getData();
assertThat(enrichedData).hasSize(1);
String data = enrichedData.get(SecretConstants.DOCKER_DATA_KEY);
assertThat(data).isNotNull();
JsonObject auths = (JsonObject) JsonParser.parseString(new String(Base64.decodeBase64(data)));
assertThat(auths.size()).isEqualTo(1);
JsonObject auth = auths.getAsJsonObject("docker.io");
assertThat(auth.size()).isEqualTo(2);

assertThat(auth.get("username").getAsString()).isEqualTo("username");
assertThat(auth.get("password").getAsString()).isEqualTo("password");
}

@Test
public void testDockerRegistryWithDeprecatedAnnotation() {
setupExpectations();
DockerRegistrySecretEnricher enricher = new DockerRegistrySecretEnricher(context);
KubernetesListBuilder builder = new KubernetesListBuilder();
Secret secretEnriched = createBaseSecret(true, "maven.jkube.io/dockerServerId");
builder.addToSecretItems(secretEnriched);
enricher.create(PlatformMode.kubernetes, builder);

Expand All @@ -93,9 +116,9 @@ public void testDockerRegistryWithBadKind() {
setupExpectations();
DockerRegistrySecretEnricher enricher = new DockerRegistrySecretEnricher(context);
KubernetesListBuilder builder = new KubernetesListBuilder();
Secret secret = createBaseSecret(true);
Secret secret = createBaseSecret(true, annotation);
secret.setKind("Secrets");
builder.addToSecretItems(createBaseSecret(true));
builder.addToSecretItems(createBaseSecret(true, annotation));
KubernetesList expected = builder.build();

enricher.create(PlatformMode.kubernetes, builder);
Expand All @@ -107,23 +130,23 @@ public void testDockerRegistryWithBadAnnotation() {
DockerRegistrySecretEnricher enricher = new DockerRegistrySecretEnricher(context);
setupExpectations();
KubernetesListBuilder builder = new KubernetesListBuilder();
Secret secret = createBaseSecret(true);
Secret secret = createBaseSecret(true, annotation);
secret.getMetadata().getAnnotations().put(annotation, "docker1.io");
builder.addToSecretItems(createBaseSecret(true));
builder.addToSecretItems(createBaseSecret(true, annotation));

KubernetesList expected = builder.build();

enricher.create(PlatformMode.kubernetes, builder);
assertEquals(expected, builder.build());
}

private Secret createBaseSecret(boolean withAnnotation) {
private Secret createBaseSecret(boolean withAnnotation, String annotationValue) {
ObjectMetaBuilder metaBuilder = new ObjectMetaBuilder()
.withNamespace("default");

if (withAnnotation) {
Map<String, String> annotations = new HashMap<>();
annotations.put(annotation, dockerUrl);
annotations.put(annotationValue, dockerUrl);
metaBuilder = metaBuilder.withAnnotations(annotations);
}

Expand Down
Loading

0 comments on commit beabba8

Please sign in to comment.