first commit

This commit is contained in:
Yann Hamon 2020-05-30 02:44:13 +02:00
commit fe33d14dc4
8 changed files with 173 additions and 0 deletions

4
Makefile Normal file
View file

@ -0,0 +1,4 @@
#!/usr/bin/make -f
all:
go build

0
Readme.md Normal file
View file

19
fixtures/valid_1.yaml Normal file
View file

@ -0,0 +1,19 @@
apiVersion: v1
kind: ReplicationController
metadata:
name: "bob"
spec:
replicas: 2
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

5
go.mod Normal file
View file

@ -0,0 +1,5 @@
module github.com/yannh/kubeconform
go 1.14
require gopkg.in/yaml.v2 v2.3.0

3
go.sum Normal file
View file

@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

41
main.go Normal file
View file

@ -0,0 +1,41 @@
package main
import (
"log"
"os"
"github.com/yannh/kubeconform/pkg/registry"
"github.com/yannh/kubeconform/pkg/resource"
)
func realMain() int {
filename := "fixtures/valid_1.yaml"
f, err := os.Open(filename)
if err != nil {
log.Fatalf("failed opening %s", filename)
return 1
}
defer f.Close()
res, err := resource.Read(f)
if err != nil {
log.Printf("failed parsing %s", filename)
return 1
}
r := registry.NewKubernetesRegistry()
schema, err := r.DownloadSchema(res.Kind, res.Version, "1.18.0")
if err != nil {
log.Printf("error downloading Schema: %s")
return 1
}
log.Printf("downloaded schema successfully: %s", schema)
return 0
}
func main() {
os.Exit(realMain())
}

71
pkg/registry/registry.go Normal file
View 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
View 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
}