Skip to content

Commit

Permalink
and then there was only one...
Browse files Browse the repository at this point in the history
  • Loading branch information
leviramsey committed Jul 30, 2024
1 parent 220d106 commit c1380d1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class KubernetesApiIntegrationTest
crName = None,
cleanupAfter = 60.seconds
),
revisionAnnotations = KubernetesJsonSupport.defaultRevisionAnnotations.revisionAnnotations
revisionAnnotation = KubernetesJsonSupport.defaultRevisionAnnotation.revisionAnnotation
)

private val underTest =
Expand Down
7 changes: 3 additions & 4 deletions rolling-update-kubernetes/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ akka.rollingupdate.kubernetes {
pod-name = ""
pod-name = ${?KUBERNETES_POD_NAME}

# Annotations to check to determine the revision. The first one in the list which matches
# wins. The default is suitable for "vanilla" Kubernetes Deployments, but other CI/CD systems
# may set a different annotation.
revision-annotation = [ "deployment.kubernetes.io/revision" ]
# Annotations to check to determine the revision. The default is suitable for "vanilla"
# Kubernetes Deployments, but other CI/CD systems may set a different annotation.
revision-annotation = "deployment.kubernetes.io/revision"

secure-api-server = true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import scala.util.control.NonFatal

import system.dispatcher

override val _revisionAnnotations = settings
override val _revisionAnnotation: HasRevisionAnnotation = settings

private implicit val sys: ActorSystem = system
private val log = Logging(system, classOf[KubernetesApiImpl])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ case class Spec(pods: immutable.Seq[PodCost])
*/
@InternalApi
trait KubernetesJsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
val _revisionAnnotations: HasRevisionAnnotations = KubernetesJsonSupport.defaultRevisionAnnotations
val _revisionAnnotation: HasRevisionAnnotation = KubernetesJsonSupport.defaultRevisionAnnotation

// If adding more formats here, remember to also add in META-INF/native-image reflect config
implicit val metadataFormat: JsonFormat[Metadata] = jsonFormat2(Metadata.apply)
Expand All @@ -102,16 +102,12 @@ trait KubernetesJsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
def read(json: JsValue): ReplicaAnnotation = {
json match {
case JsObject(fields) =>
_revisionAnnotations.revisionAnnotations.find { annotation =>
fields.get(annotation).exists(_.isInstanceOf[JsString])
} match {
case Some(winningAnnotation) =>
ReplicaAnnotation(
fields(winningAnnotation).asInstanceOf[JsString].value,
winningAnnotation,
fields - winningAnnotation)

case None =>
val annotation = _revisionAnnotation.revisionAnnotation
fields.get(annotation) match {
case Some(JsString(foundRevision)) =>
ReplicaAnnotation(foundRevision, annotation, fields - annotation)

case _ =>
ReplicaAnnotation("", "", fields)
}

Expand All @@ -125,7 +121,7 @@ trait KubernetesJsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
}

private[kubernetes] object KubernetesJsonSupport {
val defaultRevisionAnnotations: HasRevisionAnnotations = new HasRevisionAnnotations {
val revisionAnnotations = Seq("deployment.kubernetes.io/revision")
val defaultRevisionAnnotation: HasRevisionAnnotation = new HasRevisionAnnotation {
val revisionAnnotation = "deployment.kubernetes.io/revision"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private[kubernetes] object KubernetesSettings {
config.getBoolean("secure-api-server"),
config.getDuration("api-service-request-timeout").asScala,
customResourceSettings,
config.getStringList("revision-annotation").asScala.toList
config.getString("revision-annotation")
)
}
}
Expand All @@ -70,16 +70,16 @@ private[kubernetes] class KubernetesSettings(
val secure: Boolean,
val apiServiceRequestTimeout: FiniteDuration,
val customResourceSettings: CustomResourceSettings,
val revisionAnnotations: Seq[String],
val revisionAnnotation: String,
val bodyReadTimeout: FiniteDuration = 1.second
) extends HasRevisionAnnotations
) extends HasRevisionAnnotation

/**
* INTERNAL API
*/
@InternalApi
private[kubernetes] trait HasRevisionAnnotations {
def revisionAnnotations: Seq[String]
private[kubernetes] trait HasRevisionAnnotation {
def revisionAnnotation: String
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class KubernetesApiSpec
secure = false,
apiServiceRequestTimeout = 2.seconds,
customResourceSettings = new CustomResourceSettings(enabled = false, crName = None, 60.seconds),
revisionAnnotations = Seq("custom.akka.io/revision", "deployment.kubernetes.io/revision")
revisionAnnotation = "deployment.kubernetes.io/revision"
)
}

Expand Down Expand Up @@ -172,10 +172,35 @@ class KubernetesApiSpec
stubPodResponse()
stubReplicaResponse(defaultReplicaResponseJson.replaceAllLiterally("deployment.kubernetes.io", "custom.akka.io"))

val customSettings = {
val base = settings(podName1)
new KubernetesSettings(
apiCaPath = base.apiCaPath,
apiTokenPath = base.apiTokenPath,
apiServiceHost = base.apiServiceHost,
apiServicePort = base.apiServicePort,
namespace = base.namespace,
namespacePath = base.namespacePath,
podName = base.podName,
secure = base.secure,
apiServiceRequestTimeout = base.apiServiceRequestTimeout,
customResourceSettings = base.customResourceSettings,
revisionAnnotation = "custom.akka.io/revision"
)
}

val customKubernetesApi =
new KubernetesApiImpl(
system,
customSettings,
namespace,
apiToken = "apiToken",
clientHttpsConnectionContext = None)

EventFilter
.info(pattern = "Reading revision from Kubernetes: akka.cluster.app-version was set to 1", occurrences = 1)
.intercept {
kubernetesApi.readRevision().futureValue should be("1")
customKubernetesApi.readRevision().futureValue should be("1")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class PodDeletionCostAnnotatorCrSpec
secure = false,
apiServiceRequestTimeout = 2.seconds,
customResourceSettings = new CustomResourceSettings(enabled = false, crName = None, 60.seconds),
revisionAnnotations = Seq("deployment.kubernetes.io/revision")
revisionAnnotation = "deployment.kubernetes.io/revision"
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PodDeletionCostAnnotatorSpec
secure = false,
apiServiceRequestTimeout = 2.seconds,
customResourceSettings = new CustomResourceSettings(enabled = false, crName = None, 60.seconds),
revisionAnnotations = Seq("deployment.kubernetes.io/revision")
revisionAnnotation = "deployment.kubernetes.io/revision"
)
}

Expand Down

0 comments on commit c1380d1

Please sign in to comment.