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

update cf discovery defaults in service discovery #63

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.cloud.cloudfoundry.discovery.reactive;

import java.util.List;

import org.cloudfoundry.operations.CloudFoundryOperations;
import reactor.core.publisher.Flux;

Expand All @@ -37,8 +39,6 @@
public class CloudFoundryAppServiceReactiveDiscoveryClient
extends CloudFoundryNativeReactiveDiscoveryClient {

private static final String INTERNAL_DOMAIN = "apps.internal";

private final CloudFoundryService cloudFoundryService;

CloudFoundryAppServiceReactiveDiscoveryClient(
Expand All @@ -61,8 +61,8 @@ public Flux<ServiceInstance> getInstances(String serviceId) {
.map(this::mapApplicationInstanceToServiceInstance);
}

private boolean isInternalDomain(String url) {
return url != null && url.endsWith(INTERNAL_DOMAIN);
protected String getRouteURL(List<String> urls) {
return urls.stream().filter(this::isInternalDomain).findFirst().orElse("");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use stream apis please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, changed it

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.cloud.cloudfoundry.discovery.reactive;

import java.util.HashMap;
import java.util.List;

import org.cloudfoundry.operations.CloudFoundryOperations;
import org.cloudfoundry.operations.applications.ApplicationDetail;
Expand Down Expand Up @@ -85,15 +86,25 @@ protected ServiceInstance mapApplicationInstanceToServiceInstance(
String instanceId = applicationId + "." + applicationIndex;
String name = applicationDetail.getName();
String url = applicationDetail.getUrls().size() > 0
? applicationDetail.getUrls().get(0) : null;
boolean secure = (url + "").toLowerCase().startsWith("https");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change this?

Copy link
Author

@srinivasa-vasu srinivasa-vasu Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • getUrls().get(0) always returns the external URL. It's not picking up the apps.internal url when we use CloudFoundryAppServiceReactiveDiscoveryClient
  • protocol information is never returned as part of the cf-client call. URL info always gets returned without the protocol.

? getRouteURL(applicationDetail.getUrls()) : "";

HashMap<String, String> metadata = new HashMap<>();
metadata.put("applicationId", applicationId);
metadata.put("instanceId", applicationIndex);

return new DefaultServiceInstance(instanceId, name, url, secure ? 443 : 80,
secure, metadata);
return new DefaultServiceInstance(instanceId, name, url,
properties.getDefaultServerPort() != 80
? properties.getDefaultServerPort() : (isInternalDomain(url))
? 8080 : properties.getDefaultServerPort(),
false, metadata);
}

protected final boolean isInternalDomain(String url) {
return (url != null) && url.endsWith(properties.getInternalDomain());
}

protected String getRouteURL(List<String> urls) {
return urls.get(0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ public SimpleDnsBasedReactiveDiscoveryClient dnsBasedReactiveDiscoveryClient(
ObjectProvider<ServiceIdToHostnameConverter> provider,
CloudFoundryDiscoveryProperties properties) {
ServiceIdToHostnameConverter converter = provider.getIfAvailable();
return converter == null
? new SimpleDnsBasedReactiveDiscoveryClient(properties)
: new SimpleDnsBasedReactiveDiscoveryClient(converter);
return new SimpleDnsBasedReactiveDiscoveryClient(properties, converter);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ public class SimpleDnsBasedReactiveDiscoveryClient implements ReactiveDiscoveryC

private final ServiceIdToHostnameConverter serviceIdToHostnameConverter;

public SimpleDnsBasedReactiveDiscoveryClient(
spencergibb marked this conversation as resolved.
Show resolved Hide resolved
ServiceIdToHostnameConverter serviceIdToHostnameConverter) {
this.serviceIdToHostnameConverter = serviceIdToHostnameConverter;
}
private final CloudFoundryDiscoveryProperties properties;

public SimpleDnsBasedReactiveDiscoveryClient(
CloudFoundryDiscoveryProperties properties) {
this(serviceId -> serviceId + "." + properties.getInternalDomain());
CloudFoundryDiscoveryProperties properties,
ServiceIdToHostnameConverter serviceIdToHostnameConverter) {
this.properties = properties;
this.serviceIdToHostnameConverter = serviceIdToHostnameConverter == null
? (serviceId -> serviceId + "." + this.properties.getInternalDomain())
: serviceIdToHostnameConverter;
}

@Override
Expand All @@ -67,7 +68,13 @@ public Flux<ServiceInstance> getInstances(String serviceId) {
return Mono.justOrEmpty(serviceIdToHostnameConverter.toHostname(serviceId))
.flatMapMany(getInetAddresses())
.map(address -> new DefaultServiceInstance(serviceId,
address.getHostAddress(), 8080, false));
address.getHostAddress(),
properties.getDefaultServerPort() != 80
? properties.getDefaultServerPort()
: (serviceIdToHostnameConverter.toHostname(serviceId)
.endsWith(properties.getInternalDomain()) ? 8080
: properties.getDefaultServerPort()),
false));
}

private Function<String, Publisher<? extends InetAddress>> getInetAddresses() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.cloudfoundry.CloudFoundryService;
import org.springframework.cloud.cloudfoundry.discovery.CloudFoundryDiscoveryProperties;

import static org.mockito.Mockito.when;

Expand All @@ -44,11 +45,15 @@ class CloudFoundryAppServiceReactiveDiscoveryClientTests {
@Mock
private CloudFoundryService svc;

@Mock
private CloudFoundryDiscoveryProperties properties;

@InjectMocks
private CloudFoundryAppServiceReactiveDiscoveryClient client;

@Test
public void shouldReturnFluxOfServiceInstances() {
when(properties.getInternalDomain()).thenReturn("apps.internal");
ApplicationDetail appDetail1 = ApplicationDetail.builder()
.id(UUID.randomUUID().toString()).stack("stack").instances(1)
.memoryLimit(1024).requestedState("requestedState").diskQuota(1024)
Expand Down