|
| 1 | +--- |
| 2 | +id: resources-api |
| 3 | +--- |
| 4 | + |
| 5 | +# Resources API (Experimental) |
| 6 | + |
| 7 | +> Note: This API is experimental and may change in future releases. Available from Rancher `2.15` and onwards |
| 8 | +
|
| 9 | +## What is the Resources API? |
| 10 | + |
| 11 | +The Resources API helps extension developers interact with Kubernetes and Rancher resources programmatically. This API provides a type-safe interface for common resource operations such as listing, retrieving, and filtering resources with full TypeScript support and autocomplete. |
| 12 | + |
| 13 | +The API is organized into two contexts: |
| 14 | + |
| 15 | +- **Cluster Context** (`cluster`) - For cluster-scoped Kubernetes resources (Pods, Deployments, Services, etc.) |
| 16 | +- **Management Context** (`mgmt`) - For global Rancher resources (Users, Clusters, Settings, etc.) |
| 17 | + |
| 18 | +Both contexts implement the same base `ResourcesApi` interface, so they share the same methods (`find`, `findFiltered`, `findAll`). |
| 19 | + |
| 20 | +## How to use the Resources API |
| 21 | + |
| 22 | +### Using Options API in Vue |
| 23 | + |
| 24 | +To use the Resources API in the context of the **Options API** of a Vue component, we globally expose the `$resources` property, which is available under the `this` object: |
| 25 | + |
| 26 | +```ts |
| 27 | +import { K8S } from '@shell/apis'; |
| 28 | + |
| 29 | +export default { |
| 30 | + async mounted() { |
| 31 | + // Cluster context |
| 32 | + const pods = await this.$resources.cluster.findFiltered(K8S.POD); |
| 33 | + |
| 34 | + // Management context |
| 35 | + const users = await this.$resources.mgmt.findFiltered(K8S.USER); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Using Composition API in Vue |
| 41 | + |
| 42 | +To use the Resources API in the context of the **Composition API** of a Vue component, we'll need to import the correct method to make the API available in the component: |
| 43 | + |
| 44 | +```ts |
| 45 | +import { useResources } from '@shell/apis'; |
| 46 | +``` |
| 47 | + |
| 48 | +then just assign to a constant in the context for your component and use it: |
| 49 | + |
| 50 | +```ts |
| 51 | +import { useResources, K8S } from '@shell/apis'; |
| 52 | + |
| 53 | +const resources = useResources(); |
| 54 | + |
| 55 | +// Cluster context - for cluster-scoped resources |
| 56 | +const pods = await resources.cluster.findFiltered(K8S.POD); |
| 57 | +const deployment = await resources.cluster.find(K8S.DEPLOYMENT, 'default/my-app'); |
| 58 | + |
| 59 | +// Management context - for global resources |
| 60 | +const users = await resources.mgmt.findFiltered(K8S.USER); |
| 61 | +const user = await resources.mgmt.find(K8S.USER, 'u-xyz789'); |
| 62 | +``` |
| 63 | + |
| 64 | +## Resource Constants |
| 65 | + |
| 66 | +The `K8S` object contains constants for common Kubernetes and Rancher resources: |
| 67 | + |
| 68 | +```ts |
| 69 | +import { K8S } from '@shell/apis'; |
| 70 | + |
| 71 | +// Core Kubernetes resources |
| 72 | +K8S.POD |
| 73 | +K8S.SERVICE |
| 74 | +K8S.CONFIG_MAP |
| 75 | +K8S.SECRET |
| 76 | +K8S.NAMESPACE |
| 77 | +K8S.NODE |
| 78 | + |
| 79 | +// Workload resources |
| 80 | +K8S.DEPLOYMENT |
| 81 | +K8S.STATEFUL_SET |
| 82 | +K8S.DAEMON_SET |
| 83 | + |
| 84 | +// Rancher Management |
| 85 | +K8S.USER |
| 86 | +K8S.PROJECT |
| 87 | +K8S.GLOBAL_ROLE |
| 88 | + |
| 89 | +// And many more... |
| 90 | +``` |
| 91 | + |
| 92 | +For custom resources or CRDs, you can use strings directly: |
| 93 | + |
| 94 | +```ts |
| 95 | +const customResources = await resources.cluster.findFiltered('mycompany.io.customresource'); |
| 96 | +``` |
| 97 | + |
| 98 | +## Available API's |
| 99 | + |
| 100 | +| API | Description | |
| 101 | +| :--- | :--- | |
| 102 | +| [Cluster API](./resources-api/interfaces/ClusterApi) | Interact with cluster-scoped Kubernetes resources (Pods, Deployments, Services, etc.) | |
| 103 | +| [Management API](./resources-api/interfaces/MgmtApi) | Interact with global Rancher resources (Users, Clusters, Settings, etc.) | |
0 commit comments