kubernetes-fluent-client 2.5.1 → 2.6.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/dist/fluent/watch.d.ts +10 -24
- package/dist/fluent/watch.d.ts.map +1 -1
- package/dist/fluent/watch.js +188 -117
- package/dist/fluent/watch.spec.js +92 -66
- package/package.json +5 -5
- package/src/fluent/watch.spec.ts +96 -78
- package/src/fluent/watch.ts +227 -135
|
@@ -20,17 +20,23 @@ const types_1 = require("./types");
|
|
|
20
20
|
let watcher;
|
|
21
21
|
(0, globals_1.beforeEach)(() => {
|
|
22
22
|
globals_1.jest.resetAllMocks();
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
(0, nock_1.default)("http://jest-test:8080")
|
|
24
|
+
.get("/api/v1/pods")
|
|
25
|
+
.reply(200, {
|
|
26
|
+
kind: "PodList",
|
|
27
|
+
apiVersion: "v1",
|
|
28
|
+
metadata: {
|
|
29
|
+
resourceVersion: "10",
|
|
30
|
+
},
|
|
31
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
25
32
|
});
|
|
26
33
|
(0, nock_1.default)("http://jest-test:8080")
|
|
27
34
|
.get("/api/v1/pods")
|
|
28
|
-
.query({ watch: "true",
|
|
35
|
+
.query({ watch: "true", resourceVersion: "10" })
|
|
29
36
|
.reply(200, () => {
|
|
30
37
|
const stream = new readable_stream_1.PassThrough();
|
|
31
38
|
const resources = [
|
|
32
39
|
{ type: "ADDED", object: createMockPod(`pod-0`, `1`) },
|
|
33
|
-
{ type: "BOOKMARK", object: { metadata: { resourceVersion: "1" } } },
|
|
34
40
|
{ type: "MODIFIED", object: createMockPod(`pod-0`, `2`) },
|
|
35
41
|
];
|
|
36
42
|
resources.forEach(resource => {
|
|
@@ -47,26 +53,17 @@ const types_1 = require("./types");
|
|
|
47
53
|
nock_1.default.cleanAll();
|
|
48
54
|
(0, nock_1.default)("http://jest-test:8080")
|
|
49
55
|
.get("/api/v1/namespaces/tester/pods")
|
|
50
|
-
.query({
|
|
51
|
-
.reply(200);
|
|
52
|
-
watcher = (0, _1.K8s)(__1.kind.Pod, { name: "demo" }).InNamespace("tester").Watch(evtMock);
|
|
53
|
-
setupAndStartWatcher(__1.WatchEvent.CONNECT, () => {
|
|
54
|
-
done();
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
(0, globals_1.it)("should start the watch at the specified resource version", done => {
|
|
58
|
-
nock_1.default.cleanAll();
|
|
56
|
+
.query({ fieldSelector: "metadata.name=demo" })
|
|
57
|
+
.reply(200, createMockPod(`demo`, `15`));
|
|
59
58
|
(0, nock_1.default)("http://jest-test:8080")
|
|
60
|
-
.get("/api/v1/pods")
|
|
59
|
+
.get("/api/v1/namespaces/tester/pods")
|
|
61
60
|
.query({
|
|
62
61
|
watch: "true",
|
|
63
|
-
|
|
64
|
-
resourceVersion: "
|
|
62
|
+
fieldSelector: "metadata.name=demo",
|
|
63
|
+
resourceVersion: "15",
|
|
65
64
|
})
|
|
66
65
|
.reply(200);
|
|
67
|
-
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock
|
|
68
|
-
resourceVersion: "25",
|
|
69
|
-
});
|
|
66
|
+
watcher = (0, _1.K8s)(__1.kind.Pod, { name: "demo" }).InNamespace("tester").Watch(evtMock);
|
|
70
67
|
setupAndStartWatcher(__1.WatchEvent.CONNECT, () => {
|
|
71
68
|
done();
|
|
72
69
|
});
|
|
@@ -75,7 +72,17 @@ const types_1 = require("./types");
|
|
|
75
72
|
nock_1.default.cleanAll();
|
|
76
73
|
(0, nock_1.default)("http://jest-test:8080")
|
|
77
74
|
.get("/api/v1/pods")
|
|
78
|
-
.
|
|
75
|
+
.reply(200, {
|
|
76
|
+
kind: "PodList",
|
|
77
|
+
apiVersion: "v1",
|
|
78
|
+
metadata: {
|
|
79
|
+
resourceVersion: "25",
|
|
80
|
+
},
|
|
81
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
82
|
+
});
|
|
83
|
+
(0, nock_1.default)("http://jest-test:8080")
|
|
84
|
+
.get("/api/v1/pods")
|
|
85
|
+
.query({ watch: "true", resourceVersion: "25" })
|
|
79
86
|
.reply(200, () => {
|
|
80
87
|
const stream = new readable_stream_1.PassThrough();
|
|
81
88
|
stream.write(JSON.stringify({
|
|
@@ -93,11 +100,9 @@ const types_1 = require("./types");
|
|
|
93
100
|
stream.end();
|
|
94
101
|
return stream;
|
|
95
102
|
});
|
|
96
|
-
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock
|
|
97
|
-
resourceVersion: "45",
|
|
98
|
-
});
|
|
103
|
+
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock);
|
|
99
104
|
setupAndStartWatcher(__1.WatchEvent.OLD_RESOURCE_VERSION, res => {
|
|
100
|
-
(0, globals_1.expect)(res).toEqual("
|
|
105
|
+
(0, globals_1.expect)(res).toEqual("25");
|
|
101
106
|
done();
|
|
102
107
|
});
|
|
103
108
|
});
|
|
@@ -110,90 +115,110 @@ const types_1 = require("./types");
|
|
|
110
115
|
watcher.start().catch(errMock);
|
|
111
116
|
});
|
|
112
117
|
(0, globals_1.it)("should return the cache id", () => {
|
|
113
|
-
(0,
|
|
114
|
-
|
|
115
|
-
(0, globals_1.it)("should use an updated resourceVersion", done => {
|
|
116
|
-
nock_1.default.cleanAll();
|
|
117
|
-
(0, nock_1.default)("http://jest-test:8080")
|
|
118
|
-
.get("/api/v1/pods")
|
|
119
|
-
.query({
|
|
120
|
-
watch: "true",
|
|
121
|
-
allowWatchBookmarks: "true",
|
|
122
|
-
resourceVersion: "35",
|
|
123
|
-
})
|
|
124
|
-
.reply(200);
|
|
125
|
-
// Update the resource version, could be combined with getCacheID to store the value
|
|
126
|
-
watcher.resourceVersion = "35";
|
|
127
|
-
setupAndStartWatcher(__1.WatchEvent.CONNECT, () => {
|
|
128
|
-
done();
|
|
118
|
+
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
119
|
+
retryDelaySec: 1,
|
|
129
120
|
});
|
|
121
|
+
(0, globals_1.expect)(watcher.getCacheID()).toEqual("d69b75a611");
|
|
130
122
|
});
|
|
131
123
|
(0, globals_1.it)("should handle the CONNECT event", done => {
|
|
124
|
+
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
125
|
+
retryDelaySec: 1,
|
|
126
|
+
});
|
|
132
127
|
setupAndStartWatcher(__1.WatchEvent.CONNECT, () => {
|
|
133
128
|
done();
|
|
134
129
|
});
|
|
135
130
|
});
|
|
136
131
|
(0, globals_1.it)("should handle the DATA event", done => {
|
|
132
|
+
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
133
|
+
retryDelaySec: 1,
|
|
134
|
+
});
|
|
137
135
|
setupAndStartWatcher(__1.WatchEvent.DATA, (pod, phase) => {
|
|
138
136
|
(0, globals_1.expect)(pod.metadata?.name).toEqual(`pod-0`);
|
|
139
137
|
(0, globals_1.expect)(phase).toEqual(types_1.WatchPhase.Added);
|
|
140
138
|
done();
|
|
141
139
|
});
|
|
142
140
|
});
|
|
143
|
-
(0, globals_1.it)("should handle the BOOKMARK event", done => {
|
|
144
|
-
setupAndStartWatcher(__1.WatchEvent.BOOKMARK, bookmark => {
|
|
145
|
-
(0, globals_1.expect)(bookmark.metadata?.resourceVersion).toEqual("1");
|
|
146
|
-
done();
|
|
147
|
-
});
|
|
148
|
-
});
|
|
149
141
|
(0, globals_1.it)("should handle the NETWORK_ERROR event", done => {
|
|
150
142
|
nock_1.default.cleanAll();
|
|
151
143
|
(0, nock_1.default)("http://jest-test:8080")
|
|
152
144
|
.get("/api/v1/pods")
|
|
153
|
-
.
|
|
145
|
+
.reply(200, {
|
|
146
|
+
kind: "PodList",
|
|
147
|
+
apiVersion: "v1",
|
|
148
|
+
metadata: {
|
|
149
|
+
resourceVersion: "45",
|
|
150
|
+
},
|
|
151
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
152
|
+
});
|
|
153
|
+
(0, nock_1.default)("http://jest-test:8080")
|
|
154
|
+
.get("/api/v1/pods")
|
|
155
|
+
.query({ watch: "true", resourceVersion: "45" })
|
|
154
156
|
.replyWithError("Something bad happened");
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
done();
|
|
157
|
+
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
158
|
+
retryDelaySec: 1,
|
|
158
159
|
});
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
setupAndStartWatcher(__1.WatchEvent.RESOURCE_VERSION, resourceVersion => {
|
|
162
|
-
(0, globals_1.expect)(watcher.resourceVersion).toEqual("2");
|
|
163
|
-
(0, globals_1.expect)(resourceVersion).toEqual("2");
|
|
160
|
+
setupAndStartWatcher(__1.WatchEvent.NETWORK_ERROR, error => {
|
|
161
|
+
(0, globals_1.expect)(error.message).toEqual("request to http://jest-test:8080/api/v1/pods?watch=true&resourceVersion=45 failed, reason: Something bad happened");
|
|
164
162
|
done();
|
|
165
163
|
});
|
|
166
164
|
});
|
|
167
|
-
(0, globals_1.it)("should handle the RECONNECT event", done => {
|
|
165
|
+
(0, globals_1.it)("should handle the RECONNECT event on an error", done => {
|
|
168
166
|
nock_1.default.cleanAll();
|
|
169
167
|
(0, nock_1.default)("http://jest-test:8080")
|
|
170
168
|
.get("/api/v1/pods")
|
|
171
|
-
.
|
|
169
|
+
.reply(200, {
|
|
170
|
+
kind: "PodList",
|
|
171
|
+
apiVersion: "v1",
|
|
172
|
+
metadata: {
|
|
173
|
+
resourceVersion: "65",
|
|
174
|
+
},
|
|
175
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
176
|
+
});
|
|
177
|
+
(0, nock_1.default)("http://jest-test:8080")
|
|
178
|
+
.get("/api/v1/pods")
|
|
179
|
+
.query({ watch: "true", resourceVersion: "65" })
|
|
172
180
|
.replyWithError("Something bad happened");
|
|
173
|
-
|
|
174
|
-
|
|
181
|
+
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
182
|
+
retryDelaySec: 0.01,
|
|
183
|
+
});
|
|
184
|
+
setupAndStartWatcher(__1.WatchEvent.RECONNECT, count => {
|
|
185
|
+
(0, globals_1.expect)(count).toEqual(1);
|
|
175
186
|
done();
|
|
176
187
|
});
|
|
177
188
|
});
|
|
178
189
|
(0, globals_1.it)("should perform a resync after the resync interval", done => {
|
|
179
190
|
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
180
|
-
|
|
191
|
+
retryDelaySec: 0.01,
|
|
192
|
+
resyncIntervalSec: 0.01,
|
|
181
193
|
});
|
|
182
|
-
setupAndStartWatcher(__1.WatchEvent.
|
|
183
|
-
(0, globals_1.expect)(
|
|
184
|
-
(0, globals_1.expect)(err.message).toEqual("Resync triggered by resyncIntervalSec");
|
|
194
|
+
setupAndStartWatcher(__1.WatchEvent.RECONNECT, count => {
|
|
195
|
+
(0, globals_1.expect)(count).toEqual(1);
|
|
185
196
|
done();
|
|
186
197
|
});
|
|
187
198
|
});
|
|
188
199
|
(0, globals_1.it)("should handle the GIVE_UP event", done => {
|
|
189
200
|
nock_1.default.cleanAll();
|
|
190
|
-
(0, nock_1.default)("http://jest-test:8080")
|
|
201
|
+
(0, nock_1.default)("http://jest-test:8080")
|
|
202
|
+
.get("/api/v1/pods")
|
|
203
|
+
.reply(200, {
|
|
204
|
+
kind: "PodList",
|
|
205
|
+
apiVersion: "v1",
|
|
206
|
+
metadata: {
|
|
207
|
+
resourceVersion: "75",
|
|
208
|
+
},
|
|
209
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
210
|
+
});
|
|
211
|
+
(0, nock_1.default)("http://jest-test:8080")
|
|
212
|
+
.get("/api/v1/pods")
|
|
213
|
+
.query({ watch: "true", resourceVersion: "75" })
|
|
214
|
+
.replyWithError("Something bad happened");
|
|
191
215
|
watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
|
|
192
216
|
retryMax: 1,
|
|
193
|
-
retryDelaySec:
|
|
217
|
+
retryDelaySec: 0.01,
|
|
218
|
+
resyncIntervalSec: 1,
|
|
194
219
|
});
|
|
195
220
|
setupAndStartWatcher(__1.WatchEvent.GIVE_UP, error => {
|
|
196
|
-
(0, globals_1.expect)(error.message).toContain("
|
|
221
|
+
(0, globals_1.expect)(error.message).toContain("Retry limit (1) exceeded, giving up");
|
|
197
222
|
done();
|
|
198
223
|
});
|
|
199
224
|
});
|
|
@@ -212,6 +237,7 @@ function createMockPod(name, resourceVersion) {
|
|
|
212
237
|
metadata: {
|
|
213
238
|
name: name,
|
|
214
239
|
resourceVersion: resourceVersion,
|
|
240
|
+
uid: Math.random().toString(36).substring(7),
|
|
215
241
|
// ... other metadata fields
|
|
216
242
|
},
|
|
217
243
|
spec: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kubernetes-fluent-client",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "A @kubernetes/client-node fluent API wrapper that leverages K8s Server Side Apply.",
|
|
5
5
|
"bin": "./dist/cli.js",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -51,14 +51,14 @@
|
|
|
51
51
|
"@types/byline": "4.2.36",
|
|
52
52
|
"@types/readable-stream": "4.0.14",
|
|
53
53
|
"@types/yargs": "17.0.32",
|
|
54
|
-
"@typescript-eslint/eslint-plugin": "7.
|
|
55
|
-
"@typescript-eslint/parser": "7.
|
|
56
|
-
"eslint-plugin-jsdoc": "48.2.
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "7.10.0",
|
|
55
|
+
"@typescript-eslint/parser": "7.10.0",
|
|
56
|
+
"eslint-plugin-jsdoc": "48.2.5",
|
|
57
57
|
"jest": "29.7.0",
|
|
58
58
|
"nock": "13.5.4",
|
|
59
59
|
"prettier": "3.2.5",
|
|
60
60
|
"semantic-release": "23.1.1",
|
|
61
|
-
"ts-jest": "29.1.
|
|
61
|
+
"ts-jest": "29.1.3",
|
|
62
62
|
"typescript": "5.4.5"
|
|
63
63
|
},
|
|
64
64
|
"release": {
|
package/src/fluent/watch.spec.ts
CHANGED
|
@@ -22,19 +22,26 @@ describe("Watcher", () => {
|
|
|
22
22
|
|
|
23
23
|
beforeEach(() => {
|
|
24
24
|
jest.resetAllMocks();
|
|
25
|
-
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
26
|
-
retryDelaySec: 1,
|
|
27
|
-
});
|
|
28
25
|
|
|
29
26
|
nock("http://jest-test:8080")
|
|
30
27
|
.get("/api/v1/pods")
|
|
31
|
-
.
|
|
28
|
+
.reply(200, {
|
|
29
|
+
kind: "PodList",
|
|
30
|
+
apiVersion: "v1",
|
|
31
|
+
metadata: {
|
|
32
|
+
resourceVersion: "10",
|
|
33
|
+
},
|
|
34
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
nock("http://jest-test:8080")
|
|
38
|
+
.get("/api/v1/pods")
|
|
39
|
+
.query({ watch: "true", resourceVersion: "10" })
|
|
32
40
|
.reply(200, () => {
|
|
33
41
|
const stream = new PassThrough();
|
|
34
42
|
|
|
35
43
|
const resources = [
|
|
36
44
|
{ type: "ADDED", object: createMockPod(`pod-0`, `1`) },
|
|
37
|
-
{ type: "BOOKMARK", object: { metadata: { resourceVersion: "1" } } },
|
|
38
45
|
{ type: "MODIFIED", object: createMockPod(`pod-0`, `2`) },
|
|
39
46
|
];
|
|
40
47
|
|
|
@@ -56,30 +63,19 @@ describe("Watcher", () => {
|
|
|
56
63
|
nock.cleanAll();
|
|
57
64
|
nock("http://jest-test:8080")
|
|
58
65
|
.get("/api/v1/namespaces/tester/pods")
|
|
59
|
-
.query({
|
|
60
|
-
.reply(200);
|
|
61
|
-
|
|
62
|
-
watcher = K8s(kind.Pod, { name: "demo" }).InNamespace("tester").Watch(evtMock);
|
|
66
|
+
.query({ fieldSelector: "metadata.name=demo" })
|
|
67
|
+
.reply(200, createMockPod(`demo`, `15`));
|
|
63
68
|
|
|
64
|
-
setupAndStartWatcher(WatchEvent.CONNECT, () => {
|
|
65
|
-
done();
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("should start the watch at the specified resource version", done => {
|
|
70
|
-
nock.cleanAll();
|
|
71
69
|
nock("http://jest-test:8080")
|
|
72
|
-
.get("/api/v1/pods")
|
|
70
|
+
.get("/api/v1/namespaces/tester/pods")
|
|
73
71
|
.query({
|
|
74
72
|
watch: "true",
|
|
75
|
-
|
|
76
|
-
resourceVersion: "
|
|
73
|
+
fieldSelector: "metadata.name=demo",
|
|
74
|
+
resourceVersion: "15",
|
|
77
75
|
})
|
|
78
76
|
.reply(200);
|
|
79
77
|
|
|
80
|
-
watcher = K8s(kind.Pod).Watch(evtMock
|
|
81
|
-
resourceVersion: "25",
|
|
82
|
-
});
|
|
78
|
+
watcher = K8s(kind.Pod, { name: "demo" }).InNamespace("tester").Watch(evtMock);
|
|
83
79
|
|
|
84
80
|
setupAndStartWatcher(WatchEvent.CONNECT, () => {
|
|
85
81
|
done();
|
|
@@ -90,7 +86,17 @@ describe("Watcher", () => {
|
|
|
90
86
|
nock.cleanAll();
|
|
91
87
|
nock("http://jest-test:8080")
|
|
92
88
|
.get("/api/v1/pods")
|
|
93
|
-
.
|
|
89
|
+
.reply(200, {
|
|
90
|
+
kind: "PodList",
|
|
91
|
+
apiVersion: "v1",
|
|
92
|
+
metadata: {
|
|
93
|
+
resourceVersion: "25",
|
|
94
|
+
},
|
|
95
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
96
|
+
});
|
|
97
|
+
nock("http://jest-test:8080")
|
|
98
|
+
.get("/api/v1/pods")
|
|
99
|
+
.query({ watch: "true", resourceVersion: "25" })
|
|
94
100
|
.reply(200, () => {
|
|
95
101
|
const stream = new PassThrough();
|
|
96
102
|
stream.write(
|
|
@@ -112,12 +118,10 @@ describe("Watcher", () => {
|
|
|
112
118
|
return stream;
|
|
113
119
|
});
|
|
114
120
|
|
|
115
|
-
watcher = K8s(kind.Pod).Watch(evtMock
|
|
116
|
-
resourceVersion: "45",
|
|
117
|
-
});
|
|
121
|
+
watcher = K8s(kind.Pod).Watch(evtMock);
|
|
118
122
|
|
|
119
123
|
setupAndStartWatcher(WatchEvent.OLD_RESOURCE_VERSION, res => {
|
|
120
|
-
expect(res).toEqual("
|
|
124
|
+
expect(res).toEqual("25");
|
|
121
125
|
done();
|
|
122
126
|
});
|
|
123
127
|
});
|
|
@@ -133,35 +137,25 @@ describe("Watcher", () => {
|
|
|
133
137
|
});
|
|
134
138
|
|
|
135
139
|
it("should return the cache id", () => {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
it("should use an updated resourceVersion", done => {
|
|
140
|
-
nock.cleanAll();
|
|
141
|
-
nock("http://jest-test:8080")
|
|
142
|
-
.get("/api/v1/pods")
|
|
143
|
-
.query({
|
|
144
|
-
watch: "true",
|
|
145
|
-
allowWatchBookmarks: "true",
|
|
146
|
-
resourceVersion: "35",
|
|
147
|
-
})
|
|
148
|
-
.reply(200);
|
|
149
|
-
|
|
150
|
-
// Update the resource version, could be combined with getCacheID to store the value
|
|
151
|
-
watcher.resourceVersion = "35";
|
|
152
|
-
|
|
153
|
-
setupAndStartWatcher(WatchEvent.CONNECT, () => {
|
|
154
|
-
done();
|
|
140
|
+
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
141
|
+
retryDelaySec: 1,
|
|
155
142
|
});
|
|
143
|
+
expect(watcher.getCacheID()).toEqual("d69b75a611");
|
|
156
144
|
});
|
|
157
145
|
|
|
158
146
|
it("should handle the CONNECT event", done => {
|
|
147
|
+
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
148
|
+
retryDelaySec: 1,
|
|
149
|
+
});
|
|
159
150
|
setupAndStartWatcher(WatchEvent.CONNECT, () => {
|
|
160
151
|
done();
|
|
161
152
|
});
|
|
162
153
|
});
|
|
163
154
|
|
|
164
155
|
it("should handle the DATA event", done => {
|
|
156
|
+
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
157
|
+
retryDelaySec: 1,
|
|
158
|
+
});
|
|
165
159
|
setupAndStartWatcher(WatchEvent.DATA, (pod, phase) => {
|
|
166
160
|
expect(pod.metadata?.name).toEqual(`pod-0`);
|
|
167
161
|
expect(phase).toEqual(WatchPhase.Added);
|
|
@@ -169,76 +163,99 @@ describe("Watcher", () => {
|
|
|
169
163
|
});
|
|
170
164
|
});
|
|
171
165
|
|
|
172
|
-
it("should handle the BOOKMARK event", done => {
|
|
173
|
-
setupAndStartWatcher(WatchEvent.BOOKMARK, bookmark => {
|
|
174
|
-
expect(bookmark.metadata?.resourceVersion).toEqual("1");
|
|
175
|
-
done();
|
|
176
|
-
});
|
|
177
|
-
});
|
|
178
|
-
|
|
179
166
|
it("should handle the NETWORK_ERROR event", done => {
|
|
180
167
|
nock.cleanAll();
|
|
181
168
|
nock("http://jest-test:8080")
|
|
182
169
|
.get("/api/v1/pods")
|
|
183
|
-
.
|
|
170
|
+
.reply(200, {
|
|
171
|
+
kind: "PodList",
|
|
172
|
+
apiVersion: "v1",
|
|
173
|
+
metadata: {
|
|
174
|
+
resourceVersion: "45",
|
|
175
|
+
},
|
|
176
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
177
|
+
});
|
|
178
|
+
nock("http://jest-test:8080")
|
|
179
|
+
.get("/api/v1/pods")
|
|
180
|
+
.query({ watch: "true", resourceVersion: "45" })
|
|
184
181
|
.replyWithError("Something bad happened");
|
|
185
182
|
|
|
183
|
+
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
184
|
+
retryDelaySec: 1,
|
|
185
|
+
});
|
|
186
|
+
|
|
186
187
|
setupAndStartWatcher(WatchEvent.NETWORK_ERROR, error => {
|
|
187
188
|
expect(error.message).toEqual(
|
|
188
|
-
"request to http://jest-test:8080/api/v1/pods?watch=true&
|
|
189
|
+
"request to http://jest-test:8080/api/v1/pods?watch=true&resourceVersion=45 failed, reason: Something bad happened",
|
|
189
190
|
);
|
|
190
191
|
done();
|
|
191
192
|
});
|
|
192
193
|
});
|
|
193
194
|
|
|
194
|
-
it("should handle the
|
|
195
|
-
setupAndStartWatcher(WatchEvent.RESOURCE_VERSION, resourceVersion => {
|
|
196
|
-
expect(watcher.resourceVersion).toEqual("2");
|
|
197
|
-
expect(resourceVersion).toEqual("2");
|
|
198
|
-
done();
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it("should handle the RECONNECT event", done => {
|
|
195
|
+
it("should handle the RECONNECT event on an error", done => {
|
|
203
196
|
nock.cleanAll();
|
|
204
197
|
nock("http://jest-test:8080")
|
|
205
198
|
.get("/api/v1/pods")
|
|
206
|
-
.
|
|
199
|
+
.reply(200, {
|
|
200
|
+
kind: "PodList",
|
|
201
|
+
apiVersion: "v1",
|
|
202
|
+
metadata: {
|
|
203
|
+
resourceVersion: "65",
|
|
204
|
+
},
|
|
205
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
206
|
+
});
|
|
207
|
+
nock("http://jest-test:8080")
|
|
208
|
+
.get("/api/v1/pods")
|
|
209
|
+
.query({ watch: "true", resourceVersion: "65" })
|
|
207
210
|
.replyWithError("Something bad happened");
|
|
208
211
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
212
|
+
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
213
|
+
retryDelaySec: 0.01,
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
setupAndStartWatcher(WatchEvent.RECONNECT, count => {
|
|
217
|
+
expect(count).toEqual(1);
|
|
213
218
|
done();
|
|
214
219
|
});
|
|
215
220
|
});
|
|
216
221
|
|
|
217
222
|
it("should perform a resync after the resync interval", done => {
|
|
218
223
|
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
219
|
-
|
|
224
|
+
retryDelaySec: 0.01,
|
|
225
|
+
resyncIntervalSec: 0.01,
|
|
220
226
|
});
|
|
221
227
|
|
|
222
|
-
setupAndStartWatcher(WatchEvent.
|
|
223
|
-
expect(
|
|
224
|
-
expect(err.message).toEqual("Resync triggered by resyncIntervalSec");
|
|
228
|
+
setupAndStartWatcher(WatchEvent.RECONNECT, count => {
|
|
229
|
+
expect(count).toEqual(1);
|
|
225
230
|
done();
|
|
226
231
|
});
|
|
227
232
|
});
|
|
228
233
|
|
|
229
234
|
it("should handle the GIVE_UP event", done => {
|
|
230
235
|
nock.cleanAll();
|
|
231
|
-
nock("http://jest-test:8080")
|
|
236
|
+
nock("http://jest-test:8080")
|
|
237
|
+
.get("/api/v1/pods")
|
|
238
|
+
.reply(200, {
|
|
239
|
+
kind: "PodList",
|
|
240
|
+
apiVersion: "v1",
|
|
241
|
+
metadata: {
|
|
242
|
+
resourceVersion: "75",
|
|
243
|
+
},
|
|
244
|
+
items: [createMockPod(`pod-0`, `1`)],
|
|
245
|
+
});
|
|
246
|
+
nock("http://jest-test:8080")
|
|
247
|
+
.get("/api/v1/pods")
|
|
248
|
+
.query({ watch: "true", resourceVersion: "75" })
|
|
249
|
+
.replyWithError("Something bad happened");
|
|
232
250
|
|
|
233
251
|
watcher = K8s(kind.Pod).Watch(evtMock, {
|
|
234
252
|
retryMax: 1,
|
|
235
|
-
retryDelaySec:
|
|
253
|
+
retryDelaySec: 0.01,
|
|
254
|
+
resyncIntervalSec: 1,
|
|
236
255
|
});
|
|
237
256
|
|
|
238
257
|
setupAndStartWatcher(WatchEvent.GIVE_UP, error => {
|
|
239
|
-
expect(error.message).toContain(
|
|
240
|
-
"request to http://jest-test:8080/api/v1/pods?watch=true&allowWatchBookmarks=true failed",
|
|
241
|
-
);
|
|
258
|
+
expect(error.message).toContain("Retry limit (1) exceeded, giving up");
|
|
242
259
|
done();
|
|
243
260
|
});
|
|
244
261
|
});
|
|
@@ -258,6 +275,7 @@ function createMockPod(name: string, resourceVersion: string): kind.Pod {
|
|
|
258
275
|
metadata: {
|
|
259
276
|
name: name,
|
|
260
277
|
resourceVersion: resourceVersion,
|
|
278
|
+
uid: Math.random().toString(36).substring(7),
|
|
261
279
|
// ... other metadata fields
|
|
262
280
|
},
|
|
263
281
|
spec: {
|