|
| 1 | +# @interweb/kubernetes-test |
| 2 | + |
| 3 | +Kubernetes testing utilities with wait-for-pods and other helpers for integration tests. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @interweb/kubernetes-test |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Wait for Pods |
| 14 | + |
| 15 | +The `waitForPods` function allows you to wait for pods to become ready in a Kubernetes cluster. |
| 16 | + |
| 17 | +```typescript |
| 18 | +import { KubernetesClient } from 'kubernetesjs'; |
| 19 | +import { waitForPods, waitForPodByName, waitForPodsWithLabel } from '@interweb/kubernetes-test'; |
| 20 | + |
| 21 | +// Create a Kubernetes client |
| 22 | +const client = new KubernetesClient({ |
| 23 | + restEndpoint: 'http://localhost:8001', // kubectl proxy endpoint |
| 24 | +}); |
| 25 | + |
| 26 | +// Wait for all pods with a specific label to be ready |
| 27 | +const result = await waitForPods(client, { |
| 28 | + namespace: 'default', |
| 29 | + labelSelector: 'app=my-app', |
| 30 | + timeoutMs: 60000, // 1 minute timeout |
| 31 | + pollIntervalMs: 2000, // Check every 2 seconds |
| 32 | + onProgress: (progress) => { |
| 33 | + console.log(`Ready: ${progress.ready}/${progress.total}`); |
| 34 | + }, |
| 35 | +}); |
| 36 | + |
| 37 | +console.log(result.message); // "3/3 pods are ready" |
| 38 | + |
| 39 | +// Wait for a specific pod by name |
| 40 | +const podResult = await waitForPodByName(client, 'my-pod-name', { |
| 41 | + namespace: 'default', |
| 42 | + timeoutMs: 30000, |
| 43 | +}); |
| 44 | + |
| 45 | +// Wait for pods with a specific label |
| 46 | +const labelResult = await waitForPodsWithLabel(client, 'app', 'nginx', { |
| 47 | + namespace: 'production', |
| 48 | + minReady: 2, // Wait for at least 2 pods to be ready |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +### Options |
| 53 | + |
| 54 | +#### `WaitForPodsOptions` |
| 55 | + |
| 56 | +| Option | Type | Default | Description | |
| 57 | +|--------|------|---------|-------------| |
| 58 | +| `namespace` | `string` | `'default'` | Kubernetes namespace to query | |
| 59 | +| `labelSelector` | `string` | - | Label selector to filter pods (e.g., `'app=nginx'`) | |
| 60 | +| `fieldSelector` | `string` | - | Field selector to filter pods | |
| 61 | +| `timeoutMs` | `number` | `300000` | Maximum time to wait (5 minutes) | |
| 62 | +| `pollIntervalMs` | `number` | `2000` | Interval between status checks | |
| 63 | +| `minReady` | `number` | - | Minimum number of ready pods required | |
| 64 | +| `allReady` | `boolean` | `true` | Wait for all pods to be ready | |
| 65 | +| `onProgress` | `function` | - | Callback for progress updates | |
| 66 | + |
| 67 | +### Error Handling |
| 68 | + |
| 69 | +The library provides specific error types for different failure scenarios: |
| 70 | + |
| 71 | +```typescript |
| 72 | +import { |
| 73 | + waitForPods, |
| 74 | + WaitForPodsTimeoutError, |
| 75 | + WaitForPodsError |
| 76 | +} from '@interweb/kubernetes-test'; |
| 77 | + |
| 78 | +try { |
| 79 | + await waitForPods(client, { labelSelector: 'app=my-app' }); |
| 80 | +} catch (error) { |
| 81 | + if (error instanceof WaitForPodsTimeoutError) { |
| 82 | + console.log('Timeout! Progress:', error.progress); |
| 83 | + } else if (error instanceof WaitForPodsError) { |
| 84 | + console.log('Error:', error.message); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## License |
| 90 | + |
| 91 | +MIT |
0 commit comments