Commit b89dcff3 authored by Mathieu's avatar Mathieu

Use withK8s

parent fa53aa78
......@@ -26,133 +26,137 @@ object K8sService {
def listPods = {
implicit val system = ActorSystem()
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
}
// val k8s = k8sInit
Await.result (
withK8s { k8s =>
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))
}
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
PodInfo(pod.name, st.value, restarts.restartCount, createTime.toEpochSecond, podIP, DB.email(UUID(pod.metadata.name)).map {
_.value
})
val allPodsMapFut: Future[Map[String, PodList]] = k8s listByNamespace[PodList]()
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()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
// val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault)
val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault.setCurrentNamespace("ingress-nginx"))
}, Duration.Inf)
}
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()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault)
def withK8sToResult(k8Action: String)(kubeAction: KubernetesClient => Future[_ <: ObjectResource]): K8ActionResult = {
Try {
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)
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
val k8s = k8sInit(K8SConfiguration.useLocalProxyDefault)
Try {
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) = {
withK8sToResult("OpenMOLE depolyment for user " + DB.email(uuid).map {
_.value
}.getOrElse("")) {
k8s =>
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 openmoleTemplate = Pod.Template.Spec(ObjectMeta(name = uuid.value, namespace = "ingress-nginx"))
.addContainer(openmoleContainer)
.addLabel(openmoleLabel)
//.named("openmole")
val desiredCount = 1
val openmoleDeployment = Deployment(uuid.value)
.withReplicas(desiredCount)
.withTemplate(openmoleTemplate)
println("\nCreating openmole deployment")
k8s create openmoleDeployment
def deployOpenMOLE(uuid: UUID) = {
withK8sToResult("OpenMOLE depolyment for user " + DB.email(uuid).map {
_.value
}.getOrElse("")) {
k8s =>
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 openmoleTemplate = Pod.Template.Spec(ObjectMeta(name = uuid.value, namespace = "ingress-nginx"))
.addContainer(openmoleContainer)
.addLabel(openmoleLabel)
//.named("openmole")
val desiredCount = 1
val openmoleDeployment = Deployment(uuid.value)
.withReplicas(desiredCount)
.withTemplate(openmoleTemplate)
println("\nCreating openmole deployment")
k8s create openmoleDeployment
}
}
}
def deployIfNotDeployedYet(uuid: UUID) = {
if (!isDeploymentExists(uuid))
deployOpenMOLE(uuid)
}
def deployIfNotDeployedYet(uuid: UUID) = {
if (!isDeploymentExists(uuid))
deployOpenMOLE(uuid)
}
private def podInfo(uuid: UUID)
private def podInfo(uuid: UUID) =
// import monix.execution.Scheduler.Implicits.global
=
// import monix.execution.Scheduler.Implicits.global
listPods.find {
_.name.contains(uuid.value)
}
def isServiceUp(uuid: UUID): Boolean = {
podInfo(uuid).map {
_.status
} == Some(Running)
}
def isServiceUp(uuid: UUID): Boolean = {
podInfo(uuid).map {
_.status
} == 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 {
uuid <- DB.uuids
podInfo <- podInfo(uuid)
} yield (podInfo)
}
for {
uuid <- DB.uuids
podInfo <- podInfo(uuid)
} yield (podInfo)
}
def hostIP(uuid: UUID) = {
podInfo(uuid).map {
_.podIP
def hostIP(uuid: UUID) = {
podInfo(uuid).map {
_.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