1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-05 07:26:55 +00:00

fix: cache invalidation in FindResources (#8316)

Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
Charles-Edouard Brétéché 2023-09-08 15:51:25 +02:00 committed by GitHub
parent 10dacd5292
commit 5beaec677f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 4 deletions

View file

@ -163,10 +163,17 @@ func (c serverResources) FindResource(groupVersion string, kind string) (apiReso
func (c serverResources) FindResources(group, version, kind, subresource string) (map[TopLevelApiDescription]metav1.APIResource, error) { func (c serverResources) FindResources(group, version, kind, subresource string) (map[TopLevelApiDescription]metav1.APIResource, error) {
resources, err := c.findResources(group, version, kind, subresource) resources, err := c.findResources(group, version, kind, subresource)
if err != nil { // if no resource was found, we have to force cache invalidation
if !c.cachedClient.Fresh() { if err != nil || len(resources) == 0 {
if !c.cachedClient.Fresh() || len(resources) == 0 {
c.cachedClient.Invalidate() c.cachedClient.Invalidate()
return c.findResources(group, version, kind, subresource) resources, err := c.findResources(group, version, kind, subresource)
if err != nil {
return nil, err
} else if len(resources) == 0 {
return nil, fmt.Errorf("failed to find resource (%s/%s/%s/%s)", group, version, kind, subresource)
}
return resources, err
} }
} }
return resources, err return resources, err

View file

@ -2,6 +2,7 @@ package adapters
import ( import (
"context" "context"
"fmt"
"io" "io"
"github.com/kyverno/kyverno/pkg/auth" "github.com/kyverno/kyverno/pkg/auth"
@ -59,6 +60,9 @@ func (a *dclientAdapter) IsNamespaced(group, version, kind string) (bool, error)
if err != nil { if err != nil {
return false, err return false, err
} }
if len(gvrss) != 1 {
return false, fmt.Errorf("function IsNamespaced expects only one resource, got (%d)", len(gvrss))
}
for _, apiResource := range gvrss { for _, apiResource := range gvrss {
return apiResource.Namespaced, nil return apiResource.Namespaced, nil
} }

View file

@ -178,7 +178,7 @@ func collectParams(ctx context.Context, client engineapi.Client, paramKind *admi
var paramsNamespace string var paramsNamespace string
isNamespaced, err := client.IsNamespaced(gv.Group, gv.Version, kind) isNamespaced, err := client.IsNamespaced(gv.Group, gv.Version, kind)
if err != nil { if err != nil {
return nil, err return nil, fmt.Errorf("failed to check if resource is namespaced or not (%w)", err)
} }
// check if `paramKind` is namespace-scoped // check if `paramKind` is namespace-scoped