A Kubernetes client for Scala
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
project
src/main
.gitignore
.scalafix.conf
.scalafmt.conf
LICENSE
README.md
build.sbt

README.md

Kubernetes Client for Scala

Latest version

Installation

libraryDependencies += "com.goyeau" %% "kubernetes-client" % "<latest version>"

Usage

Client configuration

import scala.io.Source
import java.io.File
import com.goyeau.kubernetes.client._

val client = KubernetesClient(
  KubeConfig(
    server = "https://k8s.goyeau.com",
    oauthToken = Option(Source.fromFile("/var/run/secrets/kubernetes.io/serviceaccount/token").mkString),
    caCertFile = Option(new File("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"))
  )
)
import java.io.File
import com.goyeau.kubernetes.client._

val client = KubernetesClient(KubeConfig(new File("/opt/docker/secrets/kube/config")))

Requests

import java.io.File
import com.goyeau.kubernetes.client._
import io.k8s.api.apps.v1beta1.{Deployment, DeploymentSpec, DeploymentStrategy, RollingUpdateDeployment}
import io.k8s.api.core.v1._
import io.k8s.apimachinery.pkg.api.resource.Quantity
import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta
import io.k8s.apimachinery.pkg.util.intstr.IntOrString

val client = KubernetesClient(KubeConfig(new File("/opt/docker/secrets/kube/config")))

val deployment = Deployment(
  metadata = Option(ObjectMeta(name = Option("web-backend"), namespace = Option("my-namespace"))),
  spec = Option(
    DeploymentSpec(
      strategy = Option(
        DeploymentStrategy(
          `type` = Option("RollingUpdate"),
          rollingUpdate = Option(RollingUpdateDeployment(Option(IntOrString("10%")), Option(IntOrString("50%"))))
        )
      ),
      template = PodTemplateSpec(
        metadata = Option(
          ObjectMeta(
            labels = Option(Map("app" -> "web", "tier" -> "frontend", "environment" -> "myenv"))
          )
        ),
        spec = Option(
          PodSpec(
            containers = Seq(
              Container(
                name = "nginx",
                image = "nginx",
                resources = Option(
                  ResourceRequirements(
                    Option(Map("cpu" -> Quantity("100m"), "memory" -> Quantity("128Mi"))),
                    Option(Map("cpu" -> Quantity("80m"), "memory" -> Quantity("64Mi")))
                  )
                ),
                volumeMounts = Option(Seq(VolumeMount(name = "nginx-config", mountPath = "/etc/nginx/conf.d"))),
                ports = Option(Seq(ContainerPort(name = Option("http"), containerPort = 8080)))
              )
            ),
            volumes = Option(
              Seq(
                Volume(
                  name = "nginx-config",
                  configMap = Option(ConfigMapVolumeSource(name = Option("nginx-config")))
                )
              )
            )
          )
        )
      )
    )
  )
)

client.deployments.namespace("my-namespace").create(deployment)

Related projects

Why Kubernetes Client for Scala?

You might wonder why using this library instead of Skuber for example? The main reason is that Kubernetes Client has been designed so that we can just create all the payload case classes by just ingesting the swagger api provided by Kubernetes' main repo, just like Kubernetes Client for Java is doing. So we will always be up to date with the latest Kubernetes API.