This is an automated email from the ASF dual-hosted git repository.
kirs pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-dolphinscheduler.git
The following commit(s) were added to refs/heads/dev by this push:
new d667525 [Fix-4905][K8s] Fix incorrect host problem in minikube (#4906)
d667525 is described below
commit d6675259132c7eeebb183782bdc7f09fe04ff21b
Author: Shiwen Cheng <[email protected]>
AuthorDate: Thu Mar 4 10:05:24 2021 +0800
[Fix-4905][K8s] Fix incorrect host problem in minikube (#4906)
* [Fix][K8s] Fix incorrect host problem in minikube
* [Improvement][K8s] Rename MASTER_WORKER_STS_PATTERN to STS_PATTERN
---
.../dolphinscheduler/common/utils/NetUtils.java | 13 +++++-
.../apache/dolphinscheduler/common/CommonTest.java | 46 ++++++++++++++++++++++
.../common/utils/NetUtilsTest.java | 30 ++++++++++++--
3 files changed, 84 insertions(+), 5 deletions(-)
diff --git
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
index df4fb98..8a283c7 100644
---
a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
+++
b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/NetUtils.java
@@ -44,6 +44,7 @@ import org.slf4j.LoggerFactory;
*/
public class NetUtils {
+ private static final Pattern STS_PATTERN = Pattern.compile("-\\d+$"); //
StatefulSet pattern
private static final Pattern IP_PATTERN =
Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
private static final String NETWORK_PRIORITY_DEFAULT = "default";
private static final String NETWORK_PRIORITY_INNER = "inner";
@@ -78,7 +79,17 @@ public class NetUtils {
*/
public static String getHost(InetAddress inetAddress) {
if (inetAddress != null) {
- return Constants.KUBERNETES_MODE ? inetAddress.getHostName() :
inetAddress.getHostAddress();
+ if (Constants.KUBERNETES_MODE) {
+ String canonicalHost = inetAddress.getCanonicalHostName();
+ if (!canonicalHost.contains(".") ||
IP_PATTERN.matcher(canonicalHost).matches()) {
+ String host = inetAddress.getHostName();
+ if (STS_PATTERN.matcher(host).find()) {
+ return String.format("%s.%s", host,
host.replaceFirst("\\d+$", "headless"));
+ }
+ }
+ return canonicalHost;
+ }
+ return inetAddress.getHostAddress();
}
return null;
}
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/CommonTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/CommonTest.java
new file mode 100644
index 0000000..3752fe6
--- /dev/null
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/CommonTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dolphinscheduler.common;
+
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import org.junit.Test;
+
+/**
+ * CommonTest
+ */
+public class CommonTest {
+
+ public static void setFinalStatic(Field field, Object newValue) throws
NoSuchFieldException, IllegalAccessException {
+ field.setAccessible(true);
+ Field modifiersField = Field.class.getDeclaredField("modifiers");
+ modifiersField.setAccessible(true);
+ modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
+ field.set(null, newValue);
+ }
+
+ @Test
+ public void testSetFinalStatic() throws Exception {
+ setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
+ assertTrue(Constants.KUBERNETES_MODE);
+ }
+
+}
diff --git
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
index d2461ec..1202b58 100644
---
a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
+++
b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/NetUtilsTest.java
@@ -16,13 +16,19 @@
*/
package org.apache.dolphinscheduler.common.utils;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.dolphinscheduler.common.CommonTest;
+import org.apache.dolphinscheduler.common.Constants;
import java.net.InetAddress;
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import org.junit.Test;
/**
* NetUtilsTest
@@ -37,6 +43,22 @@ public class NetUtilsTest {
}
@Test
+ public void testGetHost() throws Exception {
+ InetAddress address = mock(InetAddress.class);
+
when(address.getCanonicalHostName()).thenReturn("dolphinscheduler-worker-0.dolphinscheduler-worker-headless.default.svc.cluster.local");
+ when(address.getHostName()).thenReturn("dolphinscheduler-worker-0");
+ when(address.getHostAddress()).thenReturn("172.17.0.15");
+ assertEquals("172.17.0.15", NetUtils.getHost(address));
+
CommonTest.setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
+
assertEquals("dolphinscheduler-worker-0.dolphinscheduler-worker-headless.default.svc.cluster.local",
NetUtils.getHost(address));
+ address = mock(InetAddress.class);
+
when(address.getCanonicalHostName()).thenReturn("dolphinscheduler-worker-0");
+ when(address.getHostName()).thenReturn("dolphinscheduler-worker-0");
+
CommonTest.setFinalStatic(Constants.class.getDeclaredField("KUBERNETES_MODE"),
true);
+
assertEquals("dolphinscheduler-worker-0.dolphinscheduler-worker-headless",
NetUtils.getHost(address));
+ }
+
+ @Test
public void testGetLocalHost() {
assertNotNull(NetUtils.getHost());
}