Skip to content

Commit

Permalink
Merge branch 'release/2.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pvorb committed Feb 11, 2021
2 parents 492c04b + f27225a commit 6d8a9fc
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 127 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

== Unreleased

== Version 2.10.0

* Add a new goal, `resolve`, for downloading the helm binary as a separate step. This will still be done automatically
by the other goals, but it can be used when running maven builds in parallel, so the helm binary doesn't need to be
downloaded twice and to prevent concurrency issues.

== Version 2.9.0

* Support configuring the default URL of the helm repository for helm 2.x via `stableRepoUrl`
Expand Down
10 changes: 5 additions & 5 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Add the following to your `pom.xml`
<plugin>
<groupId>com.deviceinsight.helm</groupId>
<artifactId>helm-maven-plugin</artifactId>
<version>2.9.0</version>
<version>2.10.0</version>
<configuration>
<chartName>my-chart</chartName>
<chartRepoUrl>https://charts.helm.sh/stable</chartRepoUrl>
<helmVersion>3.4.2</helmVersion>
<helmVersion>3.5.2</helmVersion>
<strictLint>true</strictLint>
<valuesFile>src/test/helm/my-chart/values.yaml</valuesFile>
</configuration>
Expand Down Expand Up @@ -143,11 +143,11 @@ To use the `deployAtEnd` functionality it's mandatory to put the Helm Maven Plug
<plugin>
<groupId>com.deviceinsight.helm</groupId>
<artifactId>helm-maven-plugin</artifactId>
<version>2.9.0</version>
<version>2.10.0</version>
<configuration>
<chartName>my-chart</chartName>
<chartRepoUrl>https://charts.helm.sh/stable</chartRepoUrl>
<helmVersion>3.4.2</helmVersion>
<helmVersion>3.5.2</helmVersion>
<strictLint>true</strictLint>
<valuesFile>src/test/helm/my-chart/values.yaml</valuesFile>
<deployAtEnd>true</deployAtEnd>
Expand Down Expand Up @@ -177,7 +177,7 @@ Problem:: The following error message is a common source of trouble, lately:
...
[ERROR] Failed to execute goal com.deviceinsight.helm:helm-maven-plugin:2.9.0:package (default) on project my-project: Error creating helm chart: When executing '/home/user/.m2/repository/com/deviceinsight/helm/helm/2.16.2/helm-2.16.2-linux-amd64.binary init --client-only' got result code '1' -> [Help 1]
[ERROR] Failed to execute goal com.deviceinsight.helm:helm-maven-plugin:2.10.0:package (default) on project my-project: Error creating helm chart: When executing '/home/user/.m2/repository/com/deviceinsight/helm/helm/2.16.2/helm-2.16.2-linux-amd64.binary init --client-only' got result code '1' -> [Help 1]
----
Solution:: This is likely due to an old version of helm itself. Make sure to configure `<helmVersion>` to a version >= 3.4.0 or, if you are still using Helm 2, a version >= 2.17.0 (https://github.com/helm/charts#%EF%B8%8F-deprecation-and-archive-notice[background information]).
. {blank}
Expand Down
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

<groupId>com.deviceinsight.helm</groupId>
<artifactId>helm-maven-plugin</artifactId>
<version>2.9.0</version>
<version>2.10.0</version>
<packaging>maven-plugin</packaging>

<name>Helm Maven Plugin</name>
<description>A Maven Plugin for Helm Charts</description>
<url>https://github.com/deviceinsight/helm-maven-plugin</url>

<properties>
<kotlin.version>1.4.21</kotlin.version>
<kotlin.version>1.4.30</kotlin.version>
<kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand All @@ -25,8 +25,8 @@
<jackson.version>2.12.1</jackson.version>
<httpclient.version>4.5.13</httpclient.version>

<junit.jupiter.version>5.7.0</junit.jupiter.version>
<assertj-core.version>3.18.1</assertj-core.version>
<junit.jupiter.version>5.7.1</junit.jupiter.version>
<assertj-core.version>3.19.0</assertj-core.version>

<dokka-maven-plugin.version>1.4.20</dokka-maven-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
Expand Down
111 changes: 0 additions & 111 deletions src/main/kotlin/com/deviceinsight/helm/AbstractHelmMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,19 @@

package com.deviceinsight.helm

import com.deviceinsight.helm.util.PlatformDetector
import org.apache.maven.artifact.Artifact
import org.apache.maven.artifact.repository.ArtifactRepository
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest
import org.apache.maven.artifact.resolver.ArtifactResolutionResult
import org.apache.maven.plugin.AbstractMojo
import org.apache.maven.plugins.annotations.Component
import org.apache.maven.plugins.annotations.Parameter
import org.apache.maven.project.MavenProject
import org.apache.maven.repository.RepositorySystem
import java.io.File
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import kotlin.system.measureTimeMillis

abstract class AbstractHelmMojo : AbstractMojo() {

@Parameter(property = "helmGroupId", defaultValue = "com.deviceinsight.helm")
private lateinit var helmGroupId: String

@Parameter(property = "helmArtifactId", defaultValue = "helm")
private lateinit var helmArtifactId: String

@Parameter(property = "helmVersion", required = true)
private lateinit var helmVersion: String

@Parameter(property = "helmDownloadUrl", defaultValue = "https://get.helm.sh/")
private lateinit var helmDownloadUrl: URI

@Parameter(property = "chartVersion", required = false, defaultValue = "\${project.model.version}")
protected lateinit var chartVersion: String

@Parameter(defaultValue = "\${project}", readonly = true, required = true)
protected lateinit var project: MavenProject

@Parameter(readonly = true, required = true, defaultValue = "\${localRepository}")
private lateinit var localRepository: ArtifactRepository

@Parameter(readonly = true, required = true, defaultValue = "\${project.remoteArtifactRepositories}")
private lateinit var remoteRepositories: List<ArtifactRepository>

@Component
private lateinit var repositorySystem: RepositorySystem

@Parameter(property = "chartFolder", required = false)
private var chartFolder: String? = null

Expand All @@ -75,31 +38,6 @@ abstract class AbstractHelmMojo : AbstractMojo() {
@Parameter(property = "chartName", required = false)
private var chartName: String? = null

protected fun resolveHelmBinary(): String {

val platformIdentifier = PlatformDetector.detectHelmReleasePlatformIdentifier()
val helmArtifact: Artifact =
repositorySystem.createArtifactWithClassifier(helmGroupId, helmArtifactId, helmVersion, "binary",
platformIdentifier)

val request = ArtifactResolutionRequest()
request.artifact = helmArtifact
request.isResolveTransitively = false
request.localRepository = localRepository
request.remoteRepositories = remoteRepositories

val resolutionResult: ArtifactResolutionResult = repositorySystem.resolve(request)

if (!resolutionResult.isSuccess) {
log.info("Artifact not found in remote repositories")
downloadAndInstallHelm(helmArtifact, platformIdentifier)
}

helmArtifact.file.setExecutable(true)

return helmArtifact.file.absolutePath
}

protected fun executeCmd(cmd: String, directory: File = target(),
redirectOutput: ProcessBuilder.Redirect = ProcessBuilder.Redirect.PIPE) {
val proc = ProcessBuilder(cmd.split(" "))
Expand All @@ -119,7 +57,6 @@ abstract class AbstractHelmMojo : AbstractMojo() {
}
}

protected fun majorHelmVersion(): Int = helmVersion.splitToSequence('.').first().toInt()

protected fun target() = File(project.build.directory).resolve("helm")

Expand All @@ -131,54 +68,6 @@ abstract class AbstractHelmMojo : AbstractMojo() {

protected fun isChartFolderPresent() = File("${project.basedir}/${chartFolder()}").exists()

private fun downloadAndInstallHelm(artifact: Artifact, platformIdentifier: String) {

val fileName = "helm-v$helmVersion-$platformIdentifier"

val targetFile = artifact.file.toPath()
Files.createDirectories(targetFile.parent)

val url = helmDownloadUrl.resolve("./$fileName.zip").toURL()

downloadFileAndExtractBinary(url, targetFile)
}

private fun downloadFileAndExtractBinary(url: URL, destination: Path) {
val httpConnection = url.openConnection()
httpConnection.connect()
if (httpConnection !is HttpURLConnection || httpConnection.responseCode != 200) {
throw RuntimeException("Could not download file from $url")
}

val sizeInMiB: Double = httpConnection.contentLengthLong / 1024.0 / 1024.0
log.info("Downloading $url; need to get %.1f MiB...".format(sizeInMiB))

val downloadTimeMillis = measureTimeMillis {
httpConnection.inputStream.use {
ZipInputStream(it).use { zip ->
var entry: ZipEntry? = zip.nextEntry
do {
if (entry != null) {
if (isHelmBinary(entry)) {
Files.copy(zip, destination, StandardCopyOption.REPLACE_EXISTING)
zip.closeEntry()
break
} else {
zip.closeEntry()
entry = zip.nextEntry
}
}
} while (entry != null)
}
}
}

log.info("Download took %.1f seconds".format(downloadTimeMillis / 1000.0))
}

private fun isHelmBinary(entry: ZipEntry): Boolean =
!entry.isDirectory && (entry.name.endsWith("helm") || entry.name.endsWith("helm.exe"))

protected fun quoteFilePath(filePath: String): String =
if (filePath.contains(Regex("\\s"))) {
"\"$filePath\""
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/deviceinsight/helm/LintMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.io.File


@Mojo(name = "lint", defaultPhase = LifecyclePhase.PRE_INTEGRATION_TEST)
class LintMojo : AbstractHelmMojo() {
class LintMojo : ResolveHelmMojo() {

/**
* An optional values.yaml file that is used to run linting, relative to `${project.basedir}`.
Expand Down Expand Up @@ -53,7 +53,7 @@ class LintMojo : AbstractHelmMojo() {
return
}

val helm = resolveHelmBinary()
super.execute()

val command = mutableListOf(helm, "lint", chartName())

Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/com/deviceinsight/helm/PackageMojo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.io.File
* Packages helm charts
*/
@Mojo(name = "package", defaultPhase = LifecyclePhase.PACKAGE)
class PackageMojo : AbstractHelmMojo() {
class PackageMojo : ResolveHelmMojo() {

companion object {
private val PLACEHOLDER_REGEX = Regex("""\$\{(.*?)}""")
Expand Down Expand Up @@ -69,7 +69,7 @@ class PackageMojo : AbstractHelmMojo() {
return
}

val helm = resolveHelmBinary()
super.execute()

val targetHelmDir = File(target(), chartName())

Expand All @@ -85,7 +85,6 @@ class PackageMojo : AbstractHelmMojo() {
if (majorHelmVersion() < 3) {
executeCmd("$helm init --client-only --stable-repo-url $stableRepoUrl")
}

if (addIncubatorRepo) {
executeCmd("$helm repo add incubator $incubatorRepoUrl")
}
Expand Down
Loading

0 comments on commit 6d8a9fc

Please sign in to comment.