Skip to content

Commit

Permalink
skip incomplete or broken YAML - warn user (FairwindsOps#678)
Browse files Browse the repository at this point in the history
* skip broken yaml (eg, patch file)

* skip in visitFile, not in parser

* restore filepath.Walk() error handling

* restore test; correct assertion

* Update pkg/kube/resources_test.go

Co-authored-by: Robert Brennan <[email protected]>

* Fix tests

* update kind

Co-authored-by: Robert Brennan <[email protected]>
Co-authored-by: Luke Reed <[email protected]>
Co-authored-by: Barnabas Makonda <[email protected]>
Co-authored-by: MAKOSCAFEE <[email protected]>
  • Loading branch information
5 people committed Jun 7, 2022
1 parent f71ca99 commit 3b865fc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ references:
sudo apt-get install -yqq jq git
echo "Installing KIND"
curl -sLO https://github.com/kubernetes-sigs/kind/releases/download/v0.8.1/kind-linux-amd64
curl -sLO https://github.com/kubernetes-sigs/kind/releases/download/v0.14.0/kind-linux-amd64
chmod 0755 kind-linux-amd64
sudo mv kind-linux-amd64 /usr/local/bin/kind
kind version
echo "Installing Kubectl"
curl -sLO https://storage.googleapis.com/kubernetes-release/release/v1.18.6/bin/linux/amd64/kubectl
curl -sLO https://storage.googleapis.com/kubernetes-release/release/v1.21.12/bin/linux/amd64/kubectl
chmod 0755 kubectl
sudo mv kubectl /usr/local/bin/
kubectl version --client
echo "Creating Kubernetes Cluster with Kind"
kind create cluster --wait=90s --image kindest/node:v1.15.11
kind create cluster --wait=90s --image kindest/node:v1.21.12
docker ps -a
kubectl version
Expand Down
7 changes: 5 additions & 2 deletions pkg/kube/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,11 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
logrus.Errorf("Error reading file: %v", path)
return err
}
return resources.addResourcesFromYaml(string(contents))
err = resources.addResourcesFromYaml(string(contents))
if err != nil {
logrus.Warnf("Skipping %s: cannot add resource from YAML: %v", path, err)
}
return nil
}

err := filepath.Walk(directory, visitFile)
Expand Down Expand Up @@ -443,7 +447,6 @@ func (resources *ResourceProvider) addResourceFromString(contents string) error
decoder = k8sYaml.NewYAMLOrJSONDecoder(bytes.NewReader(contentBytes), 1000)

if err != nil {
logrus.Errorf("Invalid YAML: %s", string(contents))
return err
}
if resource.Kind == "Namespace" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kube/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestGetMultipleResourceFromSingleFile(t *testing.T) {

func TestGetMultipleResourceFromBadFile(t *testing.T) {
_, err := CreateResourceProviderFromPath("./test_files/test_3")
assert.NotEqual(t, nil, err, "CreateResource From Path should fail with bad yaml")
assert.Equal(t, nil, err, "CreateResource From Path should not fail with bad yaml")
}

func TestAddResourcesFromReader(t *testing.T) {
Expand Down

0 comments on commit 3b865fc

Please sign in to comment.