kubernetes-test 0.2.0
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/LICENSE +21 -0
- package/README.md +129 -0
- package/esm/index.js +2 -0
- package/esm/types.js +16 -0
- package/esm/wait-for-pods.js +132 -0
- package/index.d.ts +2 -0
- package/index.js +18 -0
- package/package.json +51 -0
- package/types.d.ts +59 -0
- package/types.js +21 -0
- package/wait-for-pods.d.ts +5 -0
- package/wait-for-pods.js +137 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# 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 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 '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 '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
|
|
92
|
+
|
|
93
|
+
---
|
|
94
|
+
|
|
95
|
+
## Development
|
|
96
|
+
|
|
97
|
+
### Setup
|
|
98
|
+
|
|
99
|
+
1. Clone the repository:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
git clone https://github.com/constructive-io/dev-utils.git
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
2. Install dependencies:
|
|
106
|
+
|
|
107
|
+
```bash
|
|
108
|
+
cd dev-utils
|
|
109
|
+
pnpm install
|
|
110
|
+
pnpm build
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
3. Test the package of interest:
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
cd packages/<packagename>
|
|
117
|
+
pnpm test:watch
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Credits
|
|
121
|
+
|
|
122
|
+
Built for developers, with developers.
|
|
123
|
+
👉 https://launchql.com | https://hyperweb.io
|
|
124
|
+
|
|
125
|
+
## Disclaimer
|
|
126
|
+
|
|
127
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
128
|
+
|
|
129
|
+
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/esm/index.js
ADDED
package/esm/types.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export class WaitForPodsTimeoutError extends Error {
|
|
2
|
+
progress;
|
|
3
|
+
constructor(message, progress) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.progress = progress;
|
|
6
|
+
this.name = 'WaitForPodsTimeoutError';
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class WaitForPodsError extends Error {
|
|
10
|
+
progress;
|
|
11
|
+
constructor(message, progress) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.progress = progress;
|
|
14
|
+
this.name = 'WaitForPodsError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { WaitForPodsTimeoutError, WaitForPodsError, } from './types';
|
|
2
|
+
const DEFAULT_TIMEOUT_MS = 300000; // 5 minutes
|
|
3
|
+
const DEFAULT_POLL_INTERVAL_MS = 2000; // 2 seconds
|
|
4
|
+
function sleep(ms) {
|
|
5
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
6
|
+
}
|
|
7
|
+
function getContainerState(status) {
|
|
8
|
+
if (status.state?.running) {
|
|
9
|
+
return { state: 'running' };
|
|
10
|
+
}
|
|
11
|
+
if (status.state?.waiting) {
|
|
12
|
+
return { state: 'waiting', reason: status.state.waiting.reason };
|
|
13
|
+
}
|
|
14
|
+
if (status.state?.terminated) {
|
|
15
|
+
return { state: 'terminated', reason: status.state.terminated.reason };
|
|
16
|
+
}
|
|
17
|
+
return { state: 'unknown' };
|
|
18
|
+
}
|
|
19
|
+
function extractPodStatusInfo(pod) {
|
|
20
|
+
const containerStatuses = (pod.status?.containerStatuses || []).map((cs) => {
|
|
21
|
+
const { state, reason } = getContainerState(cs);
|
|
22
|
+
return {
|
|
23
|
+
name: cs.name,
|
|
24
|
+
ready: cs.ready,
|
|
25
|
+
restartCount: cs.restartCount,
|
|
26
|
+
state,
|
|
27
|
+
stateReason: reason,
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
const conditions = (pod.status?.conditions || []).map((c) => ({
|
|
31
|
+
type: c.type,
|
|
32
|
+
status: c.status,
|
|
33
|
+
reason: c.reason,
|
|
34
|
+
message: c.message,
|
|
35
|
+
}));
|
|
36
|
+
const readyCondition = pod.status?.conditions?.find((c) => c.type === 'Ready');
|
|
37
|
+
const isReady = readyCondition?.status === 'True' && pod.status?.phase === 'Running';
|
|
38
|
+
return {
|
|
39
|
+
name: pod.metadata?.name || 'unknown',
|
|
40
|
+
namespace: pod.metadata?.namespace || 'default',
|
|
41
|
+
phase: pod.status?.phase || 'Unknown',
|
|
42
|
+
ready: isReady,
|
|
43
|
+
conditions,
|
|
44
|
+
containerStatuses,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function buildProgress(pods, startTime) {
|
|
48
|
+
const ready = pods.filter((p) => p.ready).length;
|
|
49
|
+
const failed = pods.filter((p) => p.phase === 'Failed').length;
|
|
50
|
+
const pending = pods.filter((p) => !p.ready && p.phase !== 'Failed').length;
|
|
51
|
+
return {
|
|
52
|
+
total: pods.length,
|
|
53
|
+
ready,
|
|
54
|
+
pending,
|
|
55
|
+
failed,
|
|
56
|
+
pods,
|
|
57
|
+
elapsedMs: Date.now() - startTime,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export async function waitForPods(client, options = {}) {
|
|
61
|
+
const { namespace = 'default', labelSelector, fieldSelector, timeoutMs = DEFAULT_TIMEOUT_MS, pollIntervalMs = DEFAULT_POLL_INTERVAL_MS, minReady, allReady = true, onProgress, } = options;
|
|
62
|
+
const startTime = Date.now();
|
|
63
|
+
while (true) {
|
|
64
|
+
const elapsed = Date.now() - startTime;
|
|
65
|
+
if (elapsed >= timeoutMs) {
|
|
66
|
+
const podList = await client.listCoreV1NamespacedPod({
|
|
67
|
+
path: { namespace },
|
|
68
|
+
query: {
|
|
69
|
+
labelSelector,
|
|
70
|
+
fieldSelector,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const podStatuses = podList.items.map(extractPodStatusInfo);
|
|
74
|
+
const progress = buildProgress(podStatuses, startTime);
|
|
75
|
+
throw new WaitForPodsTimeoutError(`Timeout waiting for pods after ${timeoutMs}ms. ` +
|
|
76
|
+
`Ready: ${progress.ready}/${progress.total}`, progress);
|
|
77
|
+
}
|
|
78
|
+
let podList;
|
|
79
|
+
try {
|
|
80
|
+
podList = await client.listCoreV1NamespacedPod({
|
|
81
|
+
path: { namespace },
|
|
82
|
+
query: {
|
|
83
|
+
labelSelector,
|
|
84
|
+
fieldSelector,
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new WaitForPodsError(`Failed to list pods: ${error instanceof Error ? error.message : String(error)}`);
|
|
90
|
+
}
|
|
91
|
+
const podStatuses = podList.items.map(extractPodStatusInfo);
|
|
92
|
+
const progress = buildProgress(podStatuses, startTime);
|
|
93
|
+
if (onProgress) {
|
|
94
|
+
onProgress(progress);
|
|
95
|
+
}
|
|
96
|
+
if (progress.failed > 0) {
|
|
97
|
+
const failedPods = podStatuses
|
|
98
|
+
.filter((p) => p.phase === 'Failed')
|
|
99
|
+
.map((p) => p.name)
|
|
100
|
+
.join(', ');
|
|
101
|
+
throw new WaitForPodsError(`Pods failed: ${failedPods}`, progress);
|
|
102
|
+
}
|
|
103
|
+
if (progress.total === 0) {
|
|
104
|
+
await sleep(pollIntervalMs);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const targetReady = minReady ?? (allReady ? progress.total : 1);
|
|
108
|
+
if (progress.ready >= targetReady) {
|
|
109
|
+
return {
|
|
110
|
+
success: true,
|
|
111
|
+
pods: podStatuses,
|
|
112
|
+
message: `${progress.ready}/${progress.total} pods are ready`,
|
|
113
|
+
elapsedMs: progress.elapsedMs,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
await sleep(pollIntervalMs);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
export async function waitForPodByName(client, name, options = {}) {
|
|
120
|
+
return waitForPods(client, {
|
|
121
|
+
...options,
|
|
122
|
+
fieldSelector: `metadata.name=${name}`,
|
|
123
|
+
minReady: 1,
|
|
124
|
+
allReady: true,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
export async function waitForPodsWithLabel(client, label, value, options = {}) {
|
|
128
|
+
return waitForPods(client, {
|
|
129
|
+
...options,
|
|
130
|
+
labelSelector: `${label}=${value}`,
|
|
131
|
+
});
|
|
132
|
+
}
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./types"), exports);
|
|
18
|
+
__exportStar(require("./wait-for-pods"), exports);
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kubernetes-test",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "Kubernetes testing utilities with wait-for-pods and other helpers for integration tests",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/dev-utils",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/dev-utils"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/dev-utils/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "makage build",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"kubernetes",
|
|
33
|
+
"k8s",
|
|
34
|
+
"testing",
|
|
35
|
+
"integration-tests",
|
|
36
|
+
"pods",
|
|
37
|
+
"wait-for-pods",
|
|
38
|
+
"kubernetesjs",
|
|
39
|
+
"container",
|
|
40
|
+
"orchestration",
|
|
41
|
+
"devops",
|
|
42
|
+
"cloud-native"
|
|
43
|
+
],
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"kubernetesjs": "^0.7.6"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"makage": "0.1.8"
|
|
49
|
+
},
|
|
50
|
+
"gitHead": "af38e269a8db3da7e2a98092b04852056f47cc50"
|
|
51
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Pod, PodList, PodCondition } from 'kubernetesjs';
|
|
2
|
+
export type { Pod, PodList, PodCondition };
|
|
3
|
+
export interface WaitForPodsOptions {
|
|
4
|
+
namespace?: string;
|
|
5
|
+
labelSelector?: string;
|
|
6
|
+
fieldSelector?: string;
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
pollIntervalMs?: number;
|
|
9
|
+
minReady?: number;
|
|
10
|
+
allReady?: boolean;
|
|
11
|
+
onProgress?: (status: WaitForPodsProgress) => void;
|
|
12
|
+
}
|
|
13
|
+
export interface WaitForPodsProgress {
|
|
14
|
+
total: number;
|
|
15
|
+
ready: number;
|
|
16
|
+
pending: number;
|
|
17
|
+
failed: number;
|
|
18
|
+
pods: PodStatusInfo[];
|
|
19
|
+
elapsedMs: number;
|
|
20
|
+
}
|
|
21
|
+
export interface PodStatusInfo {
|
|
22
|
+
name: string;
|
|
23
|
+
namespace: string;
|
|
24
|
+
phase: string;
|
|
25
|
+
ready: boolean;
|
|
26
|
+
conditions: PodConditionInfo[];
|
|
27
|
+
containerStatuses: ContainerStatusInfo[];
|
|
28
|
+
}
|
|
29
|
+
export interface PodConditionInfo {
|
|
30
|
+
type: string;
|
|
31
|
+
status: string;
|
|
32
|
+
reason?: string;
|
|
33
|
+
message?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface ContainerStatusInfo {
|
|
36
|
+
name: string;
|
|
37
|
+
ready: boolean;
|
|
38
|
+
restartCount: number;
|
|
39
|
+
state: 'running' | 'waiting' | 'terminated' | 'unknown';
|
|
40
|
+
stateReason?: string;
|
|
41
|
+
}
|
|
42
|
+
export interface WaitForPodsResult {
|
|
43
|
+
success: boolean;
|
|
44
|
+
pods: PodStatusInfo[];
|
|
45
|
+
message: string;
|
|
46
|
+
elapsedMs: number;
|
|
47
|
+
}
|
|
48
|
+
export interface KubernetesTestClientOptions {
|
|
49
|
+
restEndpoint?: string;
|
|
50
|
+
namespace?: string;
|
|
51
|
+
}
|
|
52
|
+
export declare class WaitForPodsTimeoutError extends Error {
|
|
53
|
+
readonly progress: WaitForPodsProgress;
|
|
54
|
+
constructor(message: string, progress: WaitForPodsProgress);
|
|
55
|
+
}
|
|
56
|
+
export declare class WaitForPodsError extends Error {
|
|
57
|
+
readonly progress?: WaitForPodsProgress;
|
|
58
|
+
constructor(message: string, progress?: WaitForPodsProgress);
|
|
59
|
+
}
|
package/types.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WaitForPodsError = exports.WaitForPodsTimeoutError = void 0;
|
|
4
|
+
class WaitForPodsTimeoutError extends Error {
|
|
5
|
+
progress;
|
|
6
|
+
constructor(message, progress) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.progress = progress;
|
|
9
|
+
this.name = 'WaitForPodsTimeoutError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.WaitForPodsTimeoutError = WaitForPodsTimeoutError;
|
|
13
|
+
class WaitForPodsError extends Error {
|
|
14
|
+
progress;
|
|
15
|
+
constructor(message, progress) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.progress = progress;
|
|
18
|
+
this.name = 'WaitForPodsError';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.WaitForPodsError = WaitForPodsError;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { KubernetesClient } from 'kubernetesjs';
|
|
2
|
+
import { WaitForPodsOptions, WaitForPodsResult } from './types';
|
|
3
|
+
export declare function waitForPods(client: KubernetesClient, options?: WaitForPodsOptions): Promise<WaitForPodsResult>;
|
|
4
|
+
export declare function waitForPodByName(client: KubernetesClient, name: string, options?: Omit<WaitForPodsOptions, 'labelSelector' | 'minReady' | 'allReady'>): Promise<WaitForPodsResult>;
|
|
5
|
+
export declare function waitForPodsWithLabel(client: KubernetesClient, label: string, value: string, options?: Omit<WaitForPodsOptions, 'labelSelector'>): Promise<WaitForPodsResult>;
|
package/wait-for-pods.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.waitForPods = waitForPods;
|
|
4
|
+
exports.waitForPodByName = waitForPodByName;
|
|
5
|
+
exports.waitForPodsWithLabel = waitForPodsWithLabel;
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
const DEFAULT_TIMEOUT_MS = 300000; // 5 minutes
|
|
8
|
+
const DEFAULT_POLL_INTERVAL_MS = 2000; // 2 seconds
|
|
9
|
+
function sleep(ms) {
|
|
10
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
11
|
+
}
|
|
12
|
+
function getContainerState(status) {
|
|
13
|
+
if (status.state?.running) {
|
|
14
|
+
return { state: 'running' };
|
|
15
|
+
}
|
|
16
|
+
if (status.state?.waiting) {
|
|
17
|
+
return { state: 'waiting', reason: status.state.waiting.reason };
|
|
18
|
+
}
|
|
19
|
+
if (status.state?.terminated) {
|
|
20
|
+
return { state: 'terminated', reason: status.state.terminated.reason };
|
|
21
|
+
}
|
|
22
|
+
return { state: 'unknown' };
|
|
23
|
+
}
|
|
24
|
+
function extractPodStatusInfo(pod) {
|
|
25
|
+
const containerStatuses = (pod.status?.containerStatuses || []).map((cs) => {
|
|
26
|
+
const { state, reason } = getContainerState(cs);
|
|
27
|
+
return {
|
|
28
|
+
name: cs.name,
|
|
29
|
+
ready: cs.ready,
|
|
30
|
+
restartCount: cs.restartCount,
|
|
31
|
+
state,
|
|
32
|
+
stateReason: reason,
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
const conditions = (pod.status?.conditions || []).map((c) => ({
|
|
36
|
+
type: c.type,
|
|
37
|
+
status: c.status,
|
|
38
|
+
reason: c.reason,
|
|
39
|
+
message: c.message,
|
|
40
|
+
}));
|
|
41
|
+
const readyCondition = pod.status?.conditions?.find((c) => c.type === 'Ready');
|
|
42
|
+
const isReady = readyCondition?.status === 'True' && pod.status?.phase === 'Running';
|
|
43
|
+
return {
|
|
44
|
+
name: pod.metadata?.name || 'unknown',
|
|
45
|
+
namespace: pod.metadata?.namespace || 'default',
|
|
46
|
+
phase: pod.status?.phase || 'Unknown',
|
|
47
|
+
ready: isReady,
|
|
48
|
+
conditions,
|
|
49
|
+
containerStatuses,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
function buildProgress(pods, startTime) {
|
|
53
|
+
const ready = pods.filter((p) => p.ready).length;
|
|
54
|
+
const failed = pods.filter((p) => p.phase === 'Failed').length;
|
|
55
|
+
const pending = pods.filter((p) => !p.ready && p.phase !== 'Failed').length;
|
|
56
|
+
return {
|
|
57
|
+
total: pods.length,
|
|
58
|
+
ready,
|
|
59
|
+
pending,
|
|
60
|
+
failed,
|
|
61
|
+
pods,
|
|
62
|
+
elapsedMs: Date.now() - startTime,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
async function waitForPods(client, options = {}) {
|
|
66
|
+
const { namespace = 'default', labelSelector, fieldSelector, timeoutMs = DEFAULT_TIMEOUT_MS, pollIntervalMs = DEFAULT_POLL_INTERVAL_MS, minReady, allReady = true, onProgress, } = options;
|
|
67
|
+
const startTime = Date.now();
|
|
68
|
+
while (true) {
|
|
69
|
+
const elapsed = Date.now() - startTime;
|
|
70
|
+
if (elapsed >= timeoutMs) {
|
|
71
|
+
const podList = await client.listCoreV1NamespacedPod({
|
|
72
|
+
path: { namespace },
|
|
73
|
+
query: {
|
|
74
|
+
labelSelector,
|
|
75
|
+
fieldSelector,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
const podStatuses = podList.items.map(extractPodStatusInfo);
|
|
79
|
+
const progress = buildProgress(podStatuses, startTime);
|
|
80
|
+
throw new types_1.WaitForPodsTimeoutError(`Timeout waiting for pods after ${timeoutMs}ms. ` +
|
|
81
|
+
`Ready: ${progress.ready}/${progress.total}`, progress);
|
|
82
|
+
}
|
|
83
|
+
let podList;
|
|
84
|
+
try {
|
|
85
|
+
podList = await client.listCoreV1NamespacedPod({
|
|
86
|
+
path: { namespace },
|
|
87
|
+
query: {
|
|
88
|
+
labelSelector,
|
|
89
|
+
fieldSelector,
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
throw new types_1.WaitForPodsError(`Failed to list pods: ${error instanceof Error ? error.message : String(error)}`);
|
|
95
|
+
}
|
|
96
|
+
const podStatuses = podList.items.map(extractPodStatusInfo);
|
|
97
|
+
const progress = buildProgress(podStatuses, startTime);
|
|
98
|
+
if (onProgress) {
|
|
99
|
+
onProgress(progress);
|
|
100
|
+
}
|
|
101
|
+
if (progress.failed > 0) {
|
|
102
|
+
const failedPods = podStatuses
|
|
103
|
+
.filter((p) => p.phase === 'Failed')
|
|
104
|
+
.map((p) => p.name)
|
|
105
|
+
.join(', ');
|
|
106
|
+
throw new types_1.WaitForPodsError(`Pods failed: ${failedPods}`, progress);
|
|
107
|
+
}
|
|
108
|
+
if (progress.total === 0) {
|
|
109
|
+
await sleep(pollIntervalMs);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
const targetReady = minReady ?? (allReady ? progress.total : 1);
|
|
113
|
+
if (progress.ready >= targetReady) {
|
|
114
|
+
return {
|
|
115
|
+
success: true,
|
|
116
|
+
pods: podStatuses,
|
|
117
|
+
message: `${progress.ready}/${progress.total} pods are ready`,
|
|
118
|
+
elapsedMs: progress.elapsedMs,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
await sleep(pollIntervalMs);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function waitForPodByName(client, name, options = {}) {
|
|
125
|
+
return waitForPods(client, {
|
|
126
|
+
...options,
|
|
127
|
+
fieldSelector: `metadata.name=${name}`,
|
|
128
|
+
minReady: 1,
|
|
129
|
+
allReady: true,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
async function waitForPodsWithLabel(client, label, value, options = {}) {
|
|
133
|
+
return waitForPods(client, {
|
|
134
|
+
...options,
|
|
135
|
+
labelSelector: `${label}=${value}`,
|
|
136
|
+
});
|
|
137
|
+
}
|