Skip to content

Commit

Permalink
Fixes for pod toleration, scale subresouce and minor typo issues (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
doriordan authored May 28, 2018
1 parent 73d22d9 commit 1f2e6cf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
6 changes: 2 additions & 4 deletions client/src/main/scala/skuber/Pod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions client/src/main/scala/skuber/Scale.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion client/src/main/scala/skuber/api/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down
36 changes: 21 additions & 15 deletions client/src/main/scala/skuber/json/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,20 @@ 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])
}

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'")
}

Expand All @@ -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)
}
}

Expand Down
9 changes: 7 additions & 2 deletions client/src/test/scala/skuber/json/PodFormatSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ import Pod._
"operator": "Equal",
"value": "value",
"effect": "NoExecute"
},
{
"effect": "NoSchedule",
"operator": "Exists"
}],
"serviceAccount": "default",
"nodeName": "10.245.1.5"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1f2e6cf

Please sign in to comment.