[Enhancement] jsonschema: use Go1.16's go:embed to embed the json schema file (dedup) (#529)

pull/532/head
Thorsten Klein 4 years ago committed by GitHub
parent dfa1472155
commit 9cc7915c55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      pkg/config/jsonschema_test.go
  2. 237
      pkg/config/v1alpha2/schema.go
  3. 7
      pkg/config/v1alpha2/types.go

@ -22,30 +22,11 @@ THE SOFTWARE.
package config
import (
"bytes"
"io/ioutil"
"testing"
"github.com/rancher/k3d/v4/pkg/config/v1alpha2"
)
// TestEnsureHardcodedSchemaMatchesFile ensures that the JSONSchema hardcoded in the config package matches the corresponding file
/*
* TODO: as soon as we move to Go 1.16, the file will be embedded using //go:embed and we can drop this test
*/
func TestEnsureHardcodedSchemaMatchesFile(t *testing.T) {
schemaFilePath := "./v1alpha2/schema.json"
schemaContents, err := ioutil.ReadFile(schemaFilePath)
if err != nil {
t.Fatalf("Failed to read schema file %s: %+v", schemaFilePath, err)
}
if bytes.Compare([]byte(v1alpha2.JSONSchema), schemaContents) != 0 {
t.Errorf("Schema file %s does not match hardcoded schema!", schemaFilePath)
}
}
func TestValidateSchema(t *testing.T) {
cfgPath := "./test_assets/config_test_simple.yaml"

@ -1,237 +0,0 @@
/*
Copyright © 2020 The k3d Author(s)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package v1alpha2
// JSONSchema describes the schema used to validate config files
/* TODO: JSONSchema should be an embedded file. We're moving to the //go:embed tag as of Go 1.16
* ... and only hardcode it here to avoid using 3rd party tools like go-bindata or packr right now for the time being
*/
var JSONSchema = `{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "SimpleConfig",
"type": "object",
"required": [
"apiVersion",
"kind"
],
"properties": {
"apiVersion": {
"type": "string",
"enum": [
"k3d.io/v1alpha2"
]
},
"kind": {
"type": "string",
"enum": [
"Simple"
]
},
"name": {
"description": "Name of the cluster (must be a valid hostname and will be prefixed with 'k3d-'). Example: 'mycluster'.",
"type": "string",
"format": "hostname"
},
"servers": {
"type": "number",
"minimum": 1
},
"agents": {
"type": "number",
"minimum": 0
},
"kubeAPI": {
"type": "object",
"properties": {
"host": {
"type": "string",
"format": "hostname"
},
"hostIP": {
"type": "string",
"format": "ipv4"
},
"hostPort": {
"type":"string"
}
},
"additionalProperties": false
},
"image": {
"type": "string"
},
"network": {
"type": "string"
},
"token": {
"type": "string"
},
"volumes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"volume": {
"type": "string"
},
"nodeFilters": {
"$ref": "#/definitions/nodeFilters"
}
},
"additionalProperties": false
}
},
"ports": {
"type": "array",
"items": {
"type": "object",
"properties": {
"port": {
"type": "string"
},
"nodeFilters": {
"$ref": "#/definitions/nodeFilters"
}
},
"additionalProperties": false
}
},
"labels": {
"type": "array",
"items": {
"type": "object",
"properties": {
"label": {
"type": "string"
},
"nodeFilters": {
"$ref": "#/definitions/nodeFilters"
}
},
"additionalProperties": false
}
},
"options": {
"type": "object",
"properties": {
"k3d": {
"type": "object",
"properties": {
"wait": {
"type": "boolean",
"default": true
},
"timeout": {
"type": "string"
},
"disableLoadbalancer": {
"type": "boolean",
"default": false
},
"disableImageVolume": {
"type": "boolean",
"default": false
},
"disableRollback": {
"type": "boolean",
"default": false
},
"disableHostIPInjection": {
"type": "boolean",
"default": false
}
},
"additionalProperties": false
},
"k3s": {
"type": "object",
"properties": {
"extraServerArgs": {
"type": "array",
"items": {
"type": "string"
}
},
"extraAgentArgs": {
"type": "array",
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
"kubeconfig": {
"type": "object",
"properties": {
"updateDefaultKubeconfig": {
"type": "boolean",
"default": true
},
"switchCurrentContext": {
"type": "boolean",
"default": true
}
},
"additionalProperties": false
},
"runtime": {
"type": "object",
"properties": {
"gpuRequest": {
"type": "string"
}
}
}
},
"additionalProperties": false
},
"env": {
"type": "array",
"items": {
"type": "object",
"properties": {
"envVar": {
"type": "string"
},
"nodeFilters": {
"$ref": "#/definitions/nodeFilters"
}
},
"additionalProperties": false
}
},
"registries": {
"type": "object"
}
},
"additionalProperties": false,
"definitions": {
"nodeFilters": {
"type": "array",
"items": {
"type": "string"
}
}
}
}`

@ -23,6 +23,7 @@ THE SOFTWARE.
package v1alpha2
import (
_ "embed"
"fmt"
"time"
@ -30,6 +31,10 @@ import (
"github.com/rancher/k3d/v4/version"
)
// JSONSchema describes the schema used to validate config files
//go:embed schema.json
var JSONSchema string
// DefaultConfigTpl for printing
const DefaultConfigTpl = `---
apiVersion: k3d.io/v1alpha2
@ -47,7 +52,7 @@ var DefaultConfig = fmt.Sprintf(
fmt.Sprintf("%s:%s", k3d.DefaultK3sImageRepo, version.GetK3sVersion(false)),
)
// TypeMeta, basically copied from https://github.com/kubernetes/apimachinery/blob/a3b564b22db316a41e94fdcffcf9995424fe924c/pkg/apis/meta/v1/types.go#L36-L56
// TypeMeta is basically copied from https://github.com/kubernetes/apimachinery/blob/a3b564b22db316a41e94fdcffcf9995424fe924c/pkg/apis/meta/v1/types.go#L36-L56
type TypeMeta struct {
Kind string `mapstructure:"kind,omitempty" yaml:"kind,omitempty" json:"kind,omitempty"`
APIVersion string `mapstructure:"apiVersion,omitempty" yaml:"apiVersion,omitempty" json:"apiVersion,omitempty"`

Loading…
Cancel
Save