From 1f2e6cf1310bc7034d3a842c71ddfa6f44b7c938 Mon Sep 17 00:00:00 2001 From: David O'Riordan Date: Mon, 28 May 2018 14:35:52 +0100 Subject: [PATCH] Fixes for pod toleration, scale subresouce and minor typo issues (#157) --- client/src/main/scala/skuber/Pod.scala | 6 ++-- client/src/main/scala/skuber/Scale.scala | 6 ++-- .../src/main/scala/skuber/api/package.scala | 2 +- .../src/main/scala/skuber/json/package.scala | 36 +++++++++++-------- .../scala/skuber/json/PodFormatSpec.scala | 9 +++-- 5 files changed, 34 insertions(+), 25 deletions(-) diff --git a/client/src/main/scala/skuber/Pod.scala b/client/src/main/scala/skuber/Pod.scala index 80bc822f..8dacb516 100644 --- a/client/src/main/scala/skuber/Pod.scala +++ b/client/src/main/scala/skuber/Pod.scala @@ -214,10 +214,8 @@ object Pod { } sealed trait Toleration - case class EqualToleration(key: String, value: String, - effect: Option[TolerationEffect] = None) extends Toleration - case class ExistsToleration(key: String, - effect: Option[TolerationEffect] = None) extends Toleration + case class EqualToleration(key: String, value: String, effect: Option[TolerationEffect] = None, tolerationSeconds: Option[Int] = None) extends Toleration + case class ExistsToleration(key: Option[String] = None, effect: Option[TolerationEffect] = None, tolerationSeconds: Option[Int] = None) extends Toleration sealed trait TolerationEffect { val name: String diff --git a/client/src/main/scala/skuber/Scale.scala b/client/src/main/scala/skuber/Scale.scala index cf0f6f6d..50838514 100644 --- a/client/src/main/scala/skuber/Scale.scala +++ b/client/src/main/scala/skuber/Scale.scala @@ -3,7 +3,7 @@ package skuber import play.api.libs.functional.syntax._ import play.api.libs.json.{Format, JsPath, Json} import skuber.apps.{Deployment, StatefulSet} -import skuber.json.format.{maybeEmptyFormatMethods,objectMetaFormat} +import skuber.json.format.{maybeEmptyFormatMethods,objectMetaFormat, jsPath2LabelSelFormat} /** * @author David O'Riordan @@ -29,14 +29,14 @@ object Scale { case class Status( replicas: Int = 0, - selector: Option[String] = None, + selector: Option[LabelSelector] = None, targetSelector: Option[String] = None ) object Status { implicit val scaleStatusFormat: Format[Scale.Status] = ( (JsPath \ "replicas").formatMaybeEmptyInt() and - (JsPath \ "selector").formatNullable[String] and + (JsPath \ "selector").formatNullableLabelSelector and (JsPath \ "targetSelector").formatNullable[String] )(Scale.Status.apply _, unlift(Scale.Status.unapply)) } diff --git a/client/src/main/scala/skuber/api/package.scala b/client/src/main/scala/skuber/api/package.scala index c6e6b6f9..410cc1a1 100644 --- a/client/src/main/scala/skuber/api/package.scala +++ b/client/src/main/scala/skuber/api/package.scala @@ -332,7 +332,7 @@ package object client { logInfo(logConfig.logResponseBasicMetadata,s"received list resource of kind ${result.kind}") logInfo(logConfig.logResponseListSize,s"number of items in received list resource: ${result.items.size}") logInfo(logConfig.logResponseListNames, s"received ${result.kind} contains item(s): ${result.itemNames}]") - logInfo(logConfig.logResponseFullListResource, s" Unamrshalled list resource: ${result.toString}") + logInfo(logConfig.logResponseFullListResource, s" Unmarshalled list resource: ${result.toString}") } private[skuber] def makeRequestReturningObjectResource[O <: ObjectResource](httpRequest: HttpRequest)( diff --git a/client/src/main/scala/skuber/json/package.scala b/client/src/main/scala/skuber/json/package.scala index 66b32ac9..c0c7a933 100644 --- a/client/src/main/scala/skuber/json/package.scala +++ b/client/src/main/scala/skuber/json/package.scala @@ -256,7 +256,7 @@ package object format { override def reads(json: JsValue): JsResult[Pod.Toleration] = json match { case JsObject(fields) if fields.contains("operator") => - val key = fields("key").as[String] + val tolerationSeconds=fields.get("tolerationSeconds").map(js => js.as[Int]) val effect: Option[Pod.TolerationEffect] = fields.get("effect").flatMap{ case JsNull => None case e @ _ => Some(e.as[Pod.TolerationEffect]) @@ -264,9 +264,12 @@ package object format { fields("operator") match { case JsString("Equal") => + val key = fields("key").as[String] val value = fields("value").as[String] - JsSuccess(Pod.EqualToleration(key, value, effect)) - case JsString("Exists") => JsSuccess(Pod.ExistsToleration(key, effect)) + JsSuccess(Pod.EqualToleration(key, value, effect, tolerationSeconds)) + case JsString("Exists") => + val key = fields.get("key").map(js => js.as[String]) + JsSuccess(Pod.ExistsToleration(key, effect, tolerationSeconds)) case operator => JsError(s"Unknown operator '$operator'") } @@ -275,18 +278,21 @@ package object format { override def writes(toleration: Pod.Toleration): JsValue = toleration match { - case Pod.EqualToleration(key, value, effect) => Json.obj( - "key" -> key, - "value" -> value, - "operator" -> "Equal", - "effect" -> Json.toJson(effect) - ) - - case Pod.ExistsToleration(key, effect) => Json.obj( - "key" -> key, - "operator" -> "Exists", - "effect" -> Json.toJson(effect) - ) + case Pod.EqualToleration(key, value, effect, tolerationSeconds) => + val fields: List[(String, JsValue)] = List( + Some("key" -> JsString(key)), + Some("value" -> JsString(value)), + Some("operator" -> JsString("Equal")), + effect.map(e => "effect" -> Json.toJson(e)), + tolerationSeconds.map(ts => "tolerationSeconds" -> JsNumber(ts))).flatten + JsObject(fields) + case Pod.ExistsToleration(key, effect, tolerationSeconds) => + val fields: List[(String, JsValue)] = List( + key.map(k => "key" -> JsString(k)), + Some("operator" -> JsString("Exists")), + effect.map(e => "effect" -> Json.toJson(e)), + tolerationSeconds.map(ts => "tolerationSeconds" -> JsNumber(ts))).flatten + JsObject(fields) } } diff --git a/client/src/test/scala/skuber/json/PodFormatSpec.scala b/client/src/test/scala/skuber/json/PodFormatSpec.scala index 547614ef..e072bf8a 100644 --- a/client/src/test/scala/skuber/json/PodFormatSpec.scala +++ b/client/src/test/scala/skuber/json/PodFormatSpec.scala @@ -240,6 +240,10 @@ import Pod._ "operator": "Equal", "value": "value", "effect": "NoExecute" + }, + { + "effect": "NoSchedule", + "operator": "Exists" }], "serviceAccount": "default", "nodeName": "10.245.1.5" @@ -317,8 +321,9 @@ import Pod._ myPod.spec.get.dnsPolicy mustEqual DNSPolicy.Default myPod.spec.get.restartPolicy mustEqual RestartPolicy.Always myPod.spec.get.tolerations mustEqual List( - ExistsToleration("localhost.domain/url"), - EqualToleration("key","value",Some(TolerationEffect.NoExecute))) + ExistsToleration(Some("localhost.domain/url")), + EqualToleration("key","value",Some(TolerationEffect.NoExecute)), + ExistsToleration(None, Some(TolerationEffect.NoSchedule), None)) val vols = myPod.spec.get.volumes vols.length mustEqual 2