Skip to content

Commit

Permalink
Fix issue reading Kyverno policy file (#3867)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <[email protected]>
  • Loading branch information
tegioz authored Jun 10, 2024
1 parent a37e419 commit b190e9d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions cmd/ah/testdata/lint/test18/output.golden
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ Package lint SUCCEEDED!

Package lint FAILED. 1 error(s) occurred:

* error preparing package pkg1 version 2.0.0 data: error reading kyverno policy file: lstat testdata/lint/test18/pkgs/pkg1/2.0.0/2.0.0.yaml: no such file or directory
* error preparing package pkg1 version 2.0.0 data: error reading kyverno policy file: lstat testdata/lint/test18/pkgs/pkg1/2.0.0/pkg1.yaml: no such file or directory

------------------------------------------------------------------------------------------------------------------------
✗ name: ? version: ? (testdata/lint/test18/pkgs/pkg2/1.0.0)
------------------------------------------------------------------------------------------------------------------------

Package lint FAILED. 1 error(s) occurred:

* error preparing package pkg2 version 1.0.0 data: error reading kyverno policy file: lstat testdata/lint/test18/pkgs/pkg2/1.0.0/1.0.0.yaml: no such file or directory
* error preparing package pkg2 version 1.0.0 data: error reading kyverno policy file: lstat testdata/lint/test18/pkgs/pkg2/1.0.0/pkg2.yaml: no such file or directory

------------------------------------------------------------------------------------------------------------------------

Expand Down
12 changes: 8 additions & 4 deletions docs/kyverno_policies_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@ path/to/packages
├── package1
│   ├── 1.0.0
│   │   ├── README.md
│   │   └── artifacthub-pkg.yml
│   │   ├── artifacthub-pkg.yml
│   │   └── package1.yaml # (policy file)
│   └── 2.0.0
│      ├── README.md
│   └── artifacthub-pkg.yml
│      ├── artifacthub-pkg.yml
│      └── package1.yaml # (policy file)
└── package2
└── 1.0.0
      ├── README.md
└── artifacthub-pkg.yml
     ├── artifacthub-pkg.yml
     └── package2.yaml # (policy file)
```

This structure is flexible, and in some cases it can be greatly simplified. Nested directories are also supported. In the case of a single package with a single version available at a time (the publisher doesn't want to make previous ones available, for example), the structure could look like this:
Expand All @@ -39,7 +42,8 @@ path/to/packages
├── artifacthub-repo.yml
└── package1
   ├── README.md
└── artifacthub-pkg.yml
  ├── artifacthub-pkg.yml
  └── package1.yaml # (policy file)
```

In the previous case, even the `package1` directory could be omitted. The reason is that both packages names and versions are read from the `artifacthub-pkg.yml` metadata file, so directories names are not used at all.
Expand Down
7 changes: 5 additions & 2 deletions internal/tracker/source/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func PreparePackage(r *hub.Repository, md *hub.PackageMetadata, pkgPath string)
case hub.KubeArmor:
kindData, err = prepareKubeArmorData(pkgPath, ignorer)
case hub.Kyverno:
kindData, err = prepareKyvernoData(pkgPath)
kindData, err = prepareKyvernoData(pkgPath, p.Name)
case hub.Meshery:
kindData, err = prepareMesheryData(pkgPath)
case hub.OPA:
Expand Down Expand Up @@ -359,9 +359,12 @@ func prepareKubeArmorData(pkgPath string, ignorer ignore.IgnoreParser) (map[stri

// prepareKyernoData reads and formats Kyverno specific data available in the
// path provided, returning the resulting data structure.
func prepareKyvernoData(pkgPath string) (map[string]interface{}, error) {
func prepareKyvernoData(pkgPath, pkgName string) (map[string]interface{}, error) {
// Read policy file
policyPath := path.Join(pkgPath, path.Base(pkgPath)+".yaml")
if _, err := os.Stat(policyPath); os.IsNotExist(err) {
policyPath = path.Join(pkgPath, pkgName+".yaml")
}
policy, err := util.ReadRegularFile(policyPath)
if err != nil {
return nil, fmt.Errorf("error reading kyverno policy file: %w", err)
Expand Down

0 comments on commit b190e9d

Please sign in to comment.