|
| 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 | +}) |
0 commit comments