Skip to content

Commit 2a2cfb6

Browse files
mccheahash211
authored andcommitted
Add proxy configuration to retrofit clients. (apache-spark-on-k8s#301)
* Add proxy configuration to retrofit clients. * Add logging
1 parent dbf7a39 commit 2a2cfb6

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/rest/kubernetes/RetrofitClientFactory.scala

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,56 @@
1717
package org.apache.spark.deploy.rest.kubernetes
1818

1919
import java.io.FileInputStream
20+
import java.net.{InetSocketAddress, URI}
2021
import java.security.{KeyStore, SecureRandom}
2122
import javax.net.ssl.{SSLContext, TrustManagerFactory, X509TrustManager}
2223

2324
import com.fasterxml.jackson.databind.ObjectMapper
2425
import com.fasterxml.jackson.module.scala.DefaultScalaModule
26+
import io.fabric8.kubernetes.client.Config
2527
import okhttp3.{Dispatcher, OkHttpClient}
2628
import retrofit2.Retrofit
2729
import retrofit2.converter.jackson.JacksonConverterFactory
2830
import retrofit2.converter.scalars.ScalarsConverterFactory
2931

3032
import org.apache.spark.SSLOptions
33+
import org.apache.spark.internal.Logging
3134
import org.apache.spark.util.{ThreadUtils, Utils}
3235

3336
private[spark] trait RetrofitClientFactory {
3437
def createRetrofitClient[T](baseUrl: String, serviceType: Class[T], sslOptions: SSLOptions): T
3538
}
3639

37-
private[spark] object RetrofitClientFactoryImpl extends RetrofitClientFactory {
40+
private[spark] object RetrofitClientFactoryImpl extends RetrofitClientFactory with Logging {
3841

3942
private val OBJECT_MAPPER = new ObjectMapper().registerModule(new DefaultScalaModule)
4043
private val SECURE_RANDOM = new SecureRandom()
4144

4245
def createRetrofitClient[T](baseUrl: String, serviceType: Class[T], sslOptions: SSLOptions): T = {
4346
val dispatcher = new Dispatcher(ThreadUtils.newDaemonCachedThreadPool(s"http-client-$baseUrl"))
44-
val okHttpClientBuilder = new OkHttpClient.Builder().dispatcher(dispatcher)
47+
val serviceUri = URI.create(baseUrl)
48+
val maybeAllProxy = Option.apply(System.getProperty(Config.KUBERNETES_ALL_PROXY))
49+
val serviceUriScheme = serviceUri.getScheme
50+
val maybeHttpProxy = (if (serviceUriScheme.equalsIgnoreCase("https")) {
51+
Option.apply(System.getProperty(Config.KUBERNETES_HTTPS_PROXY))
52+
} else if (serviceUriScheme.equalsIgnoreCase("http")) {
53+
Option.apply(System.getProperty(Config.KUBERNETES_HTTP_PROXY))
54+
} else {
55+
maybeAllProxy
56+
}).map(uriStringToProxy)
57+
val maybeNoProxy = Option.apply(System.getProperty(Config.KUBERNETES_NO_PROXY))
58+
.map(_.split(","))
59+
.toSeq
60+
.flatten
61+
val resolvedProxy = maybeNoProxy.find(_ == serviceUri.getHost)
62+
.map( _ => java.net.Proxy.NO_PROXY)
63+
.orElse(maybeHttpProxy)
64+
.getOrElse(java.net.Proxy.NO_PROXY)
65+
val okHttpClientBuilder = new OkHttpClient.Builder()
66+
.dispatcher(dispatcher)
67+
.proxy(resolvedProxy)
68+
logDebug(s"Proxying to $baseUrl through address ${resolvedProxy.address()} with proxy of" +
69+
s" type ${resolvedProxy.`type`()}")
4570
sslOptions.trustStore.foreach { trustStoreFile =>
4671
require(trustStoreFile.isFile, s"TrustStore provided at ${trustStoreFile.getAbsolutePath}"
4772
+ " does not exist, or is not a file.")
@@ -69,4 +94,9 @@ private[spark] object RetrofitClientFactoryImpl extends RetrofitClientFactory {
6994
.create(serviceType)
7095
}
7196

97+
private def uriStringToProxy(uriString: String): java.net.Proxy = {
98+
val uriObject = URI.create(uriString)
99+
new java.net.Proxy(java.net.Proxy.Type.HTTP,
100+
new InetSocketAddress(uriObject.getHost, uriObject.getPort))
101+
}
72102
}

0 commit comments

Comments
 (0)