diff --git a/CHANGELOG.md b/CHANGELOG.md index 916e1c7ef8c..dc3ddd76840 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ New Feature Improvements - * Do not repeatly create Config instance in exec - https://github.com/fabric8io/kubernetes-client/pull/1081 + * Do not repeatedly create Config instance in exec - https://github.com/fabric8io/kubernetes-client/pull/1081 + * Determine kubernetes service host and port from environment if available - https://github.com/fabric8io/kubernetes-client/pull/1086 #### 3.1.12 Bugs diff --git a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java index d7a6673841a..f7c94995b4d 100644 --- a/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java +++ b/kubernetes-client/src/main/java/io/fabric8/kubernetes/client/Config.java @@ -87,6 +87,8 @@ public class Config { public static final String KUBERNETES_NAMESPACE_FILE = "kubenamespace"; public static final String KUBERNETES_NAMESPACE_SYSTEM_PROPERTY = "kubernetes.namespace"; public static final String KUBERNETES_KUBECONFIG_FILE = "kubeconfig"; + public static final String KUBERNETES_SERVICE_HOST_PROPERTY = "KUBERNETES_SERVICE_HOST"; + public static final String KUBERNETES_SERVICE_PORT_PROPERTY = "KUBERNETES_SERVICE_PORT"; public static final String KUBERNETES_SERVICE_ACCOUNT_TOKEN_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/token"; public static final String KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"; public static final String KUBERNETES_HTTP_PROXY = "http.proxy"; @@ -329,6 +331,13 @@ public static void configFromSysPropsOrEnvVars(Config config) { private static boolean tryServiceAccount(Config config) { LOGGER.debug("Trying to configure client from service account..."); + String masterHost = Utils.getSystemPropertyOrEnvVar(KUBERNETES_SERVICE_HOST_PROPERTY, (String) null); + String masterPort = Utils.getSystemPropertyOrEnvVar(KUBERNETES_SERVICE_PORT_PROPERTY, (String) null); + if (masterHost != null && masterPort != null) { + String hostPort = joinHostPort(masterHost, masterPort); + LOGGER.debug("Found service account host and port: " + hostPort); + config.setMasterUrl("https://" + hostPort); + } if (Utils.getSystemPropertyOrEnvVar(KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, true)) { boolean serviceAccountCaCertExists = Files.isRegularFile(new File(KUBERNETES_SERVICE_ACCOUNT_CA_CRT_PATH).toPath()); if (serviceAccountCaCertExists) { @@ -357,6 +366,14 @@ private static boolean tryServiceAccount(Config config) { return false; } + private static String joinHostPort(String host, String port) { + if (host.indexOf(':') >= 0) { + // Host is an IPv6 + return "[" + host + "]:" + port; + } + return host + ":" + port; + } + private static String absolutify(File relativeTo, String filename) { if (filename == null) { return null; diff --git a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java index 05dce5baf36..8d51b10048c 100644 --- a/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java +++ b/kubernetes-client/src/test/java/io/fabric8/kubernetes/client/ConfigTest.java @@ -73,6 +73,8 @@ public void setUp() { System.getProperties().remove(Config.KUBERNETES_TRUSTSTORE_PASSPHRASE_PROPERTY); System.getProperties().remove(Config.KUBERNETES_KEYSTORE_FILE_PROPERTY); System.getProperties().remove(Config.KUBERNETES_KEYSTORE_PASSPHRASE_PROPERTY); + System.getProperties().remove(Config.KUBERNETES_SERVICE_HOST_PROPERTY); + System.getProperties().remove(Config.KUBERNETES_SERVICE_PORT_PROPERTY); } @After @@ -196,6 +198,34 @@ private static String decodeUrl(String url) { } } + @Test + public void testWithServiceAccount() { + System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); + Config config = Config.autoConfigure(null); + assertNotNull(config); + assertEquals("https://kubernetes.default.svc/", config.getMasterUrl()); + } + + @Test + public void testMasterUrlWithServiceAccount() { + System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); + System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "10.0.0.1"); + System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443"); + Config config = Config.autoConfigure(null); + assertNotNull(config); + assertEquals("https://10.0.0.1:443/", config.getMasterUrl()); + } + + @Test + public void testMasterUrlWithServiceAccountIPv6() { + System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, "/dev/null"); + System.setProperty(Config.KUBERNETES_SERVICE_HOST_PROPERTY, "2001:db8:1f70::999:de8:7648:6e8"); + System.setProperty(Config.KUBERNETES_SERVICE_PORT_PROPERTY, "443"); + Config config = Config.autoConfigure(null); + assertNotNull(config); + assertEquals("https://[2001:db8:1f70::999:de8:7648:6e8]:443/", config.getMasterUrl()); + } + @Test public void testWithKubeConfig() { System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE, TEST_KUBECONFIG_FILE);