--- description: > How to create a new API and use it with tenancy. --- # Quickstart: Tenancy and APIs ## Prerequisites A running kcp server. The [quickstart](../setup/quickstart.md) is a good starting point. ## Set your KUBECONFIG To access a workspace, you need credentials and an Kubernetes API server URL for the workspace, both of which are stored in a `kubeconfig` file. The default context in the kcp-provided `admin.kubeconfig` gives access to the `root` workspace as the `kcp-admin` user. ```sh export KUBECONFIG=.kcp/admin.kubeconfig kubectl config get-contexts ``` Output should look similar to below (depending on the kcp instance / method of kubeconfig generation): ```console CURRENT NAME CLUSTER AUTHINFO NAMESPACE base base kcp-admin * root root kcp-admin system:admin base shard-admin ``` You can use API discovery to see what resources are available in this `root` workspace. We're here to explore the `tenancy.kcp.io` and `apis.kcp.io` resources. ```sh kubectl api-resources ``` Output should include some core resources and the kcp-specific ones for tenancy and API management: ```console NAME SHORTNAMES APIVERSION NAMESPACED KIND configmaps cm v1 true ConfigMap events ev v1 true Event ... apibindings apis.kcp.io/v1alpha2 false APIBinding apiexports apis.kcp.io/v1alpha2 false APIExport ... workspaces ws tenancy.kcp.io/v1alpha1 false Workspace ``` ## Create and Navigate Workspaces !!! tip Refer to the [plugins page](../setup/kubectl-plugin.md) to install the kubectl plugins for kcp. The `ws` plugin for `kubectl` makes it easy to switch your `kubeconfig` between workspaces and use `create-workspace` plugin for `kubectl` to create new ones: ```sh kubectl ws . ``` Output should show that you are currently in the `root` workspace: ```console Current workspace is "root". ``` Next, let's create a new workspace in `root` and immediately switch to it (via `--enter`): ```sh kubectl create-workspace a --enter ``` This will create the new workspace `a` and change the current workspace to it: ```console Workspace "a" (type root:organization) created. Waiting for it to be ready... Workspace "a" (type root:organization) is ready to use. Current workspace is "root:a". ``` Let's create another workspace but not change into it: ```sh kubectl create-workspace b ``` Output will look similar to the previous workspace creation: ```console Workspace "b" (type root:universal) created. Waiting for it to be ready... Workspace "b" (type root:universal) is ready to use. ``` Since there was no switch to the newly created workspace, you are still in `root:a` and can look at child workspaces here: ```sh kubectl get workspaces ``` Output should include the new `b` workspace: ```console NAME TYPE PHASE URL b universal Ready https://myhost:6443/clusters/root:a:b ``` Here is a quick collection of commands showing the navigation between the workspaces you've just created. Note the usage of `..` to switch to the parent workspace and `-` to the previously selected workspace. ```console $ kubectl ws b Current workspace is "root:a:b". $ kubectl ws .. Current workspace is "root:a". $ kubectl ws - Current workspace is "root:a:b". $ kubectl ws :root Current workspace is "root". ``` !!! tip Workspace paths in kcp are separated by colons, not slashes. As such, absolute paths need to start with `:` (such as `:root` to navigate to the top-level root workspace as seen in the example above). Our `kubeconfig` now contains two additional contexts, one which represents the current workspace, and the other to keep track of our most recently used workspace. This highlights that the `kubectl ws` plugin is primarily a convenience wrapper for managing a `kubeconfig` that can be used for working within a workspace. ```sh kubectl config get-contexts ``` ```console CURRENT NAME CLUSTER AUTHINFO NAMESPACE base base kcp-admin root root kcp-admin system:admin base shard-admin * workspace.kcp.io/current workspace.kcp.io/current kcp-admin workspace.kcp.io/previous workspace.kcp.io/previous kcp-admin ``` ## Understand Workspace Types As we can see above, workspaces can contain sub-workspaces, and workspaces have different types. A workspace type defines which sub-workspace types can be created under such workspaces. So, for example: - A `universal` workspace is the base workspace type that most other workspace types inherit from - they may contain other universal workspaces, and they have a "default" namespace - The `root` workspace primarily contains organization workspaces - An `organization` workspace can contain universal workspaces, or can be further subdivided using team workspaces Workspace types and their behaviors are defined using the `WorkspaceType` resource: ```shell kubectl get workspacetypes ``` Output of this depends on the kcp instance, but by default the list should look like this: ```console NAME AGE home 74m organization 74m root 74m team 74m universal 74m ``` Describing a `WorkspaceType` will yield some information about its configuration: ```console $ kubectl describe workspacetype/team Name: team ... API Version: tenancy.kcp.io/v1alpha1 Kind: WorkspaceType ... Spec: Default Child Workspace Type: Name: universal Path: root Extend: With: Name: universal Path: root Limit Allowed Parents: Types: Name: organization Path: root Name: team Path: root Status: Conditions: Status: True Type: Ready ... ``` ## Publish Some APIs as a Service Provider kcp offers `APIExport` and `APIBinding` resources which allow a service provider operating in one workspace to offer its capabilities to service consumers in other workspaces. First we'll create an organization workspace in `root`, and then within that create a service provider workspace. ```sh kubectl create-workspace wildwest --enter ``` ```console Workspace "wildwest" (type root:organization) created. Waiting for it to be ready... Workspace "wildwest" (type root:organization) is ready to use. Current workspace is 'root:wildwest' (type root:organization). ``` ```sh kubectl create-workspace cowboys-service --enter ``` ```console Workspace "cowboys-service" (type root:universal) created. Waiting for it to be ready... Workspace "cowboys-service" (type root:universal) is ready to use. Current workspace is 'root:wildwest:cowboys-service' (type root:universal). ``` !!! tip The `apigen` tool used below can be found on the [release page](https://github.com/kcp-dev/kcp/releases/latest) Then we'll use a CRD to generate an `APIResourceSchema` and `APIExport` and apply these within the service provider workspace. For this example, we are using a CRD from kcp's own end-to-end testing. You can find it [here](https://github.com/kcp-dev/kcp/tree/main/test/e2e/customresourcedefinition). One of the options to follow along is to clone the kcp repository. ```sh mkdir wildwest-schemas/ ./bin/apigen --input-dir test/e2e/customresourcedefinition/ --output-dir wildwest-schemas/ ls -1 wildwest-schemas/ ``` `apigen` should have generated two files, one containing an `APIResourceSchema` and the other containing an `APIExport`: ```console apiexport-wildwest.dev.yaml apiresourceschema-cowboys.wildwest.dev.yaml ``` We can now apply both of them: ```sh kubectl apply -f wildwest-schemas/ ``` ```console apiresourceschema.apis.kcp.io/v220920-6039d110.cowboys.wildwest.dev created apiexport.apis.kcp.io/wildwest.dev created ``` You can think of an `APIResourceSchema` as being equivalent to a CRD, and an `APIExport` makes a set of schemas available to consumers. ## Use Those APIs as a Service Consumer Now we can adopt the service consumer persona and create a workspace from which we will use this new `APIExport`. Let's start by creating a new workspace under `root` and switch to it: ```sh kubectl ws :root ``` ```console Current workspace is "root". ``` ```sh kubectl create-workspace test-consumer --enter ``` ```console Workspace "test-consumer" (type root:organization) created. Waiting for it to be ready... Workspace "test-consumer" (type root:organization) is ready to use. Current workspace is 'root:test-consumer' (type root:organization). ``` Now create an `APIBinding` that references the previously created `APIExport`: ```sh kubectl apply -f - <