/* Copyright 2022 The kcp Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package workspace import ( "fmt" "strings" "testing" "time" "github.com/stretchr/testify/require" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" "k8s.io/client-go/discovery/cached/memory" "k8s.io/client-go/restmapper" kcpdynamic "github.com/kcp-dev/client-go/dynamic" apisv1alpha2 "github.com/kcp-dev/sdk/apis/apis/v1alpha2" "github.com/kcp-dev/sdk/apis/core" tenancyv1alpha1 "github.com/kcp-dev/sdk/apis/tenancy/v1alpha1" kcpclientset "github.com/kcp-dev/sdk/client/clientset/versioned/cluster" kcptesting "github.com/kcp-dev/sdk/testing" kcptestinghelpers "github.com/kcp-dev/sdk/testing/helpers" "github.com/kcp-dev/kcp/config/helpers" "github.com/kcp-dev/kcp/test/e2e/framework" ) func TestWorkspaceTypesAPIBindingInitialization(t *testing.T) { t.Parallel() framework.Suite(t, "control-plane") server := kcptesting.SharedKcpServer(t) orgPath, _ := kcptesting.NewWorkspaceFixture(t, server, core.RootCluster.Path(), kcptesting.WithType(core.RootCluster.Path(), "organization")) cowboysProviderPath, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("cowboys-provider")) cfg := server.BaseConfig(t) kcpClusterClient, err := kcpclientset.NewForConfig(cfg) require.NoError(t, err, "error creating kcp cluster client") dynamicClusterClient, err := kcpdynamic.NewForConfig(cfg) require.NoError(t, err, "error creating dynamic cluster client") cowboysProviderKCPClient, err := kcpclientset.NewForConfig(cfg) require.NoError(t, err, "error creating cowboys provider kcp client") t.Logf("Install a cowboys APIResourceSchema into workspace %q", cowboysProviderPath) mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(cowboysProviderKCPClient.Cluster(cowboysProviderPath).Discovery())) err = helpers.CreateResourceFromFS(t.Context(), dynamicClusterClient.Cluster(cowboysProviderPath), mapper, nil, "apiresourceschema_cowboys.yaml", testFiles) require.NoError(t, err) // Create rolebinding to allow bind to APIExport of coboys provider. Else nobody can bind to it. err = helpers.CreateResourceFromFS(t.Context(), dynamicClusterClient.Cluster(cowboysProviderPath), mapper, nil, "clusterrole_cowboys.yaml", testFiles) require.NoError(t, err) err = helpers.CreateResourceFromFS(t.Context(), dynamicClusterClient.Cluster(cowboysProviderPath), mapper, nil, "clusterrolebinding_cowboys.yaml", testFiles) require.NoError(t, err) t.Logf("Create today-cowboys APIExport") cowboysAPIExport := &apisv1alpha2.APIExport{ ObjectMeta: metav1.ObjectMeta{ Name: "today-cowboys", }, Spec: apisv1alpha2.APIExportSpec{ Resources: []apisv1alpha2.ResourceSchema{ { Name: "cowboys", Group: "wildwest.dev", Schema: "today.cowboys.wildwest.dev", Storage: apisv1alpha2.ResourceSchemaStorage{ CRD: &apisv1alpha2.ResourceSchemaStorageCRD{}, }, }, }, PermissionClaims: []apisv1alpha2.PermissionClaim{ { GroupResource: apisv1alpha2.GroupResource{ Resource: "configmaps", }, Verbs: []string{"*"}, }, }, }, } cowboysAPIExport, err = kcpClusterClient.Cluster(cowboysProviderPath).ApisV1alpha2().APIExports().Create(t.Context(), cowboysAPIExport, metav1.CreateOptions{}) require.NoError(t, err, "error creating APIExport") universalPath, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath, kcptesting.WithName("universal")) wtParent1 := &tenancyv1alpha1.WorkspaceType{ ObjectMeta: metav1.ObjectMeta{ Name: "parent1", }, Spec: tenancyv1alpha1.WorkspaceTypeSpec{ DefaultAPIBindings: []tenancyv1alpha1.APIExportReference{ { Path: cowboysProviderPath.String(), Export: cowboysAPIExport.Name, }, }, }, } wtParent2 := &tenancyv1alpha1.WorkspaceType{ ObjectMeta: metav1.ObjectMeta{ Name: "parent2", }, Spec: tenancyv1alpha1.WorkspaceTypeSpec{ DefaultAPIBindings: []tenancyv1alpha1.APIExportReference{ { Path: "root", Export: "topology.kcp.io", }, }, }, } wt := &tenancyv1alpha1.WorkspaceType{ ObjectMeta: metav1.ObjectMeta{ Name: "test", }, Spec: tenancyv1alpha1.WorkspaceTypeSpec{ DefaultAPIBindings: []tenancyv1alpha1.APIExportReference{ { Path: "root", Export: "tenancy.kcp.io", }, }, Extend: tenancyv1alpha1.WorkspaceTypeExtension{ With: []tenancyv1alpha1.WorkspaceTypeReference{ { Name: "parent1", Path: universalPath.String(), }, { Name: "parent2", Path: universalPath.String(), }, }, }, }, } t.Logf("Creating WorkspaceType parent1") // parent1 references an APIExport in cowboys-provider, which lives on a // different shard than universal. WorkspaceType admission resolves the // path via the cache-replicated LogicalCluster of cowboys-provider, and // that replication can lag by a beat. While the LC is invisible, // admission cannot evaluate bind and intentionally returns the same // "no permission to bind to export ..." error as a real bind denial — // distinguishing the two would let WST create permission be used to // probe for the existence of arbitrary workspaces. Retry on that // message. The test grants bind via clusterrolebinding_cowboys.yaml, // so once the LC is visible the create succeeds; a real bind regression // will still surface when the Eventually deadline expires. kcptestinghelpers.Eventually(t, func() (bool, string) { _, err := kcpClusterClient.Cluster(universalPath).TenancyV1alpha1().WorkspaceTypes().Create(t.Context(), wtParent1, metav1.CreateOptions{}) if err == nil { return true, "" } if strings.Contains(err.Error(), "no permission to bind to export") { return false, fmt.Sprintf("waiting for cross-shard LogicalCluster visibility (or bind permission): %v", err) } require.NoError(t, err, "error creating wt parent1") return true, "" }, wait.ForeverTestTimeout, 250*time.Millisecond, "expected wt parent1 create to succeed once cowboys-provider LogicalCluster is visible from universal's shard") t.Logf("Creating WorkspaceType parent2") _, err = kcpClusterClient.Cluster(universalPath).TenancyV1alpha1().WorkspaceTypes().Create(t.Context(), wtParent2, metav1.CreateOptions{}) require.NoError(t, err, "error creating wt parent2") t.Logf("Creating WorkspaceType test") _, err = kcpClusterClient.Cluster(universalPath).TenancyV1alpha1().WorkspaceTypes().Create(t.Context(), wt, metav1.CreateOptions{}) require.NoError(t, err, "error creating wt test") // This will create and wait for ready, which only happens if the APIBinding initialization is working correctly _, _ = kcptesting.NewWorkspaceFixture(t, server, universalPath, kcptesting.WithType(universalPath, "test"), kcptesting.WithName("init")) }