mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
fix: propagate context when listing resources (#7487)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
This commit is contained in:
parent
42657f672f
commit
9387d20443
5 changed files with 17 additions and 16 deletions
|
@ -16,7 +16,7 @@ type Resource struct {
|
|||
Unstructured unstructured.Unstructured
|
||||
}
|
||||
|
||||
func GetResources(c Interface, group, version, kind, subresource, namespace, name string) ([]Resource, error) {
|
||||
func GetResources(ctx context.Context, c Interface, group, version, kind, subresource, namespace, name string) ([]Resource, error) {
|
||||
var resources []Resource
|
||||
gvrss, err := c.Discovery().FindResources(group, version, kind, subresource)
|
||||
if err != nil {
|
||||
|
@ -32,7 +32,7 @@ func GetResources(c Interface, group, version, kind, subresource, namespace, nam
|
|||
if namespace != "" && name != "" && !wildcard.ContainsWildcard(namespace) && !wildcard.ContainsWildcard(name) {
|
||||
var obj *unstructured.Unstructured
|
||||
var err error
|
||||
obj, err = dyn.Namespace(namespace).Get(context.TODO(), name, metav1.GetOptions{}, sub...)
|
||||
obj, err = dyn.Namespace(namespace).Get(ctx, name, metav1.GetOptions{}, sub...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func GetResources(c Interface, group, version, kind, subresource, namespace, nam
|
|||
} else {
|
||||
// we can use `LIST`
|
||||
if gvrs.SubResource == "" {
|
||||
list, err := dyn.List(context.TODO(), metav1.ListOptions{})
|
||||
list, err := dyn.List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func GetResources(c Interface, group, version, kind, subresource, namespace, nam
|
|||
}
|
||||
} else {
|
||||
// we need to use `LIST` / `GET`
|
||||
list, err := dyn.List(context.TODO(), metav1.ListOptions{})
|
||||
list, err := dyn.List(ctx, metav1.ListOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -77,9 +77,9 @@ func GetResources(c Interface, group, version, kind, subresource, namespace, nam
|
|||
var obj *unstructured.Unstructured
|
||||
var err error
|
||||
if parentObject.GetNamespace() == "" {
|
||||
obj, err = dyn.Get(context.TODO(), name, metav1.GetOptions{}, sub...)
|
||||
obj, err = dyn.Get(ctx, name, metav1.GetOptions{}, sub...)
|
||||
} else {
|
||||
obj, err = dyn.Namespace(parentObject.GetNamespace()).Get(context.TODO(), name, metav1.GetOptions{}, sub...)
|
||||
obj, err = dyn.Namespace(parentObject.GetNamespace()).Get(ctx, name, metav1.GetOptions{}, sub...)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -22,8 +22,8 @@ func (a *dclientAdapter) RawAbsPath(ctx context.Context, path, method string, da
|
|||
return a.client.RawAbsPath(ctx, path, method, dataReader)
|
||||
}
|
||||
|
||||
func (a *dclientAdapter) GetResources(group, version, kind, subresource, namespace, name string) ([]engineapi.Resource, error) {
|
||||
resources, err := dclient.GetResources(a.client, group, version, kind, subresource, namespace, name)
|
||||
func (a *dclientAdapter) GetResources(ctx context.Context, group, version, kind, subresource, namespace, name string) ([]engineapi.Resource, error) {
|
||||
resources, err := dclient.GetResources(ctx, a.client, group, version, kind, subresource, namespace, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ type AuthClient interface {
|
|||
|
||||
type ResourceClient interface {
|
||||
GetResource(ctx context.Context, apiVersion, kind, namespace, name string, subresources ...string) (*unstructured.Unstructured, error)
|
||||
GetResources(group, version, kind, subresource, namespace, name string) ([]Resource, error)
|
||||
GetResources(ctx context.Context, group, version, kind, subresource, namespace, name string) ([]Resource, error)
|
||||
}
|
||||
|
||||
type Client interface {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package mutation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
|
@ -29,17 +30,17 @@ type target struct {
|
|||
preconditions apiextensions.JSON
|
||||
}
|
||||
|
||||
func loadTargets(client engineapi.Client, targets []kyvernov1.TargetResourceSpec, ctx engineapi.PolicyContext, logger logr.Logger) ([]target, error) {
|
||||
func loadTargets(ctx context.Context, client engineapi.Client, targets []kyvernov1.TargetResourceSpec, policyCtx engineapi.PolicyContext, logger logr.Logger) ([]target, error) {
|
||||
var targetObjects []target
|
||||
var errors []error
|
||||
for i := range targets {
|
||||
preconditions := targets[i].GetAnyAllConditions()
|
||||
spec, err := resolveSpec(i, targets[i], ctx, logger)
|
||||
spec, err := resolveSpec(i, targets[i], policyCtx, logger)
|
||||
if err != nil {
|
||||
errors = append(errors, err)
|
||||
continue
|
||||
}
|
||||
objs, err := getTargets(client, spec, ctx)
|
||||
objs, err := getTargets(ctx, client, spec, policyCtx)
|
||||
if err != nil {
|
||||
errors = append(errors, err)
|
||||
continue
|
||||
|
@ -80,17 +81,17 @@ func resolveSpec(i int, target kyvernov1.TargetResourceSpec, ctx engineapi.Polic
|
|||
}, nil
|
||||
}
|
||||
|
||||
func getTargets(client engineapi.Client, target kyvernov1.ResourceSpec, ctx engineapi.PolicyContext) ([]resourceInfo, error) {
|
||||
func getTargets(ctx context.Context, client engineapi.Client, target kyvernov1.ResourceSpec, policyCtx engineapi.PolicyContext) ([]resourceInfo, error) {
|
||||
var targetObjects []resourceInfo
|
||||
namespace := target.Namespace
|
||||
name := target.Name
|
||||
policy := ctx.Policy()
|
||||
policy := policyCtx.Policy()
|
||||
// if it's namespaced policy, targets has to be loaded only from the policy's namespace
|
||||
if policy.IsNamespaced() {
|
||||
namespace = policy.GetNamespace()
|
||||
}
|
||||
group, version, kind, subresource := kubeutils.ParseKindSelector(target.APIVersion + "/" + target.Kind)
|
||||
resources, err := client.GetResources(group, version, kind, subresource, namespace, name)
|
||||
resources, err := client.GetResources(ctx, group, version, kind, subresource, namespace, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func (h mutateExistingHandler) Process(
|
|||
) (unstructured.Unstructured, []engineapi.RuleResponse) {
|
||||
var responses []engineapi.RuleResponse
|
||||
logger.V(3).Info("processing mutate rule")
|
||||
targets, err := loadTargets(h.client, rule.Mutation.Targets, policyContext, logger)
|
||||
targets, err := loadTargets(ctx, h.client, rule.Mutation.Targets, policyContext, logger)
|
||||
if err != nil {
|
||||
rr := engineapi.RuleError(rule.Name, engineapi.Mutation, "", err)
|
||||
responses = append(responses, *rr)
|
||||
|
|
Loading…
Reference in a new issue