Skip to content

Commit

Permalink
cache assignmetadata tester
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Beason committed Jul 14, 2021
1 parent 20b5537 commit 0e2ae5e
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pkg/mutation/mutators/assignmeta_mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
// AssignMeta instance.
type AssignMetadataMutator struct {
id types.ID
tester *tester.Tester
assignMetadata *mutationsv1alpha1.AssignMetadata
path parser.Path
}
Expand All @@ -59,13 +60,7 @@ func (m *AssignMetadataMutator) Matches(obj client.Object, ns *corev1.Namespace)
}

func (m *AssignMetadataMutator) Mutate(obj *unstructured.Unstructured) (bool, error) {
t, err := tester.New(m.Path(), []tester.Test{
{SubPath: m.Path(), Condition: tester.MustNotExist},
})
if err != nil {
return false, err
}
return core.Mutate(m, t, nil, obj)
return core.Mutate(m, m.tester, nil, obj)
}

func (m *AssignMetadataMutator) ID() types.ID {
Expand Down Expand Up @@ -96,6 +91,7 @@ func (m *AssignMetadataMutator) HasDiff(mutator types.Mutator) bool {
func (m *AssignMetadataMutator) DeepCopy() types.Mutator {
res := &AssignMetadataMutator{
id: m.id,
tester: m.tester.DeepCopy(),
assignMetadata: m.assignMetadata.DeepCopy(),
path: m.path.DeepCopy(),
}
Expand Down Expand Up @@ -142,8 +138,16 @@ func MutatorForAssignMetadata(assignMeta *mutationsv1alpha1.AssignMetadata) (*As
return nil, errors.New("spec.parameters.assign.value field must be a string for AssignMetadata " + assignMeta.GetName())
}

t, err := tester.New(path, []tester.Test{
{SubPath: path, Condition: tester.MustNotExist},
})
if err != nil {
return nil, err
}

return &AssignMetadataMutator{
id: types.MakeID(assignMeta),
tester: t,
assignMetadata: assignMeta.DeepCopy(),
path: path,
}, nil
Expand Down

0 comments on commit 0e2ae5e

Please sign in to comment.