@kubernetesjs/react 0.7.1 β 0.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +236 -49
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kubernetesjs/react
|
|
2
2
|
|
|
3
3
|
<p align="center" width="100%">
|
|
4
4
|
<img src="https://github.com/hyperweb-io/interweb-utils/assets/545047/89c743c4-be88-409f-9a77-4b02cd7fe9a4" width="80">
|
|
5
5
|
<br/>
|
|
6
|
-
|
|
6
|
+
React Hooks for Kubernetes
|
|
7
7
|
<br />
|
|
8
8
|
<a href="https://github.com/hyperweb-io/kubernetesjs/actions/workflows/ci.yml">
|
|
9
9
|
<img height="20" src="https://github.com/hyperweb-io/kubernetesjs/actions/workflows/ci.yml/badge.svg"/>
|
|
@@ -13,75 +13,262 @@
|
|
|
13
13
|
</a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
|
+
**@kubernetesjs/react** provides **fully-typed React hooks** for the Kubernetes API, powered by [TanStack Query](https://tanstack.com/query) for intelligent caching, background refetching, and optimistic updates.
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
Build reactive Kubernetes dashboards, operators, and tools with the same patterns you use for your React appsβdeclarative, composable, and type-safe.
|
|
19
|
+
|
|
20
|
+
> Real-time Kubernetes state management for React applications. No more polling loops or manual refreshes.
|
|
18
21
|
|
|
19
22
|
## Features
|
|
20
23
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
24
|
+
- **βοΈ React Hooks**: Ready-to-use hooks for all Kubernetes API operations
|
|
25
|
+
- **π Smart Caching**: Powered by TanStack Query for intelligent data synchronization
|
|
26
|
+
- **π Fully Typed**: Complete TypeScript support with IntelliSense for all Kubernetes resources
|
|
27
|
+
- **π‘ Real-time Updates**: Automatic background refetching keeps your UI in sync
|
|
28
|
+
- **π― Optimistic Updates**: Instant UI feedback for mutations with automatic rollback on errors
|
|
29
|
+
- **π Zero Configuration**: Works out of the box with sensible defaults
|
|
25
30
|
|
|
26
31
|
## Installation
|
|
27
32
|
|
|
28
|
-
To install KubernetesJS, you can use npm or yarn:
|
|
29
|
-
|
|
30
33
|
```bash
|
|
31
|
-
npm install kubernetesjs
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
npm install @kubernetesjs/react
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
34
38
|
|
|
39
|
+
### 1. Wrap your app with KubernetesProvider
|
|
40
|
+
|
|
41
|
+
```tsx
|
|
42
|
+
import { KubernetesProvider } from '@kubernetesjs/react';
|
|
43
|
+
|
|
44
|
+
function App() {
|
|
45
|
+
return (
|
|
46
|
+
<KubernetesProvider
|
|
47
|
+
initialConfig={{
|
|
48
|
+
restEndpoint: process.env.REACT_APP_K8S_API_URL || 'http://127.0.0.1:8001',
|
|
49
|
+
headers: {
|
|
50
|
+
'Authorization': `Bearer ${process.env.REACT_APP_K8S_TOKEN}`
|
|
51
|
+
}
|
|
52
|
+
}}
|
|
53
|
+
>
|
|
54
|
+
<YourApp />
|
|
55
|
+
</KubernetesProvider>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
35
58
|
```
|
|
36
59
|
|
|
37
|
-
|
|
60
|
+
### 2. Use hooks in your components
|
|
38
61
|
|
|
39
|
-
```
|
|
40
|
-
import {
|
|
62
|
+
```tsx
|
|
63
|
+
import { useListCoreV1NamespacedPodQuery } from '@kubernetesjs/react';
|
|
41
64
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
65
|
+
function PodList({ namespace }: { namespace: string }) {
|
|
66
|
+
const { data, isLoading, error } = useListCoreV1NamespacedPodQuery({
|
|
67
|
+
path: { namespace }
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (isLoading) return <div>Loading pods...</div>;
|
|
71
|
+
if (error) return <div>Error: {error.message}</div>;
|
|
45
72
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
73
|
+
return (
|
|
74
|
+
<ul>
|
|
75
|
+
{data?.items?.map(pod => (
|
|
76
|
+
<li key={pod.metadata?.uid}>
|
|
77
|
+
{pod.metadata?.name} - {pod.status?.phase}
|
|
78
|
+
</li>
|
|
79
|
+
))}
|
|
80
|
+
</ul>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
49
84
|
|
|
50
|
-
|
|
51
|
-
result.items.forEach(item => {
|
|
52
|
-
console.log('NODE:', item.spec.nodeName);
|
|
85
|
+
## Usage Examples
|
|
53
86
|
|
|
54
|
-
|
|
55
|
-
image: ic.image,
|
|
56
|
-
name: ic.name,
|
|
57
|
-
ready: ic.ready,
|
|
58
|
-
state: ic.state
|
|
59
|
-
}));
|
|
87
|
+
### Listing Resources
|
|
60
88
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
name: c.name,
|
|
64
|
-
ready: c.ready,
|
|
65
|
-
state: c.state
|
|
66
|
-
}));
|
|
89
|
+
```tsx
|
|
90
|
+
import { useListAppsV1NamespacedDeploymentQuery } from '@kubernetesjs/react';
|
|
67
91
|
|
|
68
|
-
|
|
69
|
-
|
|
92
|
+
function DeploymentDashboard() {
|
|
93
|
+
const { data: deployments } = useListAppsV1NamespacedDeploymentQuery({
|
|
94
|
+
path: { namespace: 'default' }
|
|
70
95
|
});
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<div>
|
|
99
|
+
{deployments?.items?.map(deployment => (
|
|
100
|
+
<DeploymentCard
|
|
101
|
+
key={deployment.metadata?.uid}
|
|
102
|
+
deployment={deployment}
|
|
103
|
+
/>
|
|
104
|
+
))}
|
|
105
|
+
</div>
|
|
106
|
+
);
|
|
71
107
|
}
|
|
72
108
|
```
|
|
73
109
|
|
|
74
|
-
|
|
110
|
+
### Creating Resources
|
|
111
|
+
|
|
112
|
+
```tsx
|
|
113
|
+
import { useCreateCoreV1NamespacedConfigMap } from '@kubernetesjs/react';
|
|
114
|
+
|
|
115
|
+
function CreateConfigMapForm() {
|
|
116
|
+
const createConfigMap = useCreateCoreV1NamespacedConfigMap();
|
|
117
|
+
|
|
118
|
+
const handleSubmit = async (data: FormData) => {
|
|
119
|
+
try {
|
|
120
|
+
await createConfigMap.mutateAsync({
|
|
121
|
+
path: { namespace: 'default' },
|
|
122
|
+
body: {
|
|
123
|
+
apiVersion: 'v1',
|
|
124
|
+
kind: 'ConfigMap',
|
|
125
|
+
metadata: { name: data.name },
|
|
126
|
+
data: data.configData
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
toast.success('ConfigMap created!');
|
|
130
|
+
} catch (error) {
|
|
131
|
+
toast.error(`Failed: ${error.message}`);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<form onSubmit={handleSubmit}>
|
|
137
|
+
{/* form fields */}
|
|
138
|
+
<button
|
|
139
|
+
type="submit"
|
|
140
|
+
disabled={createConfigMap.isPending}
|
|
141
|
+
>
|
|
142
|
+
{createConfigMap.isPending ? 'Creating...' : 'Create ConfigMap'}
|
|
143
|
+
</button>
|
|
144
|
+
</form>
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Updating Resources
|
|
150
|
+
|
|
151
|
+
```tsx
|
|
152
|
+
import {
|
|
153
|
+
useReadAppsV1NamespacedDeploymentQuery,
|
|
154
|
+
useReplaceAppsV1NamespacedDeployment
|
|
155
|
+
} from '@kubernetesjs/react';
|
|
156
|
+
|
|
157
|
+
function ScaleDeployment({ namespace, name }: Props) {
|
|
158
|
+
const { data: deployment } = useReadAppsV1NamespacedDeploymentQuery({
|
|
159
|
+
path: { namespace, name }
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const updateDeployment = useReplaceAppsV1NamespacedDeployment();
|
|
163
|
+
|
|
164
|
+
const handleScale = async (replicas: number) => {
|
|
165
|
+
if (!deployment) return;
|
|
166
|
+
|
|
167
|
+
await updateDeployment.mutateAsync({
|
|
168
|
+
path: { namespace, name },
|
|
169
|
+
body: {
|
|
170
|
+
...deployment,
|
|
171
|
+
spec: {
|
|
172
|
+
...deployment.spec,
|
|
173
|
+
replicas
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
return (
|
|
180
|
+
<div>
|
|
181
|
+
<h3>Current replicas: {deployment?.spec?.replicas || 0}</h3>
|
|
182
|
+
<button onClick={() => handleScale(3)}>Scale to 3</button>
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Deleting Resources
|
|
189
|
+
|
|
190
|
+
```tsx
|
|
191
|
+
import { useDeleteCoreV1NamespacedPod } from '@kubernetesjs/react';
|
|
192
|
+
|
|
193
|
+
function PodActions({ namespace, name }: Props) {
|
|
194
|
+
const deletePod = useDeleteCoreV1NamespacedPod();
|
|
195
|
+
|
|
196
|
+
const handleDelete = async () => {
|
|
197
|
+
if (confirm('Delete this pod?')) {
|
|
198
|
+
await deletePod.mutateAsync({
|
|
199
|
+
path: { namespace, name }
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<button
|
|
206
|
+
onClick={handleDelete}
|
|
207
|
+
disabled={deletePod.isPending}
|
|
208
|
+
>
|
|
209
|
+
{deletePod.isPending ? 'Deleting...' : 'Delete Pod'}
|
|
210
|
+
</button>
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Real-time Monitoring
|
|
216
|
+
|
|
217
|
+
```tsx
|
|
218
|
+
import { useListCoreV1EventForAllNamespacesQuery } from '@kubernetesjs/react';
|
|
219
|
+
|
|
220
|
+
function EventStream() {
|
|
221
|
+
const { data: events } = useListCoreV1EventForAllNamespacesQuery(
|
|
222
|
+
{},
|
|
223
|
+
{
|
|
224
|
+
refetchInterval: 5000, // Poll every 5 seconds
|
|
225
|
+
refetchIntervalInBackground: true
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
return (
|
|
230
|
+
<div className="event-stream">
|
|
231
|
+
{events?.items?.slice(0, 50).map(event => (
|
|
232
|
+
<EventCard key={event.metadata?.uid} event={event} />
|
|
233
|
+
))}
|
|
234
|
+
</div>
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
```
|
|
75
238
|
|
|
76
|
-
|
|
239
|
+
## API Reference
|
|
240
|
+
|
|
241
|
+
### Provider
|
|
242
|
+
|
|
243
|
+
```tsx
|
|
244
|
+
<KubernetesProvider initialConfig={{ restEndpoint, headers }}>
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Hooks
|
|
248
|
+
|
|
249
|
+
All Kubernetes API operations are available as hooks following this pattern:
|
|
250
|
+
|
|
251
|
+
- **Queries** (GET operations): `use{Operation}Query`
|
|
252
|
+
- **Mutations** (POST/PUT/PATCH/DELETE): `use{Operation}`
|
|
253
|
+
|
|
254
|
+
Examples:
|
|
255
|
+
- `useListCoreV1NamespacedPodQuery` - List pods in a namespace
|
|
256
|
+
- `useCreateAppsV1NamespacedDeployment` - Create a deployment
|
|
257
|
+
- `useDeleteCoreV1NamespacedService` - Delete a service
|
|
258
|
+
- `usePatchCoreV1NamespacedConfigMap` - Patch a ConfigMap
|
|
259
|
+
|
|
260
|
+
### Context Hook
|
|
261
|
+
|
|
262
|
+
```tsx
|
|
263
|
+
const { client, config, updateConfig } = useKubernetes();
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Access the underlying KubernetesClient instance and configuration.
|
|
267
|
+
|
|
268
|
+
## Related
|
|
77
269
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
* [`@schema-typescript/cli`](https://github.com/hyperweb-io/schema-typescript/tree/main/packages/cli)
|
|
81
|
-
CLI is the command line utility for `schema-typescript`.
|
|
82
|
-
* [`schema-sdk`](https://github.com/hyperweb-io/schema-typescript/tree/main/packages/schema-sdk)
|
|
83
|
-
Provides robust tools for handling OpenAPI schemas and converting them to TypeScript clients with ease and efficiency.
|
|
84
|
-
* [`starship`](https://github.com/hyperweb-io/starship) Unified Testing and Development for the Interchain.
|
|
270
|
+
- [`kubernetesjs`](https://github.com/hyperweb-io/kubernetesjs/tree/main/packages/kubernetesjs) - The core TypeScript client for Kubernetes
|
|
271
|
+
- [`@tanstack/react-query`](https://tanstack.com/query) - The powerful async state management library powering our hooks
|
|
85
272
|
|
|
86
273
|
## Credits
|
|
87
274
|
|
|
@@ -89,6 +276,6 @@ Checkout these related projects:
|
|
|
89
276
|
|
|
90
277
|
## Disclaimer
|
|
91
278
|
|
|
92
|
-
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED
|
|
279
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
93
280
|
|
|
94
|
-
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
281
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubernetesjs/react",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
5
|
"description": "Fully Typed Kubernetes React Hooks",
|
|
6
6
|
"keywords": [
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
53
|
"@tanstack/react-query": "5.79.2",
|
|
54
|
-
"kubernetesjs": "^0.7.
|
|
54
|
+
"kubernetesjs": "^0.7.2",
|
|
55
55
|
"react": "^18.2.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"@tanstack/react-query": "5.79.2",
|
|
64
64
|
"react": "^18.2.0"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "bd0eb9c325a9e3f18687882294b4eaba0c1c5b93"
|
|
67
67
|
}
|