Skip to content

Commit

Permalink
Support read yaml contents of workload from stdin (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozillazg committed Jun 22, 2020
1 parent 0a0720a commit 5b173cf
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/usage.md
Expand Up @@ -149,6 +149,7 @@ Both the dashboard and audits can run against a local directory or YAML file
rather than a cluster:
```bash
polaris audit --audit-path ./deploy/
cat pod.yaml | polaris audit --audit-path -
```

#### Running with CI/CD
Expand Down
51 changes: 37 additions & 14 deletions pkg/kube/resources.go
Expand Up @@ -3,6 +3,7 @@ package kube
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -61,8 +62,14 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
Controllers: []GenericWorkload{},
}

addYaml := func(contents string) error {
return addResourceFromString(contents, &resources)
if directory == "-" {
fi, err := os.Stdin.Stat()
if err == nil && fi.Mode()&os.ModeNamedPipe == os.ModeNamedPipe {
if err := addResourcesFromReader(os.Stdin, &resources); err != nil {
return nil, err
}
return &resources, nil
}
}

visitFile := func(path string, f os.FileInfo, err error) error {
Expand All @@ -74,18 +81,7 @@ func CreateResourceProviderFromPath(directory string) (*ResourceProvider, error)
logrus.Errorf("Error reading file: %v", path)
return err
}
specs := regexp.MustCompile("[\r\n]-+[\r\n]").Split(string(contents), -1)
for _, spec := range specs {
if strings.TrimSpace(spec) == "" {
continue
}
err = addYaml(spec)
if err != nil {
logrus.Errorf("Error parsing YAML: (%v)", err)
return err
}
}
return nil
return addResourcesFromYaml(string(contents), &resources)
}

err := filepath.Walk(directory, visitFile)
Expand Down Expand Up @@ -234,6 +230,33 @@ func GetPodSpec(yaml map[string]interface{}) interface{} {
return nil
}

func addResourcesFromReader(reader io.Reader, resources *ResourceProvider) error {
contents, err := ioutil.ReadAll(reader)
if err != nil {
logrus.Errorf("Error reading from %v: %v", reader, err)
return err
}
if err := addResourcesFromYaml(string(contents), resources); err != nil {
return err
}
return nil
}

func addResourcesFromYaml(contents string, resources *ResourceProvider) error {
specs := regexp.MustCompile("[\r\n]-+[\r\n]").Split(string(contents), -1)
for _, spec := range specs {
if strings.TrimSpace(spec) == "" {
continue
}
err := addResourceFromString(spec, resources)
if err != nil {
logrus.Errorf("Error parsing YAML: (%v)", err)
return err
}
}
return nil
}

func addResourceFromString(contents string, resources *ResourceProvider) error {
contentBytes := []byte(contents)
decoder := k8sYaml.NewYAMLOrJSONDecoder(bytes.NewReader(contentBytes), 1000)
Expand Down
28 changes: 28 additions & 0 deletions pkg/kube/resources_test.go
@@ -1,11 +1,14 @@
package kube

import (
"bytes"
"io/ioutil"
"testing"
"time"

"github.com/fairwindsops/polaris/test"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
)

func TestGetResourcesFromPath(t *testing.T) {
Expand Down Expand Up @@ -57,6 +60,31 @@ func TestGetMultipleResourceFromBadFile(t *testing.T) {
assert.NotEqual(t, nil, err, "CreateResource From Path should fail with bad yaml")
}

func TestAddResourcesFromReader(t *testing.T) {
contents, err := ioutil.ReadFile("./test_files/test_2/multi.yaml")
assert.NoError(t, err)
reader := bytes.NewBuffer(contents)
resources := &ResourceProvider{
ServerVersion: "unknown",
SourceType: "Path",
SourceName: "-",
Nodes: []corev1.Node{},
Namespaces: []corev1.Namespace{},
Controllers: []GenericWorkload{},
}
err = addResourcesFromReader(reader, resources)
assert.NoError(t, err)

assert.Equal(t, 0, len(resources.Nodes), "Should not have any nodes")

assert.Equal(t, 1, len(resources.Controllers), "Should have one controller")
assert.Equal(t, "dashboard", resources.Controllers[0].PodSpec.Containers[0].Name)

assert.Equal(t, 2, len(resources.Namespaces), "Should have a namespace")
assert.Equal(t, "polaris", resources.Namespaces[0].ObjectMeta.Name)
assert.Equal(t, "polaris-2", resources.Namespaces[1].ObjectMeta.Name)
}

func TestGetResourceFromAPI(t *testing.T) {
k8s, dynamicInterface := test.SetupTestAPI()
k8s = test.SetupAddControllers(k8s, "test")
Expand Down

0 comments on commit 5b173cf

Please sign in to comment.