Skip to content

Commit ef33fb6

Browse files
author
Auri Munoz
committed
wip
1 parent d6355ac commit ef33fb6

File tree

7 files changed

+346
-27
lines changed

7 files changed

+346
-27
lines changed

extensions/smallrye-stork/runtime/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
<groupId>io.quarkus</groupId>
3535
<artifactId>quarkus-vertx</artifactId>
3636
</dependency>
37+
<dependency>
38+
<groupId>org.junit.jupiter</groupId>
39+
<artifactId>junit-jupiter</artifactId>
40+
<scope>test</scope>
41+
</dependency>
3742
</dependencies>
3843

3944
<build>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.quarkus.stork;
2+
3+
import java.net.InetAddress;
4+
import java.net.SocketException;
5+
import java.util.Enumeration;
6+
7+
public interface NetworkInterfaceWrapper {
8+
boolean isUp() throws SocketException;
9+
10+
boolean isLoopback() throws SocketException;
11+
12+
Enumeration<InetAddress> getInetAddresses();
13+
}

extensions/smallrye-stork/runtime/src/main/java/io/quarkus/stork/SmallRyeStorkRegistrationRecorder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.List;
44
import java.util.Map;
5+
import java.util.logging.Logger;
56

67
import org.eclipse.microprofile.config.Config;
78
import org.eclipse.microprofile.config.ConfigProvider;
@@ -14,6 +15,8 @@
1415
@Recorder
1516
public class SmallRyeStorkRegistrationRecorder {
1617

18+
private static final Logger LOGGER = Logger.getLogger(StorkRegistrarConfigRecorder.class.getName());
19+
1720
public void registerServiceInstance(StorkConfiguration configuration) {
1821
List<ServiceConfig> serviceConfigs = StorkConfigUtil.toStorkServiceConfig(configuration);
1922
Config quarkusConfig = ConfigProvider.getConfig();
@@ -23,6 +26,7 @@ public void registerServiceInstance(StorkConfiguration configuration) {
2326
StorkServiceRegistrarConfiguration storkServiceRegistrarConfiguration = configuration.serviceConfiguration()
2427
.get(serviceName).serviceRegistrar().get();
2528
if (!storkServiceRegistrarConfiguration.enabled()) {
29+
LOGGER.info("Service registering disabled for '" + serviceName + "'.");
2630
continue;
2731
}
2832
}

extensions/smallrye-stork/runtime/src/main/java/io/quarkus/stork/StorkConfigUtil.java

Lines changed: 144 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.quarkus.stork;
22

33
import java.io.IOException;
4+
import java.net.Inet4Address;
45
import java.net.InetAddress;
56
import java.net.NetworkInterface;
7+
import java.net.SocketException;
68
import java.net.UnknownHostException;
79
import java.util.ArrayList;
810
import java.util.Enumeration;
@@ -55,27 +57,30 @@ public static List<ServiceConfig> toStorkServiceConfig(StorkConfiguration storkC
5557
public static ServiceConfiguration buildDefaultRegistrarConfiguration(String serviceRegistrarType, String healthCheckPath) {
5658
Map<String, String> parameters = new HashMap<>();
5759
Config quarkusConfig = ConfigProvider.getConfig();
58-
String defaultHost = quarkusConfig.getValue(QUARKUS_HTTP_HOST, String.class);
5960
if (healthCheckPath != null && !healthCheckPath.isBlank()) {
6061
healthCheckPath = HTTPS + getOrDefaultHost(parameters, quarkusConfig) + ":"
6162
+ getOrDefaultPort(parameters, quarkusConfig) + healthCheckPath;
6263
parameters.put("health-check-url", healthCheckPath);
6364
}
64-
return buildServiceConfigurationWithRegistrar(serviceRegistrarType, parameters);
65+
return buildServiceConfigurationWithRegistrar(serviceRegistrarType, true, parameters);
6566
}
6667

6768
public static ServiceConfiguration addRegistrarTypeIfAbsent(String serviceRegistrarType,
6869
ServiceConfiguration serviceConfiguration, String healthCheckUrl) {
69-
Map<String, String> parameters = serviceConfiguration.serviceRegistrar()
70+
Optional<StorkServiceRegistrarConfiguration> storkServiceRegistrarConfiguration = serviceConfiguration
71+
.serviceRegistrar();
72+
Map<String, String> parameters = storkServiceRegistrarConfiguration
7073
.map(StorkServiceRegistrarConfiguration::parameters)
7174
.orElse(new HashMap<>());
7275
if (healthCheckUrl != null && !healthCheckUrl.isBlank()) {
7376
parameters.put("health-check-url", healthCheckUrl);
7477
}
75-
return buildServiceConfigurationWithRegistrar(serviceRegistrarType, parameters);
78+
boolean enabled = storkServiceRegistrarConfiguration.map(StorkServiceRegistrarConfiguration::enabled).orElse(true);
79+
return buildServiceConfigurationWithRegistrar(serviceRegistrarType, enabled, parameters);
7680
}
7781

78-
private static ServiceConfiguration buildServiceConfigurationWithRegistrar(String type, Map<String, String> parameters) {
82+
private static ServiceConfiguration buildServiceConfigurationWithRegistrar(String type, boolean enabled,
83+
Map<String, String> parameters) {
7984
return new ServiceConfiguration() {
8085
@Override
8186
public Optional<StorkServiceDiscoveryConfiguration> serviceDiscovery() {
@@ -89,17 +94,17 @@ public StorkLoadBalancerConfiguration loadBalancer() {
8994

9095
@Override
9196
public Optional<StorkServiceRegistrarConfiguration> serviceRegistrar() {
92-
return Optional.of(buildServiceRegistrarConfiguration(type, parameters));
97+
return Optional.of(buildServiceRegistrarConfiguration(type, enabled, parameters));
9398
}
9499
};
95100
}
96101

97-
private static StorkServiceRegistrarConfiguration buildServiceRegistrarConfiguration(String type,
102+
private static StorkServiceRegistrarConfiguration buildServiceRegistrarConfiguration(String type, boolean enabled,
98103
Map<String, String> parameters) {
99104
return new StorkServiceRegistrarConfiguration() {
100105
@Override
101106
public boolean enabled() {
102-
return true;
107+
return enabled;
103108
}
104109

105110
@Override
@@ -117,7 +122,7 @@ public Map<String, String> parameters() {
117122
public static String getOrDefaultHost(Map<String, String> parameters, Config quarkusConfig) {
118123
String customHost = parameters.containsKey("ip-address") ? parameters.get("ip-address")
119124
: null;
120-
String defaultHost = quarkusConfig.getValue("quarkus.http.host", String.class);
125+
String defaultHost = quarkusConfig.getValue(QUARKUS_HTTP_HOST, String.class);
121126
if (customHost == null || customHost.isEmpty()) {
122127
InetAddress inetAddress = StorkConfigUtil.detectAddress();
123128
customHost = inetAddress != null ? inetAddress.getHostAddress() : defaultHost;
@@ -130,29 +135,143 @@ public static int getOrDefaultPort(Map<String, String> parameters, Config quarku
130135
return Integer.parseInt(customPort);
131136
}
132137

138+
// public static InetAddress detectAddress() {
139+
// InetAddress result = null;
140+
// try {
141+
// int lowest = Integer.MAX_VALUE;
142+
// Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
143+
// while (networkInterfaces.hasMoreElements()) {
144+
// NetworkInterface networkInterface = networkInterfaces.nextElement();
145+
// if (networkInterface.isUp()) {
146+
// LOGGER.debug("Testing interface: {}" + networkInterface.getDisplayName());
147+
// if (networkInterface.getIndex() < lowest || result == null) {
148+
// lowest = networkInterface.getIndex();
149+
// } else if (result != null) {
150+
// continue;
151+
// }
152+
// }
153+
// }
154+
// } catch (IOException ex) {
155+
// LOGGER.error("Unable to get first non-loopback address", ex);
156+
// }
157+
// try {
158+
// return InetAddress.getLocalHost();
159+
// } catch (UnknownHostException e) {
160+
// LOGGER.error("Unable to detect address", e);
161+
// }
162+
//
163+
// return null;
164+
// }
165+
166+
// public static InetAddress detectAddress() {
167+
// try {
168+
// Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
169+
// while (interfaces.hasMoreElements()) {
170+
// NetworkInterface iface = interfaces.nextElement();
171+
// if (!iface.isUp() || iface.isLoopback())
172+
// continue;
173+
//
174+
// Enumeration<InetAddress> addresses = iface.getInetAddresses();
175+
// while (addresses.hasMoreElements()) {
176+
// InetAddress addr = addresses.nextElement();
177+
// if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
178+
// return addr;
179+
// }
180+
// }
181+
// }
182+
// } catch (IOException e) {
183+
// LOGGER.error("Failed to detect IP address", e);
184+
// }
185+
//
186+
// try {
187+
// return InetAddress.getLocalHost();
188+
// } catch (UnknownHostException e) {
189+
// LOGGER.error("Fallback to localhost failed", e);
190+
// return null;
191+
// }
192+
// }
193+
//
194+
// public static InetAddress detectAddress() {
195+
// try {
196+
// Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
197+
// return findFirstValidAddress(interfaces);
198+
// } catch (IOException e) {
199+
// LOGGER.error("Failed to detect IP address", e);
200+
// }
201+
//
202+
// try {
203+
// return InetAddress.getLocalHost();
204+
// } catch (UnknownHostException e) {
205+
// LOGGER.error("Fallback to localhost failed", e);
206+
// return null;
207+
// }
208+
// }
209+
210+
// static InetAddress findFirstValidAddress(Enumeration<NetworkInterface> interfaces) {
211+
// try {
212+
// while (interfaces.hasMoreElements()) {
213+
// NetworkInterface iface = interfaces.nextElement();
214+
// if (!iface.isUp() || iface.isLoopback()) {
215+
// continue;
216+
// }
217+
//
218+
// Enumeration<InetAddress> addresses = iface.getInetAddresses();
219+
// while (addresses.hasMoreElements()) {
220+
// InetAddress addr = addresses.nextElement();
221+
// if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
222+
// return addr;
223+
// }
224+
// }
225+
// }
226+
// } catch (IOException e) {
227+
// LOGGER.error("Error processing network interfaces", e);
228+
// }
229+
//
230+
// return null;
231+
// }
232+
133233
public static InetAddress detectAddress() {
134-
InetAddress result = null;
135234
try {
136-
int lowest = Integer.MAX_VALUE;
137-
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
138-
while (networkInterfaces.hasMoreElements()) {
139-
NetworkInterface networkInterface = networkInterfaces.nextElement();
140-
if (networkInterface.isUp()) {
141-
LOGGER.debug("Testing interface: {}" + networkInterface.getDisplayName());
142-
if (networkInterface.getIndex() < lowest || result == null) {
143-
lowest = networkInterface.getIndex();
144-
} else if (result != null) {
145-
continue;
146-
}
147-
}
235+
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
236+
List<NetworkInterfaceWrapper> wrappedInterfaces = new ArrayList<>();
237+
238+
while (interfaces.hasMoreElements()) {
239+
NetworkInterface ni = interfaces.nextElement();
240+
wrappedInterfaces.add(new StorkNetworkInterfaceWrapper(ni));
241+
}
242+
243+
InetAddress addr = findFirstValidAddress(wrappedInterfaces);
244+
if (addr != null) {
245+
return addr;
148246
}
149-
} catch (IOException ex) {
150-
LOGGER.error("Unable to get first non-loopback address", ex);
247+
} catch (IOException e) {
248+
LOGGER.error("Failed to detect IP address", e);
151249
}
250+
152251
try {
153252
return InetAddress.getLocalHost();
154253
} catch (UnknownHostException e) {
155-
LOGGER.error("Unable to detect address", e);
254+
LOGGER.error("Fallback to localhost failed", e);
255+
return null;
256+
}
257+
}
258+
259+
static InetAddress findFirstValidAddress(List<NetworkInterfaceWrapper> interfaces) {
260+
try {
261+
for (NetworkInterfaceWrapper iface : interfaces) {
262+
if (!iface.isUp() || iface.isLoopback())
263+
continue;
264+
265+
Enumeration<InetAddress> addresses = iface.getInetAddresses();
266+
while (addresses.hasMoreElements()) {
267+
InetAddress addr = addresses.nextElement();
268+
if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
269+
return addr;
270+
}
271+
}
272+
}
273+
} catch (SocketException e) {
274+
LOGGER.error("Error checking network interfaces", e);
156275
}
157276

158277
return null;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.quarkus.stork;
2+
3+
import java.net.InetAddress;
4+
import java.net.NetworkInterface;
5+
import java.net.SocketException;
6+
import java.util.Enumeration;
7+
8+
public class StorkNetworkInterfaceWrapper implements NetworkInterfaceWrapper {
9+
private final NetworkInterface networkInterface;
10+
11+
public StorkNetworkInterfaceWrapper(NetworkInterface networkInterface) {
12+
this.networkInterface = networkInterface;
13+
}
14+
15+
@Override
16+
public boolean isUp() throws SocketException {
17+
return networkInterface.isUp();
18+
}
19+
20+
@Override
21+
public boolean isLoopback() throws SocketException {
22+
return networkInterface.isLoopback();
23+
}
24+
25+
@Override
26+
public Enumeration<InetAddress> getInetAddresses() {
27+
return networkInterface.getInetAddresses();
28+
}
29+
}

extensions/smallrye-stork/runtime/src/main/java/io/quarkus/stork/StorkRegistrarConfigRecorder.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public void setupServiceRegistrarConfig(StorkConfiguration config, String servic
2525
config.serviceConfiguration().put(serviceName,
2626
StorkConfigUtil.buildDefaultRegistrarConfiguration(serviceRegistrarType, healthCheckUrl));
2727
} else if (registrationConfigs.size() == 1) {
28+
serviceName = registrationConfigs.get(0).serviceName();
2829
config.serviceConfiguration().computeIfPresent(serviceName,
2930
(k, serviceConfiguration) -> StorkConfigUtil.addRegistrarTypeIfAbsent(serviceRegistrarType,
3031
serviceConfiguration, healthCheckUrl));
@@ -44,8 +45,9 @@ private static void failOnMissingRegistrarTypes(List<ServiceConfig> registration
4445

4546
}
4647
if (!servicesWithMissingType.isEmpty()) {
47-
throw new IllegalArgumentException("Missing required 'type' for the following services: " +
48-
String.join(", ", servicesWithMissingType));
48+
throw new IllegalArgumentException(
49+
"Impossible to register service. Missing required 'type' for the following services: " +
50+
String.join(", ", servicesWithMissingType));
4951
}
5052
}
5153

0 commit comments

Comments
 (0)