diff --git a/pkg/clients/dclient/helpers.go b/pkg/clients/dclient/helpers.go index b94b662054..fc619e7dc7 100644 --- a/pkg/clients/dclient/helpers.go +++ b/pkg/clients/dclient/helpers.go @@ -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 diff --git a/pkg/engine/adapters/dclient.go b/pkg/engine/adapters/dclient.go index f35eb64376..2ca901e4e2 100644 --- a/pkg/engine/adapters/dclient.go +++ b/pkg/engine/adapters/dclient.go @@ -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 } diff --git a/pkg/engine/api/client.go b/pkg/engine/api/client.go index 13c719b23f..5a23cbcaa8 100644 --- a/pkg/engine/api/client.go +++ b/pkg/engine/api/client.go @@ -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 { diff --git a/pkg/engine/handlers/mutation/load_targets.go b/pkg/engine/handlers/mutation/load_targets.go index 69f9e5b718..e8c0f96d27 100644 --- a/pkg/engine/handlers/mutation/load_targets.go +++ b/pkg/engine/handlers/mutation/load_targets.go @@ -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 } diff --git a/pkg/engine/handlers/mutation/mutate_existing.go b/pkg/engine/handlers/mutation/mutate_existing.go index e415d36df2..5bff5c085f 100644 --- a/pkg/engine/handlers/mutation/mutate_existing.go +++ b/pkg/engine/handlers/mutation/mutate_existing.go @@ -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)