Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Determine kubernetes service host and port from environment if available #1086

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down