Skip to content

Commit

Permalink
Add DisableDeepCopy as list option to avoid deep copy during list
Browse files Browse the repository at this point in the history
Signed-off-by: Siyu Wang <FillZpp.pub@gmail.com>
  • Loading branch information
FillZpp committed Nov 25, 2020
1 parent f3831c2 commit c71ebe7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkg/cache/internal/cache_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,31 @@ func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...cli
}
}

outObj := obj.DeepCopyObject()
outObj.GetObjectKind().SetGroupVersionKind(c.groupVersionKind)
var outObj runtime.Object
if c.disableDeepCopy(obj, listOpts) {
outObj = obj
} else {
outObj = obj.DeepCopyObject()
outObj.GetObjectKind().SetGroupVersionKind(c.groupVersionKind)
}
runtimeObjs = append(runtimeObjs, outObj)
}
return apimeta.SetList(out, runtimeObjs)
}

// disableDeepCopy checks if this object need to skip deep copy.
func (c *CacheReader) disableDeepCopy(obj runtime.Object, listOpts client.ListOptions) bool {
var disableDeepCopy bool
if listOpts.DisableDeepCopy != nil {
if listOpts.DisableDeepCopy.IgnoreGVK {
disableDeepCopy = true
} else if obj.GetObjectKind().GroupVersionKind() == c.groupVersionKind {
disableDeepCopy = true
}
}
return disableDeepCopy
}

// objectKeyToStorageKey converts an object key to store key.
// It's akin to MetaNamespaceKeyFunc. It's separate from
// String to allow keeping the key format easily in sync with
Expand Down
81 changes: 81 additions & 0 deletions pkg/cache/internal/cache_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package internal

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
)

var (
targetGVK = schema.GroupVersionKind{Group: "test.kubebuilder.io", Version: "v1beta1", Kind: "SomeCR"}
)

func TestSource(t *testing.T) {
RegisterFailHandler(Fail)
suiteName := "Cache Internal Test Suite"
RunSpecsWithDefaultAndCustomReporters(t, suiteName, []Reporter{printer.NewlineReporter{}, printer.NewProwReporter(suiteName)})
}

var _ = Describe("Cache reader", func() {
Describe("Disable deep copy", func() {
var c *CacheReader
BeforeEach(func() {
c = &CacheReader{groupVersionKind: targetGVK}
})

objWithoutGVK := &unstructured.Unstructured{}
objWithGVK := &unstructured.Unstructured{}
objWithGVK.SetGroupVersionKind(targetGVK)

It("should deep copy for object without gvk if listOptions not set disableDeepCopy", func() {
listOps := client.ListOptions{}
Expect(c.disableDeepCopy(objWithoutGVK, listOps)).To(Equal(false))
})

It("should deep copy for object with gvk if listOptions not set disableDeepCopy", func() {
listOps := client.ListOptions{}
Expect(c.disableDeepCopy(objWithGVK, listOps)).To(Equal(false))
})

It("should deep copy for object without gvk if listOptions set disableDeepCopy and IgnoreGVK is false", func() {
listOps := client.ListOptions{DisableDeepCopy: &client.DisableDeepCopy{}}
Expect(c.disableDeepCopy(objWithoutGVK, listOps)).To(Equal(false))
})

It("should not deep copy for object with gvk if listOptions set disableDeepCopy and IgnoreGVK is false", func() {
listOps := client.ListOptions{DisableDeepCopy: &client.DisableDeepCopy{}}
Expect(c.disableDeepCopy(objWithGVK, listOps)).To(Equal(true))
})

It("should not deep copy for object without gvk if listOptions set disableDeepCopy and IgnoreGVK is true", func() {
listOps := client.ListOptions{DisableDeepCopy: &client.DisableDeepCopy{IgnoreGVK: true}}
Expect(c.disableDeepCopy(objWithoutGVK, listOps)).To(Equal(true))
})

It("should not deep copy for object with gvk if listOptions set disableDeepCopy and IgnoreGVK is true", func() {
listOps := client.ListOptions{DisableDeepCopy: &client.DisableDeepCopy{IgnoreGVK: true}}
Expect(c.disableDeepCopy(objWithGVK, listOps)).To(Equal(true))
})
})
})
14 changes: 14 additions & 0 deletions pkg/client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ type ListOptions struct {
// that these may not be respected by all implementations of interface,
// and the LabelSelector, FieldSelector, Limit and Continue fields are ignored.
Raw *metav1.ListOptions

// DisableDeepCopy allows users to list objects without deep copy.
DisableDeepCopy *DisableDeepCopy
}

var _ ListOption = &ListOptions{}
Expand Down Expand Up @@ -521,6 +524,17 @@ func (c Continue) ApplyToList(opts *ListOptions) {
opts.Continue = string(c)
}

// DisableDeepCopy indicates not to deep copy objects during list objects in cache reader.
type DisableDeepCopy struct {
// if IgnoreGVK is true, cache reader will always list without deep copy.
// Otherwise, it will only avoid deep copy for objects that already have correct groupVersionKind.
IgnoreGVK bool
}

func (d DisableDeepCopy) ApplyToList(opts *ListOptions) {
opts.DisableDeepCopy = &d
}

// }}}

// {{{ Update Options
Expand Down

0 comments on commit c71ebe7

Please sign in to comment.