Skip to content

Commit

Permalink
Merge pull request #5 from chessman/ingress-fixes
Browse files Browse the repository at this point in the history
Ingress fixes
  • Loading branch information
hagay3 authored Mar 11, 2021
2 parents 4a84342 + 199cbd4 commit b5808e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
14 changes: 8 additions & 6 deletions client/src/main/scala/skuber/networking/Ingress.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package skuber.networking

import skuber.ResourceSpecification.{Names, Scope}
import skuber._
import scala.util.Try

case class Ingress(
kind: String ="Ingress",
Expand Down Expand Up @@ -96,7 +97,7 @@ case class Ingress(
val paths: List[Ingress.Path] = pathsMap.map { case (path: String, backend: String) =>
backend match {
case backendSpec(serviceName, servicePort) =>
Ingress.Path(path,Ingress.Backend(serviceName, servicePort))
Ingress.Path(path,Ingress.Backend(serviceName, toNameablePort(servicePort)))
case _ => throw new Exception(s"invalid backend format: expected 'serviceName:servicePort' (got '$backend', for host: $host)")
}

Expand All @@ -115,7 +116,7 @@ case class Ingress(
def withDefaultBackendService(serviceNameAndPort: String): Ingress = {
serviceNameAndPort match {
case backendSpec(serviceName, servicePort) =>
withDefaultBackendService(serviceName, servicePort)
withDefaultBackendService(serviceName, toNameablePort(servicePort))
case _ => throw new Exception(s"invalid default backend format: expected 'serviceName:servicePort' (got '$serviceNameAndPort')")
}
}
Expand All @@ -126,7 +127,7 @@ case class Ingress(
* @param servicePort - service port
* @return copy of this Ingress with default backend set
*/
def withDefaultBackendService(serviceName: String, servicePort: String): Ingress = {
def withDefaultBackendService(serviceName: String, servicePort: NameablePort): Ingress = {
val be = Backend(serviceName, servicePort)
this.copy(spec=Some(copySpec.copy(backend = Some(be)))
)
Expand All @@ -135,13 +136,14 @@ case class Ingress(
def addAnnotations(newAnnos: Map[String, String]): Ingress =
this.copy(metadata = this.metadata.copy(annotations = this.metadata.annotations ++ newAnnos))


private def toNameablePort(port: String): NameablePort =
Try(port.toInt).toEither.left.map(_ => port).swap
}

object Ingress {

val specification: NonCoreResourceSpecification = NonCoreResourceSpecification(
apiGroup = "extensions",
apiGroup = "networking.k8s.io",
version = "v1beta1",
scope = Scope.Namespaced,
names = Names(
Expand All @@ -156,7 +158,7 @@ object Ingress {

def apply(name: String) : Ingress = Ingress(metadata=ObjectMeta(name=name))

case class Backend(serviceName: String, servicePort: String)
case class Backend(serviceName: String, servicePort: NameablePort)
case class Path(path: String, backend: Backend)
case class HttpRule(paths: List[Path] = List())
case class Rule(host: Option[String], http: HttpRule)
Expand Down
16 changes: 11 additions & 5 deletions client/src/test/scala/skuber/network/IngressSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ class IngressSpec extends Specification {
| "serviceName": "service",
| "servicePort": "http"
| }
| },
| {
| "backend": {
| "serviceName": "ssh",
| "servicePort": 22
| }
| }
| ]
| }
Expand Down Expand Up @@ -79,14 +85,14 @@ class IngressSpec extends Specification {
ing.name mustEqual "example-ingress"

ing.spec.get.rules.head.host must beSome("example.com")
ing.spec.get.rules.head.http.paths must_== List(Ingress.Path(
path = "",
backend = Ingress.Backend("service", "http")
))
ing.spec.get.rules.head.http.paths must_== List(
Ingress.Path(path = "", backend = Ingress.Backend("service", "http")),
Ingress.Path(path = "", backend = Ingress.Backend("ssh", 22))
)
ing.spec.get.tls must_== List(Ingress.TLS(
hosts = List("abc","def"),
secretName = None
))

}
}
}

0 comments on commit b5808e2

Please sign in to comment.