Commit af5961dc authored by mengxue's avatar mengxue
Browse files

delete pod

parent e5b54591
package skuberopenmole
import skuber.{DeleteOptions, DeletePropagation, Service, ServiceList, Timestamp, k8sInit}
import java.text.SimpleDateFormat
import java.util.concurrent.Executors
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import skuber.Service.Type.ServiceType
import skuber._
import skuber.Timestamp
import scala.util.{Failure, Success}
import skuber.Pod.Phase
import skuber.json.format._
import skuberopenmole.DeploymentOpenmole.system
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration._
......@@ -20,16 +20,15 @@ import scala.concurrent.duration._
/**
* @author Mengxue
*
* Get lists of objects across all namespaces
*
* Delete a pod in kubernetes cluster
*/
object ListObjects extends App{
object DeletePod extends App{
case class PodInfo (
status: String,
restarts: Int,
createTime: Timestamp
status: String,
restarts: Int,
createTime: Timestamp
)
private def listPods(pods: List[Pod]) = {
......@@ -58,35 +57,12 @@ object ListObjects extends App{
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
// implicit val dispatcher = ExecutionContext.fromExecutorService(Executors.newCachedThreadPool())
implicit val dispatcher = system.dispatcher
// implicit val dispatcher = ExecutionContext.fromExecutorService(Executors.newCachedThreadPool())
val k8s = k8sInit
System.out.println("\n======================================== kubectl get pods ========================================")
System.out.println("\nGetting list of pods in namespace of current context ==>")
val currNsPods: Future[PodList] = k8s list[PodList]()
val printCurrNsPods = currNsPods map { podList => listPods(podList.items) }
printCurrNsPods onComplete {
case Success(value) => Success(value)
case Failure(e) => throw(e)
}
Await.ready(printCurrNsPods, 30 seconds)
System.out.println("\n============================= kubectl get pods --namespace=kube-system ============================")
System.out.println("\nGetting lists of pods in 'kube-system' namespace ==>")
val ksysPods: Future[PodList] = k8s listInNamespace[PodList]("kube-system")
val printKSysPods = ksysPods map { podList => listPods(podList.items) }
printKSysPods onComplete {
case Success(value) => Success(value)
case Failure(e) => throw(e)
}
Await.ready(printKSysPods, 30 seconds)
System.out.println("\n================================= kubectl get pods --all-namespaces ================================")
System.out.println("\nGetting lists of pods in all namespaces in the cluster ==>")
System.out.println("\nThe list of pods in all namespaces in the cluster ==>")
val allPodsMapFut: Future[Map[String, PodList]] = k8s listByNamespace[PodList]()
val allPods: Future[List[Pod]] = allPodsMapFut map { allPodsMap =>
......@@ -94,14 +70,49 @@ object ListObjects extends App{
}
val printAllPods = allPods map { pods => listPods(pods) }
printAllPods onComplete {
case Success(value) => Success(value)
case Failure(e) => throw(e)
case Success(_) =>
System.out.println("\n================================== kubectl delete pod xxx ==================================")
System.out.println("\nEnter the name of the pod to delete : ")
var deletePodName = scala.io.StdIn.readLine()
// Check if input is empty
if(Option(deletePodName).getOrElse("").isEmpty) {
do {
System.out.println("Error input : Pod name can't be empty, please retry [Press 'q' to quit] :")
deletePodName = scala.io.StdIn.readLine()
} while (Option(deletePodName).getOrElse("").isEmpty)
}
if (deletePodName.equals("q")) {
println("\n(Quitting... Waiting 30 seconds before terminating)")
Thread.sleep(30000)
system.terminate()
materializer.shutdown()
} else {
System.out.println("\nDeleting the pod <" + deletePodName + ">...")
val deleteOptions = DeleteOptions(propagationPolicy = Some(DeletePropagation.Foreground))
val deleteFut = k8s.deleteWithOptions[Pod](deletePodName, deleteOptions)
deleteFut onComplete { delete =>
delete match {
case Success(_) =>
println("\nSuccessfully deleted pod <" + deletePodName + ">")
case Failure(ex) =>
System.err.println("Failed => " + ex)
}
println("\n(Waiting 30 seconds before terminating)")
Thread.sleep(30000)
system.terminate()
materializer.shutdown()
}
Await.ready(deleteFut, 30 seconds)
}
case Failure(ex) =>
System.err.println("Failed => " + ex)
system.terminate()
materializer.shutdown()
}
Await.ready(printAllPods, 30 seconds)
k8s.close
println("\n(Waiting 30 seconds before terminating)")
Thread.sleep(30000)
system.terminate()
materializer.shutdown()
}
......@@ -49,7 +49,7 @@ object DeploymentOpenmole extends App {
.addContainer(openmoleContainer)
.addLabel(openmoleLabel)
val desiredCount = 1
val desiredCount = 2
val openmoleDeployment = Deployment(openmoleDeploymentName)
.withReplicas(desiredCount)
.withTemplate(openmoleTemplate)
......
package skuberopenmole
import skuber._
import skuber.json.format._
import java.text.SimpleDateFormat
import java.util.concurrent.Executors
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import scala.util.{Success, Failure}
import skuber._
import skuber.Timestamp
import scala.util.{Failure, Success}
import skuber.Pod.Phase
import skuber.json.format._
import skuberopenmole.DeploymentOpenmole.system
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration._
/**
* @author Mengxue
*
* List pods in kube-system namespace
* Get lists of objects across all namespaces
*
*/
object ListPods extends App {
object ListPods extends App{
case class PodInfo (
status: String,
restarts: Int,
createTime: Timestamp
)
private def listPods(pods: List[Pod]) = {
System.out.println("")
System.out.println("POD NAME NAMESPACE STATUS RESTARTS CREATE TIME")
System.out.println("======== ========= ====== ======== ===========")
pods.map { pod: Pod =>
val name = pod.name
val ns = pod.namespace
val podInfo = (for {
stat <- pod.status.toList
status <- stat.phase
restarts <- stat.containerStatuses.headOption
createTime <- pod.metadata.creationTimestamp
} yield {
//st = status.containerStatuses.map {_.restartCount}
PodInfo(status.toString, restarts.restartCount, createTime)
}).headOption
val createTimeString = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX").parse(podInfo.map{_.createTime}.getOrElse("None").toString())
System.out.println(f"${name}%-50s${ns}%-20s${podInfo.map{_.status}.getOrElse("None")}%-20s${podInfo.map{_.restarts}.getOrElse("None")}%-20s${createTimeString}")
}
}
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
// implicit val dispatcher = ExecutionContext.fromExecutorService(Executors.newCachedThreadPool())
val k8s = k8sInit
val listPodsRequest = k8s.listInNamespace[PodList]("kube-system")
listPodsRequest.onComplete {
case Success(pods) =>
println("\nGetting list of pods ==> \n")
pods.items.foreach { p => println(p.name) }
println("")
println("(Waiting 30 seconds before terminating)")
Thread.sleep(30000)
system.terminate()
case Failure(e) =>
throw (e)
system.terminate()
System.out.println("\n======================================== kubectl get pods ========================================")
System.out.println("\nGetting list of pods in namespace of current context ==>")
val currNsPods: Future[PodList] = k8s list[PodList]()
val printCurrNsPods = currNsPods map { podList => listPods(podList.items) }
printCurrNsPods onComplete {
case Success(value) => Success(value)
case Failure(e) => throw(e)
}
Await.ready(printCurrNsPods, 30 seconds)
System.out.println("\n============================= kubectl get pods --namespace=kube-system ============================")
System.out.println("\nGetting lists of pods in 'kube-system' namespace ==>")
val ksysPods: Future[PodList] = k8s listInNamespace[PodList]("kube-system")
val printKSysPods = ksysPods map { podList => listPods(podList.items) }
printKSysPods onComplete {
case Success(value) => Success(value)
case Failure(e) => throw(e)
}
Await.ready(printKSysPods, 30 seconds)
System.out.println("\n================================= kubectl get pods --all-namespaces ================================")
System.out.println("\nGetting lists of pods in all namespaces in the cluster ==>")
val allPodsMapFut: Future[Map[String, PodList]] = k8s listByNamespace[PodList]()
val allPods: Future[List[Pod]] = allPodsMapFut map { allPodsMap =>
allPodsMap.values.flatMap(_.items).toList
}
val printAllPods = allPods map { pods => listPods(pods) }
printAllPods onComplete {
case Success(value) => Success(value)
case Failure(e) => throw(e)
}
}
\ No newline at end of file
Await.ready(printAllPods, 30 seconds)
k8s.close
println("\n(Waiting 30 seconds before terminating)")
Thread.sleep(30000)
system.terminate()
materializer.shutdown()
}
package skuberopenmole
import skuber._
import skuber.json.format._
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import scala.util.{Success, Failure}
/**
* @author Mengxue
*
* List pods in kube-system namespace
*
*/
object ListPodsName extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val dispatcher = system.dispatcher
val k8s = k8sInit
val listPodsRequest = k8s.listInNamespace[PodList]("kube-system")
listPodsRequest.onComplete {
case Success(pods) =>
println("\nGetting list of pods ==> \n")
pods.items.foreach { p => println(p.name) }
println("")
println("(Waiting 30 seconds before terminating)")
Thread.sleep(30000)
system.terminate()
case Failure(e) =>
throw (e)
system.terminate()
}
}
\ No newline at end of file
Supports Markdown
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