Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Ignore helm charts when scanning manifests #993

Merged
merged 3 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cluster/kubernetes/resource/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func Load(roots ...string) (map[string]resource.Resource, error) {
if err != nil {
return errors.Wrapf(err, "walking %q for yamels", path)
}

if info.IsDir() && looksLikeChart(path) {
return filepath.SkipDir
}

if !info.IsDir() && filepath.Ext(path) == ".yaml" || filepath.Ext(path) == ".yml" {
bytes, err := ioutil.ReadFile(path)
if err != nil {
Expand All @@ -47,6 +52,24 @@ func Load(roots ...string) (map[string]resource.Resource, error) {
return objs, nil
}

// looksLikeChart returns `true` if the path `dir` (assumed to be a
// directory) looks like it contains a Helm chart, rather than
// manifest files.
func looksLikeChart(dir string) bool {
// These are the two mandatory parts of a chart. If they both
// exist, chances are it's a chart. See
// https://github.com/kubernetes/helm/blob/master/docs/charts.md#the-chart-file-structure
chartpath := filepath.Join(dir, "Chart.yaml")
valuespath := filepath.Join(dir, "values.yaml")
if _, err := os.Stat(chartpath); err != nil && os.IsNotExist(err) {
return false
}
if _, err := os.Stat(valuespath); err != nil && os.IsNotExist(err) {
return false
}
return true
}

// ParseMultidoc takes a dump of config (a multidoc YAML) and
// constructs an object set from the resources represented therein.
func ParseMultidoc(multidoc []byte, source string) (map[string]resource.Resource, error) {
Expand Down
5 changes: 2 additions & 3 deletions cluster/kubernetes/resource/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ func TestLoadSome(t *testing.T) {
if err != nil {
t.Error(err)
}
// assume it's one per file for the minute
if len(objs) != len(testfiles.Files) {
t.Errorf("expected %d objects from %d files, got result:\n%#v", len(testfiles.Files), len(testfiles.Files), objs)
if len(objs) != len(testfiles.ServiceMap(dir)) {
t.Errorf("expected %d objects from %d files, got result:\n%#v", len(testfiles.ServiceMap(dir)), len(testfiles.Files), objs)
}
}
70 changes: 68 additions & 2 deletions cluster/kubernetes/testfiles/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func TempDir(t *testing.T) (string, func()) {
func WriteTestFiles(dir string) error {
for name, content := range Files {
path := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil {
return err
}
if err := ioutil.WriteFile(path, []byte(content), 0666); err != nil {
return err
}
Expand All @@ -45,11 +48,13 @@ func ServiceMap(dir string) map[flux.ResourceID][]string {
return map[flux.ResourceID][]string{
flux.MustParseResourceID("default:deployment/helloworld"): []string{filepath.Join(dir, "helloworld-deploy.yaml")},
flux.MustParseResourceID("default:deployment/locked-service"): []string{filepath.Join(dir, "locked-service-deploy.yaml")},
flux.MustParseResourceID("default:deployment/test-service"): []string{filepath.Join(dir, "test-service-deploy.yaml")},
flux.MustParseResourceID("default:deployment/test-service"): []string{filepath.Join(dir, "test/test-service-deploy.yaml")},
}
}

var Files = map[string]string{
"garbage": "This should just be ignored, since it is not YAML",
// Some genuine manifests
"helloworld-deploy.yaml": `apiVersion: extensions/v1beta1
kind: Deployment
metadata:
Expand Down Expand Up @@ -98,7 +103,7 @@ spec:
ports:
- containerPort: 80
`,
"test-service-deploy.yaml": `apiVersion: extensions/v1beta1
"test/test-service-deploy.yaml": `apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-service
Expand All @@ -118,6 +123,67 @@ spec:
ports:
- containerPort: 80
`,

// A tricksy chart directory, which should be skipped entirely. Adapted from
// https://github.com/kubernetes/helm/tree/master/docs/examples
"charts/nginx/Chart.yaml": `---
name: nginx
description: A basic NGINX HTTP server
version: 0.1.0
kubeVersion: ">=1.2.0"
keywords:
- http
- nginx
- www
- web
home: https://github.com/kubernetes/helm
sources:
- https://hub.docker.com/_/nginx/
maintainers:
- name: technosophos
email: [email protected]
`,
"charts/nginx/values.yaml": `---
# Declare name/value pairs to be passed into your templates.
replicaCount: 1
restartPolicy: Never
index: >-
<h1>Hello</h1>
<p>This is a test</p>
image:
repository: nginx
tag: 1.11.0
pullPolicy: IfNotPresent
`,
"charts/nginx/templates/deployment.yaml": `---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: {{ template "nginx.fullname" . }}
labels:
app: {{ template "nginx.name" . }}
spec:
replicas: {{ .Values.replicaCount }}
template:
metadata:
{{- if .Values.podAnnotations }}
# Allows custom annotations to be specified
annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
labels:
app: {{ template "nginx.name" . }}
release: {{ .Release.Name }}
spec:
containers:
- name: {{ template "nginx.name" . }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
imagePullPolicy: {{ .Values.image.pullPolicy }}
ports:
- name: http
containerPort: 80
protocol: TCP
`,
}

var FilesUpdated = map[string]string{
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestDaemon_NotifyChange(t *testing.T) {
t.Errorf("Sync was not called once, was called %d times", syncCalled)
} else if syncDef == nil {
t.Errorf("Sync was called with a nil syncDef")
} else if len(syncDef.Actions) != len(testfiles.Files) {
} else if len(syncDef.Actions) != len(testfiles.ServiceMap("unimportant")) {
t.Errorf("Sync was not called with the %d actions, was called with: %d", len(testfiles.Files), len(syncDef.Actions))
}

Expand Down
7 changes: 3 additions & 4 deletions sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -45,11 +44,11 @@ func TestSync(t *testing.T) {
}
checkClusterMatchesFiles(t, manifests, clus, checkout.ManifestDir())

for file := range testfiles.Files {
if err := execCommand("rm", filepath.Join(checkout.ManifestDir(), file)); err != nil {
for _, res := range testfiles.ServiceMap(checkout.ManifestDir()) {
if err := execCommand("rm", res[0]); err != nil {
t.Fatal(err)
}
commitAction := &git.CommitAction{Author: "", Message: "deleted " + file}
commitAction := &git.CommitAction{Author: "", Message: "deleted " + res[0]}
if err := checkout.CommitAndPush(context.Background(), commitAction, nil); err != nil {
t.Fatal(err)
}
Expand Down