diff --git a/pallets.go b/pallets.go new file mode 100644 index 0000000..a166ba9 --- /dev/null +++ b/pallets.go @@ -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) +} diff --git a/targets.go b/targets.go index 3794795..275a2a5 100644 --- a/targets.go +++ b/targets.go @@ -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 } @@ -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 @@ -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) diff --git a/template.go b/template.go index 4260401..1f2f390 100644 --- a/template.go +++ b/template.go @@ -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"` diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..2305e56 --- /dev/null +++ b/utils.go @@ -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 +}