Commit b89dcff3 authored by Mathieu's avatar Mathieu
Browse files

Use withK8s

parent fa53aa78
...@@ -26,133 +26,137 @@ object K8sService { ...@@ -26,133 +26,137 @@ object K8sService {
def listPods = { def listPods = {
implicit val system = ActorSystem() // val k8s = k8sInit
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
val k8s = k8sInit
val allPodsMapFut: Future[Map[String, PodList]] = k8s listByNamespace[PodList]()
val allPodsFuture: Future[List[Pod]] = allPodsMapFut map { allPodsMap =>
allPodsMap.values.flatMap(_.items).toList
}
Await.result (
withK8s { k8s =>
def listPods0(pods: List[Pod]) = { implicit val system = ActorSystem()
pods.flatMap { pod: Pod => implicit val materializer = ActorMaterializer()
val name = pod.name implicit val dispatcher = system.dispatcher
val ns = pod.namespace
for {
stat <- pod.status.toList
containerStat <- stat.containerStatuses
status <- containerStat.state
restarts <- stat.containerStatuses.headOption
createTime <- pod.metadata.creationTimestamp
podIP <- stat.podIP
} yield {
val st: Status = status match {
case Container.Waiting(reason) => Data.Waiting(reason.getOrElse(""))
case _: Container.Running => Data.Running()
case Container.Terminated(_, _, _, message, _, finishedAt, _) => Data.Terminated(message.getOrElse(""), finishedAt.map {
_.toEpochSecond
}.getOrElse(0L))
}
PodInfo(pod.name, st.value, restarts.restartCount, createTime.toEpochSecond, podIP, DB.email(UUID(pod.metadata.name)).map { val allPodsMapFut: Future[Map[String, PodList]] = k8s listByNamespace[PodList]()
_.value val allPodsFuture: Future[List[Pod]] = allPodsMapFut map { allPodsMap =>
}) allPodsMap.values.flatMap(_.items).toList
} }
}
}
val allPods = allPodsFuture map { pods => listPods0(pods) }
Await.result(allPods, Duration.Inf) def listPods0(pods: List[Pod]) = {
} pods.flatMap { pod: Pod =>
val name = pod.name
val ns = pod.namespace
for {
stat <- pod.status.toList
containerStat <- stat.containerStatuses
status <- containerStat.state
restarts <- stat.containerStatuses.headOption
createTime <- pod.metadata.creationTimestamp
podIP <- stat.podIP
} yield {
val st: Status = status match {
case Container.Waiting(reason) => Data.Waiting(reason.getOrElse(""))
case _: Container.Running => Data.Running()
case Container.Terminated(_, _, _, message, _, finishedAt, _) => Data.Terminated(message.getOrElse(""), finishedAt.map {
_.toEpochSecond
}.getOrElse(0L))
}
PodInfo(pod.name, st.value, restarts.restartCount, createTime.toEpochSecond, podIP, DB.email(UUID(pod.metadata.name)).map {
_.value
})
}
}
}
def withK8s[T](kubeAction: KubernetesClient => Future[T]) = { allPodsFuture map { pods => listPods0(pods) }
implicit val system = ActorSystem() }, Duration.Inf)
implicit val materializer = ActorMaterializer() }
implicit val dispatcher = system.dispatcher
// val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault)
val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault.setCurrentNamespace("ingress-nginx"))
kubeAction(k8s) def withK8s[T](kubeAction: KubernetesClient => Future[T]) = {
}
def withK8sToResult(k8Action: String)(kubeAction: KubernetesClient => Future[_ <: ObjectResource]): K8ActionResult = { implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
val k8s = k8sInit
kubeAction(k8s)
}
implicit val system = ActorSystem() def withK8sToResult(k8Action: String)(kubeAction: KubernetesClient => Future[_ <: ObjectResource]): K8ActionResult = {
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault)
Try { implicit val system = ActorSystem()
Await.result({ implicit val materializer = ActorMaterializer()
kubeAction(k8s) implicit val dispatcher = system.dispatcher
}, Duration.Inf) val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault)
} match {
case Success(o: ObjectResource) => K8Success(s"$k8Action successfully completed " + o.name + " // " + o.metadata.generateName) Try {
case Failure(t: Throwable) => K8Failure(t.getMessage, t.toStackTrace) Await.result({
kubeAction(k8s)
}, Duration.Inf)
} match {
case Success(o: ObjectResource) => K8Success(s"$k8Action successfully completed " + o.name + " // " + o.metadata.generateName)
case Failure(t: Throwable) => K8Failure(t.getMessage, t.toStackTrace)
}
} }
}
def deployOpenMOLE(uuid: UUID) = { def deployOpenMOLE(uuid: UUID) = {
withK8sToResult("OpenMOLE depolyment for user " + DB.email(uuid).map { withK8sToResult("OpenMOLE depolyment for user " + DB.email(uuid).map {
_.value _.value
}.getOrElse("")) { }.getOrElse("")) {
k8s => k8s =>
val openmoleLabel = "app" -> "openmole" val openmoleLabel = "app" -> "openmole"
val openmoleContainer = Container(name = "openmole", image = "openmole/openmole", command = List("bin/bash", "-c", "openmole --port 80 --password password --http --remote")).exposePort(80) val openmoleContainer = Container(name = "openmole", image = "openmole/openmole", command = List("bin/bash", "-c", "openmole --port 80 --password password --http --remote")).exposePort(80)
val openmoleTemplate = Pod.Template.Spec(ObjectMeta(name = uuid.value, namespace = "ingress-nginx")) val openmoleTemplate = Pod.Template.Spec(ObjectMeta(name = uuid.value, namespace = "ingress-nginx"))
.addContainer(openmoleContainer) .addContainer(openmoleContainer)
.addLabel(openmoleLabel) .addLabel(openmoleLabel)
//.named("openmole") //.named("openmole")
val desiredCount = 1 val desiredCount = 1
val openmoleDeployment = Deployment(uuid.value) val openmoleDeployment = Deployment(uuid.value)
.withReplicas(desiredCount) .withReplicas(desiredCount)
.withTemplate(openmoleTemplate) .withTemplate(openmoleTemplate)
println("\nCreating openmole deployment") println("\nCreating openmole deployment")
k8s create openmoleDeployment k8s create openmoleDeployment
}
} }
} def deployIfNotDeployedYet(uuid: UUID) = {
if (!isDeploymentExists(uuid))
deployOpenMOLE(uuid)
}
def deployIfNotDeployedYet(uuid: UUID) = { private def podInfo(uuid: UUID)
if (!isDeploymentExists(uuid))
deployOpenMOLE(uuid)
}
private def podInfo(uuid: UUID) = =
// import monix.execution.Scheduler.Implicits.global // import monix.execution.Scheduler.Implicits.global
listPods.find { listPods.find {
_.name.contains(uuid.value) _.name.contains(uuid.value)
} }
def isServiceUp(uuid: UUID): Boolean = { def isServiceUp(uuid: UUID): Boolean = {
podInfo(uuid).map { podInfo(uuid).map {
_.status _.status
} == Some(Running) } == Some(Running)
} }
def isDeploymentExists(uuid: UUID) = podInfo(uuid).isDefined def isDeploymentExists(uuid: UUID) = podInfo(uuid).isDefined
def podInfos: Seq[PodInfo] = { def podInfos: Seq[PodInfo] = {
for { for {
uuid <- DB.uuids uuid <- DB.uuids
podInfo <- podInfo(uuid) podInfo <- podInfo(uuid)
} yield (podInfo) } yield (podInfo)
} }
def hostIP(uuid: UUID) = { def hostIP(uuid: UUID) = {
podInfo(uuid).map { podInfo(uuid).map {
_.podIP _.podIP
}
} }
} }
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment