|
| 1 | +/* |
| 2 | +Copyright 2024 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package cache_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/cache" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("KCP cluster-unaware informer cache", func() { |
| 30 | + // Test whether we can have a cluster-unaware informer cache against a single workspace. |
| 31 | + // I.e. every object has a kcp.io/cluster annotation, but it should not be taken |
| 32 | + // into consideration by the cache to compute the key. |
| 33 | + It("should be able to get the default namespace despite kcp.io/cluster annotation", func() { |
| 34 | + ctx, cancel := context.WithCancel(context.Background()) |
| 35 | + defer cancel() |
| 36 | + |
| 37 | + c, err := cache.New(cfg, cache.Options{}) |
| 38 | + Expect(err).NotTo(HaveOccurred()) |
| 39 | + |
| 40 | + By("Annotating the default namespace with kcp.io/cluster") |
| 41 | + cl, err := client.New(cfg, client.Options{}) |
| 42 | + Expect(err).NotTo(HaveOccurred()) |
| 43 | + ns := &corev1.Namespace{} |
| 44 | + err = cl.Get(ctx, client.ObjectKey{Name: "default"}, ns) |
| 45 | + Expect(err).NotTo(HaveOccurred()) |
| 46 | + ns.Annotations = map[string]string{"kcp.io/cluster": "cluster1"} |
| 47 | + err = cl.Update(ctx, ns) |
| 48 | + Expect(err).NotTo(HaveOccurred()) |
| 49 | + |
| 50 | + go c.Start(ctx) //nolint:errcheck // Start is blocking, and error not relevant here. |
| 51 | + c.WaitForCacheSync(ctx) |
| 52 | + |
| 53 | + By("By getting the default namespace with the informer") |
| 54 | + err = c.Get(ctx, client.ObjectKey{Name: "default"}, ns) |
| 55 | + Expect(err).NotTo(HaveOccurred()) |
| 56 | + }) |
| 57 | +}) |
| 58 | + |
| 59 | +// TODO: get envtest in place with kcp |
| 60 | +/* |
| 61 | +var _ = Describe("KCP cluster-aware informer cache", func() { |
| 62 | + It("should be able to get the default namespace with kcp.io/cluster annotation", func() { |
| 63 | + ctx, cancel := context.WithCancel(context.Background()) |
| 64 | + defer cancel() |
| 65 | +
|
| 66 | + c, err := kcp.NewClusterAwareCache(cfg, cache.Options{}) |
| 67 | + Expect(err).NotTo(HaveOccurred()) |
| 68 | +
|
| 69 | + By("Annotating the default namespace with kcp.io/cluster") |
| 70 | + cl, err := client.New(cfg, client.Options{}) |
| 71 | + Expect(err).NotTo(HaveOccurred()) |
| 72 | + ns := &corev1.Namespace{} |
| 73 | + err = cl.Get(ctx, client.ObjectKey{Name: "default"}, ns) |
| 74 | + Expect(err).NotTo(HaveOccurred()) |
| 75 | + ns.Annotations = map[string]string{"kcp.io/cluster": "cluster1"} |
| 76 | + err = cl.Update(ctx, ns) |
| 77 | + Expect(err).NotTo(HaveOccurred()) |
| 78 | +
|
| 79 | + go c.Start(ctx) //nolint:errcheck // Start is blocking, and error not relevant here. |
| 80 | + c.WaitForCacheSync(ctx) |
| 81 | +
|
| 82 | + By("By getting the default namespace with the informer, but cluster-less key should fail") |
| 83 | + err = c.Get(ctx, client.ObjectKey{Name: "default"}, ns) |
| 84 | + Expect(err).To(HaveOccurred()) |
| 85 | +
|
| 86 | + By("By getting the default namespace with the informer, but cluster-aware key should succeed") |
| 87 | + err = c.Get(kontext.WithCluster(ctx, "cluster1"), client.ObjectKey{Name: "default", Namespace: "cluster1"}, ns) |
| 88 | + }) |
| 89 | +}) |
| 90 | +*/ |
0 commit comments