Skip to content

Commit

Permalink
Rebase controller-runtime/pkg/cache fork over v0.8.2 (#1284)
Browse files Browse the repository at this point in the history
* Rebase dynamiccache fork to controller-runtime v0.8.2

Signed-off-by: Oren Shomron <shomron@gmail.com>

* Update comments to specify base of controller-runtime v0.8.2

Signed-off-by: Oren Shomron <shomron@gmail.com>
  • Loading branch information
shomron committed May 5, 2021
1 parent aa20de6 commit f6d0fc9
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 28 deletions.
4 changes: 2 additions & 2 deletions third_party/sigs.k8s.io/controller-runtime/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# sigs.k8s.io/controller-runtime

Forked from sigs.k8s.io/controller-runtime@f7a3dc6a7650289b6ca7afca12b97819329b0d06 (v0.7.0).
Forked from sigs.k8s.io/controller-runtime@a8c19c49e49cfba2aa486ff322cbe5222d6da533 (v0.8.2).

This fork adds the ability to dynamically
remove informers from the informer cache. Additionally, non-blocking APIs were added to fetch informers
without waiting for cache sync. `pkg/cache` was renamed to `pkg/dynamiccache` for clarity.

The original code can be found at https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache.
The original code can be found at https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache_test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache_test

Expand All @@ -40,6 +40,7 @@ import (
"github.com/open-policy-agent/gatekeeper/third_party/sigs.k8s.io/controller-runtime/pkg/dynamiccache"
)

const testNodeOne = "test-node-1"
const testNamespaceOne = "test-namespace-1"
const testNamespaceTwo = "test-namespace-2"
const testNamespaceThree = "test-namespace-3"
Expand Down Expand Up @@ -103,6 +104,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
By("creating three pods")
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
err = ensureNode(testNodeOne, cl)
Expect(err).NotTo(HaveOccurred())
err = ensureNamespace(testNamespaceOne, cl)
Expect(err).NotTo(HaveOccurred())
err = ensureNamespace(testNamespaceTwo, cl)
Expand Down Expand Up @@ -418,17 +421,33 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
Expect(out.Items).Should(HaveLen(1))
Expect(out.Items[0].GetNamespace()).To(Equal(testNamespaceOne))

By("listing all namespaces - should still be able to get a cluster-scoped resource")
namespaceList := &unstructured.UnstructuredList{}
namespaceList.SetGroupVersionKind(schema.GroupVersionKind{
By("listing all nodes - should still be able to list a cluster-scoped resource")
nodeList := &unstructured.UnstructuredList{}
nodeList.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "NamespaceList",
Kind: "NodeList",
})
Expect(namespacedCache.List(context.Background(), namespaceList)).To(Succeed())
Expect(namespacedCache.List(context.Background(), nodeList)).To(Succeed())

By("verifying the node list is not empty")
Expect(nodeList.Items).NotTo(BeEmpty())

By("getting a node - should still be able to get a cluster-scoped resource")
node := &unstructured.Unstructured{}
node.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Node",
})

By("verifying that getting the node works with an empty namespace")
key1 := client.ObjectKey{Namespace: "", Name: testNodeOne}
Expect(namespacedCache.Get(context.Background(), key1, node)).To(Succeed())

By("verifying the namespace list is not empty")
Expect(namespaceList.Items).NotTo(BeEmpty())
By("verifying that the namespace is ignored when getting a cluster-scoped resource")
key2 := client.ObjectKey{Namespace: "random", Name: testNodeOne}
Expect(namespacedCache.Get(context.Background(), key2, node)).To(Succeed())
})

