Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix scale endpoint, selector field #144

Merged
merged 1 commit into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions client/src/main/scala/skuber/Scale.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ case class Scale(
val newStatus = this.status.map(_.copy(replicas = count)).getOrElse(Scale.Status(replicas=count))
this.copy(status=Some(newStatus))
}

val statusSelectorLabels: Map[String, String] =
status.flatMap(_.selector.map { labels =>
val labelsArr = labels.split(",")
labelsArr.map { singleLabel =>
singleLabel.split("=") match {
case Array(key, value) => key -> value
case _=> singleLabel -> singleLabel
}
}.toMap
}).getOrElse(Map.empty)

}

object Scale {
Expand All @@ -33,14 +45,14 @@ object Scale {

case class Status(
replicas: Int = 0,
selector: Option[LabelSelector] = None,
selector: Option[String] = None,
targetSelector: Option[String] = None
)

object Status {
implicit val scaleStatusFormat: Format[Scale.Status] = (
(JsPath \ "replicas").formatMaybeEmptyInt() and
(JsPath \ "selector").formatNullableLabelSelector and
(JsPath \ "selector").formatNullable[String] and
(JsPath \ "targetSelector").formatNullable[String]
)(Scale.Status.apply _, unlift(Scale.Status.unapply))
}
Expand Down
72 changes: 66 additions & 6 deletions client/src/test/scala/skuber/ext/ScaleSpec.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package skuber.apps
package skuber.ext

import org.specs2.mutable.Specification
import skuber.{Container, LabelSelector, ObjectMeta, Pod, ReplicationController, Scale}
import play.api.libs.json.Json
import skuber.{ObjectMeta, Scale}

/**
* @author David O'Riordan
Expand All @@ -22,7 +22,8 @@ class ScaleSpec extends Specification {
val scale= Scale(
apiVersion="autoscaling/v1",
metadata=ObjectMeta(name="example", namespace="na"),
spec=Scale.Spec(replicas=Some(10))
spec=Scale.Spec(replicas=Some(10)),
status = Some(Scale.Status(replicas = 10, selector = Some("env=prod,app=app1")))
)

val readScale = Json.fromJson[Scale](Json.toJson(scale)).get
Expand All @@ -45,15 +46,17 @@ class ScaleSpec extends Specification {
},
"status": {
"replicas": 1,
"targetSelector": "redis-master"
"targetSelector": "redis-master",
"selector": "app=service1,environment=dev"
}
}
"""
val scale = Json.parse(scaleJsonStr).as[Scale]
scale.kind mustEqual "Scale"
scale.name mustEqual "redis-master"
scale.spec.replicas mustEqual Some(1)
scale.status mustEqual Some(Scale.Status(replicas=1, selector=None, targetSelector=Some("redis-master")))
scale.spec.replicas must beSome(1)
scale.status must beSome(Scale.Status(replicas=1, selector=Some("app=service1,environment=dev"), targetSelector=Some("redis-master")))
scale.statusSelectorLabels mustEqual Map("app" -> "service1", "environment" -> "dev")
}

"A scale object can contain NO replicas" >> {
Expand Down Expand Up @@ -81,4 +84,61 @@ class ScaleSpec extends Specification {
scale.name mustEqual "redis-master"
scale.spec.replicas mustEqual None
}

"A scale json with single key->value in status selector" >> {
val scaleJsonStr = """
{
"kind": "Scale",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "redis-master",
"namespace": "default",
"selfLink": "/apis/extensions/v1beta1/namespaces/default/replicationcontrollers/redis-master/scale",
"creationTimestamp": "2015-12-29T11:55:14Z"
},
"spec": {
"replicas": 1
},
"status": {
"replicas": 1,
"targetSelector": "redis-master",
"selector": "app=service1"
}
}
"""
val scale = Json.parse(scaleJsonStr).as[Scale]
scale.kind mustEqual "Scale"
scale.name mustEqual "redis-master"
scale.spec.replicas must beSome(1)
scale.status must beSome(Scale.Status(replicas=1, selector=Some("app=service1"), targetSelector=Some("redis-master")))
scale.statusSelectorLabels mustEqual Map("app" -> "service1")
}

"A scale json with wrong spec status string should not produce errors" >> {
val scaleJsonStr = """
{
"kind": "Scale",
"apiVersion": "extensions/v1beta1",
"metadata": {
"name": "redis-master",
"namespace": "default",
"selfLink": "/apis/extensions/v1beta1/namespaces/default/replicationcontrollers/redis-master/scale",
"creationTimestamp": "2015-12-29T11:55:14Z"
},
"spec": {
"replicas": 1
},
"status": {
"replicas": 1,
"targetSelector": "redis-master",
"selector": "app==se,,rvice1,==environment=dev,,"
}
}
"""
val scale = Json.parse(scaleJsonStr).as[Scale]
scale.kind mustEqual "Scale"
scale.name mustEqual "redis-master"
scale.spec.replicas must beSome(1)
scale.status must beSome(Scale.Status(replicas=1, selector=Some("app==se,,rvice1,==environment=dev,,"), targetSelector=Some("redis-master")))
}
}