Skip to content

Commit

Permalink
docs: add docs snip for azure rbac aks api discovery
Browse files Browse the repository at this point in the history
add: License file
refactor: general clean up
  • Loading branch information
girdharshubham committed Aug 15, 2024
1 parent 00c4825 commit 148202e
Show file tree
Hide file tree
Showing 13 changed files with 903 additions and 35 deletions.
2 changes: 1 addition & 1 deletion discovery-azure-api/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ akka.discovery {
# Set the following in your application.conf if you want to use this discovery mechanism:
# method = azure-rbac-aks-api
azure-rbac-aks-api {
class = akka.discovery.azureapi.AzureRbacAksServiceDiscovery
class = akka.discovery.azureapi.rbac.aks.AzureRbacAksServiceDiscovery

authority-host = "https://login.microsoftonline.com/"
authority-host = ${?AZURE_AUTHORITY_HOST}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Copyright (C) 2017-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.discovery.azureapi
package akka.discovery.azureapi.rbac.aks

import akka.actor.ExtendedActorSystem
import akka.annotation.InternalApi
import akka.discovery.ServiceDiscovery.{ Resolved, ResolvedTarget }
import akka.discovery.azureapi.AzureRbacAksServiceDiscovery._
import akka.discovery.azureapi.JsonFormat._
import AzureRbacAksServiceDiscovery._
import JsonFormat._
import akka.discovery.{ Lookup, ServiceDiscovery }
import akka.dispatch.Dispatchers.DefaultBlockingDispatcherId
import akka.event.Logging
Expand Down Expand Up @@ -45,7 +45,7 @@ object AzureRbacAksServiceDiscovery {
* to do that.
*/
@InternalApi
private[azureapi] def targets(
private[aks] def targets(
podList: PodList,
portName: Option[String],
podNamespace: String,
Expand Down Expand Up @@ -99,7 +99,7 @@ class AzureRbacAksServiceDiscovery(implicit system: ExtendedActorSystem) extends

log.debug("Settings {}", settings)

private def fetchToken: Future[AccessToken] =
private def fetchAccessToken: Future[AccessToken] =
azureDefaultCredential.getToken(accessTokenRequestContext.addScopes(settings.entraServerId)).toFuture.asScala

private val kubernetesSetup = {
Expand All @@ -118,9 +118,9 @@ class AzureRbacAksServiceDiscovery(implicit system: ExtendedActorSystem) extends

import system.dispatcher

private def accessToken(retries: Int = 3): Future[AccessToken] = {
fetchToken.flatMap {
case token if token.isExpired && retries > 0 => accessToken(retries - 1)
private def fetchAccessToken(retries: Int = 3): Future[AccessToken] = {
fetchAccessToken.flatMap {
case token if token.isExpired && retries > 0 => fetchAccessToken(retries - 1)
case token if token.isExpired =>
Future.failed(new RuntimeException("Failed to fetch a valid token after multiple attempts"))
case token => Future.successful(token)
Expand All @@ -133,22 +133,18 @@ class AzureRbacAksServiceDiscovery(implicit system: ExtendedActorSystem) extends
for {
ks <- kubernetesSetup

at <- accessToken()

request <- {
log.info(
"Querying for pods with label selector: [{}]. Namespace: [{}]. Port: [{}]",
selector,
ks.namespace,
lookup.portName
)

optionToFuture(
// FIXME | remove .get
podRequest(at.getToken, ks.namespace, selector),
s"Unable to form request; check Kubernetes environment (expecting env vars ${settings.apiServiceHostEnvName}, ${settings.apiServicePortEnvName})"
)
}
at <- fetchAccessToken()

_ = log.info(
"Querying for pods with label selector: [{}]. Namespace: [{}]. Port: [{}]",
selector,
ks.namespace,
lookup.portName)

request <- optionToFuture(
podRequest(at.getToken, ks.namespace, selector),
s"Unable to form request; check Kubernetes environment (expecting env vars ${settings.apiServiceHostEnvName}, ${settings.apiServicePortEnvName})"
)

response <- http.singleRequest(request, ks.ctx)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
* Copyright (C) 2017-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.discovery.azureapi
package akka.discovery.azureapi.rbac.aks

import akka.annotation.InternalApi
import akka.discovery.azureapi.PodList._
import PodList._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json._

/**
* INTERNAL API
*/
@InternalApi private[azureapi] object JsonFormat extends SprayJsonSupport with DefaultJsonProtocol {
// If adding more formats here, remember to also add in META-INF/native-image reflect config
@InternalApi private[aks] object JsonFormat extends SprayJsonSupport with DefaultJsonProtocol {
implicit val containerPortFormat: JsonFormat[ContainerPort] = jsonFormat2(ContainerPort.apply)
implicit val containerFormat: JsonFormat[Container] = jsonFormat2(Container.apply)
implicit val podSpecFormat: JsonFormat[PodSpec] = jsonFormat1(PodSpec.apply)
implicit val containerStatusFormat: JsonFormat[ContainerStatus] = jsonFormat2(ContainerStatus.apply)
implicit val podStatusFormat: JsonFormat[PodStatus] = jsonFormat3(PodStatus.apply)
implicit val metadataFormat: JsonFormat[Metadata] = jsonFormat1(Metadata.apply)
implicit val metadataFormat: JsonFormat[Metadata] = jsonFormat2(Metadata.apply)
implicit val podFormat: JsonFormat[Pod] = jsonFormat3(Pod.apply)
implicit val podListFormat: RootJsonFormat[PodList] = jsonFormat1(PodList.apply)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2017-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.discovery.azureapi
package akka.discovery.azureapi.rbac.aks

import akka.annotation.InternalApi

Expand All @@ -11,8 +11,9 @@ import scala.collection.immutable
/**
* INTERNAL API
*/
@InternalApi private[azureapi] object PodList {
final case class Metadata(deletionTimestamp: Option[String])
@InternalApi
private[aks] object PodList {
final case class Metadata(deletionTimestamp: Option[String], labels: Map[String, String] = Map.empty)

final case class ContainerPort(name: Option[String], containerPort: Int)

Expand All @@ -33,4 +34,5 @@ import scala.collection.immutable
/**
* INTERNAL API
*/
@InternalApi private[azureapi] final case class PodList(items: immutable.Seq[PodList.Pod])
@InternalApi
private[aks] final case class PodList(items: immutable.Seq[PodList.Pod])
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2017-2024 Lightbend Inc. <https://www.lightbend.com>
*/

package akka.discovery.azureapi
package akka.discovery.azureapi.rbac.aks

import akka.actor.{
ActorSystem,
Expand Down
Loading

0 comments on commit 148202e

Please sign in to comment.