8000 fix: use the generic policy in the CLI by MariamFahmy98 · Pull Request #13035 · kyverno/kyverno · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: use the generic policy in the CLI #13035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cmd/cli/kubectl-kyverno/commands/apply/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,15 @@ func (c *ApplyCommandConfig) applyImageValidatingPolicies(
return responses, nil
}

func (c *ApplyCommandConfig) loadResources(out io.Writer, paths []string, policies []kyvernov1.PolicyInterface, vap []admissionregistrationv1.ValidatingAdmissionPolicy, dClient dclient.Interface) ([]*unstructured.Unstructured, []*unstructured.Unstructured, error) {
resources, err := common.GetResourceAccordingToResourcePath(out, nil, paths, c.Cluster, policies, vap, dClient, c.Namespace, c.PolicyReport, c.ClusterWideResources, "")
func (c *ApplyCommandConfig) loadResources(out io.Writer, paths []string, policies []kyvernov1.PolicyInterface, vaps []admissionregistrationv1.ValidatingAdmissionPolicy, dClient dclient.Interface) ([]*unstructured.Unstructured, []*unstructured.Unstructured, error) {
genericPolicies := make([]engineapi.GenericPolicy, 0, len(policies)+len(vaps))
for _, pol := range policies {
genericPolicies = append(genericPolicies, engineapi.NewKyvernoPolicy(pol))
}
for _, pol := range vaps {
genericPolicies = append(genericPolicies, engineapi.NewValidatingAdmissionPolicy(&pol))
}
resources, err := common.GetResourceAccordingToResourcePath(out, nil, paths, c.Cluster, genericPolicies, dClient, c.Namespace, c.PolicyReport, c.ClusterWideResources, "")
if err != nil {
return resources, nil, fmt.Errorf("failed to load resources (%w)", err)
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/cli/kubectl-kyverno/commands/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,17 @@
if err != nil {
return nil, fmt.Errorf("error: failed to load policies (%s)", err)
}
genericPolicies := make([]engineapi.GenericPolicy, 0, len(results.Policies)+len(results.VAPs))
for _, pol := range results.Policies {
genericPolicies = append(genericPolicies, engineapi.NewKyvernoPolicy(pol))
}

Check warning on line 91 in cmd/cli/kubectl-kyverno/commands/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/commands/test/test.go#L90-L91

Added lines #L90 - L91 were not covered by tests
for _, pol := range results.VAPs {
genericPolicies = append(genericPolicies, engineapi.NewValidatingAdmissionPolicy(&pol))
}

Check warning on line 94 in cmd/cli/kubectl-kyverno/commands/test/test.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/commands/test/test.go#L93-L94

Added lines #L93 - L94 were not covered by tests
// resources
fmt.Fprintln(out, " Loading resources", "...")
resourceFullPath := path.GetFullPaths(testCase.Test.Resources, testDir, isGit)
resources, err := common.GetResourceAccordingToResourcePath(out, testCase.Fs, resourceFullPath, false, results.Policies, results.VAPs, dClient, "", false, false, testDir)
resources, err := common.GetResourceAccordingToResourcePath(out, testCase.Fs, resourceFullPath, false, genericPolicies, dClient, "", false, false, testDir)
if err != nil {
return nil, fmt.Errorf("error: failed to load resources (%s)", err)
}
Expand All @@ -110,7 +117,7 @@
}
}
targetResourcesPath := path.GetFullPaths(testCase.Test.TargetResources, testDir, isGit)
targetResources, err := common.GetResourceAccordingToResourcePath(out, testCase.Fs, targetResourcesPath, false, results.Policies, results.VAPs, dClient, "", false, false, testDir)
targetResources, err := common.GetResourceAccordingToResourcePath(out, testCase.Fs, targetResourcesPath, false, genericPolicies, dClient, "", false, false, testDir)
if err != nil {
return nil, fmt.Errorf("error: failed to load target resources (%s)", err)
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/cli/kubectl-kyverno/utils/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/source"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -29,8 +29,7 @@
fs billy.Filesystem,
resourcePaths []string,
cluster bool,
policies []kyvernov1.PolicyInterface,
validatingAdmissionPolicies []admissionregistrationv1.ValidatingAdmissionPolicy,
policies []engineapi.GenericPolicy,
dClient dclient.Interface,
namespace string,
policyReport bool,
Expand Down Expand Up @@ -79,15 +78,15 @@
}
}
if clusterWideResources {
resources, err = GetResources(out, policies, validatingAdmissionPolicies, resourcePaths, dClient, cluster, "", policyReport, clusterWideResources)
resources, err = GetResources(out, policies, resourcePaths, dClient, cluster, "", policyReport, clusterWideResources)

Check warning on line 81 in cmd/cli/kubectl-kyverno/utils/common/common.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/common.go#L81

Added line #L81 was not covered by tests
if err != nil {
return resources, err
}
if namespace == "" {
return resources, nil
}
}
namespaceResources, err := GetResources(out, policies, validatingAdmissionPolicies, resourcePaths, dClient, cluster, namespace, policyReport, false)
namespaceResources, err := GetResources(out, policies, resourcePaths, dClient, cluster, namespace, policyReport, false)

