Skip to content

Commit

Permalink
fix (jkube-kit/enricher) : Add hint for using jkube.domain if `crea…
Browse files Browse the repository at this point in the history
…teExternalUrls` is used without domain (eclipse-jkube#1439)

Currently value for IngressRule's `.host` field depends either on `HOST`
enricher configuration or `jkube.domain` configuration. If none of these
options are provided, we set `.spec.defaultBackend`.

Add a hint to suggest specifying `jkube.domain` in case no value for
host is found.

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia authored and manusa committed Jan 5, 2023
1 parent 751a8aa commit bb9794e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage:
./scripts/extract-changelog-for-version.sh 1.3.37 5
```
### 1.11-SNAPSHOT
* Fix #1439: Add hint to use jkube.domain if createExternalUrls is used without domain
* Fix #1459: Route Generation should support `8443` as default web port
* Fix #1546: Migrate to JUnit5 testing framework
* Fix #1858: Properties in image name not replaced
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void visit(ServiceBuilder serviceBuilder) {
}
}
});
logHintIfNoDomainOrHostProvided();
}
}

Expand Down Expand Up @@ -142,6 +143,32 @@ protected String getRouteDomain() {
return null;
}

void logHintIfNoDomainOrHostProvided() {
ResourceConfig resourceConfig = getContext().getConfiguration().getResource();
if (resourceConfig != null && resourceConfig.getIngress() != null) {
logHintIfNoDomainOrHostForResourceConfig(resourceConfig);
} else if (StringUtils.isBlank(getRouteDomain()) && StringUtils.isBlank(getConfig(Config.HOST))) {
logJKubeDomainHint();
}
}

private void logHintIfNoDomainOrHostForResourceConfig(ResourceConfig resourceConfig) {
List<IngressRuleConfig> ingressRuleConfigs = getIngressRuleXMLConfig(resourceConfig);
if (!ingressRuleConfigs.isEmpty()) {
Optional<String> configuredHost = ingressRuleConfigs.stream()
.map(IngressRuleConfig::getHost)
.filter(StringUtils::isNotBlank)
.findAny();
if (!configuredHost.isPresent()) {
logJKubeDomainHint();
}
}
}

private void logJKubeDomainHint() {
getContext().getLog().info("[[B]]HINT:[[B]] No host configured for Ingress. You might want to use `jkube.domain` to add desired host suffix");
}

static List<IngressRuleConfig> getIngressRuleXMLConfig(ResourceConfig resourceConfig) {
return Optional.ofNullable(resourceConfig).map(ResourceConfig::getIngress).map(IngressConfig::getIngressRules)
.orElse(Collections.emptyList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.fabric8.kubernetes.api.model.networking.v1.Ingress;
import io.fabric8.kubernetes.api.model.networking.v1.IngressSpec;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.resource.IngressConfig;
import org.eclipse.jkube.kit.config.resource.IngressRuleConfig;
Expand All @@ -44,6 +45,8 @@
import static org.eclipse.jkube.kit.enricher.api.BaseEnricher.CREATE_EXTERNAL_URLS;
import static org.mockito.Mockito.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 IngressEnricherTest {
Expand All @@ -53,9 +56,13 @@ class IngressEnricherTest {

private IngressEnricher ingressEnricher;

private KitLogger logger;

@BeforeEach
void setUp() throws Exception {
context = mock(JKubeEnricherContext.class,RETURNS_DEEP_STUBS);
logger = spy(new KitLogger.SilentLogger());
when(context.getLog()).thenReturn(logger);
imageConfiguration = mock(ImageConfiguration.class);
ingressEnricher = new IngressEnricher(context);
}
Expand Down Expand Up @@ -244,6 +251,42 @@ void networkingV1IngressIsGenerated() {
.containsExactly("test.192.168.39.25.nip.io");
}

@Test
void logHintIfNoDomainOrHostProvided_whenResourceConfigWithNoHost_thenLogHint() {
// Given
when(context.getConfiguration().getResource())
.thenReturn(ResourceConfig.builder()
.ingress(IngressConfig.builder()
.ingressRule(IngressRuleConfig.builder()
.path(IngressRulePathConfig.builder()
.path("/foo")
.serviceName("test-svc")
.servicePort(8080)
.build())
.build())
.build())
.build());

// When
ingressEnricher.logHintIfNoDomainOrHostProvided();

// Then
verify(logger)
.info("[[B]]HINT:[[B]] No host configured for Ingress. You might want to use `jkube.domain` to add desired host suffix");
}

@Test
void logHintIfNoDomainOrHostProvided_whenNoJkubeDomainNoHost_thenLogHint() {
// Given
when(context.getConfiguration().getResource()).thenReturn(null);

// When
ingressEnricher.logHintIfNoDomainOrHostProvided();

// Then
verify(logger)
.info("[[B]]HINT:[[B]] No host configured for Ingress. You might want to use `jkube.domain` to add desired host suffix");
}

private ServiceBuilder initTestService() {
return new ServiceBuilder()
Expand Down

0 comments on commit bb9794e

Please sign in to comment.