|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2026 The KCP Authors. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package cacheserver |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/go-logr/logr" |
| 28 | + "github.com/kcp-dev/logicalcluster/v3" |
| 29 | + |
| 30 | + corev1 "k8s.io/api/core/v1" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + ctrlruntime "sigs.k8s.io/controller-runtime" |
| 34 | + |
| 35 | + operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1" |
| 36 | + "github.com/kcp-dev/kcp-operator/test/utils" |
| 37 | +) |
| 38 | + |
| 39 | +func TestCacheWithRootShard(t *testing.T) { |
| 40 | + ctrlruntime.SetLogger(logr.Discard()) |
| 41 | + |
| 42 | + client := utils.GetKubeClient(t) |
| 43 | + ctx := context.Background() |
| 44 | + |
| 45 | + // create namespace |
| 46 | + namespace := utils.CreateSelfDestructingNamespace(t, ctx, client, "cache-rootshard") |
| 47 | + |
| 48 | + // deploy the cache server |
| 49 | + cacheServer := utils.DeployCacheServer(ctx, t, client, namespace.Name) |
| 50 | + |
| 51 | + // deploy a root shard that uses our cache |
| 52 | + rootShard := utils.DeployRootShard(ctx, t, client, namespace.Name, "example.localhost", func(rs *operatorv1alpha1.RootShard) { |
| 53 | + rs.Spec.Cache.Reference = &corev1.LocalObjectReference{ |
| 54 | + Name: cacheServer.Name, |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + // create a kubeconfig to access the root shard |
| 59 | + configSecretName := fmt.Sprintf("%s-shard-kubeconfig", rootShard.Name) |
| 60 | + |
| 61 | + rsConfig := operatorv1alpha1.Kubeconfig{} |
| 62 | + rsConfig.Name = configSecretName |
| 63 | + rsConfig.Namespace = namespace.Name |
| 64 | + |
| 65 | + rsConfig.Spec = operatorv1alpha1.KubeconfigSpec{ |
| 66 | + Target: operatorv1alpha1.KubeconfigTarget{ |
| 67 | + RootShardRef: &corev1.LocalObjectReference{ |
| 68 | + Name: rootShard.Name, |
| 69 | + }, |
| 70 | + }, |
| 71 | + Username: "e2e", |
| 72 | + Validity: metav1.Duration{Duration: 2 * time.Hour}, |
| 73 | + SecretRef: corev1.LocalObjectReference{ |
| 74 | + Name: configSecretName, |
| 75 | + }, |
| 76 | + Groups: []string{"system:masters"}, |
| 77 | + } |
| 78 | + |
| 79 | + t.Log("Creating kubeconfig for RootShard...") |
| 80 | + if err := client.Create(ctx, &rsConfig); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + utils.WaitForObject(t, ctx, client, &corev1.Secret{}, types.NamespacedName{Namespace: rsConfig.Namespace, Name: rsConfig.Spec.SecretRef.Name}) |
| 84 | + |
| 85 | + t.Log("Connecting to RootShard...") |
| 86 | + rootShardClient := utils.ConnectWithKubeconfig(t, ctx, client, namespace.Name, rsConfig.Name, logicalcluster.NewPath("root")) |
| 87 | + |
| 88 | + // proof of life: list something every logicalcluster in kcp has |
| 89 | + t.Log("Should be able to list Secrets.") |
| 90 | + secrets := &corev1.SecretList{} |
| 91 | + if err := rootShardClient.List(ctx, secrets); err != nil { |
| 92 | + t.Fatalf("Failed to list secrets in kcp: %v", err) |
| 93 | + } |
| 94 | +} |
0 commit comments