Repository: zegl/kube-score
Pull request #95 (2019-01-31 20:50:55)

Internals refactor


Patch:
diff --git a/Dockerfile b/Dockerfile
index 1ae7837..0e31b46 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -3,12 +3,12 @@ WORKDIR /go/app
 ENV CGO_ENABLED=0
 
 ADD "go.*" /go/app/
-ADD "*.go" /go/app/
 ADD cmd /go/app/cmd/
 ADD config /go/app/config
+ADD domain /go/app/domain
 ADD parser /go/app/parser
-ADD score /go/app/score/
-ADD scorecard /go/app/scorecard/
+ADD score /go/app/score
+ADD scorecard /go/app/scorecard
 
 RUN go build github.com/zegl/kube-score/cmd/kube-score
 
diff --git a/README_CHECKS.md b/README_CHECKS.md
index c1cc8d1..35c43f7 100644
--- a/README_CHECKS.md
+++ b/README_CHECKS.md
@@ -5,7 +5,7 @@
 | cronjob-has-deadline | CronJob | Makes sure that all CronJobs has a configured deadline | 
 | container-resources | Pod | Makes sure that all pods have resource limits and requests set. The --ignore-container-cpu-limit flag can be used to disable the requirement of having a CPU limit | 
 | container-image-tag | Pod | Makes sure that a explicit non-latest tag is used | 
-| container-image-pull-policy | Pod | Makes sure that the pullPolicy is set to Always | 
+| container-image-pull-policy | Pod | Makes sure that the pullPolicy is set to Always. This makes sure that imagePullSecrets are always validated. | 
 | statefulset-has-poddisruptionbudget | StatefulSet | Makes sure that all StatefulSets are targeted by a PDB | 
 | deployment-has-poddisruptionbudget | Deployment | Makes sure that all Deployments are targeted by a PDB | 
 | pod-networkpolicy | Pod | Makes sure that all Pods are targeted by a NetworkPolicy | 
