Skip to content

Commit b10927a

Browse files
committed
Update Readme, add 'default' value for -schema-location parameter
1 parent 171d894 commit b10927a

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

Readme.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
[![PkgGoDev](https://pkg.go.dev/badge/github.com/yannh/kubeconform/pkg/validator)](https://pkg.go.dev/github.com/yannh/kubeconform/pkg/validator)
66

77
Kubeconform is a Kubernetes manifests validation tool. Build it into your CI to validate your Kubernetes
8-
configuration using the schemas from the registry maintained by the
9-
[kubernetes-json-schema](https://github.com/instrumenta/kubernetes-json-schema) project!
8+
configuration!
109

1110
It is inspired by, contains code from and is designed to stay close to
1211
[Kubeval](https://github.com/instrumenta/kubeval), but with the following improvements:
1312
* **high performance**: will validate & download manifests over multiple routines, caching
1413
downloaded files in memory
1514
* configurable list of **remote, or local schemas locations**, enabling validating Kubernetes
16-
custom resources (CRDs) and offline validation capabilities.
15+
custom resources (CRDs) and offline validation capabilities
16+
* uses by default a [self-updating fork](https://github.com/yannh/kubernetes-json-schema) of the schemas registry maintained
17+
by the [kubernetes-json-schema](https://github.com/instrumenta/kubernetes-json-schema) project - which guarantees
18+
up-to-date **schemas for all recent versions of Kubernetes**.
1719

1820
### A small overview of Kubernetes manifest validation
1921

@@ -132,29 +134,29 @@ Summary: 65 resources found in 34 files - Valid: 55, Invalid: 2, Errors: 8 Skipp
132134

133135
### Overriding schemas location - CRD and Openshift support
134136

135-
When the `-schema-location` parameter is not used, kubeconform will default to downloading schemas from
136-
`https://kubernetesjsonschema.dev`. Kubeconform however supports passing one, or multiple, schemas
137-
locations - HTTP URLs, or local filesystem paths, in which case it will lookup for schema definitions
137+
When the `-schema-location` parameter is not used, or set to "default", kubeconform will default to downloading
138+
schemas from `https://github.com/yannh/kubernetes-json-schema`. Kubeconform however supports passing one, or multiple,
139+
schemas locations - HTTP(s) URLs, or local filesystem paths, in which case it will lookup for schema definitions
138140
in each of them, in order, stopping as soon as a matching file is found.
139141

140142
* If the -schema-location value does not end with '.json', Kubeconform will assume filenames / a file
141-
structure identical to that of kubernetesjsonschema.dev
143+
structure identical to that of kubernetesjsonschema.dev or github.com/yannh/kubernetes-json-schema.
142144
* if the -schema-location value ends with '.json' - Kubeconform assumes the value is a Go templated
143145
string that indicates how to search for JSON schemas.
144-
145-
All 3 following command lines are equivalent:
146+
* the -schema-location value of "default" is an alias for https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json.
147+
Both following command lines are equivalent:
146148
```
147149
$ ./bin/kubeconform fixtures/valid.yaml
148-
$ ./bin/kubeconform -schema-location https://kubernetesjsonschema.dev fixtures/valid.yaml
149-
$ ./bin/kubeconform -schema-location 'https://kubernetesjsonschema.dev/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/valid.yaml
150+
$ ./bin/kubeconform -schema-location default fixtures/valid.yaml
151+
$ ./bin/kubeconform -schema-location 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/valid.yaml
150152
```
151153

152154
To support validating CRDs, we need to convert OpenAPI files to JSON schema, storing the JSON schemas
153155
in a local folder - for example schemas. Then we specify this folder as an additional registry to lookup:
154156

155157
```
156158
# If the resource Kind is not found in kubernetesjsonschema.dev, also lookup in the schemas/ folder for a matching file
157-
$ ./bin/kubeconform -registry https://kubernetesjsonschema.dev -schema-location 'schemas/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/custom-resource.yaml
159+
$ ./bin/kubeconform -schema-location default -schema-location 'schemas/{{ .ResourceKind }}{{ .KindSuffix }}.json' fixtures/custom-resource.yaml
158160
```
159161

160162
You can validate Openshift manifests using a custom schema location. Set the OpenShift version to validate

acceptance.bats

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ resetCacheFolder() {
127127
}
128128

129129
@test "Pass when using a valid, preset -schema-location" {
130-
run bin/kubeconform -schema-location https://kubernetesjsonschema.dev fixtures/valid.yaml
130+
run bin/kubeconform -schema-location default fixtures/valid.yaml
131131
[ "$status" -eq 0 ]
132132
}
133133

pkg/registry/registry.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ func schemaPath(tpl, resourceKind, resourceAPIVersion, k8sVersion string, strict
8080
}
8181

8282
func New(schemaLocation string, cache string, strict bool, skipTLS bool) (Registry, error) {
83-
if !strings.HasSuffix(schemaLocation, "json") { // If we dont specify a full templated path, we assume the paths of kubernetesjsonschema.dev
83+
if schemaLocation == "default" {
84+
schemaLocation = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json"
85+
} else if !strings.HasSuffix(schemaLocation, "json") { // If we dont specify a full templated path, we assume the paths of our fork of kubernetes-json-schema
8486
schemaLocation += "/{{ .NormalizedKubernetesVersion }}-standalone{{ .StrictSuffix }}/{{ .ResourceKind }}{{ .KindSuffix }}.json"
8587
}
8688

0 commit comments

Comments
 (0)