Skip to content

Commit

Permalink
watch in all namespaces for non namespaces resources (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
novakov-alexey authored Aug 10, 2021
1 parent 599e26c commit 15fbeb8
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class ConfigMapsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[ConfigMapList],
encoder: Encoder[ConfigMap],
decoder: Decoder[ConfigMap]
) extends Listable[F, ConfigMapList] {
val resourceDecoder: Decoder[ConfigMap],
encoder: Encoder[ConfigMap]
) extends Listable[F, ConfigMapList]
with Watchable[F, ConfigMap] {
val resourceUri: Uri = uri"/api" / "v1" / "configmaps"

def namespace(namespace: String): NamespacedConfigMapsApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class CronJobsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[CronJobList],
encoder: Encoder[CronJob],
decoder: Decoder[CronJob]
) extends Listable[F, CronJobList] {
val resourceDecoder: Decoder[CronJob],
encoder: Encoder[CronJob]
) extends Listable[F, CronJobList]
with Watchable[F, CronJob] {
val resourceUri: Uri = uri"/apis" / "batch" / "v1beta1" / "cronjobs"

def namespace(namespace: String): NamespacedCronJobsApi[F] = new NamespacedCronJobsApi(httpClient, config, namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ private[client] class CustomResourcesApi[F[_], A, B](
)(implicit
val F: Async[F],
val listDecoder: Decoder[CustomResourceList[A, B]],
encoder: Encoder[CustomResource[A, B]],
decoder: Decoder[CustomResource[A, B]]
) extends Listable[F, CustomResourceList[A, B]] {
val resourceDecoder: Decoder[CustomResource[A, B]],
encoder: Encoder[CustomResource[A, B]]
) extends Listable[F, CustomResourceList[A, B]]
with Watchable[F, CustomResource[A, B]] {

val resourceUri: Uri = uri"/apis" / context.group / context.version / context.plural

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class DeploymentsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[DeploymentList],
encoder: Encoder[Deployment],
decoder: Decoder[Deployment]
) extends Listable[F, DeploymentList] {
val resourceDecoder: Decoder[Deployment],
encoder: Encoder[Deployment]
) extends Listable[F, DeploymentList]
with Watchable[F, Deployment] {
val resourceUri: Uri = uri"/apis" / "apps" / "v1" / "deployments"

def namespace(namespace: String): NamespacedDeploymentsApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class HorizontalPodAutoscalersApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[HorizontalPodAutoscalerList],
encoder: Encoder[HorizontalPodAutoscaler],
decoder: Decoder[HorizontalPodAutoscaler]
) extends Listable[F, HorizontalPodAutoscalerList] {
val resourceDecoder: Decoder[HorizontalPodAutoscaler],
encoder: Encoder[HorizontalPodAutoscaler]
) extends Listable[F, HorizontalPodAutoscalerList]
with Watchable[F, HorizontalPodAutoscaler] {
val resourceUri: Uri = uri"/apis" / "autoscaling" / "v1" / "horizontalpodautoscalers"

def namespace(namespace: String): NamespacedHorizontalPodAutoscalersApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import cats.effect.Async
import com.goyeau.kubernetes.client.KubeConfig
import com.goyeau.kubernetes.client.operation._
import io.circe._
import io.k8s.api.networking.v1beta1.{Ingress, IngressList}
import org.http4s.Uri
import org.http4s.client.Client
import org.http4s.implicits._
import io.k8s.api.networking.v1beta1.{Ingress, IngressList}

private[client] class IngressessApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[IngressList],
encoder: Encoder[Ingress],
decoder: Decoder[Ingress]
) extends Listable[F, IngressList] {
val resourceDecoder: Decoder[Ingress],
encoder: Encoder[Ingress]
) extends Listable[F, IngressList]
with Watchable[F, Ingress] {
val resourceUri: Uri = uri"/apis" / "extensions" / "v1beta1" / "ingresses"

def namespace(namespace: String): NamespacedIngressesApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class JobsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[JobList],
encoder: Encoder[Job],
decoder: Decoder[Job]
) extends Listable[F, JobList] {
val resourceDecoder: Decoder[Job],
encoder: Encoder[Job]
) extends Listable[F, JobList]
with Watchable[F, Job] {
val resourceUri: Uri = uri"/apis" / "batch" / "v1" / "jobs"

def namespace(namespace: String): NamespacedJobsApi[F] = new NamespacedJobsApi(httpClient, config, namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class PodDisruptionBudgetsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[PodDisruptionBudgetList],
encoder: Encoder[PodDisruptionBudget],
decoder: Decoder[PodDisruptionBudget]
) extends Listable[F, PodDisruptionBudgetList] {
val resourceDecoder: Decoder[PodDisruptionBudget],
encoder: Encoder[PodDisruptionBudget]
) extends Listable[F, PodDisruptionBudgetList]
with Watchable[F, PodDisruptionBudget] {
val resourceUri: Uri = uri"/apis" / "policy" / "v1beta1" / "poddisruptionbudgets"

def namespace(namespace: String): NamespacedPodDisruptionBudgetApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import io.k8s.api.core.v1.{Pod, PodList}
import io.k8s.apimachinery.pkg.apis.meta.v1.Status
import org.http4s._
import org.http4s.client.Client
import org.http4s.jdkhttpclient._
import org.http4s.implicits._
import scodec.bits.ByteVector
import org.http4s.jdkhttpclient._
import org.typelevel.ci.CIString
import scodec.bits.ByteVector

private[client] class PodsApi[F[_]](val httpClient: Client[F], wsClient: WSClient[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[PodList],
encoder: Encoder[Pod],
decoder: Decoder[Pod]
) extends Listable[F, PodList] {
val resourceDecoder: Decoder[Pod],
encoder: Encoder[Pod]
) extends Listable[F, PodList]
with Watchable[F, Pod] {
val resourceUri: Uri = uri"/api" / "v1" / "pods"

def namespace(namespace: String): NamespacedPodsApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class ReplicaSetsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[ReplicaSetList],
encoder: Encoder[ReplicaSet],
decoder: Decoder[ReplicaSet]
) extends Listable[F, ReplicaSetList] {
val resourceDecoder: Decoder[ReplicaSet],
encoder: Encoder[ReplicaSet]
) extends Listable[F, ReplicaSetList]
with Watchable[F, ReplicaSet] {
val resourceUri: Uri = uri"/apis" / "apps" / "v1" / "replicasets"

def namespace(namespace: String): NamespacedReplicaSetsApi[F] =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.goyeau.kubernetes.client.api

import java.util.Base64
import cats.effect.Async
import com.goyeau.kubernetes.client.KubeConfig
import com.goyeau.kubernetes.client.operation._
import io.circe._
import io.k8s.api.core.v1.{Secret, SecretList}
import org.http4s.{Status, Uri}
import org.http4s.client.Client
import org.http4s.implicits._
import org.http4s.{Status, Uri}

import java.util.Base64
import scala.collection.compat._

private[client] class SecretsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[SecretList],
encoder: Encoder[Secret],
decoder: Decoder[Secret]
) extends Listable[F, SecretList] {
val resourceDecoder: Decoder[Secret],
encoder: Encoder[Secret]
) extends Listable[F, SecretList]
with Watchable[F, Secret] {
val resourceUri = uri"/api" / "v1" / "secrets"

def namespace(namespace: String) = new NamespacedSecretsApi(httpClient, config, namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class ServiceAccountsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[ServiceAccountList],
encoder: Encoder[ServiceAccount],
decoder: Decoder[ServiceAccount]
) extends Listable[F, ServiceAccountList] {
val resourceDecoder: Decoder[ServiceAccount],
encoder: Encoder[ServiceAccount]
) extends Listable[F, ServiceAccountList]
with Watchable[F, ServiceAccount] {
val resourceUri: Uri = uri"/api" / "v1" / "serviceaccounts"

def namespace(namespace: String): NamespacedServiceAccountsApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class ServicesApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[ServiceList],
encoder: Encoder[Service],
decoder: Decoder[Service]
) extends Listable[F, ServiceList] {
val resourceDecoder: Decoder[Service],
encoder: Encoder[Service]
) extends Listable[F, ServiceList]
with Watchable[F, Service] {
val resourceUri: Uri = uri"/api" / "v1" / "services"

def namespace(namespace: String): NamespacedServicesApi[F] = new NamespacedServicesApi(httpClient, config, namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import org.http4s.implicits._
private[client] class StatefulSetsApi[F[_]](val httpClient: Client[F], val config: KubeConfig)(implicit
val F: Async[F],
val listDecoder: Decoder[StatefulSetList],
encoder: Encoder[StatefulSet],
decoder: Decoder[StatefulSet]
) extends Listable[F, StatefulSetList] {
val resourceDecoder: Decoder[StatefulSet],
encoder: Encoder[StatefulSet]
) extends Listable[F, StatefulSetList]
with Watchable[F, StatefulSet] {
val resourceUri: Uri = uri"/apis" / "apps" / "v1" / "statefulsets"

def namespace(namespace: String): NamespacedStatefulSetsApi[F] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import scala.concurrent.duration.DurationInt
trait ContextProvider {

private val dispatcher: Resource[IO, Dispatcher[IO]] = Dispatcher[IO]
private val runTimeout = 1.minute
private val runTimeout = 90.seconds

def unsafeRunSync[A](f: IO[A]): A =
dispatcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import cats.syntax.option._
import com.goyeau.kubernetes.client.KubernetesClient
import com.goyeau.kubernetes.client.api.CustomResourceDefinitionsApiTest._
import com.goyeau.kubernetes.client.operation._
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger
import io.k8s.apiextensionsapiserver.pkg.apis.apiextensions.v1._
import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta
import munit.FunSuite
import org.http4s.Status
import cats.effect.unsafe.implicits.global
import org.typelevel.log4cats.Logger
import org.typelevel.log4cats.slf4j.Slf4jLogger

object CustomResourceDefinitionsApiTest {
val versions: CustomResourceDefinitionVersion =
Expand Down Expand Up @@ -80,6 +79,7 @@ class CustomResourceDefinitionsApiTest
implicit lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO]
lazy val resourceName: String = classOf[CustomResourceDefinition].getSimpleName
override val resourceIsNamespaced = false
override val watchIsNamespaced = resourceIsNamespaced

override def api(implicit client: KubernetesClient[IO]) = client.customResourceDefinitions
override def delete(namespaceName: String, resourceName: String)(implicit client: KubernetesClient[IO]) =
Expand Down Expand Up @@ -133,11 +133,8 @@ class CustomResourceDefinitionsApiTest

override def afterAll(): Unit = {
super.afterAll()
val status = kubernetesClient
.use(_.customResourceDefinitions.deleteAll(crdLabel))
.unsafeRunSync()
val status = usingMinikube(_.customResourceDefinitions.deleteAll(crdLabel))
assertEquals(status, Status.Ok)
()
}

override def namespacedApi(namespaceName: String)(implicit client: KubernetesClient[IO]) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import cats.effect._
import cats.implicits._
import com.goyeau.kubernetes.client.api.NamespacesApiTest
import com.goyeau.kubernetes.client.{KubeConfig, KubernetesClient}
import munit.Suite
import org.typelevel.log4cats.Logger

import java.io.File
import munit.Suite

trait MinikubeClientProvider[F[_]] {
this: Suite =>
Expand All @@ -26,20 +27,26 @@ trait MinikubeClientProvider[F[_]] {

def resourceName: String

private val createNamespace: F[Unit] = kubernetesClient.use { implicit client =>
def defaultNamespace: String = resourceName.toLowerCase

protected val extraNamespace = Option.empty[String]

protected def createNamespace(namespace: String): F[Unit] = kubernetesClient.use { implicit client =>
for {
_ <- client.namespaces.deleteTerminated(resourceName.toLowerCase)
_ <- NamespacesApiTest.createChecked[F](resourceName.toLowerCase)
_ <- client.namespaces.deleteTerminated(namespace)
_ <- NamespacesApiTest.createChecked[F](namespace)
} yield ()
}

private val deleteNamespace = kubernetesClient.use { client =>
client.namespaces.delete(resourceName.toLowerCase).void
private def deleteNamespace(namespace: String) = kubernetesClient.use { client =>
client.namespaces.delete(namespace).void
}

override def beforeAll(): Unit = unsafeRunSync(createNamespace)
override def beforeAll(): Unit =
(defaultNamespace +: extraNamespace.toList).foreach(name => unsafeRunSync(createNamespace(name)))

override def afterAll(): Unit = unsafeRunSync(deleteNamespace)
override def afterAll(): Unit =
(defaultNamespace +: extraNamespace.toList).foreach(name => unsafeRunSync(deleteNamespace(name)))

def usingMinikube[T](body: KubernetesClient[F] => F[T]): T =
unsafeRunSync(kubernetesClient.use(body))
Expand Down
Loading

0 comments on commit 15fbeb8

Please sign in to comment.