forked from seglo/kafka-lag-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sbt
282 lines (269 loc) · 9.51 KB
/
build.sbt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import Dependencies._
import ReleasePlugin.autoImport._
import ReleaseKeys._
import ReleaseTransformations._
import com.typesafe.sbt.packager.docker.{Cmd, DockerPermissionStrategy, ExecCmd}
import com.typesafe.sbt.packager.docker.DockerPlugin.autoImport._
import com.typesafe.sbt.packager.docker.DockerApiVersion
import com.typesafe.sbt.packager.docker.DockerPlugin.UnixSeparatorChar
import sbt.IO
import java.time.format.DateTimeFormatter
import java.time.Instant
import scala.sys.process._
lazy val kafkaLagExporter =
Project(id = "kafka-lag-exporter", base = file("."))
.enablePlugins(AutomateHeaderPlugin)
.enablePlugins(JavaAppPackaging)
.enablePlugins(DockerPlugin)
.configs(IntegrationTest.extend(Test))
.settings(Defaults.itSettings)
.settings(
inConfig(IntegrationTest)(
org.scalafmt.sbt.ScalafmtPlugin.scalafmtConfigSettings
)
)
.settings(headerSettings(IntegrationTest))
.settings(commonSettings)
.settings(
name := "kafka-lag-exporter",
libraryDependencies ++= Vector(
LightbendConfig,
Kafka,
Akka,
AkkaTyped,
AkkaSlf4j,
AkkaStreams,
AkkaStreamsProtobuf,
AkkaInfluxDB,
Fabric8Model,
Fabric8Client,
Prometheus,
PrometheusHotSpot,
PrometheusHttpServer,
ScalaJava8Compat,
AkkaHttp,
Logback,
IAMAuthLib,
ScalaTest,
AkkaTypedTestKit,
MockitoScala,
AkkaStreamsTestKit,
AlpakkaKafkaTestKit,
TestcontainersKafka,
TestcontainersInfluxDb
),
dockerApiVersion := Some(DockerApiVersion(1, 41)),
dockerRepository := Option(System.getenv("DOCKER_REPOSITORY"))
.orElse(None),
dockerUsername := Option(System.getenv("DOCKER_USERNAME"))
.orElse(Some("seglo")),
dockerUpdateLatest := true,
dockerPermissionStrategy := DockerPermissionStrategy.None,
dockerExposedPorts := Seq(8000),
dockerBuildCommand := Seq("docker", "buildx", "build", "--platform", "linux/amd64", "--force-rm", "--load", "-t", dockerAlias.value.toString(), "."),
// Based on best practices found in OpenShift Creating images guidelines
// https://docs.openshift.com/container-platform/4.10/openshift_images/create-images.html
dockerCommands := {
// OCI Image Spec Annotations
// https://github.com/opencontainers/image-spec/blob/main/annotations.md
val labelsStr = Map(
"org.opencontainers.image.title" -> name.value,
"org.opencontainers.image.description" -> description.value,
"org.opencontainers.image.vendor" -> organizationName.value,
"org.opencontainers.image.created" -> DateTimeFormatter.ISO_INSTANT
.format(Instant.now()),
"org.opencontainers.image.authors" -> maintainer.value,
"org.opencontainers.image.url" -> homepage.value.get.toString,
"org.opencontainers.image.version" -> version.value,
"org.opencontainers.image.licenses" -> licenses.value.map(l => l._1 + ": " + l._2).mkString(", ")
)
.map(l => l._1 + "=\"" + l._2 + "\"")
.mkString(", \\\n")
val labels = Cmd("LABEL", labelsStr)
val dockerBaseDirectory = (Docker / defaultLinuxInstallLocation).value
val layerIdsAscending = (Docker / dockerLayerMappings).value
.map(_.layerId)
.distinct
.sortWith { (a, b) =>
// Make the None (unspecified) layer the last layer
a.getOrElse(Int.MaxValue) < b.getOrElse(Int.MaxValue)
}
val layerCopy = layerIdsAscending.map { layerId =>
val files = dockerBaseDirectory.split(UnixSeparatorChar)(1)
val path = layerId.map(i => s"$i/$files").getOrElse(s"$files")
Cmd("COPY", s"$path /$files")
}
Seq(Cmd("FROM", "redhat/ubi8:latest")) ++
Seq(
Cmd(
"RUN",
"yum update -y && yum -y install java-17-openjdk-headless && yum clean all -y"
),
Cmd("RUN", "useradd -r -m -u 1001 kafkalagexporter")
) ++
layerCopy ++ Seq(
labels,
Cmd(
"RUN",
"chgrp -R 1001 /opt && chmod -R g=u /opt && chmod +x /opt/docker/bin/kafka-lag-exporter"
),
Cmd("WORKDIR", "/opt/docker"),
Cmd("USER", "1001")
) ++
dockerExposedPorts.value.map(p => Cmd("EXPOSE", p.toString)) ++ Seq(
ExecCmd(
"CMD",
"/opt/docker/bin/kafka-lag-exporter",
"-Dconfig.file=/opt/docker/conf/application.conf",
"-Dlogback.configurationFile=/opt/docker/conf/logback.xml"
)
)
},
updateHelmChart := {
import scala.sys.process._
val repo = dockerAlias.value.withTag(None).toString
s"./scripts/update_chart.sh ${version.value} $repo" !
},
skip in publish := true,
parallelExecution in Test := false,
releaseProcess := Seq[ReleaseStep](
lintHelmChart, // Lint the Helm Chart for errors
checkSnapshotDependencies,
inquireVersions,
runClean,
runTest,
setReleaseVersion,
updateHelmChartRelease, // Update the Helm Chart
publishDockerImage, // Publish the Docker images used by the chart
packageChart, // Package the Helm Chart
buildChartsIndex, // Build Helm Charts index
packageJavaApp, // Package the standalone Java App
updateReadmeRelease, // Update the README.md with this version
setNextVersion,
updateHelmChartNextVersion // Update the Helm Chart with the next snapshot version
)
)
lazy val commonSettings = Seq(
description := "Kafka lag exporter finds and reports Kafka consumer group lag metrics",
organization := "com.lightbend.kafkalagexporter",
organizationName := "Lightbend Inc. <http://www.lightbend.com> (2018-2022), Sean Glover <https://seanglover.com/> (2022+)",
organizationHomepage := Some(url("https://seanglover.com/")),
homepage := Some(url("https://github.com/seglo/kafka-lag-exporter")),
maintainer := "sean@seanglover.com",
licenses += ("Apache-2.0", new URL(
"https://www.apache.org/licenses/LICENSE-2.0.txt"
)),
scmInfo := Some(
ScmInfo(homepage.value.get, "git@github.com:seglo/kafka-lag-exporter.git")
),
developers += Developer(
"contributors",
"Contributors",
maintainer.value,
organizationHomepage.value.get
),
scalaVersion := Version.Scala,
scalacOptions ++= Seq(
"-encoding",
"UTF-8",
"-target:jvm-1.8",
"-Xlog-reflective-calls",
"-Xlint",
"-Ywarn-unused",
"-Ywarn-unused-import",
"-deprecation",
"-feature",
"-language:_",
"-unchecked"
),
scalacOptions in (Compile, console) := (scalacOptions in (Global)).value
.filter(_ == "-Ywarn-unused-import"),
scalacOptions in (Test, console) := (scalacOptions in (Compile, console)).value
)
lazy val updateHelmChart = taskKey[Unit]("Update Helm Chart")
def exec(cmd: String, errorMessage: String): Unit = {
val e = cmd.!
if (e != 0) sys.error(errorMessage)
}
lazy val lintHelmChart = ReleaseStep(action = st => {
exec("./scripts/lint_chart.sh", "Error while linting Helm Chart")
st
})
lazy val updateHelmChartRelease = ReleaseStep(action = st => {
val (releaseVersion, _) = st
.get(versions)
.getOrElse(
sys.error(
"No versions are set! Was this release part executed before inquireVersions?"
)
)
val extracted = Project.extract(st)
val repo = extracted.get(dockerAlias in thisProjectRef).withTag(None)
exec(
s"./scripts/update_chart.sh $releaseVersion $repo",
"Error while updating Helm Chart"
)
st
})
lazy val updateHelmChartNextVersion = ReleaseStep(action = st => {
val (_, nextVersion) = st
.get(versions)
.getOrElse(
sys.error(
"No versions are set! Was this release part executed before inquireVersions?"
)
)
val extracted = Project.extract(st)
val repo = extracted.get(dockerAlias in thisProjectRef).withTag(None)
exec(
s"./scripts/update_chart.sh $nextVersion $repo",
"Error while updating Helm Chart"
)
st
})
lazy val updateReadmeRelease = ReleaseStep(action = st => {
val (releaseVersion, _) = st
.get(versions)
.getOrElse(
sys.error(
"No versions are set! Was this release part executed before inquireVersions?"
)
)
exec(
s"./scripts/update_readme_version.sh $releaseVersion",
"Error while updating README"
)
st
})
lazy val packageChart = ReleaseStep(action = st => {
exec("./scripts/package_chart.sh", "Error while packaging Helm Chart")
st
})
lazy val buildChartsIndex = ReleaseStep(action = st => {
val (releaseVersion, _) = st
.get(versions)
.getOrElse(
sys.error(
"No versions are set! Was this release part executed before inquireVersions?"
)
)
exec(
s"./scripts/build_charts_index.sh https://github.com/seglo/kafka-lag-exporter/releases/download/v$releaseVersion/ https://seglo.github.io/kafka-lag-exporter/repo/index.yaml",
"Error while building Helm Charts index"
)
st
})
lazy val publishDockerImage = ReleaseStep(
action = { st: State =>
val extracted = Project.extract(st)
val ref = extracted.get(thisProjectRef)
extracted.runAggregated(publish in Docker in ref, st)
}
)
lazy val packageJavaApp = ReleaseStep(
action = { st: State =>
val extracted = Project.extract(st)
val ref = extracted.get(thisProjectRef)
extracted.runAggregated(packageBin in Universal in ref, st)
}
)