Skip to content

Commit

Permalink
Reduce System.Mutate runtime by 87% (#1454)
Browse files Browse the repository at this point in the history
* Benchmark System.Mutate

Signed-off-by: Will Beason <willbeason@google.com>

* Early exit Mutate if no mutations

Signed-off-by: Will Beason <willbeason@google.com>

Co-authored-by: Oren Shomron <shomron@gmail.com>
  • Loading branch information
Will Beason and shomron committed Jul 26, 2021
1 parent b7a8674 commit ff826b2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
9 changes: 8 additions & 1 deletion pkg/mutation/system.go
Expand Up @@ -105,14 +105,21 @@ func (s *System) Mutate(obj *unstructured.Unstructured, ns *corev1.Namespace) (b

if m.Matches(obj, ns) {
mutated, err := m.Mutate(obj)
if mutated && (*MutationLoggingEnabled || *MutationAnnotationsEnabled) {
if mutated {
appliedMutations = append(appliedMutations, m)
}
if err != nil {
return false, errors.Wrapf(err, "mutation %s for mutator %v failed for %s %s %s %s", mutationUUID, m.ID(), obj.GroupVersionKind().Group, obj.GroupVersionKind().Kind, obj.GetNamespace(), obj.GetName())
}
}
}

if len(appliedMutations) == 0 {
// If no mutations were applied, we can safely assume the object is
// identical to before.
return i > 0, nil
}

if cmp.Equal(old, obj) {
if i == 0 {
return false, nil
Expand Down
62 changes: 62 additions & 0 deletions pkg/mutation/system_benchmark_test.go
@@ -0,0 +1,62 @@
package mutation

import (
"encoding/json"
"testing"

"github.com/open-policy-agent/gatekeeper/apis/mutations/v1alpha1"
"github.com/open-policy-agent/gatekeeper/pkg/mutation/match"
"github.com/open-policy-agent/gatekeeper/pkg/mutation/mutators"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)

func makeValue(v interface{}) runtime.RawExtension {
v2 := map[string]interface{}{
"value": v,
}
j, err := json.Marshal(v2)
if err != nil {
panic(err)
}
return runtime.RawExtension{Raw: j}
}

func assign(value interface{}, location string) *v1alpha1.Assign {
result := &v1alpha1.Assign{
Spec: v1alpha1.AssignSpec{
ApplyTo: []match.ApplyTo{{
Groups: []string{"*"},
Versions: []string{"*"},
Kinds: []string{"*"},
}},
Location: location,
Parameters: v1alpha1.Parameters{
Assign: makeValue(value),
},
},
}

return result
}

func BenchmarkSystem_Mutate(b *testing.B) {
s := NewSystem()

a := assign("", "spec")
m, err := mutators.MutatorForAssign(a)
if err != nil {
b.Fatal(err)
}

err = s.Upsert(m)
if err != nil {
b.Fatal(err)
}

for i := 0; i < b.N; i++ {
u := &unstructured.Unstructured{}

_, _ = s.Mutate(u, nil)
}
}
10 changes: 5 additions & 5 deletions pkg/mutation/system_test.go
Expand Up @@ -98,7 +98,7 @@ func (m *fakeMutator) SchemaBindings() []schema.GroupVersionKind {
return m.GVKs
}

var mutators = []types.Mutator{
var initMutators = []types.Mutator{
&fakeMutator{MID: types.ID{Group: "bbb", Kind: "aaa", Namespace: "aaa", Name: "aaa"}},
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "bbb", Namespace: "ccc", Name: "ddd"}},
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "bbb", Namespace: "aaa", Name: "aaa"}},
Expand All @@ -116,7 +116,7 @@ func TestSorting(t *testing.T) {
}{
{
tname: "testsort",
initial: mutators,
initial: initMutators,
expected: []types.Mutator{
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "aaa", Name: "aaa"}},
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "ccc", Name: "ddd"}},
Expand All @@ -129,7 +129,7 @@ func TestSorting(t *testing.T) {
},
{
tname: "testremove",
initial: mutators,
initial: initMutators,
expected: []types.Mutator{
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "aaa", Name: "aaa"}},
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "ccc", Name: "ddd"}},
Expand All @@ -143,7 +143,7 @@ func TestSorting(t *testing.T) {
},
{
tname: "testaddingsame",
initial: mutators,
initial: initMutators,
expected: []types.Mutator{
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "aaa", Name: "aaa"}},
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "ccc", Name: "ddd"}},
Expand All @@ -158,7 +158,7 @@ func TestSorting(t *testing.T) {
},
{
tname: "testaddingdifferent",
initial: mutators,
initial: initMutators,
expected: []types.Mutator{
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "aaa", Name: "aaa"}},
&fakeMutator{MID: types.ID{Group: "aaa", Kind: "aaa", Namespace: "ccc", Name: "ddd"}},
Expand Down

0 comments on commit ff826b2

Please sign in to comment.