-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
KubernetesCrudDispatcher.java
380 lines (346 loc) · 13.7 KB
/
KubernetesCrudDispatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.server.mock;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.client.Watcher.Action;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import io.fabric8.kubernetes.client.server.mock.crud.KubernetesCrudDispatcherException;
import io.fabric8.kubernetes.client.server.mock.crud.KubernetesCrudDispatcherHandler;
import io.fabric8.kubernetes.client.server.mock.crud.KubernetesCrudPersistence;
import io.fabric8.kubernetes.client.server.mock.crud.PatchHandler;
import io.fabric8.kubernetes.client.server.mock.crud.PostHandler;
import io.fabric8.kubernetes.client.server.mock.crud.PutHandler;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.Utils;
import io.fabric8.mockwebserver.Context;
import io.fabric8.mockwebserver.crud.Attribute;
import io.fabric8.mockwebserver.crud.AttributeSet;
import io.fabric8.mockwebserver.crud.CrudDispatcher;
import io.fabric8.mockwebserver.http.MockResponse;
import io.fabric8.mockwebserver.http.RecordedRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Supplier;
public class KubernetesCrudDispatcher extends CrudDispatcher implements KubernetesCrudPersistence, CustomResourceAware {
private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesCrudDispatcher.class);
private final Set<WatchEventsListener> watchEventListeners;
private final CustomResourceDefinitionProcessor crdProcessor;
private final KubernetesAttributesExtractor kubernetesAttributesExtractor;
private final AtomicLong resourceVersion;
private final KubernetesResponseComposer kubernetesResponseComposer;
private final KubernetesCrudDispatcherHandler postHandler;
private final KubernetesCrudDispatcherHandler putHandler;
private final KubernetesCrudDispatcherHandler patchHandler;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
public KubernetesCrudDispatcher() {
this(Collections.emptyList());
}
public KubernetesCrudDispatcher(List<CustomResourceDefinitionContext> crdContexts) {
super(new Context(Serialization.jsonMapper()), new KubernetesAttributesExtractor(), new KubernetesResponseComposer());
this.kubernetesAttributesExtractor = (KubernetesAttributesExtractor) this.attributeExtractor;
this.kubernetesResponseComposer = (KubernetesResponseComposer) this.responseComposer;
watchEventListeners = new CopyOnWriteArraySet<>();
crdProcessor = new CustomResourceDefinitionProcessor();
this.kubernetesAttributesExtractor.setCustomResourceDefinitionProcessor(crdProcessor);
resourceVersion = new AtomicLong();
postHandler = new PostHandler(this.kubernetesAttributesExtractor, this);
putHandler = new PutHandler(this);
patchHandler = new PatchHandler(this);
crdContexts.stream().forEach(this::expectCustomResource);
}
MockResponse process(RecordedRequest request, KubernetesCrudDispatcherHandler handler) {
return withLock(lock.writeLock(), () -> {
try {
return handler.handle(request);
} catch (KubernetesCrudDispatcherException e) {
return new MockResponse().setResponseCode(e.getCode()).setBody(e.toStatusBody());
}
});
}
/**
* Adds the specified object to the in-memory db.
*
* @param request RecordedRequest information.
* @return a MockResponse to be dispatched.
*/
@Override
public MockResponse handleCreate(RecordedRequest request) {
return process(request, postHandler);
}
/**
* Updates the specified object to the in-memory db.
*
* @param request RecordedRequest information.
* @return a MockResponse to be dispatched.
*/
@Override
public MockResponse handleUpdate(RecordedRequest request) {
return process(request, putHandler);
}
/**
* Performs a get for the corresponding object from the in-memory db.
*
* @param path The path.
* @return The {@link MockResponse}
*/
@Override
public MockResponse handleGet(String path) {
return withLock(lock.readLock(), () -> {
if (detectWatchMode(path)) {
return handleWatch(path);
}
return handle(path, null);
});
}
private interface EventProcessor {
void processEvent(String path, AttributeSet pathAttributes, AttributeSet oldAttributes);
}
private MockResponse handle(String path, EventProcessor eventProcessor) {
MockResponse response = new MockResponse();
List<String> items = new ArrayList<>();
AttributeSet query = attributeExtractor.fromPath(path);
new ArrayList<>(map.entrySet()).stream()
.filter(entry -> entry.getKey().matches(query))
.forEach(entry -> {
LOGGER.debug("Entry found for query {} : {}", query, entry);
items.add(entry.getValue());
if (eventProcessor != null) {
eventProcessor.processEvent(path, query, entry.getKey());
}
});
if (query.containsKey(KubernetesAttributesExtractor.NAME)) {
if (!items.isEmpty()) {
response.setBody(items.get(0));
response.setResponseCode(HttpURLConnection.HTTP_OK);
} else {
response.setResponseCode(HttpURLConnection.HTTP_NOT_FOUND);
}
} else {
// if there are no items, and this is a get it may be an api metadata request
String metadataResult = null;
if (items.isEmpty() && eventProcessor == null) {
metadataResult = crdProcessor.getApiResources(path);
}
if (metadataResult != null) {
response.setBody(metadataResult);
} else {
response.setBody(kubernetesResponseComposer.compose(items, String.valueOf(resourceVersion.get())));
}
response.setResponseCode(HttpURLConnection.HTTP_OK);
}
return response;
}
/**
* Patches the specified object to the in-memory db.
*
* @return The {@link MockResponse}
*/
@Override
public MockResponse handlePatch(RecordedRequest request) {
return process(request, patchHandler);
}
/**
* Performs a delete for the corresponding object from the in-memory db.
*
* @param path String
* @return The {@link MockResponse}
*/
@Override
public MockResponse handleDelete(String path) {
return withLock(lock.writeLock(), () -> handle(path, this::processDelete));
}
private void processDelete(String path, AttributeSet pathAttributes, AttributeSet oldAttributes) {
String jsonStringOfResource = map.get(oldAttributes);
final GenericKubernetesResource resource = Serialization.unmarshal(jsonStringOfResource, GenericKubernetesResource.class);
if (resource.getFinalizers().isEmpty()) {
// No finalizers left, actually remove the resource.
processEvent(path, pathAttributes, oldAttributes, null, null);
return;
}
if (!resource.isMarkedForDeletion()) {
// Mark the resource as deleted, but don't remove it yet (wait for finalizer-removal).
resource.getMetadata().setDeletionTimestamp(LocalDateTime.now().toString());
resource.getMetadata().setResourceVersion(String.valueOf(requestResourceVersion()));
String updatedResource = Serialization.asJson(resource);
processEvent(path, pathAttributes, oldAttributes, resource, updatedResource);
return;
}
// else: if the resource is already marked for deletion and still has finalizers, do nothing.
}
@Override
public long requestResourceVersion() {
return resourceVersion.incrementAndGet();
}
@Override
public AttributeSet getKey(String path) {
return kubernetesAttributesExtractor.fromPath(path);
}
@Override
public Map.Entry<AttributeSet, String> findResource(AttributeSet attributes) {
// A lock (either read or write) has already been acquired by the point this method is invoked
return map.entrySet().stream()
.filter(entry -> entry.getKey().matches(attributes))
.findFirst().orElse(null);
}
@Override
public boolean isStatusSubresourceEnabledForResource(String path) {
return crdProcessor.isStatusSubresourceEnabledForResource(kubernetesAttributesExtractor.fromKubernetesPath(path));
}
@Override
public void processEvent(String path, AttributeSet pathAttributes, AttributeSet oldAttributes,
GenericKubernetesResource resource, String newState) {
// A write lock has already been acquired by the point this method is invoked
String existing = map.remove(oldAttributes);
AttributeSet newAttributes = null;
if (newState != null) {
if (resource != null) {
newAttributes = kubernetesAttributesExtractor.extract(resource);
} else {
newAttributes = kubernetesAttributesExtractor.fromResource(newState);
}
// corner case - we need to get the plural from the path
if (!newAttributes.containsKey(KubernetesAttributesExtractor.PLURAL)) {
newAttributes = AttributeSet.merge(pathAttributes, newAttributes);
}
map.put(newAttributes, newState);
}
if (!Objects.equals(existing, newState)) {
AttributeSet finalAttributeSet = newAttributes;
watchEventListeners.forEach(listener -> {
boolean matchesOld = oldAttributes != null && listener.attributeMatches(oldAttributes);
boolean matchesNew = finalAttributeSet != null && listener.attributeMatches(finalAttributeSet);
if (matchesOld && matchesNew) {
listener.sendWebSocketResponse(newState, Action.MODIFIED);
} else if (matchesOld) {
listener.sendWebSocketResponse(existing, Action.DELETED);
} else if (matchesNew) {
listener.sendWebSocketResponse(newState, Action.ADDED);
}
});
crdProcessor.process(path, Utils.getNonNullOrElse(newState, existing), newState == null);
}
}
/**
* Watch the resource list on `path` endpoint
*
* @param path String
* @return The {@link MockResponse}
*/
public MockResponse handleWatch(String path) {
MockResponse mockResponse = new MockResponse();
String resourceName = fetchResourceNameFromWatchRequestPath(path);
AttributeSet query = attributeExtractor.fromPath(path);
if (resourceName != null) {
query = query.add(new Attribute("name", resourceName));
}
WatchEventsListener watchEventListener = new WatchEventsListener(context, query, watchEventListeners, LOGGER,
watch -> withLock(lock.readLock(), () -> map.entrySet().stream()
.filter(entry -> watch.attributeMatches(entry.getKey()))
.forEach(entry -> watch.sendWebSocketResponse(entry.getValue(), Action.ADDED))));
watchEventListeners.add(watchEventListener);
return mockResponse.withWebSocketUpgrade(watchEventListener);
}
private boolean detectWatchMode(String path) {
String queryString = null;
try {
queryString = new URI(path).getQuery();
} catch (URISyntaxException e) {
LOGGER.debug("incorrect URI string: [{}]", path);
return false;
}
if (queryString != null && !queryString.isEmpty()) {
return queryString.contains("watch=true");
}
return false;
}
private String fetchResourceNameFromWatchRequestPath(String path) {
String queryString = null;
try {
queryString = new URI(path).getQuery();
} catch (URISyntaxException e) {
LOGGER.debug("Incorrect URI string: [{}]", path);
return null;
}
if (queryString == null || queryString.isEmpty()) {
return null;
}
String name = "";
for (String q : queryString.split("&")) {
if (q.contains("fieldSelector") && q.contains("metadata.name")) {
String[] s = q.split("=");
name = s[s.length - 1];
}
}
return name.isEmpty() ? null : name;
}
@Override
public void reset() {
withLock(lock.writeLock(), map::clear);
// what about the initial crds? That should likely be deprecated
this.crdProcessor.reset();
}
@Override
public void expectCustomResource(CustomResourceDefinitionContext rdc) {
this.crdProcessor.addCrdContext(rdc);
}
/**
* Run the given task after acquiring the lock.
*
* @param lock a lock to be acquired
* @param task runnable task to execute with the acquired lock
*/
private void withLock(Lock lock, Runnable task) {
withLock(lock, () -> {
task.run();
return null;
});
}
/**
* Retrieve the value provided by the supplier after acquiring the lock.
*
* @param <T> the type supplied by source
* @param lock a lock to be acquired
* @param source supplier giving a return value
* @return the value given by the source supplier
*/
private <T> T withLock(Lock lock, Supplier<T> source) {
lock.lock();
try {
locked(lock);
return source.get();
} finally {
lock.unlock();
}
}
protected void locked(Lock lock) {
// For testing
}
}