Skip to content

Commit

Permalink
fix(bake/helm): ignore tests drectory when baking Helm chart (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaquiff authored and ethanfrogers committed Sep 10, 2019
1 parent 09a9b61 commit 22f25af
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public Artifact bake(HelmBakeManifestRequest bakeManifestRequest) throws IOExcep
return Artifact.builder()
.type("embedded/base64")
.name(bakeManifestRequest.getOutputArtifactName())
.reference(Base64.getEncoder().encodeToString(bakeResult))
.reference(
Base64.getEncoder()
.encodeToString(helmTemplateUtils.removeTestsDirectoryTemplates(bakeResult)))
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.xml.bind.DatatypeConverter;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.util.ArrayUtils;

@Component
public class HelmTemplateUtils extends TemplateUtils {

private static final String MANIFEST_SEPARATOR = "---\n";
private static final String REGEX_TESTS_MANIFESTS = "# Source: .*/templates/tests/.*";

public HelmTemplateUtils(ClouddriverService clouddriverService) {
super(clouddriverService);
}
Expand Down Expand Up @@ -81,6 +86,26 @@ public BakeRecipe buildBakeRecipe(BakeManifestEnvironment env, HelmBakeManifestR
return result;
}

public byte[] removeTestsDirectoryTemplates(byte[] input) {

final String inputString = new String(input);
final List<String> inputManifests =
ArrayUtils.toUnmodifiableList(inputString.split(MANIFEST_SEPARATOR));

final List<String> outputManifests =
inputManifests.stream()
.filter(
manifest ->
!manifest.trim().isEmpty()
&& !Pattern.compile(REGEX_TESTS_MANIFESTS).matcher(manifest).find())
.collect(Collectors.toList());

final String manifestBody =
MANIFEST_SEPARATOR
+ outputManifests.stream().collect(Collectors.joining(MANIFEST_SEPARATOR));
return manifestBody.getBytes();
}

private String nameFromReference(String reference) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* Copyright 2019 Netflix, 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.helm

import com.netflix.spinnaker.rosco.services.ClouddriverService
import spock.lang.Specification

class HelmTemplateUtilsSpec extends Specification {

def "removeTestsDirectoryTemplates returns a Kubernetes manifest with the test manifests removed from the input"() {
given:
def inputManifests = """
---
# Source: mysql/templates/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: release-name-mysql
namespace: default
spec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: "8Gi"
---
# Source: mysql/templates/tests/test-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: release-name-mysql-test
namespace: default
data:
run.sh: |-
"""

def cloudDriverService = Mock(ClouddriverService)
def helmTemplateUtils = new HelmTemplateUtils(cloudDriverService)

when:
def output = new String(helmTemplateUtils.removeTestsDirectoryTemplates(inputManifests.getBytes()))

then:
def expected = """
---
# Source: mysql/templates/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: release-name-mysql
namespace: default
spec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: "8Gi"
"""
output.trim() == expected.trim()
}

def "removeTestsDirectoryTemplates returns the input Kubernetes manifest unchanged since no tests are available"() {
given:
def inputManifests = """
---
# Source: mysql/templates/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: release-name-mysql
namespace: default
spec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: "8Gi"
---
# Source: mysql/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: release-name-mysql-test
namespace: default
data:
run.sh: |-
"""

def cloudDriverService = Mock(ClouddriverService)
def helmTemplateUtils = new HelmTemplateUtils(cloudDriverService)

when:
def output = new String(helmTemplateUtils.removeTestsDirectoryTemplates(inputManifests.getBytes()))

then:
def expected = """
---
# Source: mysql/templates/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: release-name-mysql
namespace: default
spec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: "8Gi"
---
# Source: mysql/templates/configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: release-name-mysql-test
namespace: default
data:
run.sh: |-
"""
output.trim() == expected.trim()
}

}

0 comments on commit 22f25af

Please sign in to comment.