diff --git a/cmd/kube-score/main.go b/cmd/kube-score/main.go
index 29ad244..0cce98b 100644
--- a/cmd/kube-score/main.go
+++ b/cmd/kube-score/main.go
@@ -162,7 +162,7 @@ Use "-" as filename to read from STDIN.`)
 	// Detect which output format we should use
 	humanOutput := *outputFormat == "human"
 
-	for _, scoredObject := range scoreCard.Objects {
+	for _, scoredObject := range scoreCard {
 		// Headers for each object
 		if humanOutput {
 			color.New(color.FgMagenta).Printf("%s/%s %s", scoredObject.TypeMeta.APIVersion, scoredObject.TypeMeta.Kind, scoredObject.ObjectMeta.Name)
@@ -174,7 +174,7 @@ Use "-" as filename to read from STDIN.`)
 		}
 
 		for _, card := range scoredObject.Checks {
-			if _, ok := ignoredTests[card.ID]; ok {
+			if _, ok := ignoredTests[card.Check.ID]; ok {
 				continue
 			}
 
@@ -198,7 +198,7 @@ Use "-" as filename to read from STDIN.`)
 			}
 
 			if humanOutput {
-				color.New(col).Printf("    [%s] %s\n", status, card.Name)
+				color.New(col).Printf("    [%s] %s\n", status, card.Check.Name)
 
 				for _, comment := range card.Comments {
 					fmt.Printf("        * ")
diff --git a/kube-score.go b/domain/kube-score.go
similarity index 98%
rename from kube-score.go
rename to domain/kube-score.go
index 22859df..389d140 100644
--- a/kube-score.go
+++ b/domain/kube-score.go
@@ -1,4 +1,4 @@
-package kube_score
+package domain
 
 import (
 	appsv1 "k8s.io/api/apps/v1"
diff --git a/parser/parse.go b/parser/parse.go
index d80c396..aaf2931 100644
--- a/parser/parse.go
+++ b/parser/parse.go
@@ -2,8 +2,8 @@ package parser
 
 import (
 	"bytes"
-	ks "github.com/zegl/kube-score"
 	"github.com/zegl/kube-score/config"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/parser/internal"
 	"gopkg.in/yaml.v2"
 	"io/ioutil"
diff --git a/score/checks/checks.go b/score/checks/checks.go
index 99af268..61bd5b4 100644
--- a/score/checks/checks.go
+++ b/score/checks/checks.go
@@ -1,7 +1,7 @@
 package checks
 
 import (
-	ks "github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/scorecard"
 	appsv1 "k8s.io/api/apps/v1"
 	batchv1beta1 "k8s.io/api/batch/v1beta1"
diff --git a/score/container/container.go b/score/container/container.go
index bee7f00..2249ee5 100644
--- a/score/container/container.go
+++ b/score/container/container.go
@@ -11,7 +11,7 @@ import (
 func Register(allChecks *checks.Checks, cnf config.Configuration) {
 	allChecks.RegisterPodCheck("Container Resources", `Makes sure that all pods have resource limits and requests set. The --ignore-container-cpu-limit flag can be used to disable the requirement of having a CPU limit`, containerResources(!cnf.IgnoreContainerCpuLimitRequirement))
 	allChecks.RegisterPodCheck("Container Image Tag", `Makes sure that a explicit non-latest tag is used`, containerImageTag)
-	allChecks.RegisterPodCheck("Container Image Pull Policy", `Makes sure that the pullPolicy is set to Always`, containerImagePullPolicy)
+	allChecks.RegisterPodCheck("Container Image Pull Policy", `Makes sure that the pullPolicy is set to Always. This makes sure that imagePullSecrets are always validated.`, containerImagePullPolicy)
 }
 
 // containerResources makes sure that the container has resource requests and limits set
diff --git a/score/disruptionbudget/disruptionbudget.go b/score/disruptionbudget/disruptionbudget.go
index b010127..4ed68a9 100644
--- a/score/disruptionbudget/disruptionbudget.go
+++ b/score/disruptionbudget/disruptionbudget.go
@@ -1,7 +1,7 @@
 package disruptionbudget
 
 import (
-	"github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/score/checks"
 	"github.com/zegl/kube-score/score/internal"
 	"github.com/zegl/kube-score/scorecard"
@@ -11,7 +11,7 @@ import (
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 )
 
-func Register(allChecks *checks.Checks, budgets kube_score.PodDisruptionBudgets) {
+func Register(allChecks *checks.Checks, budgets ks.PodDisruptionBudgets) {
 	allChecks.RegisterStatefulSetCheck("StatefulSet has PodDisruptionBudget", `Makes sure that all StatefulSets are targeted by a PDB`, statefulSetHas(budgets.PodDisruptionBudgets()))
 	allChecks.RegisterDeploymentCheck("Deployment has PodDisruptionBudget", `Makes sure that all Deployments are targeted by a PDB`, deploymentHas(budgets.PodDisruptionBudgets()))
 }
diff --git a/score/ingress/ingress.go b/score/ingress/ingress.go
index 97071f7..872fa1a 100644
--- a/score/ingress/ingress.go
+++ b/score/ingress/ingress.go
@@ -2,14 +2,14 @@ package ingress
 
 import (
 	"fmt"
-	"github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/score/checks"
 	"github.com/zegl/kube-score/scorecard"
 	corev1 "k8s.io/api/core/v1"
 	extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
 )
 
-func Register(allChecks *checks.Checks, services kube_score.Services) {
+func Register(allChecks *checks.Checks, services ks.Services) {
 	allChecks.RegisterIngressCheck("Ingress targets Service", `Makes sure that the Ingress targets a Service`, ingressTargetsService(services.Services()))
 }
 
diff --git a/score/networkpolicy/networkpolicy.go b/score/networkpolicy/networkpolicy.go
index 282e590..b7f9b80 100644
--- a/score/networkpolicy/networkpolicy.go
+++ b/score/networkpolicy/networkpolicy.go
@@ -1,7 +1,7 @@
 package networkpolicy
 
 import (
-	ks "github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/score/checks"
 	"github.com/zegl/kube-score/score/internal"
 	"github.com/zegl/kube-score/scorecard"
diff --git a/score/probes/probes.go b/score/probes/probes.go
index b78bce6..84ad26f 100644
--- a/score/probes/probes.go
+++ b/score/probes/probes.go
@@ -1,7 +1,7 @@
 package probes
 
 import (
-	ks "github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/score/checks"
 	"github.com/zegl/kube-score/scorecard"
 	corev1 "k8s.io/api/core/v1"
diff --git a/score/score.go b/score/score.go
index f928024..d22bad8 100644
--- a/score/score.go
+++ b/score/score.go
@@ -1,8 +1,8 @@
 package score
 
 import (
-	ks "github.com/zegl/kube-score"
 	"github.com/zegl/kube-score/config"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/score/apps"
 	"github.com/zegl/kube-score/score/checks"
 	"github.com/zegl/kube-score/score/container"
@@ -38,7 +38,7 @@ func RegisterAllChecks(allObjects ks.AllTypes, cnf config.Configuration) *checks
 
 // Score runs a pre-configured list of tests against the files defined in the configuration, and returns a scorecard.
 // Additional configuration and tuning parameters can be provided via the config.
-func Score(allObjects ks.AllTypes, cnf config.Configuration) (*scorecard.Scorecard, error) {
+func Score(allObjects ks.AllTypes, cnf config.Configuration) (scorecard.Scorecard, error) {
 	allChecks := RegisterAllChecks(allObjects, cnf)
 	scoreCard := scorecard.New()
 
diff --git a/score/service/service.go b/score/service/service.go
index a7debe9..4e38f36 100644
--- a/score/service/service.go
+++ b/score/service/service.go
@@ -1,7 +1,7 @@
 package service
 
 import (
-	ks "github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	"github.com/zegl/kube-score/score/checks"
 	"github.com/zegl/kube-score/scorecard"
 	corev1 "k8s.io/api/core/v1"
diff --git a/scorecard/scorecard.go b/scorecard/scorecard.go
index 61ea8cd..5b3181d 100644
--- a/scorecard/scorecard.go
+++ b/scorecard/scorecard.go
@@ -1,7 +1,7 @@
 package scorecard
 
 import (
-	ks "github.com/zegl/kube-score"
+	ks "github.com/zegl/kube-score/domain"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 	"strings"
 )
@@ -10,18 +10,14 @@ const (
 	ignoredChecksAnnotation = "kube-score/ignore"
 )
 
-type Scorecard struct {
-	Objects map[string]*ScoredObject
-}
+type Scorecard map[string]*ScoredObject
 
 // New creates and initializes a new Scorecard
-func New() *Scorecard {
-	return &Scorecard{
-		Objects: make(map[string]*ScoredObject),
-	}
+func New() Scorecard {
+	return make(Scorecard)
 }
 
-func (s *Scorecard) NewObject(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) *ScoredObject {
+func (s Scorecard) NewObject(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) *ScoredObject {
 	o := &ScoredObject{
 		TypeMeta:   typeMeta,
 		ObjectMeta: objectMeta,
@@ -29,13 +25,13 @@ func (s *Scorecard) NewObject(typeMeta metav1.TypeMeta, objectMeta metav1.Object
 	}
 
 	// If this object already exists, return the previous version
-	if object, ok := s.Objects[o.resourceRefKey()]; ok {
+	if object, ok := s[o.resourceRefKey()]; ok {
 		return object
 	}
 
 	o.setIgnoredTests()
 
-	s.Objects[o.resourceRefKey()] = o
+	s[o.resourceRefKey()] = o
 	return o
 }
 
@@ -81,19 +77,18 @@ func (so *ScoredObject) Add(ts TestScore, check ks.Check) {
 }
 
 type TestScore struct {
-	ks.Check
+	Check ks.Check
 	Grade        Grade
 	Comments     []TestScoreComment
-	IgnoredTests []string
 }
 
 type Grade int
 
 const (
-	GradeCritical = 1
-	GradeWarning  = 5
-	GradeAlmostOK = 7
-	GradeAllOK    = 10
+	GradeCritical Grade = 1
+	GradeWarning  Grade = 5
+	GradeAlmostOK Grade = 7
+	GradeAllOK    Grade = 10
 )
 
 type TestScoreComment struct {


Test patch:
diff --git a/score/score_test.go b/score/score_test.go
index 55a0d33..3f9d755 100644
--- a/score/score_test.go
+++ b/score/score_test.go
@@ -25,9 +25,9 @@ func testExpectedScoreWithConfig(t *testing.T, config config.Configuration, file
 	sc, err := testScore(config, filename)
 	assert.NoError(t, err)
 
-	for _, objectScore := range sc.Objects {
+	for _, objectScore := range sc {
 		for _, s := range objectScore.Checks {
-			if s.Name == testcase {
+			if s.Check.Name == testcase {
 				assert.Equal(t, expectedScore, s.Grade)
 				return s.Comments
 			}
@@ -38,7 +38,7 @@ func testExpectedScoreWithConfig(t *testing.T, config config.Configuration, file
 	return nil
 }
 
-func testScore(config config.Configuration, filename string) (*scorecard.Scorecard, error) {
+func testScore(config config.Configuration, filename string) (scorecard.Scorecard, error) {
 	config.AllFiles = []io.Reader{testFile(filename)}
 
 	parsed, err := parser.ParseFiles(config)
@@ -137,11 +137,11 @@ func TestConfigMapMultiDash(t *testing.T) {
 func TestAnnotationIgnore(t *testing.T) {
 	s, err := testScore(config.Configuration{}, "ignore-annotation-service.yaml")
 	assert.Nil(t, err)
-	assert.Len(t, s.Objects, 1)
+	assert.Len(t, s, 1)
 
-	for _, o := range s.Objects {
+	for _, o := range s {
 		for _, c := range o.Checks {
-			if c.ID == "service-type" {
+			if c.Check.ID == "service-type" {
 				t.Error("service-type was tested")
 			}
 		}
@@ -152,12 +152,12 @@ func TestAnnotationIgnore(t *testing.T) {
 func TestList(t *testing.T) {
 	s, err := testScore(config.Configuration{}, "list.yaml")
 	assert.Nil(t, err)
-	assert.Len(t, s.Objects, 2)
+	assert.Len(t, s, 2)
 
 	hasService := false
 	hasDeployment := false
 
-	for _, obj := range s.Objects {
+	for _, obj := range s {
 		if obj.ObjectMeta.Name == "list-service-test" {
 			hasService = true
 		}