diff --git a/README.md b/README.md index ba29751..0b94008 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@ For example: ``` $ check-argocd --base-dir=$(pwd) --apps=apps-of-apps,apps --components=components -INFO 👀 checking Applications and ApplicationSets path=/path/to/apps-of-apps -INFO 👀 checking Applications and ApplicationSets path=/path/to/apps -INFO 👀 checking Components path=/path/to/components +INFO 🏁 Checking Argo CD configuration base-dir=/path/to/repository +INFO 👀 checking Applications and ApplicationSets path=apps-of-apps +INFO 👀 checking Applications and ApplicationSets path=apps +INFO 👀 checking Components path=components ``` ## Building diff --git a/pkg/validation/apps.go b/pkg/validation/apps.go index 21eadc4..ddef63f 100644 --- a/pkg/validation/apps.go +++ b/pkg/validation/apps.go @@ -17,7 +17,7 @@ func CheckApplications(logger Logger, afs afero.Afero, baseDir string, apps ...s for _, path := range apps { p := filepath.Join(baseDir, path) - logger.Info("👀 checking Applications and ApplicationSets", "path", p) + logger.Info("👀 checking Applications and ApplicationSets", "path", path) fsys, err := NewInMemoryFS(logger, afs, p) if err != nil { return err @@ -29,8 +29,8 @@ func CheckApplications(logger Logger, afs afero.Afero, baseDir string, apps ...s } if info.IsDir() { logger.Debug("👀 checking contents", "path", path) - if kp, found := lookupKustomizationFile(logger, afs, path); found { - if err := checkKustomizeResources(logger, afs, kp); err != nil { + if kpath, found := lookupKustomizationFile(logger, afs, path); found { + if err := checkKustomizeResources(logger, afs, baseDir, kpath); err != nil { return err } if info.Name() != "base" { diff --git a/pkg/validation/components.go b/pkg/validation/components.go index 1513c0e..37068f4 100644 --- a/pkg/validation/components.go +++ b/pkg/validation/components.go @@ -1,7 +1,6 @@ package validation import ( - "fmt" "io/fs" "path/filepath" @@ -14,7 +13,7 @@ func CheckComponents(logger Logger, afs afero.Afero, baseDir string, components for _, path := range components { p := filepath.Join(baseDir, path) - logger.Info("👀 checking Components", "path", p) + logger.Info("👀 checking Components", "path", path) fsys, err := NewInMemoryFS(logger, afs, p) if err != nil { return err @@ -30,13 +29,13 @@ func CheckComponents(logger Logger, afs afero.Afero, baseDir string, components } // look for a Kustomization file in the directory if kp, found := lookupKustomizationFile(logger, afs, path); found { - if err := checkKustomizeResources(logger, afs, kp); err != nil { - return fmt.Errorf("invalid resources at %s: %w", path, err) + if err := checkKustomizeResources(logger, afs, baseDir, kp); err != nil { + return err } if d.Name() != "base" { logger.Debug("checking Kustomization build ", "path", path) if err := checkBuild(logger, fsys, path); err != nil { - return fmt.Errorf("invalid resources at %s: %w", path, err) + return err } } } diff --git a/pkg/validation/components_test.go b/pkg/validation/components_test.go index a2baa53..2f7b84c 100644 --- a/pkg/validation/components_test.go +++ b/pkg/validation/components_test.go @@ -330,7 +330,7 @@ data: err = validation.CheckComponents(logger, afs, "/path/to", "components") // then - require.Error(t, err, "resource is not referenced: /path/to/components/configmap2.yaml") + require.EqualError(t, err, "resource is not referenced in components/kustomization.yaml: configmap2.yaml") assert.Empty(t, logger.Errors()) assert.Empty(t, logger.Warnings()) }) diff --git a/pkg/validation/kustomize_build.go b/pkg/validation/kustomize_build.go index 7913683..58dbc38 100644 --- a/pkg/validation/kustomize_build.go +++ b/pkg/validation/kustomize_build.go @@ -9,9 +9,9 @@ import ( kfsys "sigs.k8s.io/kustomize/kyaml/filesys" ) -func lookupKustomizationFile(logger Logger, afs afero.Afero, basedir string) (string, bool) { +func lookupKustomizationFile(logger Logger, afs afero.Afero, componentPath string) (string, bool) { for _, k := range []string{"kustomization.yaml", "kustomization.yml", "Kustomization"} { - p := filepath.Join(basedir, k) + p := filepath.Join(componentPath, k) if _, err := afs.Open(p); err == nil { logger.Debug("found Kustomization file", "path", p) return p, true diff --git a/pkg/validation/resources.go b/pkg/validation/resources.go index 7666fae..df2a93f 100644 --- a/pkg/validation/resources.go +++ b/pkg/validation/resources.go @@ -12,9 +12,9 @@ import ( // Compares the entries of `resources` in the Kustomize file with the contents in the current directory to see if // any local file is missing (not referenced as a resource). // Files starting with an underscore character (`_`) are ignored -func checkKustomizeResources(logger Logger, afs afero.Afero, path string) error { - logger.Debug("checking kustomization resource", "path", path) - data, err := afs.ReadFile(path) +func checkKustomizeResources(logger Logger, afs afero.Afero, basedir, kpath string) error { + logger.Debug("checking kustomization resource", "path", kpath) + data, err := afs.ReadFile(kpath) if err != nil { return err } @@ -24,8 +24,8 @@ func checkKustomizeResources(logger Logger, afs afero.Afero, path string) error } // list resources - logger.Debug("checking kustomization resources", "dir", filepath.Dir(path)) - entries, err := afs.ReadDir(filepath.Dir(path)) + logger.Debug("checking kustomization resources", "dir", filepath.Dir(kpath)) + entries, err := afs.ReadDir(filepath.Dir(kpath)) if err != nil { return err } @@ -34,14 +34,14 @@ entries: switch { case e.IsDir(): fallthrough - case e.Name() == filepath.Base(path): + case e.Name() == filepath.Base(kpath): fallthrough case strings.HasPrefix(e.Name(), "_"): fallthrough case !(filepath.Ext(e.Name()) == ".yaml" || filepath.Ext(e.Name()) == ".yml"): fallthrough case filepath.Base(e.Name()) == "kustomization.yaml": - logger.Debug("ignoring file", "path", path) + logger.Debug("ignoring file", "path", kpath) continue entries } for _, r := range kobj.Resources { @@ -86,7 +86,9 @@ entries: continue entries } } - return fmt.Errorf("resource is not referenced: %s", filepath.Join(path, e.Name())) + rkpath, _ := filepath.Rel(basedir, kpath) + + return fmt.Errorf("resource is not referenced in %s: %s", rkpath, e.Name()) } return nil }