mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-19 18:07:02 +00:00
Fail early on incorrect version of k8s
This commit is contained in:
parent
808e6d4aa5
commit
a182e2abdc
4 changed files with 30 additions and 4 deletions
2
Makefile
2
Makefile
|
|
@ -5,7 +5,7 @@ RELEASE_VERSION ?= latest
|
||||||
.PHONY: local-test local-build local-build-static docker-test docker-build docker-build-static build-bats docker-acceptance release update-deps build-single-target
|
.PHONY: local-test local-build local-build-static docker-test docker-build docker-build-static build-bats docker-acceptance release update-deps build-single-target
|
||||||
|
|
||||||
local-test:
|
local-test:
|
||||||
go test -race ./...
|
go test -race ./... -count=1
|
||||||
|
|
||||||
local-build:
|
local-build:
|
||||||
git config --global --add safe.directory $$PWD
|
git config --global --add safe.directory $$PWD
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,13 @@ resetCacheFolder() {
|
||||||
[ "$status" -eq 1 ]
|
[ "$status" -eq 1 ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "Fail early when passing a non valid -kubernetes-version" {
|
||||||
|
run bin/kubeconform -kubernetes-version 1.25 fixtures/valid.yaml
|
||||||
|
[[ "$output" == "invalid value "1.25" for flag -kubernetes-version: 1.25 is not a valid version. Valid values are 'master' (default) or full version x.y.z (e.g. '1.27.2')"* ]]
|
||||||
|
[[ `echo "$output" | wc -l` -eq 1 ]]
|
||||||
|
[ "$status" -eq 1 ]
|
||||||
|
}
|
||||||
|
|
||||||
@test "Pass with a valid input when validating against openshift manifests" {
|
@test "Pass with a valid input when validating against openshift manifests" {
|
||||||
run bin/kubeconform -kubernetes-version 3.8.0 -schema-location 'https://raw.githubusercontent.com/garethr/openshift-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}.json' -summary fixtures/valid.yaml
|
run bin/kubeconform -kubernetes-version 3.8.0 -schema-location 'https://raw.githubusercontent.com/garethr/openshift-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}.json' -summary fixtures/valid.yaml
|
||||||
[ "$status" -eq 0 ]
|
[ "$status" -eq 0 ]
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@ func kubeconform(cfg config.Config) int {
|
||||||
SkipTLS: cfg.SkipTLS,
|
SkipTLS: cfg.SkipTLS,
|
||||||
SkipKinds: cfg.SkipKinds,
|
SkipKinds: cfg.SkipKinds,
|
||||||
RejectKinds: cfg.RejectKinds,
|
RejectKinds: cfg.RejectKinds,
|
||||||
KubernetesVersion: cfg.KubernetesVersion,
|
KubernetesVersion: cfg.KubernetesVersion.String(),
|
||||||
Strict: cfg.Strict,
|
Strict: cfg.Strict,
|
||||||
IgnoreMissingSchemas: cfg.IgnoreMissingSchemas,
|
IgnoreMissingSchemas: cfg.IgnoreMissingSchemas,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -15,7 +16,7 @@ type Config struct {
|
||||||
Help bool `yaml:"help" json:"help"`
|
Help bool `yaml:"help" json:"help"`
|
||||||
IgnoreFilenamePatterns []string `yaml:"ignoreFilenamePatterns" json:"ignoreFilenamePatterns"`
|
IgnoreFilenamePatterns []string `yaml:"ignoreFilenamePatterns" json:"ignoreFilenamePatterns"`
|
||||||
IgnoreMissingSchemas bool `yaml:"ignoreMissingSchemas" json:"ignoreMissingSchemas"`
|
IgnoreMissingSchemas bool `yaml:"ignoreMissingSchemas" json:"ignoreMissingSchemas"`
|
||||||
KubernetesVersion string `yaml:"kubernetesVersion" json:"kubernetesVersion"`
|
KubernetesVersion k8sVersionValue `yaml:"kubernetesVersion" json:"kubernetesVersion"`
|
||||||
NumberOfWorkers int `yaml:"numberOfWorkers" json:"numberOfWorkers"`
|
NumberOfWorkers int `yaml:"numberOfWorkers" json:"numberOfWorkers"`
|
||||||
OutputFormat string `yaml:"output" json:"output"`
|
OutputFormat string `yaml:"output" json:"output"`
|
||||||
RejectKinds map[string]struct{} `yaml:"reject" json:"reject"`
|
RejectKinds map[string]struct{} `yaml:"reject" json:"reject"`
|
||||||
|
|
@ -39,6 +40,24 @@ func (ap *arrayParam) Set(value string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type k8sVersionValue string
|
||||||
|
|
||||||
|
func (kv *k8sVersionValue) String() string {
|
||||||
|
return string(*kv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv k8sVersionValue) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(kv), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (kv *k8sVersionValue) UnmarshalText(v []byte) error {
|
||||||
|
if ok, _ := regexp.MatchString(`^(master|\d+\.\d+\.\d+)$`, string(v)); ok != true {
|
||||||
|
return fmt.Errorf("%v is not a valid version. Valid values are 'master' (default) or full version x.y.z (e.g. '1.27.2')", kv.String())
|
||||||
|
}
|
||||||
|
*kv = k8sVersionValue(v)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func splitCSV(csvStr string) map[string]struct{} {
|
func splitCSV(csvStr string) map[string]struct{} {
|
||||||
splitValues := strings.Split(csvStr, ",")
|
splitValues := strings.Split(csvStr, ",")
|
||||||
valuesMap := map[string]struct{}{}
|
valuesMap := map[string]struct{}{}
|
||||||
|
|
@ -63,7 +82,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
|
||||||
c := Config{}
|
c := Config{}
|
||||||
c.Files = []string{}
|
c.Files = []string{}
|
||||||
|
|
||||||
flags.StringVar(&c.KubernetesVersion, "kubernetes-version", "master", "version of Kubernetes to validate against, e.g.: 1.18.0")
|
flags.TextVar(&c.KubernetesVersion, "kubernetes-version", k8sVersionValue("master"), "version of Kubernetes to validate against, e.g.: 1.18.0")
|
||||||
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
|
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
|
||||||
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds or GVKs to ignore")
|
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds or GVKs to ignore")
|
||||||
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds or GVKs to reject")
|
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds or GVKs to reject")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue