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 (jkube-kit/enricher) : Add hint for using jkube.domain if createExternalUrls is used without domain (#1439) #1980

Merged
merged 1 commit into from
Jan 5, 2023
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
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