Skip to content

Commit

Permalink
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 8454e28

Please sign in to comment.