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.
Implement feature request: a configuration file zegl#384
- Loading branch information
Showing
4 changed files
with
233 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
flag "github.com/spf13/pflag" | ||
"github.com/zegl/kube-score/config" | ||
"github.com/zegl/kube-score/parser" | ||
"github.com/zegl/kube-score/score" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type kubescorechecks struct { | ||
AddAllDefaultChecks bool `yaml:"addAllDefaultChecks"` | ||
AddAllOptionalChecks bool `yaml:"addAllOptionalChecks"` | ||
DisableIgnoreChecksAnnotations bool `yaml:"disableIgnoreChecksAnnotations"` | ||
DefaultChecks []string `yaml:"defaultChecks"` | ||
OptionalChecks []string `yaml:"optionalChecks"` | ||
IncludeChecks []string `yaml:"include"` | ||
ExcludeChecks []string `yaml:"exclude"` | ||
} | ||
|
||
func mkConfigFile(binName string, args []string) { | ||
fs := flag.NewFlagSet(binName, flag.ExitOnError) | ||
printHelp := fs.Bool("help", false, "Print help") | ||
setDefault(fs, binName, "mkconfig", false) | ||
cfgFile := fs.String("config", ".kube-score.yml", "Optional kube-score configuration file") | ||
cfgForce := fs.Bool("force", false, "Force overwrite of existing .kube-score.yml file") | ||
|
||
err := fs.Parse(args) | ||
|
||
if err != nil { | ||
panic("Failed to parse mkconfig arguments") | ||
} | ||
|
||
if *printHelp { | ||
fs.Usage() | ||
return | ||
} | ||
|
||
if _, err := os.Stat(*cfgFile); err == nil { | ||
if !*cfgForce { | ||
errmsg := fmt.Errorf("File %s exists. Use --force flag to overwrite\n", *cfgFile) | ||
fmt.Println(errmsg) | ||
fs.Usage() | ||
return | ||
} | ||
} | ||
|
||
allChecks := score.RegisterAllChecks(parser.Empty(), config.Configuration{}) | ||
|
||
var checks kubescorechecks | ||
|
||
checks.AddAllDefaultChecks = true | ||
checks.AddAllOptionalChecks = false | ||
checks.DisableIgnoreChecksAnnotations = false | ||
|
||
for _, c := range allChecks.All() { | ||
if c.Optional { | ||
checks.OptionalChecks = append(checks.OptionalChecks, c.ID) | ||
} else { | ||
checks.DefaultChecks = append(checks.DefaultChecks, c.ID) | ||
} | ||
} | ||
|
||
if o, err := yaml.Marshal(&checks); err != nil { | ||
err := fmt.Errorf("Failed to marshal checks") | ||
fmt.Println(err.Error()) | ||
} else { | ||
if err := os.WriteFile(*cfgFile, []byte(o), 0600); err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("Created kube-score configuration file ", *cfgFile) | ||
} | ||
} | ||
|
||
func loadConfigFile(fp string) (config kubescorechecks) { | ||
|
||
content, err := os.ReadFile(fp) | ||
|
||
// if the file does not exist, create it | ||
if err != nil { | ||
mkConfigFile("mkconfig", []string{fp}) | ||
} | ||
|
||
err2 := yaml.Unmarshal(content, &config) | ||
if err2 != nil { | ||
panic(err2) | ||
} | ||
|
||
return config | ||
} | ||
|
||
func includeChecks(k *kubescorechecks) (checks []string) { | ||
if k.AddAllOptionalChecks { | ||
checks = append(checks, k.OptionalChecks...) | ||
} | ||
if len(k.IncludeChecks) > 0 { | ||
checks = append(checks, k.IncludeChecks...) | ||
} | ||
return | ||
} | ||
|
||
func excludeChecks(k *kubescorechecks) (checks []string) { | ||
if !k.AddAllDefaultChecks { | ||
checks = append(checks, k.DefaultChecks...) | ||
} | ||
if len(k.ExcludeChecks) > 0 { | ||
checks = append(checks, k.ExcludeChecks...) | ||
} | ||
return | ||
} |
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,55 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestKubeScoreConfigExcludeAllDefaultChecks(t *testing.T) { | ||
|
||
cfg := loadConfigFile("testdata/kube-score.yml") | ||
cfg.AddAllDefaultChecks = false | ||
excludeThese := excludeChecks(&cfg) | ||
|
||
assert.Equal(t, len(excludeThese), len(cfg.DefaultChecks)) | ||
} | ||
|
||
func TestKubeScoreConfigIncludeAllOptionalChecks(t *testing.T) { | ||
|
||
cfg := loadConfigFile("testdata/kube-score.yml") | ||
cfg.AddAllOptionalChecks = true | ||
includeThese := includeChecks(&cfg) | ||
|
||
assert.Equal(t, len(includeThese), len(cfg.OptionalChecks)) | ||
} | ||
|
||
func TestKubeScoreConfigExcludeSelectDefaultChecks(t *testing.T) { | ||
|
||
cfg := loadConfigFile("testdata/kube-score.yml") | ||
cfg.AddAllDefaultChecks = true | ||
cfg.ExcludeChecks = append(cfg.ExcludeChecks, "pod-probes") | ||
excludeThese := excludeChecks(&cfg) | ||
|
||
assert.Contains(t, cfg.ExcludeChecks, "pod-probes") | ||
assert.Equal(t, len(excludeThese), 1) | ||
} | ||
|
||
func TestKubeScoreConfigNoDefaultChecksIncludeSelectChecks(t *testing.T) { | ||
|
||
cfg := loadConfigFile("testdata/kube-score.yml") | ||
cfg.AddAllDefaultChecks = false | ||
|
||
onlyThese := []string{"container-resources", "image-tag", "image-pull-policy"} | ||
|
||
cfg.IncludeChecks = append(cfg.IncludeChecks, onlyThese...) | ||
includeThese := includeChecks(&cfg) | ||
|
||
for _, v := range onlyThese { | ||
assert.Contains(t, cfg.IncludeChecks, v) | ||
} | ||
|
||
assert.NotContains(t, cfg.IncludeChecks, "pod-networkpolicy") | ||
|
||
assert.Equal(t, len(includeThese), len(onlyThese)) | ||
} |
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,40 @@ | ||
addAllDefaultChecks: true | ||
addAllOptionalChecks: false | ||
disableIgnoreChecksAnnotations: false | ||
defaultChecks: | ||
- ingress-targets-service | ||
- cronjob-has-deadline | ||
- container-resources | ||
- container-image-tag | ||
- container-image-pull-policy | ||
- container-ephemeral-storage-request-and-limit | ||
- statefulset-has-poddisruptionbudget | ||
- deployment-has-poddisruptionbudget | ||
- poddisruptionbudget-has-policy | ||
- pod-networkpolicy | ||
- networkpolicy-targets-pod | ||
- pod-probes | ||
- container-security-context-user-group-id | ||
- container-security-context-privileged | ||
- container-security-context-readonlyrootfilesystem | ||
- service-targets-pod | ||
- service-type | ||
- stable-version | ||
- deployment-has-host-podantiaffinity | ||
- statefulset-has-host-podantiaffinity | ||
- deployment-targeted-by-hpa-does-not-have-replicas-configured | ||
- statefulset-has-servicename | ||
- deployment-pod-selector-labels-match-template-metadata-labels | ||
- statefulset-pod-selector-labels-match-template-metadata-labels | ||
- label-values | ||
- horizontalpodautoscaler-has-target | ||
- container-ephemeral-storage-requests-and-limits | ||
optionalChecks: | ||
- container-resource-requests-equal-limits | ||
- container-cpu-requests-equal-limits | ||
- container-memory-requests-equal-limits | ||
- container-ephemeral-storage-request-equals-limit | ||
- container-ports-check | ||
- container-seccomp-profile | ||
include: [] | ||
exclude: [] |