Skip to content

Commit

Permalink
Support for kubeconfig for creating clients
Browse files Browse the repository at this point in the history
Added separate http4s backends
Added java ssl module
  • Loading branch information
hnaderi committed May 1, 2023
1 parent 030a4b5 commit 3a0b7fa
Show file tree
Hide file tree
Showing 16 changed files with 614 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p modules/json4s/.js/target modules/http4s/.jvm/target modules/http4s/.js/target modules/client-test/.js/target modules/circe/.jvm/target modules/scalacheck/.native/target target modules/client/.native/target modules/client-test/.jvm/target unidocs/target modules/sttp/.native/target .js/target site/target modules/client/.js/target modules/json4s/.native/target modules/jawn/.jvm/target modules/codec-test/.jvm/target modules/objects-test/.js/target modules/client/.jvm/target modules/codec-test/.native/target modules/manifests/.js/target modules/circe/.js/target modules/objects/.js/target modules/objects/.jvm/target modules/jawn/.js/target modules/spray-json/.jvm/target modules/play-json/.jvm/target modules/zio/.jvm/target modules/codec-test/.js/target modules/manifests/.jvm/target .jvm/target .native/target modules/sttp/.js/target modules/objects-test/.jvm/target modules/http4s/.native/target modules/sttp/.jvm/target modules/jawn/.native/target modules/circe/.native/target example/.jvm/target modules/zio-json/.js/target modules/objects/.native/target modules/json4s/.jvm/target modules/scalacheck/.jvm/target modules/zio-json/.jvm/target modules/manifests/.native/target modules/scalacheck/.js/target project/target
run: mkdir -p modules/http4s-ember/.jvm/target modules/json4s/.js/target modules/http4s-ember/.js/target modules/http4s/.jvm/target modules/http4s/.js/target modules/client-test/.js/target modules/circe/.jvm/target modules/scalacheck/.native/target target modules/client/.native/target modules/client-test/.jvm/target unidocs/target modules/sttp/.native/target .js/target site/target modules/client/.js/target modules/json4s/.native/target modules/jawn/.jvm/target modules/codec-test/.jvm/target modules/http4s-netty/.jvm/target modules/objects-test/.js/target modules/client/.jvm/target modules/codec-test/.native/target modules/manifests/.js/target modules/circe/.js/target modules/objects/.js/target modules/objects/.jvm/target modules/jawn/.js/target modules/spray-json/.jvm/target modules/play-json/.jvm/target modules/zio/.jvm/target modules/codec-test/.js/target modules/manifests/.jvm/target modules/http4s-blaze/.jvm/target .jvm/target .native/target modules/sttp/.js/target modules/objects-test/.jvm/target modules/http4s/.native/target modules/sttp/.jvm/target modules/jawn/.native/target modules/circe/.native/target example/.jvm/target modules/zio-json/.js/target modules/objects/.native/target modules/json4s/.jvm/target modules/scalacheck/.jvm/target modules/zio-json/.jvm/target modules/java-ssl/.jvm/target modules/http4s-ember/.native/target modules/manifests/.native/target modules/scalacheck/.js/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar modules/json4s/.js/target modules/http4s/.jvm/target modules/http4s/.js/target modules/client-test/.js/target modules/circe/.jvm/target modules/scalacheck/.native/target target modules/client/.native/target modules/client-test/.jvm/target unidocs/target modules/sttp/.native/target .js/target site/target modules/client/.js/target modules/json4s/.native/target modules/jawn/.jvm/target modules/codec-test/.jvm/target modules/objects-test/.js/target modules/client/.jvm/target modules/codec-test/.native/target modules/manifests/.js/target modules/circe/.js/target modules/objects/.js/target modules/objects/.jvm/target modules/jawn/.js/target modules/spray-json/.jvm/target modules/play-json/.jvm/target modules/zio/.jvm/target modules/codec-test/.js/target modules/manifests/.jvm/target .jvm/target .native/target modules/sttp/.js/target modules/objects-test/.jvm/target modules/http4s/.native/target modules/sttp/.jvm/target modules/jawn/.native/target modules/circe/.native/target example/.jvm/target modules/zio-json/.js/target modules/objects/.native/target modules/json4s/.jvm/target modules/scalacheck/.jvm/target modules/zio-json/.jvm/target modules/manifests/.native/target modules/scalacheck/.js/target project/target
run: tar cf targets.tar modules/http4s-ember/.jvm/target modules/json4s/.js/target modules/http4s-ember/.js/target modules/http4s/.jvm/target modules/http4s/.js/target modules/client-test/.js/target modules/circe/.jvm/target modules/scalacheck/.native/target target modules/client/.native/target modules/client-test/.jvm/target unidocs/target modules/sttp/.native/target .js/target site/target modules/client/.js/target modules/json4s/.native/target modules/jawn/.jvm/target modules/codec-test/.jvm/target modules/http4s-netty/.jvm/target modules/objects-test/.js/target modules/client/.jvm/target modules/codec-test/.native/target modules/manifests/.js/target modules/circe/.js/target modules/objects/.js/target modules/objects/.jvm/target modules/jawn/.js/target modules/spray-json/.jvm/target modules/play-json/.jvm/target modules/zio/.jvm/target modules/codec-test/.js/target modules/manifests/.jvm/target modules/http4s-blaze/.jvm/target .jvm/target .native/target modules/sttp/.js/target modules/objects-test/.jvm/target modules/http4s/.native/target modules/sttp/.jvm/target modules/jawn/.native/target modules/circe/.native/target example/.jvm/target modules/zio-json/.js/target modules/objects/.native/target modules/json4s/.jvm/target modules/scalacheck/.jvm/target modules/zio-json/.jvm/target modules/java-ssl/.jvm/target modules/http4s-ember/.native/target modules/manifests/.native/target modules/scalacheck/.js/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down
60 changes: 54 additions & 6 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ lazy val root =
objectsTest,
clientTest,
client,
javaSSL,
http4s,
http4sEmber,
http4sNetty,
http4sBlaze,
zio,
sttp,
codecTest,
Expand Down Expand Up @@ -96,24 +100,68 @@ lazy val client = module("client") {
description := "client core for kubernetes",
k8sUnmanagedTarget := rootDir.value / "modules" / "client" / "src" / "main" / "scala"
)
.jvmSettings(
libraryDependencies += "org.bouncycastle" % "bcpkix-jdk18on" % "1.73"
)
.dependsOn(objects)
.dependsOn(objects, manifests)
.enablePlugins(KubernetesJsonPointerGeneratorPlugin)
}

