mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-15 00:44:45 +00:00
add support for local schemas
This commit is contained in:
parent
f82baa834c
commit
5f3504724f
3 changed files with 16 additions and 67 deletions
|
|
@ -1,71 +1,9 @@
|
|||
package registry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Manifest struct {
|
||||
Kind, Version string
|
||||
}
|
||||
|
||||
type Registry interface {
|
||||
DownloadSchema(kind, apiVersion string) error
|
||||
DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error)
|
||||
}
|
||||
|
||||
type KubernetesRegistry struct {
|
||||
baseURL string
|
||||
strict bool
|
||||
}
|
||||
|
||||
func NewKubernetesRegistry(strict bool) *KubernetesRegistry {
|
||||
return &KubernetesRegistry{
|
||||
baseURL: "https://kubernetesjsonschema.dev",
|
||||
strict: strict,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (r KubernetesRegistry) schemaURL(resourceKind, resourceAPIVersion, k8sVersion string) string {
|
||||
normalisedVersion := k8sVersion
|
||||
if normalisedVersion != "master" {
|
||||
normalisedVersion = "v" + normalisedVersion
|
||||
}
|
||||
|
||||
strictSuffix := ""
|
||||
if r.strict {
|
||||
strictSuffix = "-strict"
|
||||
}
|
||||
|
||||
groupParts := strings.Split(resourceAPIVersion, "/")
|
||||
versionParts := strings.Split(groupParts[0], ".")
|
||||
|
||||
kindSuffix := "-" + strings.ToLower(versionParts[0])
|
||||
if len(groupParts) > 1 {
|
||||
kindSuffix += "-" + strings.ToLower(groupParts[1])
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s/%s-standalone%s/%s%s.json", r.baseURL, normalisedVersion, strictSuffix, strings.ToLower(resourceKind), kindSuffix)
|
||||
}
|
||||
|
||||
func (r KubernetesRegistry) DownloadSchema(resourceKind, resourceAPIVersion, k8sVersion string) ([]byte, error) {
|
||||
url := r.schemaURL(resourceKind, resourceAPIVersion, k8sVersion)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return []byte{}, fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return []byte{}, fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
}
|
||||
|
||||
fmt.Printf("downloaded %s\n", url)
|
||||
|
||||
return body, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue