Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
refactor(cmd): improve error message when resource file is not refere…
Browse files Browse the repository at this point in the history
…nced (#17)

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon authored May 30, 2024
1 parent 9f4cf32 commit 52d61fd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/validation/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" {
Expand Down
9 changes: 4 additions & 5 deletions pkg/validation/components.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package validation

import (
"fmt"
"io/fs"
"path/filepath"

Expand All @@ -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
Expand All @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/validation/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/validation/kustomize_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions pkg/validation/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 52d61fd

Please sign in to comment.