Skip to content

Commit aa4e247

Browse files
authored
Implement first pass of kube-linter lint command (#4)
1 parent e272586 commit aa4e247

File tree

42 files changed

+1898
-6
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1898
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@
99

1010
# Empty file touched by `make deps`
1111
/deps
12+
13+
# Packr generated files
14+
*-packr.go

Makefile

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ $(STATICCHECK_BIN): deps
3333
@echo "+ $@"
3434
@go install honnef.co/go/tools/cmd/staticcheck
3535

36+
PACKR_BIN := $(GOBIN)/packr
37+
$(PACKR_BIN): deps
38+
@echo "+ $@"
39+
@go install github.com/gobuffalo/packr/packr
40+
3641
###########
3742
## Lint ##
3843
###########
@@ -50,7 +55,19 @@ endif
5055

5156
.PHONY: staticcheck
5257
staticcheck: $(STATICCHECK_BIN)
53-
staticcheck -checks=all ./...
58+
staticcheck -checks=all,-ST1000 ./...
5459

5560
.PHONY: lint
5661
lint: golangci-lint staticcheck
62+
63+
#############
64+
## Compile ##
65+
#############
66+
67+
.PHONY: packr
68+
packr: $(PACKR_BIN)
69+
packr
70+
71+
.PHONY: build
72+
build: packr
73+
go build -o kube-linter ./cmd/kubelinter

cmd/kubelinter/kube-linter.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ package main
22

33
import (
44
"fmt"
5+
"os"
6+
7+
"golang.stackrox.io/kube-linter/internal/command/root"
8+
// Register templates
9+
_ "golang.stackrox.io/kube-linter/internal/templates/all"
510
)
611

712
func main() {
8-
fmt.Println("This is kube-linter. It does not do anything yet!")
13+
c := root.Command()
14+
if err := c.Execute(); err != nil {
15+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
16+
os.Exit(1)
17+
}
918
}

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ module golang.stackrox.io/kube-linter
33
go 1.14
44

55
require (
6+
github.com/fatih/color v1.9.0
7+
github.com/ghodss/yaml v1.0.0
8+
github.com/gobuffalo/packr v1.30.1
69
github.com/golangci/golangci-lint v1.30.0
10+
github.com/pkg/errors v0.9.1
11+
github.com/spf13/cobra v1.0.0
12+
github.com/stretchr/objx v0.2.0 // indirect
13+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
14+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
715
honnef.co/go/tools v0.0.1-2020.1.5
16+
k8s.io/api v0.19.1
17+
k8s.io/apimachinery v0.19.1
18+
k8s.io/client-go v0.19.0
819
)

go.sum

Lines changed: 167 additions & 3 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package builtinchecks
2+
3+
import (
4+
"github.com/ghodss/yaml"
5+
"github.com/gobuffalo/packr"
6+
"github.com/pkg/errors"
7+
"golang.stackrox.io/kube-linter/internal/check"
8+
"golang.stackrox.io/kube-linter/internal/checkregistry"
9+
)
10+
11+
var (
12+
box = packr.NewBox("./yamls")
13+
)
14+
15+
// LoadInto loads built-in checks into the registry.
16+
func LoadInto(registry checkregistry.CheckRegistry) error {
17+
for _, fileName := range box.List() {
18+
contents, err := box.Find(fileName)
19+
if err != nil {
20+
return errors.Wrapf(err, "loading default check from %s", fileName)
21+
}
22+
var chk check.Check
23+
if err := yaml.Unmarshal(contents, &chk); err != nil {
24+
return errors.Wrapf(err, "unmarshaling default check from %s", fileName)
25+
}
26+
if err := registry.Register(&chk); err != nil {
27+
return errors.Wrapf(err, "registering default check from %s", fileName)
28+
}
29+
}
30+
return nil
31+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: "env-var-secret"
2+
description: "Alert on objects using a secret in an environment variable"
3+
scope:
4+
objectKinds:
5+
- DeploymentLike
6+
template: "env-var"
7+
params:
8+
name: ".*secret.*"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: "privileged-container"
2+
description: "Alert on deployments with containers running in privileged mode"
3+
scope:
4+
objectKinds:
5+
- DeploymentLike
6+
template: "privileged"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: "required-label-owner"
2+
description: "Alert on objects without the 'owner' label"
3+
scope:
4+
objectKinds:
5+
- DeploymentLike
6+
template: "required-label"
7+
params:
8+
key: "owner"

internal/check/check.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package check
2+
3+
// A Check represents a single check. It is serializable.
4+
type Check struct {
5+
Name string `json:"name"`
6+
Description string `json:"description"`
7+
Scope *ObjectKindsDesc `json:"scope"`
8+
Template string `json:"template"`
9+
Params map[string]string `json:"params,omitempty"`
10+
}

0 commit comments

Comments
 (0)