|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +/* |
| 4 | +Copyright 2025 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 rootshards |
| 20 | + |
| 21 | +import ( |
| 22 | + "context" |
| 23 | + "fmt" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/go-logr/logr" |
| 28 | + kcpcorev1alpha1 "github.com/kcp-dev/kcp/sdk/apis/core/v1alpha1" |
| 29 | + kcptenancyv1alpha1 "github.com/kcp-dev/kcp/sdk/apis/tenancy/v1alpha1" |
| 30 | + "github.com/kcp-dev/logicalcluster/v3" |
| 31 | + |
| 32 | + corev1 "k8s.io/api/core/v1" |
| 33 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + "k8s.io/apimachinery/pkg/types" |
| 35 | + "k8s.io/apimachinery/pkg/util/wait" |
| 36 | + ctrlruntime "sigs.k8s.io/controller-runtime" |
| 37 | + ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 38 | + |
| 39 | + operatorv1alpha1 "github.com/kcp-dev/kcp-operator/sdk/apis/operator/v1alpha1" |
| 40 | + "github.com/kcp-dev/kcp-operator/test/utils" |
| 41 | +) |
| 42 | + |
| 43 | +func TestRootShardProxy(t *testing.T) { |
| 44 | + ctrlruntime.SetLogger(logr.Discard()) |
| 45 | + |
| 46 | + client := utils.GetKubeClient(t) |
| 47 | + ctx := context.Background() |
| 48 | + namespaceSuffix := "rootshard-proxy" |
| 49 | + |
| 50 | + namespace := utils.CreateSelfDestructingNamespace(t, ctx, client, namespaceSuffix) |
| 51 | + externalHostname := fmt.Sprintf("front-proxy-front-proxy.e2e-%s.svc.cluster.local", namespaceSuffix) |
| 52 | + |
| 53 | + // deploy a root shard incl. etcd |
| 54 | + rootShard := utils.DeployRootShard(ctx, t, client, namespace.Name, externalHostname) |
| 55 | + |
| 56 | + // deploy a 2nd shard incl. etcd |
| 57 | + shardName := "aadvark" |
| 58 | + utils.DeployShard(ctx, t, client, namespace.Name, shardName, rootShard.Name) |
| 59 | + |
| 60 | + // deploy front-proxy |
| 61 | + utils.DeployFrontProxy(ctx, t, client, namespace.Name, rootShard.Name, externalHostname) |
| 62 | + |
| 63 | + configSecretName := "kubeconfig" |
| 64 | + |
| 65 | + rsConfig := operatorv1alpha1.Kubeconfig{} |
| 66 | + rsConfig.Name = "test" |
| 67 | + rsConfig.Namespace = namespace.Name |
| 68 | + |
| 69 | + rsConfig.Spec = operatorv1alpha1.KubeconfigSpec{ |
| 70 | + Target: operatorv1alpha1.KubeconfigTarget{ |
| 71 | + RootShardRef: &corev1.LocalObjectReference{ |
| 72 | + Name: rootShard.Name, |
| 73 | + }, |
| 74 | + }, |
| 75 | + Username: "e2e", |
| 76 | + Validity: metav1.Duration{Duration: 2 * time.Hour}, |
| 77 | + SecretRef: corev1.LocalObjectReference{ |
| 78 | + Name: configSecretName, |
| 79 | + }, |
| 80 | + Groups: []string{"system:kcp:admin"}, |
| 81 | + } |
| 82 | + |
| 83 | + t.Log("Creating kubeconfig for RootShard…") |
| 84 | + if err := client.Create(ctx, &rsConfig); err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + utils.WaitForObject(t, ctx, client, &corev1.Secret{}, types.NamespacedName{Namespace: rsConfig.Namespace, Name: rsConfig.Spec.SecretRef.Name}) |
| 88 | + |
| 89 | + t.Log("Connecting to RootShard…") |
| 90 | + rootShardClient := utils.ConnectWithKubeconfig(t, ctx, client, namespace.Name, rsConfig.Name) |
| 91 | + |
| 92 | + // wait until the 2nd shard has registered itself successfully at the root shard |
| 93 | + shardKey := types.NamespacedName{Name: shardName} |
| 94 | + t.Log("Waiting for Shard to register itself on the RootShard…") |
| 95 | + utils.WaitForObject(t, ctx, rootShardClient, &kcpcorev1alpha1.Shard{}, shardKey) |
| 96 | + |
| 97 | + // create workspace that we want to have scheduled onto the 2nd shard |
| 98 | + t.Log("Creating workspace with its logicalcluster on the 2nd Shard…") |
| 99 | + workspace := &kcptenancyv1alpha1.Workspace{ |
| 100 | + ObjectMeta: metav1.ObjectMeta{ |
| 101 | + Name: "test", |
| 102 | + }, |
| 103 | + Spec: kcptenancyv1alpha1.WorkspaceSpec{ |
| 104 | + Type: kcptenancyv1alpha1.WorkspaceTypeReference{ |
| 105 | + Name: "universal", |
| 106 | + }, |
| 107 | + Location: &kcptenancyv1alpha1.WorkspaceLocation{ |
| 108 | + Selector: &metav1.LabelSelector{ |
| 109 | + MatchLabels: map[string]string{ |
| 110 | + "name": shardName, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + if err := rootShardClient.Create(ctx, workspace); err != nil { |
| 117 | + t.Fatalf("Failed to create workspace: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + err := wait.PollUntilContextTimeout(ctx, 500*time.Millisecond, 30*time.Second, false, func(ctx context.Context) (done bool, err error) { |
| 121 | + err = rootShardClient.Get(ctx, ctrlruntimeclient.ObjectKeyFromObject(workspace), workspace) |
| 122 | + if err != nil { |
| 123 | + return false, err |
| 124 | + } |
| 125 | + |
| 126 | + return workspace.Status.Phase == kcpcorev1alpha1.LogicalClusterPhaseReady, nil |
| 127 | + }) |
| 128 | + if err != nil { |
| 129 | + t.Fatalf("Failed to wait for workspace to become ready: %v", err) |
| 130 | + } |
| 131 | + |
| 132 | + // build a client through the proxy to the new workspace |
| 133 | + proxyClient := utils.ConnectWithRootShardProxy(t, ctx, client, &rootShard, logicalcluster.NewPath("root").Join(workspace.Name)) |
| 134 | + if err != nil { |
| 135 | + t.Fatalf("Failed to create root shard proxy client: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + // proof of life: list something every logicalcluster in kcp has |
| 139 | + t.Log("Should be able to list Secrets in the new workspace.") |
| 140 | + secrets := &corev1.SecretList{} |
| 141 | + if err := proxyClient.List(ctx, secrets); err != nil { |
| 142 | + t.Fatalf("Failed to list secrets in workspace: %v", err) |
| 143 | + } |
| 144 | +} |
0 commit comments