diff --git a/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/NameEnricher.java b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/NameEnricher.java index b371b43ed8..b124e862b9 100644 --- a/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/NameEnricher.java +++ b/jkube-kit/enricher/generic/src/main/java/org/eclipse/jkube/enricher/generic/NameEnricher.java @@ -54,10 +54,12 @@ public void create(PlatformMode platformMode, KubernetesListBuilder builder) { builder.accept(new TypedVisitor() { @Override public void visit(ObjectMetaBuilder resource) { - if (StringUtils.isNotBlank(configuredName)) { - resource.withName(configuredName); - } else if (StringUtils.isBlank(resource.getName())) { - resource.withName(defaultName); + if(StringUtils.isBlank(resource.getName())) { + if (StringUtils.isNotBlank(configuredName)) { + resource.withName(configuredName); + } else { + resource.withName(defaultName); + } } } }); diff --git a/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/NameEnricherTest.java b/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/NameEnricherTest.java index d935ac80ba..1d9577ff26 100644 --- a/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/NameEnricherTest.java +++ b/jkube-kit/enricher/generic/src/test/java/org/eclipse/jkube/enricher/generic/NameEnricherTest.java @@ -118,7 +118,7 @@ void create_withAlreadyExistingName_shouldKeepExistingName() { } @Test - void create_withCustomNameAndAlreadyExistingName_shouldOverrideExistingName() { + void create_withCustomNameAndAlreadyExistingName_shouldKeepExistingName() { // Given klb = new KubernetesListBuilder(); properties.put("jkube.enricher.jkube-name.name", "custom-name"); @@ -128,6 +128,20 @@ void create_withCustomNameAndAlreadyExistingName_shouldOverrideExistingName() { // Then assertThat(klb.build().getItems()) .singleElement() - .hasFieldOrPropertyWithValue("metadata.name", "custom-name"); + .hasFieldOrPropertyWithValue("metadata.name", "existing-name"); + } + + @Test + void create_withoutName_shouldSetGivenCustomName() { + // Given + klb = new KubernetesListBuilder(); + properties.put("jkube.enricher.jkube-name.name", "custom-name"); + // When + klb.addToItems(new ServiceBuilder().withNewMetadata().endMetadata().build()); + new NameEnricher(context).create(PlatformMode.kubernetes, klb); + // Then + assertThat(klb.build().getItems()) + .singleElement() + .hasFieldOrPropertyWithValue("metadata.name", "custom-name"); } }