Skip to content

Commit 63201d1

Browse files
authored
feat: resolve docker file to canonical format (#2029)
* feat: resolve docker file to canonical format * add .env to each sample * docker-compose file must be valid
1 parent 19857df commit 63201d1

File tree

114 files changed

+156
-88
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+156
-88
lines changed

devtools/src/main/scala/kalix/devtools/impl/DockerComposeUtils.scala

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import java.io.File
2020
import java.nio.file.Files
2121
import java.nio.file.Paths
2222

23+
import scala.collection.mutable
2324
import scala.io.Source
2425
import scala.jdk.CollectionConverters._
2526
import scala.sys.process._
@@ -28,19 +29,14 @@ import com.typesafe.config.Config
2829

2930
object DockerComposeUtils {
3031

31-
def apply(file: String): DockerComposeUtils = DockerComposeUtils(file, Map.empty)
32-
3332
def fromConfig(config: Config): Option[DockerComposeUtils] =
3433
Option(config.getString("kalix.dev-mode.docker-compose-file"))
3534
.filter(_.trim.toLowerCase != "none")
3635
.filter { file => new File(sys.props("user.dir"), file).exists() }
37-
.map { file => DockerComposeUtils(file, sys.env) }
36+
.map { file => DockerComposeUtils(file) }
3837
}
3938

40-
case class DockerComposeUtils(file: String, envVar: Map[String, String]) {
41-
42-
// mostly for using from Java code
43-
def this(file: String) = this(file, Map.empty)
39+
case class DockerComposeUtils(file: String) {
4440

4541
@volatile private var started = false
4642

@@ -61,12 +57,10 @@ case class DockerComposeUtils(file: String, envVar: Map[String, String]) {
6157
// we will need to iterate over it more than once
6258
private lazy val lines: Seq[String] =
6359
if (Files.exists(Paths.get(file))) {
64-
val src = Source.fromFile(file)
65-
try {
66-
src.getLines().toList
67-
} finally {
68-
src.close()
69-
}
60+
val collectedLines = mutable.Buffer.empty[String]
61+
val processLogger = ProcessLogger(out => collectedLines.append(out))
62+
Process(s"docker-compose -f $file config", None).!(processLogger)
63+
collectedLines.toSeq // to immutable Seq
7064
} else {
7165
Seq.empty
7266
}
@@ -106,12 +100,7 @@ case class DockerComposeUtils(file: String, envVar: Map[String, String]) {
106100
else 0
107101

108102
def userFunctionPort: Int =
109-
envVar
110-
.get("USER_SERVICE_PORT")
111-
.orElse(envVar.get("USER_FUNCTION_PORT")) // legacy name
112-
.map(_.toInt)
113-
.orElse(userFunctionPortFromFile)
114-
.getOrElse(8080)
103+
userFunctionPortFromFile.getOrElse(8080)
115104

116105
private def userFunctionPortFromFile: Option[Int] =
117106
lines.collectFirst { case UserServicePortExtractor(port) => port }

devtools/src/test/scala/kalix/devtools/impl/DevModeSettingsSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ object DevModeSettingsSpec {
4040
| -Dkalix.dev-mode.service-port-mappings.foo=9001
4141
| -Dkalix.dev-mode.service-port-mappings.bar=9002
4242
| -Dkalix.dev-mode.service-port-mappings.baz=host.docker.internal:9003
43-
| USER_SERVICE_HOST:${USER_SERVICE_HOST:-host.docker.internal}
44-
| USER_SERVICE_PORT:${USER_SERVICE_PORT:-8081}
43+
| USER_SERVICE_HOST: ${USER_SERVICE_HOST:-host.docker.internal}
44+
| USER_SERVICE_PORT: ${USER_SERVICE_PORT:-8081}
4545
|""".stripMargin
4646
}
4747

devtools/src/test/scala/kalix/devtools/impl/DockerComposeTestFile.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,24 @@ import java.io.FileWriter
2222

2323
object DockerComposeTestFile {
2424

25-
def createTmpFile(fileContent: String): String = {
25+
def createTmpFile(fileContent: String, env: Map[String, String] = Map.empty): String = {
2626
// write docker-compose.yml to a temporary file
2727
val userDir = sys.props("user.dir")
28+
29+
val envFile = new File(new File(userDir, "target"), ".env")
30+
// if previous exist, we should delete it
31+
if (envFile.exists()) envFile.delete()
32+
33+
// create .env file if needed
34+
if (env.nonEmpty) {
35+
envFile.deleteOnExit()
36+
val envBuff = new BufferedWriter(new FileWriter(envFile))
37+
env.foreach { case (key, value) =>
38+
envBuff.write(s"$key=$value")
39+
}
40+
envBuff.close()
41+
}
42+
2843
val dockerComposeFile = File.createTempFile("docker-compose-", ".yml", new File(userDir, "target"))
2944
dockerComposeFile.deleteOnExit()
3045
val bw = new BufferedWriter(new FileWriter(dockerComposeFile))

devtools/src/test/scala/kalix/devtools/impl/DockerComposeUtilsSpec.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DockerComposeUtilsSpec extends AnyWordSpec with Matchers with OptionValues
3535
|version: "3"
3636
|services:
3737
| kalix-runtime:
38-
| image: gcr.io/kalix-public/kalix-runtime:1.1.27
38+
| image: gcr.io/kalix-public/kalix-runtime:1.1.31
3939
| ports:
4040
| - "9000:9000"
4141
| extra_hosts:
@@ -46,8 +46,8 @@ class DockerComposeUtilsSpec extends AnyWordSpec with Matchers with OptionValues
4646
| -Dlogback.configurationFile=logback-dev-mode.xml
4747
| -Dkalix.dev-mode.service-port-mappings.foo=9001
4848
| -Dkalix.dev-mode.service-port-mappings.bar=9002
49-
| USER_SERVICE_HOST:${USER_SERVICE_HOST:-host.docker.internal}
50-
| USER_SERVICE_PORT:${USER_SERVICE_PORT:-8081}
49+
| USER_SERVICE_HOST: ${USER_SERVICE_HOST:-host.docker.internal}
50+
| USER_SERVICE_PORT: ${USER_SERVICE_PORT:-8081}
5151
|""".stripMargin
5252

5353
"DockerComposeUtils" should {
@@ -60,8 +60,8 @@ class DockerComposeUtilsSpec extends AnyWordSpec with Matchers with OptionValues
6060

6161
"favor USER_SERVICE_PORT env var if set " in {
6262
val envVar = Map("USER_SERVICE_PORT" -> "8082")
63-
val dockerComposeFile = createTmpFile(defaultFile)
64-
val dockerComposeUtils = DockerComposeUtils(dockerComposeFile, envVar)
63+
val dockerComposeFile = createTmpFile(defaultFile, envVar)
64+
val dockerComposeUtils = DockerComposeUtils(dockerComposeFile)
6565
dockerComposeUtils.userFunctionPort shouldBe 8082
6666
}
6767

@@ -71,30 +71,31 @@ class DockerComposeUtilsSpec extends AnyWordSpec with Matchers with OptionValues
7171
|version: "3"
7272
|services:
7373
| kalix-runtime:
74-
| image: gcr.io/kalix-public/kalix-runtime:1.1.27
74+
| image: gcr.io/kalix-public/kalix-runtime:1.1.31
7575
| ports:
7676
| - "9000:9000"
7777
| extra_hosts:
7878
| - "host.docker.internal:host-gateway"
7979
|""".stripMargin
80+
8081
val dockerComposeFile = createTmpFile(fileContent)
8182
val dockerComposeUtils = DockerComposeUtils(dockerComposeFile)
8283
dockerComposeUtils.userFunctionPort shouldBe 8080
8384
}
8485

85-
"be able read user function port when env var is not used" in {
86+
"be able to read user service port when env var is not used" in {
8687
val fileWithoutEnvVar =
8788
"""
8889
|version: "3"
8990
|services:
9091
| kalix-runtime:
91-
| image: gcr.io/kalix-public/kalix-runtime:1.1.27
92+
| image: gcr.io/kalix-public/kalix-runtime:1.1.31
9293
| ports:
9394
| - "9000:9000"
9495
| extra_hosts:
9596
| - "host.docker.internal:host-gateway"
9697
| environment:
97-
| USER_SERVICE_PORT:8081
98+
| USER_SERVICE_PORT: 8081
9899
|""".stripMargin
99100

100101
val dockerComposeFile = createTmpFile(fileWithoutEnvVar)
@@ -120,7 +121,7 @@ class DockerComposeUtilsSpec extends AnyWordSpec with Matchers with OptionValues
120121
val extraProxy =
121122
"""
122123
| kalix-runtime-2:
123-
| image: gcr.io/kalix-public/kalix-runtime:1.1.27
124+
| image: gcr.io/kalix-public/kalix-runtime:1.1.31
124125
| ports:
125126
| - "9000:9000"
126127
| extra_hosts:
@@ -129,8 +130,8 @@ class DockerComposeUtilsSpec extends AnyWordSpec with Matchers with OptionValues
129130
| JAVA_TOOL_OPTIONS: >
130131
| -Dconfig.resource=dev-mode.conf
131132
| -Dlogback.configurationFile=logback-dev-mode.xml
132-
| USER_SERVICE_HOST:${USER_SERVICE_HOST:-host.docker.internal}
133-
| USER_SERVICE_PORT:${USER_SERVICE_PORT:-8082}
133+
| USER_SERVICE_HOST: ${USER_SERVICE_HOST:-host.docker.internal}
134+
| USER_SERVICE_PORT: ${USER_SERVICE_PORT:-8082}
134135
|""".stripMargin
135136

136137
val dockerComposeFile = createTmpFile(defaultFile + extraProxy)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
USER_SERVICE_PORT=8080

samples/java-protobuf-customer-registry-kafka-quickstart/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
JAVA_TOOL_OPTIONS: >
1616
-Dkalix.proxy.eventing.support=kafka
1717
USER_SERVICE_HOST: ${USER_SERVICE_HOST:-host.docker.internal}
18-
USER_SERVICE_PORT: ${USER_SERVICE_PORT:-8080}
18+
USER_SERVICE_PORT: ${USER_SERVICE_PORT}
1919
# configuring kafka broker used for eventing
2020
BROKER_SERVERS: kafka:29092
2121

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
USER_SERVICE_PORT=8080

samples/java-protobuf-customer-registry-quickstart/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
# jvm -D properties can be added under this environment map (note: remove this comment when adding properties)
1616

1717
USER_SERVICE_HOST: ${USER_SERVICE_HOST:-host.docker.internal}
18-
USER_SERVICE_PORT: ${USER_SERVICE_PORT:-8080}
18+
USER_SERVICE_PORT: ${USER_SERVICE_PORT}
1919
#PUBSUB_EMULATOR_HOST: gcloud-pubsub-emulator
2020
#gcloud-pubsub-emulator:
2121
# image: gcr.io/google.com/cloudsdktool/cloud-sdk:341.0.0
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
USER_SERVICE_PORT=8080

samples/java-protobuf-customer-registry-views-quickstart/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
# jvm -D properties can be added under this environment map (note: remove this comment when adding properties)
1616

1717
USER_SERVICE_HOST: ${USER_SERVICE_HOST:-host.docker.internal}
18-
USER_SERVICE_PORT: ${USER_SERVICE_PORT:-8080}
18+
USER_SERVICE_PORT: ${USER_SERVICE_PORT}
1919
#PUBSUB_EMULATOR_HOST: gcloud-pubsub-emulator
2020
#gcloud-pubsub-emulator:
2121
# image: gcr.io/google.com/cloudsdktool/cloud-sdk:341.0.0

0 commit comments

Comments
 (0)