Skip to content

Commit a4d74ce

Browse files
authored
Fail early on incorrect version of k8s (#254)
* Fail early on incorrect version of k8s * fix tests
1 parent 808e6d4 commit a4d74ce

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ RELEASE_VERSION ?= latest
55
.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
66

77
local-test:
8-
go test -race ./...
8+
go test -race ./... -count=1
99

1010
local-build:
1111
git config --global --add safe.directory $$PWD

acceptance.bats

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ resetCacheFolder() {
180180
[ "$status" -eq 1 ]
181181
}
182182

183+
@test "Fail early when passing a non valid -kubernetes-version" {
184+
run bin/kubeconform -kubernetes-version 1.25 fixtures/valid.yaml
185+
[ "${lines[0]}" == '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")' ]
186+
[[ "${lines[1]}" == "Usage:"* ]]
187+
[ "$status" -eq 1 ]
188+
}
189+
183190
@test "Pass with a valid input when validating against openshift manifests" {
184191
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
185192
[ "$status" -eq 0 ]

cmd/kubeconform/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func kubeconform(cfg config.Config) int {
8484
SkipTLS: cfg.SkipTLS,
8585
SkipKinds: cfg.SkipKinds,
8686
RejectKinds: cfg.RejectKinds,
87-
KubernetesVersion: cfg.KubernetesVersion,
87+
KubernetesVersion: cfg.KubernetesVersion.String(),
8888
Strict: cfg.Strict,
8989
IgnoreMissingSchemas: cfg.IgnoreMissingSchemas,
9090
})

pkg/config/config.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"flag"
66
"fmt"
7+
"regexp"
78
"strings"
89
)
910

@@ -15,7 +16,7 @@ type Config struct {
1516
Help bool `yaml:"help" json:"help"`
1617
IgnoreFilenamePatterns []string `yaml:"ignoreFilenamePatterns" json:"ignoreFilenamePatterns"`
1718
IgnoreMissingSchemas bool `yaml:"ignoreMissingSchemas" json:"ignoreMissingSchemas"`
18-
KubernetesVersion string `yaml:"kubernetesVersion" json:"kubernetesVersion"`
19+
KubernetesVersion k8sVersionValue `yaml:"kubernetesVersion" json:"kubernetesVersion"`
1920
NumberOfWorkers int `yaml:"numberOfWorkers" json:"numberOfWorkers"`
2021
OutputFormat string `yaml:"output" json:"output"`
2122
RejectKinds map[string]struct{} `yaml:"reject" json:"reject"`
@@ -39,6 +40,24 @@ func (ap *arrayParam) Set(value string) error {
3940
return nil
4041
}
4142

43+
type k8sVersionValue string
44+
45+
func (kv *k8sVersionValue) String() string {
46+
return string(*kv)
47+
}
48+
49+
func (kv k8sVersionValue) MarshalText() ([]byte, error) {
50+
return []byte(kv), nil
51+
}
52+
53+
func (kv *k8sVersionValue) UnmarshalText(v []byte) error {
54+
if ok, _ := regexp.MatchString(`^(master|\d+\.\d+\.\d+)$`, string(v)); ok != true {
55+
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\")", string(v))
56+
}
57+
*kv = k8sVersionValue(v)
58+
return nil
59+
}
60+
4261
func splitCSV(csvStr string) map[string]struct{} {
4362
splitValues := strings.Split(csvStr, ",")
4463
valuesMap := map[string]struct{}{}
@@ -63,7 +82,7 @@ func FromFlags(progName string, args []string) (Config, string, error) {
6382
c := Config{}
6483
c.Files = []string{}
6584

66-
flags.StringVar(&c.KubernetesVersion, "kubernetes-version", "master", "version of Kubernetes to validate against, e.g.: 1.18.0")
85+
flags.TextVar(&c.KubernetesVersion, "kubernetes-version", k8sVersionValue("master"), "version of Kubernetes to validate against, e.g.: 1.18.0")
6786
flags.Var(&schemaLocationsParam, "schema-location", "override schemas location search path (can be specified multiple times)")
6887
flags.StringVar(&skipKindsCSV, "skip", "", "comma-separated list of kinds or GVKs to ignore")
6988
flags.StringVar(&rejectKindsCSV, "reject", "", "comma-separated list of kinds or GVKs to reject")

0 commit comments

Comments
 (0)