Skip to content

Commit

Permalink
fix fqdn name query on ECS
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelianhan007 committed Nov 29, 2024
1 parent 5251908 commit 6f5ea9e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.consoleconnect.kraken.operator.core.toolkit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
Expand Down Expand Up @@ -53,9 +56,17 @@ public static String getHostAddress() {

public static String getFQDN() {
try {
InetAddress address = InetAddress.getLocalHost();
return address.getCanonicalHostName();
} catch (UnknownHostException e) {
ProcessBuilder builder = new ProcessBuilder();
builder.command("bash", "-c", "hostname -f");
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
StringBuilder result = new StringBuilder();
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} catch (IOException e) {
return IP_UNKNOWN;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ void givenRequest_whenGetIpAddress_thenReturnIpAddress() {
String ip = IpUtils.getIP(request);
Assertions.assertNotNull(ip);
}

@Test
void givenECS_whenQueryFQDN_thenReturnSuccess() {
String result = IpUtils.getFQDN();
Assertions.assertNotEquals("unknown", result);
}
}

0 comments on commit 6f5ea9e

Please sign in to comment.