It("should deep copy the object unless told otherwise", func() {
Expand Down Expand Up @@ -612,17 +631,33 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
Expect(out.Items).Should(HaveLen(1))
Expect(out.Items[0].GetNamespace()).To(Equal(testNamespaceOne))

By("listing all namespaces - should still be able to get a cluster-scoped resource")
namespaceList := &kmetav1.PartialObjectMetadataList{}
namespaceList.SetGroupVersionKind(schema.GroupVersionKind{
By("listing all nodes - should still be able to list a cluster-scoped resource")
nodeList := &kmetav1.PartialObjectMetadataList{}
nodeList.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "NamespaceList",
Kind: "NodeList",
})
Expect(namespacedCache.List(context.Background(), namespaceList)).To(Succeed())
Expect(namespacedCache.List(context.Background(), nodeList)).To(Succeed())

By("verifying the node list is not empty")
Expect(nodeList.Items).NotTo(BeEmpty())

By("getting a node - should still be able to get a cluster-scoped resource")
node := &kmetav1.PartialObjectMetadata{}
node.SetGroupVersionKind(schema.GroupVersionKind{
Group: "",
Version: "v1",
Kind: "Node",
})

By("verifying that getting the node works with an empty namespace")
key1 := client.ObjectKey{Namespace: "", Name: testNodeOne}
Expect(namespacedCache.Get(context.Background(), key1, node)).To(Succeed())

By("verifying the namespace list is not empty")
Expect(namespaceList.Items).NotTo(BeEmpty())
By("verifying that the namespace is ignored when getting a cluster-scoped resource")
key2 := client.ObjectKey{Namespace: "random", Name: testNodeOne}
Expect(namespacedCache.Get(context.Background(), key2, node)).To(Succeed())
})

It("should deep copy the object unless told otherwise", func() {
Expand Down Expand Up @@ -1084,6 +1119,23 @@ func ensureNamespace(namespace string, client client.Client) error {
return err
}

func ensureNode(name string, client client.Client) error {
node := kcorev1.Node{
ObjectMeta: kmetav1.ObjectMeta{
Name: name,
},
TypeMeta: kmetav1.TypeMeta{
Kind: "Node",
APIVersion: "v1",
},
}
err := client.Create(context.TODO(), &node)
if errors.IsAlreadyExists(err) {
return nil
}
return err
}

//nolint:interfacer
func isKubeService(svc kmetav1.Object) bool {
// grumble grumble linters grumble grumble
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

// Package cache provides object caches that act as caching client.Reader
// instances and help drive Kubernetes-object-based event handlers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache_test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/tools/cache"

"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -42,10 +43,16 @@ type CacheReader struct {

// groupVersionKind is the group-version-kind of the resource.
groupVersionKind schema.GroupVersionKind

// scopeName is the scope of the resource (namespaced or cluster-scoped).
scopeName apimeta.RESTScopeName
}

// Get checks the indexer for the object and writes a copy of it if found
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object) error {
if c.scopeName == apimeta.RESTScopeNameRoot {
key.Namespace = ""
}
storeKey := objectKeyToStoreKey(key)

// Lookup the object from the indexer cache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package internal

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package internal

Expand Down Expand Up @@ -233,9 +233,13 @@ func (ip *specificInformersMap) addInformerToMap(gvk schema.GroupVersionKind, ob
ni := cache.NewSharedIndexInformer(lw, obj, resyncPeriod(ip.resync)(), cache.Indexers{
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
})
rm, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
return nil, false, err
}
i := &MapEntry{
Informer: ni,
Reader: CacheReader{indexer: ni.GetIndexer(), groupVersionKind: gvk},
Reader: CacheReader{indexer: ni.GetIndexer(), groupVersionKind: gvk, scopeName: rm.Scope.Name()},
stop: make(chan struct{}),
}
ip.informersByGVK[gvk] = i
Expand Down Expand Up @@ -355,7 +359,7 @@ func createMetadataListWatch(gvk schema.GroupVersionKind, ip *specificInformersM
// pass in their own contexts instead of relying on this fixed one here.
ctx := context.TODO()

// create the relevant listwaatch
// create the relevant listwatch
return &cache.ListWatch{
ListFunc: func(opts metav1.ListOptions) (runtime.Object, error) {
if ip.namespace != "" && mapping.Scope.Name() != meta.RESTScopeNameRoot {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.
*/

// Modified from the original source (available at
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.7.0/pkg/cache)
// https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache)

package dynamiccache

Expand Down

0 comments on commit f6d0fc9

Please sign in to comment.