lazy val javaSSL = module("java-ssl") {
crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.settings(
description := "java ssl for kubernetes config",
libraryDependencies ++= Seq(
"org.bouncycastle" % "bcpkix-jdk18on" % "1.73"
)
)
.dependsOn(client)
}

lazy val http4s = module("http4s") {
crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.settings(
description := "http4s based client for kubernetes",
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-ember-client" % "0.23.18",
"org.http4s" %%% "http4s-client" % "0.23.18",
"org.typelevel" %%% "jawn-fs2" % "2.4.0"
)
)
.dependsOn(client, jawn)
.jvmConfigure(_.dependsOn(javaSSL.jvm))
}

lazy val http4sEmber = module("http4s-ember") {
crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.settings(
description := "http4s ember based client for kubernetes",
libraryDependencies ++= Seq(
"org.http4s" %%% "http4s-ember-client" % "0.23.18"
)
)
.dependsOn(http4s)
}
lazy val http4sNetty = module("http4s-netty") {
crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.settings(
description := "http4s netty based client for kubernetes",
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-netty-client" % "0.5.6"
)
)
.dependsOn(http4s)
}
lazy val http4sBlaze = module("http4s-blaze") {
crossProject(JVMPlatform)
.crossType(CrossType.Pure)
.settings(
description := "http4s blaze based client for kubernetes",
libraryDependencies ++= Seq(
"org.http4s" %% "http4s-blaze-client" % "0.23.14"
)
)
.dependsOn(http4s)
}

lazy val sttp = module("sttp") {
Expand Down Expand Up @@ -345,7 +393,7 @@ lazy val example = crossProject(JVMPlatform)
"com.softwaremill.sttp.client3" %%% "circe" % "3.8.15"
)
)
.dependsOn(http4s, circe, zio, sttp)
.dependsOn(http4sNetty, http4sEmber, circe, zio, sttp)
.enablePlugins(NoPublishPlugin)

def addAlias(name: String)(tasks: String*) =
Expand Down
16 changes: 8 additions & 8 deletions example/src/main/scala/Http4sExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ import cats.effect._
import cats.implicits._
import dev.hnaderi.k8s.circe._
import dev.hnaderi.k8s.client._
import dev.hnaderi.k8s.implicits._
import dev.hnaderi.k8s.client.http4s.EmberKubernetesClient
import dev.hnaderi.k8s.client.implicits._
import dev.hnaderi.k8s.implicits._
import fs2.Stream._
import io.circe.Json
import io.k8s.api.core.v1.ConfigMap
import io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta
import org.http4s.circe._

