Skip to content

Commit

Permalink
Add release name config option (#2847)
Browse files Browse the repository at this point in the history
  • Loading branch information
dswiecki authored and arkadius committed Feb 11, 2022
1 parent 651aa76 commit 523c98a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class SwaggerParserTest extends FunSuite with BaseOpenAPITest with Matchers {

test("reads only configured methods and patterns") {

forAll(Table(("allowedMethods", "namePattern", "expectedNames"),
forAll(Table(
("allowedMethods", "namePattern", "expectedNames"),
(List("GET", "POST"), ".*", List("getService", "postService")),
(List("GET"), ".*", List("getService")),
(List("GET", "POST"), "post.*", List("postService")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class K8sDeploymentManager(modelData: ModelData, config: K8sDeploymentManagerCon

protected def configMapForData(processVersion: ProcessVersion, deploymentData: ProcessDeploymentData, noOfTasksInReplica: Int, nussknackerInstanceName: Option[String]): ConfigMap = {
val scenario = deploymentData.asInstanceOf[GraphProcess].processAsJson
val objectName = objectNameForScenario(processVersion, Some(scenario + serializedModelConfig))
val objectName = objectNameForScenario(processVersion, config.nussknackerInstanceName, Some(scenario + serializedModelConfig))
// TODO: extract lite-kafka-runtime-api module with LiteKafkaRuntimeDeploymentConfig class and use here
val deploymentConfig = ConfigFactory.empty().withValue("tasksCount", fromAnyRef(noOfTasksInReplica))
ConfigMap(
Expand Down Expand Up @@ -204,10 +204,12 @@ object K8sDeploymentManager {
(other way to mitigate this would be to generate some hash, but it's a bit more complex...)
- ensure some level of readability - only id would be hard to match name to scenario
*/
private[manager] def objectNameForScenario(processVersion: ProcessVersion, hashInput: Option[String]): String = {
private[manager] def objectNameForScenario(processVersion: ProcessVersion, nussknackerInstanceName: Option[String], hashInput: Option[String]): String = {
//we simulate (more or less) --append-hash kubectl behaviour...
val hashToAppend = hashInput.map(input => "-" + shortHash(input)).getOrElse("")
sanitizeObjectName(s"scenario-${processVersion.processId.value}-${processVersion.processName.value}", hashToAppend)
val plainScenarioName = s"scenario-${processVersion.processId.value}-${processVersion.processName.value}"
val scenarioName = nussknackerInstanceName.map(in=>s"$in-$plainScenarioName").getOrElse(plainScenarioName)
sanitizeObjectName(scenarioName, hashToAppend)
}

private[manager] def parseVersionAnnotation(deployment: Deployment): Option[ProcessVersion] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DeploymentPreparer(config: K8sDeploymentManagerConfig) extends LazyLogging
}

private def applyDeploymentDefaults(userConfigurationBasedDeployment: Deployment, processVersion: ProcessVersion, configMapId: String, determinedReplicasCount: Int, nussknackerInstanceName: Option[String]) = {
val objectName = objectNameForScenario(processVersion, None)
val objectName = objectNameForScenario(processVersion, config.nussknackerInstanceName, None)
val annotations = Map(scenarioVersionAnnotation -> processVersion.asJson.spaces2)
val labels = labelsForScenario(processVersion, nussknackerInstanceName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,25 @@ class K8sDeploymentManagerUnitTest extends FunSuite with Matchers {
)

forAll(names) { (scenarioName: String, hashInput: Option[String], expectedId: String) =>
val generated = objectNameForScenario(versionForName(scenarioName), hashInput)
val generated = objectNameForScenario(versionForName(scenarioName), None, hashInput)
generated.length should be <= maxK8sLength
generated shouldBe expectedId
}
}

test("should generate correct object id for scenario names with nussknacker instance name") {

val names = Table(
("scenario name", "hashInput", "object id", "nussknacker instance name"),
("standard", None, "x-scenario-256-standard", Some("")),
("standard", None, "nu1-scenario-256-standard", Some("nu1"))
)

forAll(names) { (scenarioName: String, hashInput: Option[String], expectedId: String, nussknackerInstanceName: Option[String]) =>
val generated = objectNameForScenario(versionForName(scenarioName), nussknackerInstanceName, hashInput)
generated.length should be <= maxK8sLength
generated shouldBe expectedId
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import skuber.EnvVar.{FieldRef, SecretKeyRef}
import skuber.apps.v1.Deployment
import skuber.{Container, EnvVar, HTTPGetAction, LabelSelector, ObjectMeta, Pod, Probe, Volume}

import scala.collection.immutable.ListMap
import scala.jdk.CollectionConverters.seqAsJavaListConverter

class DeploymentPreparerTest extends FunSuite {
Expand Down Expand Up @@ -108,7 +107,7 @@ class DeploymentPreparerTest extends FunSuite {

preparedDeployment shouldBe Deployment(
metadata = ObjectMeta(
name = "scenario-1-x",
name = "foo-release-scenario-1-x",
labels = Map("my-label" -> "my-key", "nussknacker.io/nussknackerInstanceName" -> nussknackerInstanceName) ++ labels,
annotations = Map("my-label" -> "my-key") ++ anotations
),
Expand All @@ -121,7 +120,7 @@ class DeploymentPreparerTest extends FunSuite {
minReadySeconds = 3,
template = Pod.Template.Spec(
metadata = ObjectMeta(
name = "scenario-1-x",
name = "foo-release-scenario-1-x",
labels = Map("my-label" -> "my-key", "nussknacker.io/nussknackerInstanceName" -> nussknackerInstanceName) ++ labels
), spec = Some(
Pod.Spec(containers = List(
Expand Down

0 comments on commit 523c98a

Please sign in to comment.