mirror of
https://github.com/yannh/kubeconform.git
synced 2026-04-12 07:44:17 +00:00
first commit
This commit is contained in:
commit
fe33d14dc4
8 changed files with 173 additions and 0 deletions
71
pkg/registry/registry.go
Normal file
71
pkg/registry/registry.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
package registry
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Manifest struct {
|
||||
Kind, Version string
|
||||
}
|
||||
|
||||
type Registry interface {
|
||||
DownloadSchema(kind, apiVersion string) error
|
||||
}
|
||||
|
||||
type KubernetesRegistry struct {
|
||||
baseURL string
|
||||
strict bool
|
||||
}
|
||||
|
||||
func NewKubernetesRegistry() *KubernetesRegistry {
|
||||
return &KubernetesRegistry{
|
||||
baseURL: "https://kubernetesjsonschema.dev",
|
||||
strict: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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) (string, error) {
|
||||
url := r.schemaURL(resourceKind, resourceAPIVersion, k8sVersion)
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed downloading schema at %s: %s", url, err)
|
||||
}
|
||||
|
||||
fmt.Printf("downloaded %s\n", url)
|
||||
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
30
pkg/resource/main.go
Normal file
30
pkg/resource/main.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
type Resource struct {
|
||||
Kind, Version, Namespace string
|
||||
}
|
||||
|
||||
// TODO: Support multi-resources yaml files
|
||||
func Read(r io.Reader) (Resource, error) {
|
||||
s, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return Resource{}, err
|
||||
}
|
||||
|
||||
resource := struct {
|
||||
APIVersion string `yaml:"apiVersion"`
|
||||
Kind string `yaml:"kind"`
|
||||
Metadata struct {
|
||||
Namespace string `yaml:"Namespace"`
|
||||
} `yaml:"Metadata"`
|
||||
}{}
|
||||
err = yaml.Unmarshal(s, &resource)
|
||||
|
||||
return Resource{Kind: resource.Kind, Version: resource.APIVersion, Namespace: resource.Metadata.Namespace}, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue