Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

score: allow to disable scoring of objects in specific namespaces #365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/kube-score/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func scoreFiles(binName string, args []string) error {
outputVersion := fs.String("output-version", "", "Changes the version of the --output-format. The 'json' format has version 'v2' (default) and 'v1' (deprecated, will be removed in v1.7.0). The 'human' and 'ci' formats has only version 'v1' (default). If not explicitly set, the default version for that particular output format will be used.")
optionalTests := fs.StringSlice("enable-optional-test", []string{}, "Enable an optional test, can be set multiple times")
ignoreTests := fs.StringSlice("ignore-test", []string{}, "Disable a test, can be set multiple times")
ignoreNamespaces := fs.StringSlice("ignore-namespace", []string{}, "Disable test on specific namespace, can be set multiple times")
disableIgnoreChecksAnnotation := fs.Bool("disable-ignore-checks-annotations", false, "Set to true to disable the effect of the 'kube-score/ignore' annotations")
kubernetesVersion := fs.String("kubernetes-version", "v1.18", "Setting the kubernetes-version will affect the checks ran against the manifests. Set this to the version of Kubernetes that you're using in production for the best results.")
setDefault(fs, binName, "score", false)
Expand Down Expand Up @@ -154,6 +155,7 @@ Use "-" as filename to read from STDIN.`, execName(binName))
}

ignoredTests := listToStructMap(ignoreTests)
ignoredNamespaces := listToStructMap(ignoreNamespaces)
enabledOptionalTests := listToStructMap(optionalTests)

kubeVer, err := config.ParseSemver(*kubernetesVersion)
Expand All @@ -166,6 +168,7 @@ Use "-" as filename to read from STDIN.`, execName(binName))
VerboseOutput: *verboseOutput,
IgnoreContainerCpuLimitRequirement: *ignoreContainerCpuLimit,
IgnoreContainerMemoryLimitRequirement: *ignoreContainerMemoryLimit,
IgnoredNamespaces: ignoredNamespaces,
IgnoredTests: ignoredTests,
EnabledOptionalTests: enabledOptionalTests,
UseIgnoreChecksAnnotation: !*disableIgnoreChecksAnnotation,
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Configuration struct {
VerboseOutput int
IgnoreContainerCpuLimitRequirement bool
IgnoreContainerMemoryLimitRequirement bool
IgnoredNamespaces map[string]struct{}
IgnoredTests map[string]struct{}
EnabledOptionalTests map[string]struct{}
UseIgnoreChecksAnnotation bool
Expand Down
2 changes: 1 addition & 1 deletion score/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Score(allObjects ks.AllTypes, cnf config.Configuration) (*scorecard.Scoreca
scoreCard := scorecard.New()

newObject := func(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) *scorecard.ScoredObject {
return scoreCard.NewObject(typeMeta, objectMeta, cnf.UseIgnoreChecksAnnotation)
return scoreCard.NewObject(typeMeta, objectMeta, cnf.UseIgnoreChecksAnnotation, cnf.IgnoredNamespaces)
}

for _, ingress := range allObjects.Ingresses() {
Expand Down
28 changes: 28 additions & 0 deletions score/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,31 @@ func TestContainerSecurityContextReadOnlyRootFilesystemNoSecurityContext(t *test
Description: "Set securityContext to run the container in a more secure context.",
})
}

func TestServiceIgnoreNamespace(t *testing.T) {
t.Parallel()

structMap := make(map[string]struct{})
structMap["site"] = struct{}{}

s, err := testScore(config.Configuration{
VerboseOutput: 0,
AllFiles: []ks.NamedReader{testFile("service-externalname.yaml")},
IgnoredNamespaces: structMap,
})
assert.Nil(t, err)
assert.Len(t, s, 1)

tested := false

for _, o := range s {
for _, c := range o.Checks {
if c.Check.ID == "service-targets-pod" {
assert.True(t, c.Skipped)
assert.Equal(t, scorecard.GradeAllOK, c.Grade)
tested = true
}
}
}
assert.True(t, tested)
}
17 changes: 12 additions & 5 deletions scorecard/scorecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ func New() Scorecard {
return make(Scorecard)
}

func (s Scorecard) NewObject(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta, useIgnoreChecksAnnotation bool) *ScoredObject {
func (s Scorecard) NewObject(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta, useIgnoreChecksAnnotation bool, ignoredNamespaces map[string]struct{}) *ScoredObject {
o := &ScoredObject{
TypeMeta: typeMeta,
ObjectMeta: objectMeta,
Checks: make([]TestScore, 0),
TypeMeta: typeMeta,
ObjectMeta: objectMeta,
Checks: make([]TestScore, 0),
ignoredNamespaces: ignoredNamespaces,
}

// If this object already exists, return the previous version
Expand Down Expand Up @@ -54,7 +55,8 @@ type ScoredObject struct {
FileLocation ks.FileLocation
Checks []TestScore

ignoredChecks map[string]struct{}
ignoredChecks map[string]struct{}
ignoredNamespaces map[string]struct{}
}

func (s ScoredObject) AnyBelowOrEqualToGrade(threshold Grade) bool {
Expand Down Expand Up @@ -99,6 +101,11 @@ func (so *ScoredObject) Add(ts TestScore, check ks.Check, locationer ks.FileLoca
ts.Comments = []TestScoreComment{{Summary: fmt.Sprintf("Skipped because %s is ignored", check.ID)}}
}

if _, ok := so.ignoredNamespaces[so.ObjectMeta.Namespace]; ok {
ts.Skipped = true
ts.Comments = []TestScoreComment{{Summary: fmt.Sprintf("Skipped because the %s namespace is ignored", so.ObjectMeta.Namespace)}}
}

so.Checks = append(so.Checks, ts)
}

Expand Down