in lease-kubernetes/src/main/scala/org/apache/pekko/coordination/lease/kubernetes/internal/NativeKubernetesApiImpl.scala [139:174]
override def pathForLease(name: String): Uri.Path =
Uri.Path.Empty / "apis" / "coordination.k8s.io" / "v1" / "namespaces" / namespace / "leases" / name
.replaceAll("[^\\d\\w\\-\\.]", "")
.toLowerCase
override def createLeaseResource(name: String): Future[Option[LeaseResource]] = {
val lcr = NativeLeaseResource(Metadata(name, None), NativeSpec("", currentTimeRFC3339))
for {
entity <- Marshal(lcr).to[RequestEntity]
response <- makeRequest(
requestForPath(pathForLease(""), HttpMethods.POST, entity = entity),
s"Timed out creating lease $name")
responseEntity <- response.entity.toStrict(settings.bodyReadTimeout)
lr <- response.status match {
case StatusCodes.Created =>
log.debug("lease resource created")
Unmarshal(responseEntity).to[NativeLeaseResource].map(lcr => Some(toLeaseResource(lcr)))
case StatusCodes.Conflict =>
log.debug("creation of lease resource failed as already exists. Will attempt to read again")
entity.discardBytes()
// someone else has created it
Future.successful(None)
case StatusCodes.Unauthorized =>
handleUnauthorized(response)
case unexpected =>
responseEntity
.toStrict(settings.bodyReadTimeout)
.flatMap(e => Unmarshal(e).to[String])
.flatMap(body => {
Future.failed(
new LeaseException(
s"Unexpected response from API server when creating Lease StatusCode: $unexpected. Body: $body"))
})
}
} yield lr
}