diff --git a/pkg/background/generate/generate.go b/pkg/background/generate/generate.go index f5859a7a06..87369cbfa7 100644 --- a/pkg/background/generate/generate.go +++ b/pkg/background/generate/generate.go @@ -37,62 +37,48 @@ import ( ) type GenerateController struct { - // GenerateController updaterequest.GenerateController - client dclient.Interface - - // typed client for Kyverno CRDs + // clients + client dclient.Interface kyvernoClient kyvernoclient.Interface - - // urStatusControl is used to update UR status statusControl common.StatusControlInterface - // event generator interface - eventGen event.Interface - - log logr.Logger - - // urLister can list/get update request from the shared informer's store - urLister kyvernov1beta1listers.UpdateRequestNamespaceLister - - // nsLister can list/get namespaces from the shared informer's store - nsLister corev1listers.NamespaceLister - - // policyLister can list/get cluster policy from the shared informer's store - policyLister kyvernov1listers.ClusterPolicyLister - - // policyLister can list/get Namespace policy from the shared informer's store + // listers + urLister kyvernov1beta1listers.UpdateRequestNamespaceLister + nsLister corev1listers.NamespaceLister + policyLister kyvernov1listers.ClusterPolicyLister npolicyLister kyvernov1listers.PolicyLister - Config config.Configuration + configuration config.Configuration + eventGen event.Interface + log logr.Logger } // NewGenerateController returns an instance of the Generate-Request Controller func NewGenerateController( - kyvernoClient kyvernoclient.Interface, client dclient.Interface, + kyvernoClient kyvernoclient.Interface, + statusControl common.StatusControlInterface, policyLister kyvernov1listers.ClusterPolicyLister, npolicyLister kyvernov1listers.PolicyLister, urLister kyvernov1beta1listers.UpdateRequestNamespaceLister, - eventGen event.Interface, nsLister corev1listers.NamespaceLister, - log logr.Logger, dynamicConfig config.Configuration, -) (*GenerateController, error) { + eventGen event.Interface, + log logr.Logger, +) *GenerateController { c := GenerateController{ client: client, kyvernoClient: kyvernoClient, - eventGen: eventGen, - log: log, - Config: dynamicConfig, + statusControl: statusControl, policyLister: policyLister, npolicyLister: npolicyLister, urLister: urLister, + nsLister: nsLister, + configuration: dynamicConfig, + eventGen: eventGen, + log: log, } - - c.statusControl = common.NewStatusControl(kyvernoClient, urLister) - c.nsLister = nsLister - - return &c, nil + return &c } func (c *GenerateController) ProcessUR(ur *kyvernov1beta1.UpdateRequest) error { @@ -200,7 +186,7 @@ func (c *GenerateController) applyGenerate(resource unstructured.Unstructured, u return nil, false, err } - policyContext, precreatedResource, err := common.NewBackgroundContext(c.client, &ur, &policy, &resource, c.Config, namespaceLabels, logger) + policyContext, precreatedResource, err := common.NewBackgroundContext(c.client, &ur, &policy, &resource, c.configuration, namespaceLabels, logger) if err != nil { return nil, precreatedResource, err } diff --git a/pkg/background/mutate/mutate.go b/pkg/background/mutate/mutate.go index 03035f61cd..23456dfe53 100644 --- a/pkg/background/mutate/mutate.go +++ b/pkg/background/mutate/mutate.go @@ -8,9 +8,7 @@ import ( kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1" kyvernov1beta1 "github.com/kyverno/kyverno/api/kyverno/v1beta1" "github.com/kyverno/kyverno/pkg/background/common" - kyvernoclient "github.com/kyverno/kyverno/pkg/client/clientset/versioned" kyvernov1listers "github.com/kyverno/kyverno/pkg/client/listers/kyverno/v1" - kyvernov1beta1listers "github.com/kyverno/kyverno/pkg/client/listers/kyverno/v1beta1" "github.com/kyverno/kyverno/pkg/config" "github.com/kyverno/kyverno/pkg/dclient" "github.com/kyverno/kyverno/pkg/engine" @@ -26,55 +24,39 @@ import ( var ErrEmptyPatch error = fmt.Errorf("empty resource to patch") type MutateExistingController struct { - client dclient.Interface - - // typed client for Kyverno CRDs - kyvernoClient kyvernoclient.Interface - - // urStatusControl is used to update UR status + // clients + client dclient.Interface statusControl common.StatusControlInterface - // event generator interface - eventGen event.Interface - - log logr.Logger - - // urLister can list/get update request from the shared informer's store - urLister kyvernov1beta1listers.UpdateRequestNamespaceLister - - // policyLister can list/get cluster policy from the shared informer's store - policyLister kyvernov1listers.ClusterPolicyLister - - // policyLister can list/get Namespace policy from the shared informer's store + // listers + policyLister kyvernov1listers.ClusterPolicyLister npolicyLister kyvernov1listers.PolicyLister - Config config.Configuration + configuration config.Configuration + eventGen event.Interface + log logr.Logger } // NewMutateExistingController returns an instance of the MutateExistingController func NewMutateExistingController( - kyvernoClient kyvernoclient.Interface, client dclient.Interface, + statusControl common.StatusControlInterface, policyLister kyvernov1listers.ClusterPolicyLister, npolicyLister kyvernov1listers.PolicyLister, - urLister kyvernov1beta1listers.UpdateRequestNamespaceLister, + dynamicConfig config.Configuration, eventGen event.Interface, log logr.Logger, - dynamicConfig config.Configuration, -) (*MutateExistingController, error) { +) *MutateExistingController { c := MutateExistingController{ client: client, - kyvernoClient: kyvernoClient, - eventGen: eventGen, - log: log, + statusControl: statusControl, policyLister: policyLister, npolicyLister: npolicyLister, - urLister: urLister, - Config: dynamicConfig, + configuration: dynamicConfig, + eventGen: eventGen, + log: log, } - - c.statusControl = common.NewStatusControl(kyvernoClient, urLister) - return &c, nil + return &c } func (c *MutateExistingController) ProcessUR(ur *kyvernov1beta1.UpdateRequest) error { @@ -99,7 +81,7 @@ func (c *MutateExistingController) ProcessUR(ur *kyvernov1beta1.UpdateRequest) e continue } - policyContext, _, err := common.NewBackgroundContext(c.client, ur, policy, trigger, c.Config, nil, logger) + policyContext, _, err := common.NewBackgroundContext(c.client, ur, policy, trigger, c.configuration, nil, logger) if err != nil { logger.WithName(rule.Name).Error(err, "failed to build policy context") errs = append(errs, err) diff --git a/pkg/background/update_request_controller.go b/pkg/background/update_request_controller.go index 68931dbd55..ed16d07f05 100644 --- a/pkg/background/update_request_controller.go +++ b/pkg/background/update_request_controller.go @@ -271,16 +271,13 @@ func (c *controller) deleteUR(obj interface{}) { } func (c *controller) processUR(ur *kyvernov1beta1.UpdateRequest) error { + statusControl := common.NewStatusControl(c.kyvernoClient, c.urLister) switch ur.Spec.Type { case kyvernov1beta1.Mutate: - ctrl, _ := mutate.NewMutateExistingController(c.kyvernoClient, c.client, - c.policyLister, c.npolicyLister, c.urLister, c.eventGen, logger, c.configuration) + ctrl := mutate.NewMutateExistingController(c.client, statusControl, c.policyLister, c.npolicyLister, c.configuration, c.eventGen, logger) return ctrl.ProcessUR(ur) - case kyvernov1beta1.Generate: - ctrl, _ := generate.NewGenerateController(c.kyvernoClient, c.client, - c.policyLister, c.npolicyLister, c.urLister, c.eventGen, c.nsLister, logger, c.configuration, - ) + ctrl := generate.NewGenerateController(c.client, c.kyvernoClient, statusControl, c.policyLister, c.npolicyLister, c.urLister, c.nsLister, c.configuration, c.eventGen, logger) return ctrl.ProcessUR(ur) } return nil