All Downloads are FREE. Search and download functionalities are using the official Maven repository.

codacy.test.docker.K3SCluster.scala Maven / Gradle / Ivy

The newest version!
package codacy.test.docker

import java.nio.file.{Files, Path}
import java.util.UUID

import com.whisk.docker.{ContainerLink, DockerContainer, DockerReadyChecker, VolumeMapping}

trait K3SCluster {

  private val k3sDockerImage = "rancher/k3s:v0.6.1"

  private lazy val random = UUID.randomUUID().toString
  private val serverName = s"k3s-server-$random"
  private val clientName = s"k3s-node-$random"
  private val clusterSecret = "somethingtotallyrandom"
  private val kubeconfigFilename = "kubeconfig.yaml"

  private lazy val tmpDir = Files.createTempDirectory("DockerK3SCluster_")
  private lazy val kubeconfigDirectory: Path = {
    val path = tmpDir.resolve("kubeconfig")
    Files.createDirectories(path)
    path
  }
  lazy val hostMountDirectory: Path = {
    val path = tmpDir.resolve("tmp")
    Files.createDirectories(path)
    path
  }

  lazy val kubeconfigPath: Path = kubeconfigDirectory.resolve(kubeconfigFilename)
  val nodeMountDirectory: String = "/hostmount"
  val k3sServerPort = 6443

  private lazy val server =
    DockerContainer(k3sDockerImage, Some(serverName))
      .withCommand("server", "--disable-agent", "--https-listen-port", k3sServerPort.toString)
      .withEnv(
        s"K3S_CLUSTER_SECRET=$clusterSecret",
        s"K3S_KUBECONFIG_OUTPUT=/output/${kubeconfigFilename}",
        "K3S_KUBECONFIG_MODE=666"
      )
      .withVolumes(Seq(VolumeMapping(kubeconfigDirectory.toRealPath().toString, "/output", rw = true)))
      .withPorts((k3sServerPort, Some(k3sServerPort)))
      .withReadyChecker(DockerReadyChecker.LogLineContains("k3s is up and running"))

  private lazy val node = DockerContainer(k3sDockerImage, Some(clientName))
    .withEnv(s"K3S_URL=https://server:$k3sServerPort", s"K3S_CLUSTER_SECRET=$clusterSecret")
    .withVolumes(Seq(VolumeMapping(hostMountDirectory.toRealPath().toString, nodeMountDirectory, rw = false)))
    .withLinks(ContainerLink(server, "server"))
    .withReadyChecker(
      DockerReadyChecker.And(
        DockerReadyChecker.LogLineContains("Successfully registered node"),
        DockerReadyChecker.LogLineContains("Node controller sync successful")
      )
    )

  lazy val k3sContainers: List[DockerContainer] = List(server, node)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy