doriordan / skuber

A Scala Kubernetes client library
Apache License 2.0
326 stars 97 forks source link

Question #315

Closed Matt-Dee closed 4 years ago

Matt-Dee commented 4 years ago

I am currently writing a small piece of test code to create an ingress within my kubernetes cluster. I'm trying to follow your example in the example source code project.

object IngressMethods {

//  var conf: IngressConf = _
//  val host = conf.host.get

  def buildIngress: Ingress = {
    val spec = new Ingress.Spec()

    Ingress(kind = "Ingress", apiVersion = "extensions/v1beta1", metadata = ObjectMeta(name="elastics-kibana-0-0", namespace = "cmdaa"))
      .addHttpRule("elastic.test", "/" -> "elastic-kibana-0-0:5601")
  }

  def run(args: Array[String])= {
    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    implicit val dispatcher = system.dispatcher

    implicit val k8s = k8sInit(Configuration.inClusterConfig.get)

//    conf = new IngressConf(args)
    def updateIf409(ing: Ingress): PartialFunction[Throwable, Future[Ingress]] = {
      case ex: K8SException if ex.status.code.contains(409) => {
        println("Ingress already exists - updating to current rules and continuing")
        (k8s get[Ingress] ing.name) flatMap { curr =>
          println("...retrieved ingress, now updating the rules")
          val updated = ing.copy(metadata = curr.metadata) // copies latest resource version for update
          k8s update updated
        }
      }
    }

    def createIng(ing: Ingress): Future[Ingress] = (k8s create ing) recoverWith updateIf409(ing)

    def createIngress = createIng(buildIngress)

    Await.result( createIngress , Duration.Inf )
  }

  def main(args: Array[String]): Unit = {
    IngressMethods.run(args)
  }
}

I am getting the following error when I try to execute the preceding:

INFO] [08/05/2020 18:03:56.219] [main] [skuber.api] Using following context for connecting to Kubernetes cluster: Context(Cluster(v1,https://10.96.0.1:443,false,Some(Left(/var/run/secrets/kubernetes.io/serviceaccount/ca.crt))),TokenAuth(token=<redacted>),Namespace(Namespace,v1,ObjectMeta(cmdaa,,,,,,None,None,None,Map(),Map(),List(),0,None,None),None,None))

[INFO] [08/05/2020 18:04:01.088] [main] [skuber.api] [ { reqId=b04f193a-221d-4984-90a5-0910415bae08} } - about to send HTTP request: POST https://10.96.0.1/apis/extensions/v1beta1/namespaces/cmdaa/ingresses]

[INFO] [08/05/2020 18:04:03.931] [default-akka.actor.default-dispatcher-4] [skuber.api] [ { reqId=b04f193a-221d-4984-90a5-0910415bae08} } - received response with HTTP status 422]

[INFO] [08/05/2020 18:04:04.078] [default-akka.actor.default-dispatcher-16] [skuber.api] [Response: non-ok status returned - Status(v1,Status,ListMeta(,,None),Some(Failure),Some(Ingress.extensions "elastics-kibana-0-0" is invalid: spec.rules[0].http.backend.servicePort: Invalid value: "5601": must contain at least one letter or number (a-z, 0-9)),Some(Invalid),Some({"name":"elastics-kibana-0-0","group":"extensions","kind":"Ingress","causes":[{"reason":"FieldValueInvalid","message":"Invalid value: \"5601\": must contain at least one letter or number (a-z, 0-9)","field":"spec.rules[0].http.backend.servicePort"}]}),Some(422))

skuber.api.client.package$K8SException: Status(v1,Status,ListMeta(,,None),Some(Failure),Some(Ingress.extensions "elastics-kibana-0-0" is invalid: spec.rules[0].http.backend.servicePort: Invalid value: "5601": must contain at least one letter or number (a-z, 0-9)),Some(Invalid),Some({"name":"elastics-kibana-0-0","group":"extensions","kind":"Ingress","causes":[{"reason":"FieldValueInvalid","message":"Invalid value: \"5601\": must contain at least one letter or number (a-z, 0-9)","field":"spec.rules[0].http.backend.servicePort"}]}),Some(422))
    at skuber.api.client.impl.KubernetesClientImpl.$anonfun$toKubernetesResponse$1(KubernetesClientImpl.scala:648)
    at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:304)
    at akka.http.scaladsl.util.FastFuture$.$anonfun$transformWith$1(FastFuture.scala:37)
    at akka.http.scaladsl.util.FastFuture$.strictTransform$1(FastFuture.scala:41)
    at akka.http.scaladsl.util.FastFuture$.transformWith$extension1(FastFuture.scala:45)
    at akka.http.scaladsl.util.FastFuture$.transformWith$extension0(FastFuture.scala:37)
    at akka.http.scaladsl.util.FastFuture$FulfilledFuture.transformWith(FastFuture.scala:86)
    at scala.concurrent.Future.flatMap(Future.scala:303)
    at scala.concurrent.Future.flatMap$(Future.scala:303)
    at akka.http.scaladsl.util.FastFuture$FulfilledFuture.flatMap(FastFuture.scala:77)
    at skuber.api.client.impl.KubernetesClientImpl.toKubernetesResponse(KubernetesClientImpl.scala:646)
    at skuber.api.client.impl.KubernetesClientImpl.$anonfun$makeRequestReturningObjectResource$1(KubernetesClientImpl.scala:188)
    at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:304)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:92)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
    at scala.concurrent.BlockContext$.withBlockContext(BlockContext.scala:81)
    at akka.dispatch.BatchingExecutor$BlockableBatch.run(BatchingExecutor.scala:92)
    at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41)
    at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(ForkJoinExecutorConfigurator.scala:49)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)

Do you see anything blatantly incorrect with the code above?