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 67753ec
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 26 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 (
"os"
"errors"
)

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)
}
22 changes: 18 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,16 @@ func KubeConform() error {
if err != nil {
return err
}
return kubeConform(templates)
return kubeConform(templates,"api-platform")
}

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 +86,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
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
29 changes: 29 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 67753ec

Please sign in to comment.