-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* better raw requests * readme cruft
- Loading branch information
Showing
4 changed files
with
201 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
kubernetes-client/src/com/goyeau/kubernetes/client/api/RawApi.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.goyeau.kubernetes.client.api | ||
|
||
import cats.effect.syntax.all.* | ||
import cats.effect.{Async, Resource} | ||
import com.goyeau.kubernetes.client.KubeConfig | ||
import com.goyeau.kubernetes.client.operation.* | ||
import org.http4s.client.Client | ||
import org.http4s.headers.Authorization | ||
import org.http4s.jdkhttpclient.{WSClient, WSConnectionHighLevel, WSRequest} | ||
import org.http4s.{Request, Response} | ||
|
||
private[client] class RawApi[F[_]]( | ||
httpClient: Client[F], | ||
wsClient: WSClient[F], | ||
config: KubeConfig[F], | ||
authorization: Option[F[Authorization]] | ||
)(implicit F: Async[F]) { | ||
|
||
def runRequest( | ||
request: Request[F] | ||
): Resource[F, Response[F]] = | ||
Request[F]( | ||
method = request.method, | ||
uri = config.server.resolve(request.uri), | ||
httpVersion = request.httpVersion, | ||
headers = request.headers, | ||
body = request.body, | ||
attributes = request.attributes | ||
).withOptionalAuthorization(authorization) | ||
.toResource | ||
.flatMap(httpClient.run) | ||
|
||
def connectWS( | ||
request: WSRequest | ||
): Resource[F, WSConnectionHighLevel[F]] = | ||
request | ||
.copy(uri = config.server.resolve(request.uri)) | ||
.withOptionalAuthorization(authorization) | ||
.toResource | ||
.flatMap { request => | ||
wsClient.connectHighLevel(request) | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
kubernetes-client/test/src/com/goyeau/kubernetes/client/api/RawApiTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.goyeau.kubernetes.client.api | ||
|
||
import cats.syntax.all.* | ||
import cats.effect.unsafe.implicits.global | ||
import cats.effect.{Async, IO} | ||
import com.goyeau.kubernetes.client.KubernetesClient | ||
import com.goyeau.kubernetes.client.Utils.retry | ||
import com.goyeau.kubernetes.client.api.ExecStream.{StdErr, StdOut} | ||
import com.goyeau.kubernetes.client.operation.* | ||
import fs2.io.file.{Files, Path} | ||
import fs2.{text, Stream} | ||
import io.k8s.api.core.v1.* | ||
import io.k8s.apimachinery.pkg.apis.meta.v1 | ||
import io.k8s.apimachinery.pkg.apis.meta.v1.{ListMeta, ObjectMeta} | ||
import munit.FunSuite | ||
import org.http4s.{Request, Status, Uri} | ||
import org.typelevel.log4cats.Logger | ||
import org.typelevel.log4cats.slf4j.Slf4jLogger | ||
|
||
import java.nio.file.Files as JFiles | ||
import scala.util.Random | ||
import org.http4s.implicits.* | ||
import org.http4s.jdkhttpclient.WSConnectionHighLevel | ||
|
||
class RawApiTest extends FunSuite with MinikubeClientProvider[IO] with ContextProvider { | ||
|
||
implicit override lazy val F: Async[IO] = IO.asyncForIO | ||
implicit override lazy val logger: Logger[IO] = Slf4jLogger.getLogger[IO] | ||
|
||
// MinikubeClientProvider will create a namespace with this name, even though it's not used in this test | ||
override lazy val resourceName: String = "raw-api-tests" | ||
|
||
test("list nodes with raw requests") { | ||
kubernetesClient | ||
.use { implicit client => | ||
for { | ||
response <- client.raw | ||
.runRequest( | ||
Request[IO]( | ||
uri = uri"/api" / "v1" / "nodes" | ||
) | ||
) | ||
.use { response => | ||
response.bodyText.foldMonoid.compile.lastOrError.map { body => | ||
(response.status, body) | ||
} | ||
} | ||
(status, body) = response | ||
_ = assertEquals( | ||
status, | ||
Status.Ok, | ||
s"non 200 status for get nodes raw request" | ||
) | ||
nodeList <- F.fromEither( | ||
io.circe.parser.decode[NodeList](body) | ||
) | ||
_ = assert( | ||
nodeList.kind.contains("NodeList"), | ||
"wrong .kind in the response" | ||
) | ||
_ = assert( | ||
nodeList.items.nonEmpty, | ||
"empty node list" | ||
) | ||
} yield () | ||
} | ||
.unsafeRunSync() | ||
} | ||
|
||
} |