Check warning on line 89 in cmd/cli/kubectl-kyverno/utils/common/common.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/common.go#L89

Added line #L89 was not covered by tests
if err != nil {
return resources, err
}
Expand Down
75 changes: 52 additions & 23 deletions cmd/cli/kubectl-kyverno/utils/common/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/log"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/resource"
"github.com/kyverno/kyverno/pkg/admissionpolicy"
"github.com/kyverno/kyverno/pkg/autogen"
"github.com/kyverno/kyverno/pkg/clients/dclient"
engineapi "github.com/kyverno/kyverno/pkg/engine/api"
kubeutils "github.com/kyverno/kyverno/pkg/utils/kube"
utils "github.com/kyverno/kyverno/pkg/utils/restmapper"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
Expand All @@ -28,8 +30,7 @@
// - the k8s cluster, if given
func GetResources(
out io.Writer,
policies []kyvernov1.PolicyInterface,
validatingAdmissionPolicies []admissionregistrationv1.ValidatingAdmissionPolicy,
policies []engineapi.GenericPolicy,
resourcePaths []string,
dClient dclient.Interface,
cluster bool,
Expand All @@ -41,35 +42,63 @@
var err error

if cluster && dClient != nil {
if len(policies) > 0 {
matchedResources := &KyvernoResources{
policies: policies,
clusterWideResources: clusterWideResources,
}

resources, err = matchedResources.FetchResourcesFromPolicy(out, resourcePaths, dClient, namespace, policyReport)
if err != nil {
return resources, err
}
resources, err = fetchResourcesFromPolicy(out, policies, resourcePaths, dClient, namespace, policyReport, clusterWideResources)
if err != nil {
return resources, err

Check warning on line 47 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L45-L47

Added lines #L45 - L47 were not covered by tests
}
} else if len(resourcePaths) > 0 {
resources, err = whenClusterIsFalse(out, resourcePaths, policyReport)
if err != nil {
return resources, err
}

Check warning on line 53 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L49-L53

Added lines #L49 - L53 were not covered by tests
}
return resources, err

Check warning on line 55 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L55

Added line #L55 was not covered by tests
}

if len(validatingAdmissionPolicies) > 0 {
matchedResources := &ValidatingAdmissionResources{
policies: validatingAdmissionPolicies,
clusterWideResources: clusterWideResources,
}
func fetchResourcesFromPolicy(
out io.Writer,
policies []engineapi.GenericPolicy,
resourcePaths []string,
dClient dclient.Interface,
namespace string,
policyReport bool,
clusterWideResources bool,
) ([]*unstructured.Unstructured, error) {
var resources []*unstructured.Unstructured
var err error

Check warning on line 68 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L66-L68

Added lines #L66 - L68 were not covered by tests

resources, err = matchedResources.FetchResourcesFromPolicy(out, resourcePaths, dClient, namespace, policyReport)
resourceTypesMap := make(map[schema.GroupVersionKind]bool)
var subresourceMap map[schema.GroupVersionKind]v1alpha1.Subresource

for _, policy := range policies {
if kpol := policy.AsKyvernoPolicy(); kpol != nil {
for _, rule := range autogen.Default.ComputeRules(kpol, "") {
var resourceTypesInRule map[schema.GroupVersionKind]bool
resourceTypesInRule, subresourceMap = GetKindsFromRule(rule, dClient, clusterWideResources)
for resourceKind := range resourceTypesInRule {
resourceTypesMap[resourceKind] = true
}

Check warning on line 80 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L70-L80

Added lines #L70 - L80 were not covered by tests
}
} else if vap := policy.AsValidatingAdmissionPolicy(); vap != nil {
definition := vap.GetDefinition()
var resourceTypesInRule map[schema.GroupVersionKind]bool
resourceTypesInRule, subresourceMap = getKindsFromValidatingAdmissionPolicy(*definition, dClient, clusterWideResources)

Check warning on line 85 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L82-L85

Added lines #L82 - L85 were not covered by tests
if err != nil {
return resources, err
}
}
} else if len(resourcePaths) > 0 {
resources, err = whenClusterIsFalse(out, resourcePaths, policyReport)
if err != nil {
return resources, err
for resourceKind := range resourceTypesInRule {
resourceTypesMap[resourceKind] = true
}

Check warning on line 91 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L89-L91

Added lines #L89 - L91 were not covered by tests
}
}

resourceTypes := make([]schema.GroupVersionKind, 0, len(resourceTypesMap))
for kind := range resourceTypesMap {
resourceTypes = append(resourceTypes, kind)
}

Check warning on line 98 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L95-L98

Added lines #L95 - L98 were not covered by tests

resources, err = whenClusterIsTrue(out, resourceTypes, subresourceMap, dClient, namespace, resourcePaths, policyReport)

Check warning on line 101 in cmd/cli/kubectl-kyverno/utils/common/fetch.go

View check run for this annotation

Codecov / codecov/patch

cmd/cli/kubectl-kyverno/utils/common/fetch.go#L100-L101

Added lines #L100 - L101 were not covered by tests
return resources, err
}

Expand Down
44 changes: 0 additions & 44 deletions cmd/cli/kubectl-kyverno/utils/common/kyverno_resources_types.go

This file was deleted.

This file was deleted.

Loading
0