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

Configuration option to enable gzip compression on k8s pods api for service discovery #336

Merged
merged 3 commits into from
Oct 18, 2024
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
6 changes: 6 additions & 0 deletions discovery-kubernetes-api/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,11 @@ pekko.discovery {

# When set, validate the container is not in 'waiting' state
container-name = ""

# Enables querying the Kubernetes HTTP API with 'Accept-Encoding: <http-request-accept-encoding>' header, e.g. in order to compress response payload
#
# The empty string "" will result in no encoding being requested. E.g. use "gzip" in order to use gzip compression.
# Supports all encodings of `org.apache.pekko.http.scaladsl.model.headers.HttpEncodings`.
http-request-accept-encoding = ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import scala.util.Try
import scala.util.control.{ NoStackTrace, NonFatal }

import org.apache.pekko
import org.apache.pekko.http.javadsl.model.headers.AcceptEncoding
import org.apache.pekko.http.scaladsl.coding.Coders
import pekko.actor.ActorSystem
import pekko.annotation.InternalApi
import pekko.discovery.ServiceDiscovery.{ Resolved, ResolvedTarget }
Expand All @@ -34,7 +36,7 @@ import pekko.event.{ LogSource, Logging }
import pekko.http.scaladsl.HttpsConnectionContext
import pekko.http.scaladsl._
import pekko.http.scaladsl.model._
import pekko.http.scaladsl.model.headers.{ Authorization, OAuth2BearerToken }
import pekko.http.scaladsl.model.headers.{ Authorization, HttpEncodings, OAuth2BearerToken }
import pekko.http.scaladsl.unmarshalling.Unmarshal
import pekko.pki.kubernetes.PemManagersProvider

Expand Down Expand Up @@ -141,7 +143,7 @@ class KubernetesApiServiceDiscovery(settings: Settings)(
podRequest(apiToken, podNamespace, labelSelector),
s"Unable to form request; check Kubernetes environment (expecting env vars ${settings.apiServiceHostEnvName}, ${settings.apiServicePortEnvName})")

response <- http.singleRequest(request, clientSslContext)
response <- http.singleRequest(request, clientSslContext).map(decodeResponse)

entity <- response.entity.toStrict(resolveTimeout)

Expand Down Expand Up @@ -236,6 +238,21 @@ class KubernetesApiServiceDiscovery(settings: Settings)(
val query = Uri.Query("labelSelector" -> labelSelector)
val uri = Uri.from(scheme = "https", host = host, port = port).withPath(path).withQuery(query)

HttpRequest(uri = uri, headers = Seq(Authorization(OAuth2BearerToken(token))))
val authHeaders = Seq(Authorization(OAuth2BearerToken(token)))
val acceptEncodingHeader = HttpEncodings.getForKey(settings.httpRequestAcceptEncoding)
.map(httpEncoding => AcceptEncoding.create(httpEncoding))
HttpRequest(uri = uri, headers = authHeaders ++ acceptEncodingHeader)
}

private def decodeResponse(response: HttpResponse): HttpResponse = {
val decoder = response.encoding match {
case HttpEncodings.gzip =>
Coders.Gzip
case HttpEncodings.deflate =>
Coders.Deflate
case _ =>
thjaeckle marked this conversation as resolved.
Show resolved Hide resolved
Coders.NoCoding
}
decoder.decodeMessage(response)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ final class Settings(kubernetesApi: Config) extends Extension {

val containerName: Option[String] = Some(kubernetesApi.getString("container-name")).filter(_.nonEmpty)

val httpRequestAcceptEncoding: String = kubernetesApi.getString("http-request-accept-encoding")

override def toString =
s"Settings($apiCaPath, $apiTokenPath, $apiServiceHostEnvName, $apiServicePortEnvName, " +
s"$podNamespacePath, $podNamespace, $podDomain)"
s"$podNamespacePath, $podNamespace, $podDomain, httpRequestAcceptEncoding=$httpRequestAcceptEncoding)"
}

object Settings extends ExtensionId[Settings] with ExtensionIdProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pekko.discovery {
kubernetes-api {
# in fact, this is already the default:
pod-label-selector = "app=%s"
# ask for gzip compression as "Accept-Encoding" during the test:
http-request-accept-encoding = "gzip"
}
}

Expand Down
Loading