Skip to content

Commit 150c111

Browse files
authored
feat(proxy) : Config supports proxy-url field in .kube/config cluster configuration
Set Config's `httpProxy` / `httpsProxy` fields if current context's cluster configuration has `proxy-url` set Signed-off-by: Rohan Kumar <[email protected]>
1 parent b7ecb18 commit 150c111

File tree

5 files changed

+495
-189
lines changed

5 files changed

+495
-189
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* Fix #6052: Removed dependency on no longer maintained com.github.mifmif:generex
1414

1515
#### New Features
16+
* Fix #6150: Config uses `proxy-url` in kubeconfig's cluster configuration
1617

1718
#### _**Note**_: Breaking changes
1819
* Check detailed migration documentation for breaking changes in [7.0.0](./doc/MIGRATION-v7.md)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ System properties are preferred over environment variables. The following system
118118
| `kubernetes.keystore.passphrase` / `KUBERNETES_KEYSTORE_PASSPHRASE` | | |
119119
| `kubernetes.backwardsCompatibilityInterceptor.disable` / `KUBERNETES_BACKWARDSCOMPATIBILITYINTERCEPTOR_DISABLE` | Disable the `BackwardsCompatibilityInterceptor` | `true` |
120120
| `no.proxy` / `NO_PROXY` | comma-separated list of domain extensions [proxy](http://www.gnu.org/software/wget/manual/html_node/Proxies.html) should not be used for | |
121+
| `http.proxy` / `HTTP_PROXY` | URL to the [proxy](http://www.gnu.org/software/wget/manual/html_node/Proxies.html) for HTTP requests (See [Proxy precedence](./doc/FAQ.md#how-does-kubernetesclient-loads-proxy-url-from-various-sources)) | |
122+
| `https.proxy` / `HTTPS_PROXY` | URL to the [proxy](http://www.gnu.org/software/wget/manual/html_node/Proxies.html) for HTTPS requests (See [Proxy precedence](./doc/FAQ.md#how-does-kubernetesclient-loads-proxy-url-from-various-sources)) | |
121123

122124
Alternatively you can use the `ConfigBuilder` to create a config object for the Kubernetes client:
123125

doc/FAQ.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ we should provide it like this:
110110
NO_PROXY: localhost,127.0.0.1,.google.com,.github.com
111111
```
112112

113+
### How does KubernetesClient loads proxy URL from various sources?
114+
115+
KubernetesClient loads proxy URL from the following sources (in decreasing order of precedence):
116+
- `ConfigBuilder.withHttpProxy` / `ConfigBuilder.withHttpsProxy`
117+
- Cluster's `proxy-url` in `~/.kube/config`
118+
- System Properties or Environment Variables
119+
- `HTTP_PROXY` : Should be used for HTTP requests (when Kubernetes ApiServer is serving plain HTTP requests)
120+
- `HTTPS_PROXY` : Should be used for HTTPS requests (when Kubernetes ApiServer is serving HTTPS)
121+
122+
URLs with `http`, `https`, and `socks5` schemes are supported.
123+
113124
### Optimistic Locking Behavior
114125

115126
Unfortunately it's a little complicated as it depends on what operation you are doing - we'll work towards ensuring the Javadocs are as informative as possible. Here is quick overview:

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public class Config {
153153
private static final int DEFAULT_WATCH_RECONNECT_INTERVAL = 1000;
154154
private static final int DEFAULT_CONNECTION_TIMEOUT = 10 * 1000;
155155
private static final String DEFAULT_CLIENT_KEY_PASSPHRASE = "changeit";
156+
private static final String SOCKS5_PROTOCOL_PREFIX = "socks5://";
156157

157158
private Boolean trustCerts;
158159
private Boolean disableHostnameVerification;
@@ -696,11 +697,17 @@ public static void configFromSysPropsOrEnvVars(Config config) {
696697

697698
config.setHttp2Disable(Utils.getSystemPropertyOrEnvVar(KUBERNETES_HTTP2_DISABLE, config.isHttp2Disable()));
698699

699-
config.setHttpProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_ALL_PROXY, config.getHttpProxy()));
700-
config.setHttpsProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_ALL_PROXY, config.getHttpsProxy()));
701-
702-
config.setHttpsProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_HTTPS_PROXY, config.getHttpsProxy()));
703-
config.setHttpProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_HTTP_PROXY, config.getHttpProxy()));
700+
// Only set http(s) proxy fields if they're not set. This is done in order to align behavior of
701+
// KubernetesClient with kubectl / client-go . Please see https://github.com/fabric8io/kubernetes-client/issues/6150
702+
// Precedence is given to proxy-url read from kubeconfig .
703+
if (Utils.isNullOrEmpty(config.getHttpProxy())) {
704+
config.setHttpProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_ALL_PROXY, config.getHttpProxy()));
705+
config.setHttpProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_HTTP_PROXY, config.getHttpProxy()));
706+
}
707+
if (Utils.isNullOrEmpty(config.getHttpsProxy())) {
708+
config.setHttpsProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_ALL_PROXY, config.getHttpsProxy()));
709+
config.setHttpsProxy(Utils.getSystemPropertyOrEnvVar(KUBERNETES_HTTPS_PROXY, config.getHttpsProxy()));
710+
}
704711

705712
config.setProxyUsername(Utils.getSystemPropertyOrEnvVar(KUBERNETES_PROXY_USERNAME, config.getProxyUsername()));
706713
config.setProxyPassword(Utils.getSystemPropertyOrEnvVar(KUBERNETES_PROXY_PASSWORD, config.getProxyPassword()));
@@ -926,6 +933,18 @@ private static void mergeKubeConfigContents(Config config, String context, io.fa
926933
if (currentAuthInfo != null) {
927934
mergeKubeConfigAuthInfo(config, currentCluster, currentAuthInfo);
928935
}
936+
String proxyUrl = currentCluster.getProxyUrl();
937+
if (Utils.isNotNullOrEmpty(proxyUrl)) {
938+
if (proxyUrl.startsWith(SOCKS5_PROTOCOL_PREFIX) && config.getMasterUrl().startsWith(HTTPS_PROTOCOL_PREFIX)) {
939+
config.setHttpsProxy(proxyUrl);
940+
} else if (proxyUrl.startsWith(SOCKS5_PROTOCOL_PREFIX)) {
941+
config.setHttpProxy(proxyUrl);
942+
} else if (proxyUrl.startsWith(HTTP_PROTOCOL_PREFIX)) {
943+
config.setHttpProxy(proxyUrl);
944+
} else if (proxyUrl.startsWith(HTTPS_PROTOCOL_PREFIX)) {
945+
config.setHttpsProxy(proxyUrl);
946+
}
947+
}
929948
}
930949
}
931950

0 commit comments

Comments
 (0)