diff --git a/libs/java/server_k8s_common/src/main/java/io/athenz/server/k8s/common/impl/KubernetesPodResolverUtil.java b/libs/java/server_k8s_common/src/main/java/io/athenz/server/k8s/common/impl/KubernetesPodResolverUtil.java new file mode 100644 index 00000000000..00291e448fc --- /dev/null +++ b/libs/java/server_k8s_common/src/main/java/io/athenz/server/k8s/common/impl/KubernetesPodResolverUtil.java @@ -0,0 +1,28 @@ +/* + * Copyright The Athenz Authors + * + * Licensed 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 io.athenz.server.k8s.common.impl; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class KubernetesPodResolverUtil { + public static InetAddress[] getSiblingPodIPs(String headlessServiceName) throws UnknownHostException { + if (headlessServiceName == null || headlessServiceName.isBlank()) { + throw new IllegalArgumentException("k8s headless FQDN name is required"); + } + return InetAddress.getAllByName(headlessServiceName); + } +} diff --git a/libs/java/server_k8s_common/src/test/java/io/athenz/server/k8s/common/impl/KubernetesPodResolverUtilTest.java b/libs/java/server_k8s_common/src/test/java/io/athenz/server/k8s/common/impl/KubernetesPodResolverUtilTest.java new file mode 100644 index 00000000000..3ca48c54347 --- /dev/null +++ b/libs/java/server_k8s_common/src/test/java/io/athenz/server/k8s/common/impl/KubernetesPodResolverUtilTest.java @@ -0,0 +1,83 @@ +/* + * Copyright The Athenz Authors + * + * Licensed 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 io.athenz.server.k8s.common.impl; + +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +public class KubernetesPodResolverUtilTest { + + @Test + public void testGetPodSiblings() throws UnknownHostException { + String serviceName = "msd.api.svc.cluster.local"; + InetAddress podAddress1 = Mockito.mock(InetAddress.class); + InetAddress podAddress2 = Mockito.mock(InetAddress.class); + Mockito.when(podAddress1.getHostAddress()).thenReturn("192.168.1.10"); + Mockito.when(podAddress2.getHostAddress()).thenReturn("192.168.1.11"); + + MockedStatic inetAddressMock = Mockito.mockStatic(InetAddress.class); + inetAddressMock.when(() -> InetAddress.getAllByName(serviceName)).thenReturn(new InetAddress[]{podAddress1, podAddress2}); + InetAddress[] podsIPs = KubernetesPodResolverUtil.getSiblingPodIPs(serviceName); + Assert.assertEquals(podsIPs.length, 2); + inetAddressMock.close(); + } + + @Test + public void testGetPodSiblingsEmptyServiceNameException() { + String serviceName = ""; + Exception ex = null; + try { + KubernetesPodResolverUtil.getSiblingPodIPs(serviceName); + } catch (IllegalArgumentException | UnknownHostException e) { + ex = e; + } + if (ex == null) { + Assert.fail("expected IllegalArgumentException not thrown"); + } + + Exception nullEx = null; + try { + KubernetesPodResolverUtil.getSiblingPodIPs(null); + } catch (IllegalArgumentException | UnknownHostException e) { + nullEx = e; + } + if (nullEx == null) { + Assert.fail("expected IllegalArgumentException not thrown"); + } + } + + @Test + public void testGetPodSiblingsInvalidHostnameException() { + String serviceName = "foo"; + MockedStatic inetAddressMock = Mockito.mockStatic(InetAddress.class); + inetAddressMock.when(() -> InetAddress.getAllByName(serviceName)).thenThrow(UnknownHostException.class); + Exception ex = null; + try { + KubernetesPodResolverUtil.getSiblingPodIPs(serviceName); + } catch (IllegalArgumentException | UnknownHostException e) { + ex = e; + } + if (ex == null) { + Assert.fail("expected UnknownHostException not thrown"); + } + inetAddressMock.close(); + } +}