You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/02/23 20:59:27 UTC

[GitHub] markusthoemmes commented on a change in pull request #3338: implement suspend/resume for KubernetesContainer

markusthoemmes commented on a change in pull request #3338: implement suspend/resume for KubernetesContainer
URL: https://github.com/apache/incubator-openwhisk/pull/3338#discussion_r170364578
 
 

 ##########
 File path: core/invoker/src/main/scala/whisk/core/containerpool/kubernetes/KubernetesClient.scala
 ##########
 @@ -99,43 +111,133 @@ class KubernetesClient(
   }
   protected val kubectlCmd = Seq(findKubectlCmd)
 
-  def run(name: String, image: String, args: Seq[String] = Seq.empty[String])(
-    implicit transid: TransactionId): Future[ContainerId] = {
-    runCmd(Seq("run", name, s"--image=$image") ++ args, timeouts.run)
-      .map(_ => ContainerId(name))
-  }
+  def run(name: String,
+          image: String,
+          memory: ByteSize = 256.MB,
+          environment: Map[String, String] = Map(),
+          labels: Map[String, String] = Map())(implicit transid: TransactionId): Future[KubernetesContainer] = {
+
+    val envVars = environment.map {
+      case (key, value) => new EnvVarBuilder().withName(key).withValue(value).build()
+    }.toSeq
+
+    val pod = new PodBuilder()
+      .withNewMetadata()
+      .withName(name)
+      .addToLabels("name", name)
+      .addToLabels(mapAsJavaMap(labels))
+      .endMetadata()
+      .withNewSpec()
+      .withRestartPolicy("Always")
+      .addNewContainer()
+      .withNewResources()
+      .withLimits(mapAsJavaMap(Map("memory" -> new Quantity(memory.toMB + "Mi"))))
+      .endResources()
+      .withName("user-action")
+      .withImage(image)
+      .withEnv(envVars)
+      .addNewPort()
+      .withContainerPort(8080)
+      .withName("action")
+      .endPort()
+      .endContainer()
+      .endSpec()
+      .build()
+
+    kubeRestClient.pods.inNamespace("openwhisk").create(pod)
 
-  def inspectIPAddress(id: ContainerId)(implicit transid: TransactionId): Future[ContainerAddress] = {
     Future {
       blocking {
-        val pod =
-          kubeRestClient.pods().withName(id.asString).waitUntilReady(timeouts.inspect.length, timeouts.inspect.unit)
-        ContainerAddress(pod.getStatus().getPodIP())
+        val createdPod = kubeRestClient.pods
+          .inNamespace("openwhisk")
+          .withName(name)
+          .waitUntilReady(config.timeouts.run.length, config.timeouts.run.unit)
+        toContainer(createdPod)
       }
     }.recoverWith {
       case e =>
-        log.error(this, s"Failed to get IP of Pod '${id.asString}' within timeout: ${e.getClass} - ${e.getMessage}")
-        Future.failed(new Exception(s"Failed to get IP of Pod '${id.asString}'"))
+        log.error(this, s"Failed create pod for '$name': ${e.getClass} - ${e.getMessage}")
+        Future.failed(new Exception(s"Failed to create pod '$name'"))
     }
   }
 
-  def rm(id: ContainerId)(implicit transid: TransactionId): Future[Unit] =
-    runCmd(Seq("delete", "--now", "pod", id.asString), timeouts.rm).map(_ => ())
+  def rm(container: KubernetesContainer)(implicit transid: TransactionId): Future[Unit] = {
+    // Pod deletion will never complete if the pod contains a paused container.
+    // Therefore issue a resume before the delete (resuming a non-suspended container is harmless).
+    resume(container).map { _ =>
+      runCmd(Seq("delete", "--now", "pod", container.id.asString), config.timeouts.rm).map(_ => ())
+    }
+  }
 
-  def rm(key: String, value: String)(implicit transid: TransactionId): Future[Unit] =
-    runCmd(Seq("delete", "--now", "pod", "-l", s"$key=$value"), timeouts.rm).map(_ => ())
+  def rm(key: String, value: String)(implicit transid: TransactionId): Future[Unit] = {
+    if (config.invokerAgent) {
+      Future {
+        blocking {
+          kubeRestClient
+            .inNamespace("openwhisk")
+            .pods()
+            .withLabel(key, value)
+            .list()
+            .getItems
+            .map { pod =>
+              rm(toContainer(pod))
+            }
+            .reduce((a, b) => a.flatMap(_ => b))
+        }
+      }
+    } else {
+      runCmd(Seq("delete", "--now", "pod", "-l", s"$key=$value"), config.timeouts.rm).map(_ => ())
+    }
+  }
 
-  def logs(id: ContainerId, sinceTime: Option[Instant], waitForSentinel: Boolean = false)(
+  def suspend(container: KubernetesContainer)(implicit transid: TransactionId): Future[Unit] = {
 
 Review comment:
   Should we move those to the container instead of "faking" them in the client? Seems weird to pass the whole container to the client.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services