forked from zegl/kube-score
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
all: extendable externally usable API
Refactor/rewrite the Go API (not previously considered to be stable) to be extendable with custom checks written in Go, and to make the API easier to consume. The API is not yet considered to be stable, but this is a good step in that direction.
- Loading branch information
Showing
19 changed files
with
315 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package domain | |
|
||
import ( | ||
"io" | ||
|
||
autoscalingv1 "k8s.io/api/autoscaling/v1" | ||
|
||
appsv1 "k8s.io/api/apps/v1" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package examples | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
|
||
"github.com/zegl/kube-score/config" | ||
"github.com/zegl/kube-score/domain" | ||
"github.com/zegl/kube-score/parser" | ||
"github.com/zegl/kube-score/score" | ||
"github.com/zegl/kube-score/score/checks" | ||
"github.com/zegl/kube-score/scorecard" | ||
|
||
v1 "k8s.io/api/apps/v1" | ||
) | ||
|
||
type namedReader struct { | ||
io.Reader | ||
name string | ||
} | ||
|
||
func (n namedReader) Name() string { | ||
return n.name | ||
} | ||
|
||
// ExampleCheckObject shows how kube-score can be extended with a custom check function | ||
// | ||
// In this example, raw is a YAML encoded Kubernetes object | ||
func ExampleCheckObject(raw []byte) (*scorecard.Scorecard, error) { | ||
parser, err := parser.New(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
reader := bytes.NewReader(raw) | ||
|
||
// Parse all objects to read | ||
allObjects, err := parser.ParseFiles( | ||
[]domain.NamedReader{ | ||
namedReader{ | ||
Reader: reader, | ||
name: "input", | ||
}, | ||
}, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Register check functions to run | ||
checks := checks.New(nil) | ||
checks.RegisterDeploymentCheck("custom-deployment-check", "A custom kube-score check function", customDeploymentCheck) | ||
|
||
return score.Score(allObjects, checks, &config.RunConfiguration{}) | ||
} | ||
|
||
func customDeploymentCheck(d v1.Deployment) (scorecard.TestScore, error) { | ||
if strings.Contains(d.Name, "foo") { | ||
return scorecard.TestScore{ | ||
Grade: scorecard.GradeCritical, | ||
Comments: []scorecard.TestScoreComment{{ | ||
Summary: "Deployments names can not contian 'foo'", | ||
}}}, nil | ||
} | ||
|
||
return scorecard.TestScore{Grade: scorecard.GradeAllOK}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package examples | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/zegl/kube-score/scorecard" | ||
) | ||
|
||
func TestExampleCheckObjectAllOK(t *testing.T) { | ||
card, err := ExampleCheckObject([]byte(` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: example | ||
spec: | ||
replicas: 10 | ||
template: | ||
metadata: | ||
labels: | ||
app: foo | ||
spec: | ||
containers: | ||
- name: foobar | ||
image: foo:bar`)) | ||
|
||
assert.NoError(t, err) | ||
|
||
assert.Len(t, *card, 1) | ||
|
||
for _, v := range *card { | ||
assert.Len(t, v.Checks, 1) | ||
assert.Equal(t, "custom-deployment-check", v.Checks[0].Check.ID) | ||
assert.Equal(t, scorecard.GradeAllOK, v.Checks[0].Grade) | ||
} | ||
} | ||
|
||
func TestExampleCheckObjectErrorNameContainsFoo(t *testing.T) { | ||
card, err := ExampleCheckObject([]byte(` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: example-foo | ||
spec: | ||
replicas: 10 | ||
template: | ||
metadata: | ||
labels: | ||
app: foo | ||
spec: | ||
containers: | ||
- name: foobar | ||
image: foo:bar`)) | ||
|
||
assert.NoError(t, err) | ||
|
||
assert.Len(t, *card, 1) | ||
|
||
for _, v := range *card { | ||
assert.Len(t, v.Checks, 1) | ||
assert.Equal(t, "custom-deployment-check", v.Checks[0].Check.ID) | ||
assert.Equal(t, scorecard.GradeCritical, v.Checks[0].Grade) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.