//NOTE run `kubectl proxy` before running this example
object Http4sExample extends IOApp {

private val client =
Http4sKubernetesClient.fromUrl[IO, Json]("http://localhost:8001")
private val client = EmberKubernetesClient.defaultConfig[IO, Json]

def watchNodes(cl: StreamingClient[fs2.Stream[IO, *]]) =
CoreV1.nodes
Expand All @@ -57,8 +56,9 @@ object Http4sExample extends IOApp {
)

def operations(cl: HttpClient[IO]) = for {
_ <- APIs.namespace("hnaderi").configmaps.list.send(cl).flatMap(IO.println)
_ <- APIs
.namespace("default")
.namespace("hnaderi")
.configmaps
.create(
ConfigMap(
Expand All @@ -67,14 +67,14 @@ object Http4sExample extends IOApp {
)
)
.send(cl)
a <- APIs.namespace("default").configmaps.get("example").send(cl)
a <- APIs.namespace("hnaderi").configmaps.get("example").send(cl)
b <- APIs
.namespace("default")
.namespace("hnaderi")
.configmaps
.replace("example", a.withData(Map("test2" -> "value2")))
.send(cl)
_ <- IO.println(b)
_ <- APIs.namespace("default").configmaps.delete("example").send(cl)
_ <- APIs.namespace("hnaderi").configmaps.delete("example").send(cl)
} yield ()

def operations2(cl: HttpClient[IO]) = for {
Expand Down
36 changes: 36 additions & 0 deletions modules/http4s-blaze/src/main/scala/BlazeKubernetesClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.hnaderi.k8s.client
package http4s

import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import org.http4s.blaze.client.BlazeClientBuilder
import org.http4s.client.Client

import javax.net.ssl.SSLContext

object BlazeKubernetesClient extends Http4sKubernetesClient with JVMPlatform {

override protected def buildClient[F[_]: Async]: Resource[F, Client[F]] =
BlazeClientBuilder[F].resource

override protected def buildWithSSLContext[F[_]: Async]
: SSLContext => Resource[F, Client[F]] =
BlazeClientBuilder[F].withSslContext(_).resource

}
19 changes: 19 additions & 0 deletions modules/http4s-ember/.js/src/main/scala/PlatformCompanion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.hnaderi.k8s.client

trait PlatformCompanion {}
34 changes: 34 additions & 0 deletions modules/http4s-ember/.jvm/src/main/scala/PlatformCompanion.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.hnaderi.k8s.client

import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import fs2.io.net.tls.TLSContext
import org.http4s.client.Client
import org.http4s.ember.client.EmberClientBuilder

import javax.net.ssl.SSLContext

trait PlatformCompanion extends JVMPlatform { self: Http4sKubernetesClient =>
override protected def buildWithSSLContext[F[_]: Async]
: SSLContext => Resource[F, Client[F]] = ctx =>
EmberClientBuilder
.default[F]
.withTLSContext(TLSContext.Builder.forAsync[F].fromSSLContext(ctx))
.build
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.hnaderi.k8s.client

trait PlatformCompanion {}
32 changes: 32 additions & 0 deletions modules/http4s-ember/src/main/scala/EmberKubernetesClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.hnaderi.k8s.client
package http4s

import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import org.http4s.client.Client
import org.http4s.ember.client.EmberClientBuilder

object EmberKubernetesClient
extends Http4sKubernetesClient
with PlatformCompanion {

override protected def buildClient[F[_]: Async]: Resource[F, Client[F]] =
EmberClientBuilder.default[F].build

}
36 changes: 36 additions & 0 deletions modules/http4s-netty/src/main/scala/NettyKubernetesClient.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2022 Hossein Naderi
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.hnaderi.k8s.client
package http4s

import cats.effect.kernel.Async
import cats.effect.kernel.Resource
import org.http4s.client.Client
import org.http4s.netty.client.NettyClientBuilder

import javax.net.ssl.SSLContext

object NettyKubernetesClient extends Http4sKubernetesClient with JVMPlatform {

override protected def buildClient[F[_]: Async]: Resource[F, Client[F]] =
NettyClientBuilder[F].resource

override protected def buildWithSSLContext[F[_]: Async]
: SSLContext => Resource[F, Client[F]] =
NettyClientBuilder[F].withSSLContext(_).resource

}
Loading

0 comments on commit 3a0b7fa

Please sign in to comment.