@@ -54,6 +54,7 @@ type MutatorInterface interface {
5454
5555type Mutator struct {
5656 policy * v1alpha1.MutatingAdmissionPolicy
57+ binding * v1alpha1.MutatingAdmissionPolicyBinding
5758 evaluator mutating.PolicyEvaluator
5859}
5960
@@ -107,6 +108,15 @@ func NewMutator(policy *v1alpha1.MutatingAdmissionPolicy) (*Mutator, error) {
107108 }, nil
108109}
109110
111+ func NewMutatorWithBinding (policy * v1alpha1.MutatingAdmissionPolicy , binding * v1alpha1.MutatingAdmissionPolicyBinding ) (* Mutator , error ) {
112+ m , err := NewMutator (policy )
113+ if err != nil {
114+ return nil , err
115+ }
116+ m .binding = binding
117+ return m , nil
118+ }
119+
110120type mutatorContext struct {
111121 tcm patch.TypeConverterManager
112122 auth authorizer.Authorizer
@@ -280,21 +290,25 @@ func (m *Mutator) dispatchImpl(p MutationParams, dispatcherFactory func(mCtx *mu
280290 return nil , fmt .Errorf ("failed to initialize mutatorContext: %w" , err )
281291 }
282292
283- binding := & v1alpha1.MutatingAdmissionPolicyBinding {
284- Spec : v1alpha1.MutatingAdmissionPolicyBindingSpec {
285- ParamRef : & v1alpha1.ParamRef {},
286- MatchResources : & v1alpha1.MatchResources {
287- MatchPolicy : ptr .To (v1alpha1 .Equivalent ),
288- ObjectSelector : & metav1.LabelSelector {},
289- NamespaceSelector : & metav1.LabelSelector {},
293+ bindingGenerated := false
294+ if m .binding == nil {
295+ m .binding = & v1alpha1.MutatingAdmissionPolicyBinding {
296+ Spec : v1alpha1.MutatingAdmissionPolicyBindingSpec {
297+ ParamRef : & v1alpha1.ParamRef {},
298+ MatchResources : & v1alpha1.MatchResources {
299+ MatchPolicy : ptr .To (v1alpha1 .Equivalent ),
300+ ObjectSelector : & metav1.LabelSelector {},
301+ NamespaceSelector : & metav1.LabelSelector {},
302+ },
290303 },
291- },
304+ }
305+ bindingGenerated = true
292306 }
293307
294308 hook := mutating.PolicyHook {
295309 Policy : m .policy ,
296310 Evaluator : m .evaluator ,
297- Bindings : []* mutating.PolicyBinding {binding },
311+ Bindings : []* mutating.PolicyBinding {m . binding },
298312 }
299313
300314 if m .policy .Spec .ParamKind != nil {
@@ -322,8 +336,10 @@ func (m *Mutator) dispatchImpl(p MutationParams, dispatcherFactory func(mCtx *mu
322336 if err != nil {
323337 return nil , fmt .Errorf ("failed to access paramObj: %w" , err )
324338 }
325- binding .Spec .ParamRef .Name = metaAcc .GetName ()
326- binding .Spec .ParamRef .Namespace = metaAcc .GetNamespace ()
339+ if bindingGenerated {
340+ m .binding .Spec .ParamRef .Name = metaAcc .GetName ()
341+ m .binding .Spec .ParamRef .Namespace = metaAcc .GetNamespace ()
342+ }
327343 }
328344
329345 // Start informers
0 commit comments