Skip to content

Commit f3d0328

Browse files
authored
Adding IsHelm helper function (#335)
1 parent 650b6a1 commit f3d0328

File tree

6 files changed

+87
-13
lines changed

6 files changed

+87
-13
lines changed

pkg/safeguards/preprocessing/preprocessing.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,6 @@ func CreateTempDir(p string) error {
7979
return err
8080
}
8181

82-
// IsKustomize checks whether a given path should be treated as a kustomize project
83-
func IsKustomize(p string) bool {
84-
var err error
85-
if safeguards.IsYAML(p) {
86-
return strings.Contains(p, "kustomization.yaml")
87-
} else if _, err = os.Stat(filepath.Join(p, "kustomization.yaml")); err == nil {
88-
return true
89-
} else if _, err = os.Stat(filepath.Join(p, "kustomization.yml")); err == nil {
90-
return true
91-
}
92-
return false
93-
}
94-
9582
// Given a kustomization manifest file within kustomizationPath, RenderKustomizeManifest will render templates out to tempDir
9683
func RenderKustomizeManifest(kustomizationPath, tempDir string) ([]safeguards.ManifestFile, error) {
9784
log.Debugf("Rendering kustomization.yaml...")

pkg/safeguards/preprocessing/preprocessing_helpers.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package preprocessing
33
import (
44
"fmt"
55
"os"
6+
"path/filepath"
7+
"strings"
68

9+
"github.com/Azure/draft/pkg/safeguards"
710
"gopkg.in/yaml.v3"
811
"helm.sh/helm/v3/pkg/chart"
912
"helm.sh/helm/v3/pkg/chartutil"
@@ -52,3 +55,38 @@ func getReleaseOptions(chart *chart.Chart, vals map[string]interface{}) (chartut
5255

5356
return mergedValues, nil
5457
}
58+
59+
// IsKustomize checks whether a given path should be treated as a kustomize project
60+
func IsKustomize(p string) bool {
61+
var err error
62+
if safeguards.IsYAML(p) {
63+
return strings.Contains(p, "kustomization.yaml")
64+
} else if _, err = os.Stat(filepath.Join(p, "kustomization.yaml")); err == nil {
65+
return true
66+
} else if _, err = os.Stat(filepath.Join(p, "kustomization.yml")); err == nil {
67+
return true
68+
}
69+
return false
70+
}
71+
72+
// Checks whether a given path is a helm directory or a path to a Helm Chart (contains/is Chart.yaml)
73+
func IsHelm(path string) bool {
74+
fileInfo, err := os.Stat(path)
75+
if os.IsNotExist(err) || err != nil {
76+
return false
77+
}
78+
79+
var chartPath string
80+
if fileInfo.IsDir() {
81+
chartPath = filepath.Join(path, "Chart.yaml")
82+
} else {
83+
chartPath = path
84+
}
85+
86+
_, err = os.Stat(chartPath)
87+
if err == nil && safeguards.IsYAML(chartPath) { // Couldn't find Chart.yaml in the directory
88+
return true
89+
}
90+
91+
return false
92+
}

pkg/safeguards/preprocessing/preprocessing_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,21 @@ func TestIsKustomize(t *testing.T) {
193193
isKustomize = IsKustomize(chartPath)
194194
assert.False(t, isKustomize)
195195
}
196+
197+
func TestIsHelm(t *testing.T) {
198+
// path is a directory
199+
isHelm := IsHelm(chartPath)
200+
assert.True(t, isHelm)
201+
202+
// path is a Chart.yaml file
203+
isHelm = IsHelm(directPath_ToValidChart)
204+
assert.True(t, isHelm)
205+
206+
// Is a directory but does not contain Chart.yaml
207+
isHelm = IsHelm(invalidNoChart)
208+
assert.False(t, isHelm)
209+
210+
// invalid path
211+
isHelm = IsHelm("invalid/path")
212+
assert.False(t, isHelm)
213+
}

pkg/safeguards/preprocessing/preprocessing_test_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
const (
1111
tempDir = "testdata" // Rendered files are stored here before they are read for comparison
1212
chartPath = "../tests/testmanifests/validchart"
13+
invalidNoChart = "../tests/testmanifests/nochart-invalid"
1314
invalidChartPath = "../tests/testmanifests/invalidchart"
1415
invalidValuesChart = "../tests/testmanifests/invalidvalues"
1516
invalidDeploymentsChart = "../tests/testmanifests/invaliddeployment"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: {{ .Release.Name }}-deployment
5+
namespace: {{ .Release.Namespace }}
6+
spec:
7+
replicas: {{ .Values.replicaCount }}
8+
selector:
9+
matchLabels:
10+
app: my-web-app
11+
template:
12+
metadata:
13+
labels:
14+
app: my-web-app
15+
spec:
16+
containers:
17+
- name: nginx
18+
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
replicaCount: 1
2+
image:
3+
repository: nginx
4+
tag: stable
5+
service:
6+
type: ClusterIP
7+
port: 80
8+
ingress:
9+
enabled: true
10+
hostname: example.com
11+
releaseName: test-release
12+
releaseNamespace: test-namespace

0 commit comments

Comments
 (0)