-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(manifests/helmfile): add helmfile templating engine (#986)
* feat(manifests/helmfile): install helmfile binary * feat(manifests/helmfile): add code feat(manifests/helmfile): add tests * copyright Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * test changes Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * add javadoc Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * make the code dry Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * reduce value paths Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * avoid using wildcards Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * reduce redundant code Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * increase reuse Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * run spotless Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> * run another round of spotless Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> --------- Signed-off-by: Salvatore Mazzarino <salvatoremazz@double.cloud> Co-authored-by: Salvatore Mazzarino <salvatoremazz@double.cloud> Co-authored-by: Jason <jason.mcintosh@armory.io> Co-authored-by: Salvatore Mazzarino <dev@mazzarino.cz>
- Loading branch information
1 parent
b036df6
commit e5ea778
Showing
11 changed files
with
1,081 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ public enum TemplateRenderer { | |
HELM3, | ||
KUSTOMIZE, | ||
KUSTOMIZE4, | ||
HELMFILE, | ||
CF; | ||
|
||
@JsonCreator | ||
|
106 changes: 106 additions & 0 deletions
106
...-manifests/src/main/java/com/netflix/spinnaker/rosco/manifests/HelmBakeTemplateUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright 2023 Grab Holdings, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.rosco.manifests; | ||
|
||
import com.netflix.spinnaker.kork.artifacts.model.Artifact; | ||
import com.netflix.spinnaker.kork.exceptions.SpinnakerException; | ||
import com.netflix.spinnaker.kork.retrofit.exceptions.SpinnakerHttpException; | ||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class HelmBakeTemplateUtils<T extends BakeManifestRequest> { | ||
private static final String MANIFEST_SEPARATOR = "---\n"; | ||
private static final Pattern REGEX_TESTS_MANIFESTS = | ||
Pattern.compile("# Source: .*/templates/tests/.*"); | ||
|
||
private final ArtifactDownloader artifactDownloader; | ||
|
||
protected HelmBakeTemplateUtils(ArtifactDownloader artifactDownloader) { | ||
this.artifactDownloader = artifactDownloader; | ||
} | ||
|
||
public ArtifactDownloader getArtifactDownloader() { | ||
return artifactDownloader; | ||
} | ||
|
||
public abstract String fetchFailureMessage(String description, Exception e); | ||
|
||
public String removeTestsDirectoryTemplates(String inputString) { | ||
return Arrays.stream(inputString.split(MANIFEST_SEPARATOR)) | ||
.filter(manifest -> !REGEX_TESTS_MANIFESTS.matcher(manifest).find()) | ||
.collect(Collectors.joining(MANIFEST_SEPARATOR)); | ||
} | ||
|
||
protected Path downloadArtifactToTmpFile(BakeManifestEnvironment env, Artifact artifact) | ||
throws IOException { | ||
String fileName = UUID.randomUUID().toString(); | ||
Path targetPath = env.resolvePath(fileName); | ||
artifactDownloader.downloadArtifactToFile(artifact, targetPath); | ||
return targetPath; | ||
} | ||
|
||
public abstract String getHelmExecutableForRequest(T request); | ||
|
||
protected List<Path> getValuePaths(List<Artifact> artifacts, BakeManifestEnvironment env) { | ||
List<Path> valuePaths = new ArrayList<>(); | ||
|
||
try { | ||
// not a stream to keep exception handling cleaner | ||
for (Artifact valueArtifact : artifacts.subList(1, artifacts.size())) { | ||
valuePaths.add(downloadArtifactToTmpFile(env, valueArtifact)); | ||
} | ||
} catch (SpinnakerHttpException e) { | ||
throw new SpinnakerHttpException(fetchFailureMessage("values file", e), e); | ||
} catch (IOException | SpinnakerException e) { | ||
throw new IllegalStateException(fetchFailureMessage("values file", e), e); | ||
} | ||
|
||
return valuePaths; | ||
} | ||
|
||
protected Path getHelmTypePathFromArtifact( | ||
BakeManifestEnvironment env, List<Artifact> inputArtifacts, String filePath) | ||
throws IOException { | ||
Path helmTypeFilePath; | ||
|
||
Artifact helmTypeTemplateArtifact = inputArtifacts.get(0); | ||
String artifactType = Optional.ofNullable(helmTypeTemplateArtifact.getType()).orElse(""); | ||
|
||
if ("git/repo".equals(artifactType)) { | ||
env.downloadArtifactTarballAndExtract(getArtifactDownloader(), helmTypeTemplateArtifact); | ||
|
||
helmTypeFilePath = env.resolvePath(Optional.ofNullable(filePath).orElse("")); | ||
} else { | ||
try { | ||
helmTypeFilePath = downloadArtifactToTmpFile(env, helmTypeTemplateArtifact); | ||
} catch (SpinnakerHttpException e) { | ||
throw new SpinnakerHttpException(fetchFailureMessage("template", e), e); | ||
} catch (IOException | SpinnakerException e) { | ||
throw new IllegalStateException(fetchFailureMessage("template", e), e); | ||
} | ||
} | ||
|
||
return helmTypeFilePath; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...va/com/netflix/spinnaker/rosco/manifests/config/RoscoHelmfileConfigurationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2023 Grab Holdings, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.rosco.manifests.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties("helmfile") | ||
@Data | ||
public class RoscoHelmfileConfigurationProperties { | ||
private String executablePath = "helmfile"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...main/java/com/netflix/spinnaker/rosco/manifests/helmfile/HelmfileBakeManifestRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2023 Grab Holdings, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.rosco.manifests.helmfile; | ||
|
||
import com.netflix.spinnaker.kork.artifacts.model.Artifact; | ||
import com.netflix.spinnaker.rosco.manifests.BakeManifestRequest; | ||
import java.util.List; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
@Data | ||
@EqualsAndHashCode(callSuper = true) | ||
public class HelmfileBakeManifestRequest extends BakeManifestRequest { | ||
private String helmfileFilePath; | ||
|
||
/** | ||
* The environment name used to customize the content of the helmfile manifest. The environment | ||
* name defaults to default. | ||
*/ | ||
private String environment; | ||
|
||
/** The namespace to be released into. */ | ||
private String namespace; | ||
|
||
/** | ||
* The 0th element is (or contains) the helmfile template. The rest (possibly none) are values | ||
* files. | ||
*/ | ||
List<Artifact> inputArtifacts; | ||
|
||
/** | ||
* Include custom resource definition manifests in the templated output. Helmfile uses Helm v3 | ||
* only which provides the option to include CRDs as part of the rendered output. | ||
*/ | ||
boolean includeCRDs; | ||
} |
Oops, something went wrong.