Skip to content

Commit

Permalink
feat: Add pallets validation
Browse files Browse the repository at this point in the history
For now only json schema
  • Loading branch information
AtzeDeVries committed Jan 5, 2024
1 parent 1f0517d commit 08efdbe
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 30 deletions.
20 changes: 20 additions & 0 deletions pallets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package magekubernetes

import (
"errors"
"os"
)

const palletDirectory = ".pallet"

func listPalletFiles() ([]string, error) {
_, err := os.Stat(palletDirectory)

if errors.Is(err, os.ErrNotExist) {
return []string{}, nil
}
if err != nil {
return []string{}, err
}
return listFilesInDirectory(palletDirectory)
}
23 changes: 19 additions & 4 deletions targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func Validate() error {
return err
}
mg.Deps(mg.F(kubeScore, templates))
mg.Deps(mg.F(kubeConform, templates))
mg.Deps(mg.F(kubeConform, templates, "api-platform"))
mg.Deps(Pallets)
fmt.Println("Validation passed")
return nil
}
Expand All @@ -35,7 +36,17 @@ func KubeConform() error {
if err != nil {
return err
}
return kubeConform(templates)
return kubeConform(templates, "api-platform")
}

// Pallets validates the pallet files in the .pallet directory
func Pallets() error {
pallets, err := listPalletFiles()
if err != nil {
return err
}
fmt.Println("Validating Pallets")
return kubeConform(strings.Join(pallets, ","), "pallets")
}

// ArgoCDListApps show the apps related to this repository
Expand Down Expand Up @@ -76,12 +87,16 @@ func kubeScore(paths string) error {
return nil
}

func kubeConform(paths string) error {
func kubeConform(paths string, schemaSelection string) error {
if len(paths) == 0 {
fmt.Println("no templates provided")
return nil
}
cmdOptions := []string{
"-strict",
"-verbose",
"-schema-location", "default",
"-schema-location", "https://raw.githubusercontent.com/coopnorge/kubernetes-schemas/main/api-platform/{{ .ResourceKind }}{{ .KindSuffix }}.json"}
"-schema-location", "https://raw.githubusercontent.com/coopnorge/kubernetes-schemas/main/" + schemaSelection + "/{{ .ResourceKind }}{{ .KindSuffix }}.json"}
out, err := sh.Output("kubeconform", append(cmdOptions, strings.Split(paths, ",")...)...)
if err != nil {
fmt.Printf("kubeconform returned exit code: %d\n Output:\n %v Error:\n %v\n", sh.ExitStatus(err), out, err)
Expand Down
8 changes: 4 additions & 4 deletions targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ func TestOKKubeScore(t *testing.T) {

func TestFailedKubeConform(t *testing.T) {
paths := strings.Join([]string{"tests/templates/fail-schema/templates/deployment.yaml", "tests/templates/fail-schema/templates/service.yaml"}, ",")
err := kubeConform(paths)
err := kubeConform(paths, "api-platform")
if err == nil {
t.Fatalf(`kubeConform(paths) should fail but passed`)
t.Fatalf(`kubeConform(paths,"api-platform) should fail but passed`)
}
}

func TestOKKubeConform(t *testing.T) {
paths := strings.Join([]string{"tests/templates/ok/templates/configmap.yaml"}, ",")
err := kubeConform(paths)
err := kubeConform(paths, "api-platform")
if err != nil {
t.Fatalf(`kubeConform(paths) should pass but failed with error %v`, err)
t.Fatalf(`kubeConform(paths,"api-platform) should pass but failed with error %v`, err)
}
}
22 changes: 0 additions & 22 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,28 +115,6 @@ func addHelmRepos(path string) error {
return nil
}

func listFilesInDirectory(path string) ([]string, error) {
var files []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}

func tempDir() (string, error) {
dir, err := os.MkdirTemp("", "kubernetes-validation-*")
if err != nil {
return "", err
}
return dir, nil
}

// HelmChart contains all metadata of an helm chart
type HelmChart struct {
Dependencies []HelmDependency `yaml:"dependencies"`
Expand Down
28 changes: 28 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package magekubernetes

import (
"os"
"path/filepath"
)

func listFilesInDirectory(path string) ([]string, error) {
var files []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}

func tempDir() (string, error) {
dir, err := os.MkdirTemp("", "kubernetes-validation-*")
if err != nil {
return "", err
}
return dir, nil
}

0 comments on commit 08efdbe

Please sign in to comment.