-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathKubeApiDiscovery.java
732 lines (634 loc) · 28 KB
/
KubeApiDiscovery.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*
* Copyright The Cryostat Authors.
*
* 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.cryostat.discovery;
import java.net.URI;
import java.nio.file.Path;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.management.remote.JMXServiceURL;
import io.cryostat.core.sys.FileSystem;
import io.cryostat.targets.Target;
import io.cryostat.targets.Target.Annotations;
import io.cryostat.targets.Target.EventKind;
import io.fabric8.kubernetes.api.model.EndpointAddress;
import io.fabric8.kubernetes.api.model.EndpointPort;
import io.fabric8.kubernetes.api.model.EndpointSubset;
import io.fabric8.kubernetes.api.model.Endpoints;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ObjectReference;
import io.fabric8.kubernetes.api.model.OwnerReference;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.quarkus.narayana.jta.QuarkusTransaction;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import io.quarkus.vertx.ConsumeEvent;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import io.vertx.mutiny.core.eventbus.EventBus;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.persistence.NoResultException;
import jakarta.transaction.Transactional;
import jakarta.transaction.Transactional.TxType;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.ConcurrentException;
import org.apache.commons.lang3.concurrent.LazyInitializer;
import org.apache.commons.lang3.tuple.Pair;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;
@ApplicationScoped
public class KubeApiDiscovery {
public static final String REALM = "KubernetesApi";
public static final String DISCOVERY_NAMESPACE_LABEL_KEY = "discovery.cryostat.io/namespace";
private static final List<String> EMPTY_PORT_NAMES = new ArrayList<>();
private static final List<Integer> EMPTY_PORT_NUMBERS = new ArrayList<>();
@Inject Logger logger;
@Inject KubeConfig kubeConfig;
@Inject EventBus bus;
@ConfigProperty(name = "cryostat.discovery.kubernetes.enabled")
boolean enabled;
@ConfigProperty(name = "cryostat.discovery.kubernetes.port-names")
Optional<List<String>> jmxPortNames;
@ConfigProperty(name = "cryostat.discovery.kubernetes.port-numbers")
Optional<List<Integer>> jmxPortNumbers;
@ConfigProperty(name = "cryostat.discovery.kubernetes.resync-period")
Duration informerResyncPeriod;
private final LazyInitializer<HashMap<String, SharedIndexInformer<Endpoints>>> nsInformers =
new LazyInitializer<HashMap<String, SharedIndexInformer<Endpoints>>>() {
@Override
protected HashMap<String, SharedIndexInformer<Endpoints>> initialize()
throws ConcurrentException {
// TODO: add support for some wildcard indicating a single Informer for any
// namespace that Cryostat has permissions to. This will need some restructuring
// of how the namespaces within the discovery tree are mapped.
var result = new HashMap<String, SharedIndexInformer<Endpoints>>();
kubeConfig
.getWatchNamespaces()
.forEach(
ns -> {
result.put(
ns,
client().endpoints()
.inNamespace(ns)
.inform(
new EndpointsHandler(),
informerResyncPeriod.toMillis()));
logger.infov(
"Started Endpoints SharedInformer for"
+ " namespace \"{0}\"",
ns);
});
return result;
}
};
// Priority is set higher than default 0 such that onStart is called first before onAfterStart
// This ensures realm node is persisted before initializing informers
@Transactional
void onStart(@Observes @Priority(1) StartupEvent evt) {
if (!enabled()) {
return;
}
if (!available()) {
logger.errorv("{0} enabled but environment is not Kubernetes!", getClass().getName());
return;
}
DiscoveryNode universe = DiscoveryNode.getUniverse();
if (DiscoveryNode.getRealm(REALM).isEmpty()) {
DiscoveryPlugin plugin = new DiscoveryPlugin();
DiscoveryNode node = DiscoveryNode.environment(REALM, BaseNodeType.REALM);
plugin.realm = node;
plugin.builtin = true;
universe.children.add(node);
node.parent = universe;
plugin.persist();
universe.persist();
}
logger.infov("Starting {0} client", REALM);
}
@Transactional
void onAfterStart(@Observes StartupEvent evt) {
if (!enabled() || !available()) {
return;
}
safeGetInformers();
}
void onStop(@Observes ShutdownEvent evt) {
if (!(enabled() && available())) {
return;
}
logger.infov("Shutting down {0} client", REALM);
safeGetInformers()
.forEach(
(ns, informer) -> {
informer.close();
logger.infov(
"Closed Endpoints SharedInformer for namespace \"{0}\"", ns);
});
}
boolean enabled() {
return enabled;
}
boolean available() {
try {
boolean hasNamespace = StringUtils.isNotBlank(kubeConfig.getOwnNamespace());
return kubeConfig.kubeApiAvailable() && hasNamespace;
} catch (Exception e) {
logger.info(e);
}
return false;
}
KubernetesClient client() {
KubernetesClient client;
try {
client = kubeConfig.kubeClient();
} catch (ConcurrentException e) {
throw new IllegalStateException(e);
}
return client;
}
private boolean isCompatiblePort(EndpointPort port) {
return jmxPortNames.orElse(EMPTY_PORT_NAMES).contains(port.getName())
|| jmxPortNumbers.orElse(EMPTY_PORT_NUMBERS).contains(port.getPort());
}
List<TargetTuple> tuplesFromEndpoints(Endpoints endpoints) {
List<TargetTuple> tts = new ArrayList<>();
for (EndpointSubset subset : endpoints.getSubsets()) {
for (EndpointPort port : subset.getPorts()) {
for (EndpointAddress addr : subset.getAddresses()) {
tts.add(new TargetTuple(addr.getTargetRef(), addr, port));
}
}
}
return tts;
}
private List<TargetTuple> getTargetTuplesFrom(Endpoints endpoints) {
return tuplesFromEndpoints(endpoints).stream()
.filter(
(ref) -> {
return Objects.nonNull(ref) && isCompatiblePort(ref.port);
})
.collect(Collectors.toList());
}
private Map<String, SharedIndexInformer<Endpoints>> safeGetInformers() {
Map<String, SharedIndexInformer<Endpoints>> informers;
try {
informers = nsInformers.get();
} catch (ConcurrentException e) {
throw new IllegalStateException(e);
}
return informers;
}
private boolean isTargetUnderRealm(URI connectUrl) throws IllegalStateException {
// Check for any targets with the same connectUrl in other realms
try {
Target persistedTarget = Target.getTargetByConnectUrl(connectUrl);
String realmOfTarget = persistedTarget.annotations.cryostat().get("REALM");
if (!REALM.equals(realmOfTarget)) {
logger.warnv(
"Expected persisted target with serviceURL {0} to be under realm"
+ " {1} but found under {2} ",
persistedTarget.connectUrl, REALM, realmOfTarget);
throw new IllegalStateException();
}
return true;
} catch (NoResultException e) {
}
return false;
}
@ConsumeEvent(blocking = true, ordered = true)
@Transactional(TxType.REQUIRES_NEW)
public void handleEndpointEvent(EndpointDiscoveryEvent evt) {
String namespace = evt.namespace;
DiscoveryNode realm = DiscoveryNode.getRealm(REALM).orElseThrow();
DiscoveryNode nsNode =
DiscoveryNode.getChild(realm, n -> n.name.equals(namespace))
.orElse(
DiscoveryNode.environment(
namespace, KubeDiscoveryNodeType.NAMESPACE));
try {
if (evt.eventKind == EventKind.FOUND) {
buildOwnerChain(nsNode, evt.target, evt.objRef);
} else {
pruneOwnerChain(nsNode, evt.target);
}
if (!nsNode.hasChildren()) {
realm.children.remove(nsNode);
nsNode.parent = null;
} else if (!realm.children.contains(nsNode)) {
realm.children.add(nsNode);
nsNode.parent = realm;
}
realm.persist();
} catch (Exception e) {
logger.warn("Endpoint handler exception", e);
}
}
private void handleObservedEndpoints(String namespace) {
List<DiscoveryNode> targetNodes =
DiscoveryNode.findAllByNodeType(KubeDiscoveryNodeType.ENDPOINT).stream()
.filter(
(n) ->
namespace.equals(
n.labels.get(DISCOVERY_NAMESPACE_LABEL_KEY)))
.collect(Collectors.toList());
Map<URI, ObjectReference> targetRefMap = new HashMap<>();
Set<Target> persistedTargets = new HashSet<>();
for (DiscoveryNode node : targetNodes) {
persistedTargets.add(node.target);
}
Set<Target> observedTargets =
safeGetInformers().get(namespace).getStore().list().stream()
.map((endpoint) -> getTargetTuplesFrom(endpoint))
.flatMap(List::stream)
.filter((tuple) -> Objects.nonNull(tuple.objRef))
.map(
(tuple) -> {
Target t = tuple.toTarget();
if (t != null) {
targetRefMap.put(t.connectUrl, tuple.objRef);
}
return t;
})
.filter(Objects::nonNull)
.collect(Collectors.toSet());
// Add new targets
Target.compare(persistedTargets)
.to(observedTargets)
.added()
.forEach(
(t) ->
notify(
EndpointDiscoveryEvent.from(
namespace,
t,
targetRefMap.get(t.connectUrl),
EventKind.FOUND)));
// Prune deleted targets
Target.compare(persistedTargets)
.to(observedTargets)
.removed()
.forEach(
(t) ->
notify(
EndpointDiscoveryEvent.from(
namespace, t, null, EventKind.LOST)));
}
private void notify(EndpointDiscoveryEvent evt) {
bus.publish(KubeApiDiscovery.class.getName(), evt);
}
private void pruneOwnerChain(DiscoveryNode nsNode, Target target) {
if (!isTargetUnderRealm(target.connectUrl)) {
logger.infov(
"Target with serviceURL {0} does not exist in discovery tree. Skipped deleting",
target.connectUrl);
return;
}
// Retrieve the latest snapshot of the target
// The target received from event message is outdated as it belongs to the previous
// transaction
target = Target.getTargetByConnectUrl(target.connectUrl);
DiscoveryNode child = target.discoveryNode;
while (true) {
DiscoveryNode parent = child.parent;
if (parent == null) {
break;
}
parent.children.remove(child);
child.parent = null;
parent.persist();
if (parent.hasChildren()
|| parent.nodeType.equals(KubeDiscoveryNodeType.NAMESPACE.getKind())) {
break;
}
child = parent;
}
nsNode.persist();
target.delete();
}
private void buildOwnerChain(DiscoveryNode nsNode, Target target, ObjectReference targetRef) {
if (isTargetUnderRealm(target.connectUrl)) {
logger.infov(
"Target with serviceURL {0} already exist in discovery tree. Skipped adding",
target.connectUrl);
return;
}
String targetKind = targetRef.getKind();
KubeDiscoveryNodeType targetType = KubeDiscoveryNodeType.fromKubernetesKind(targetKind);
DiscoveryNode targetNode = DiscoveryNode.target(target, KubeDiscoveryNodeType.ENDPOINT);
target.discoveryNode = targetNode;
target.persist();
if (targetType == KubeDiscoveryNodeType.POD) {
// if the Endpoint points to a Pod, chase the owner chain up as far as possible, then
// add that to the Namespace
Pair<HasMetadata, DiscoveryNode> pod =
queryForNode(
targetRef.getNamespace(), targetRef.getName(), targetRef.getKind());
pod.getRight().children.add(targetNode);
targetNode.parent = pod.getRight();
pod.getRight().persist();
Pair<HasMetadata, DiscoveryNode> child = pod;
while (true) {
Pair<HasMetadata, DiscoveryNode> owner = getOwnerNode(child);
if (owner == null) {
break;
}
DiscoveryNode ownerNode = owner.getRight();
DiscoveryNode childNode = child.getRight();
if (!ownerNode.children.contains(childNode)) {
ownerNode.children.add(childNode);
}
childNode.parent = ownerNode;
ownerNode.persist();
childNode.persist();
child = owner;
}
nsNode.children.add(child.getRight());
child.getRight().parent = nsNode;
} else {
// if the Endpoint points to something else(?) than a Pod, just add the target straight
// to the Namespace
nsNode.children.add(targetNode);
targetNode.parent = nsNode;
targetNode.persist();
}
nsNode.persist();
}
private Pair<HasMetadata, DiscoveryNode> getOwnerNode(Pair<HasMetadata, DiscoveryNode> child) {
HasMetadata childRef = child.getLeft();
if (childRef == null) {
return null;
}
List<OwnerReference> owners = childRef.getMetadata().getOwnerReferences();
// Take first "expected" owner Kind from NodeTypes, or if none, simply use the first owner.
// If there are no owners then return null to signify this and break the chain
if (owners.isEmpty()) {
return null;
}
String namespace = childRef.getMetadata().getNamespace();
OwnerReference owner =
owners.stream()
.filter(o -> KubeDiscoveryNodeType.fromKubernetesKind(o.getKind()) != null)
.findFirst()
.orElse(owners.get(0));
return queryForNode(namespace, owner.getName(), owner.getKind());
}
private Pair<HasMetadata, DiscoveryNode> queryForNode(
String namespace, String name, String kind) {
KubeDiscoveryNodeType nodeType = KubeDiscoveryNodeType.fromKubernetesKind(kind);
if (nodeType == null) {
return null;
}
HasMetadata kubeObj =
nodeType.getQueryFunction().apply(client()).apply(namespace).apply(name);
DiscoveryNode node =
DiscoveryNode.getNode(
n -> {
return nodeType.getKind().equals(n.nodeType)
&& name.equals(n.name)
&& namespace.equals(
n.labels.get(DISCOVERY_NAMESPACE_LABEL_KEY));
})
.orElseGet(
() -> {
DiscoveryNode newNode = new DiscoveryNode();
newNode.name = name;
newNode.nodeType = nodeType.getKind();
newNode.children = new ArrayList<>();
newNode.target = null;
newNode.labels =
kubeObj != null
? kubeObj.getMetadata().getLabels()
: new HashMap<>();
// Add namespace to label to retrieve node later
newNode.labels.put(DISCOVERY_NAMESPACE_LABEL_KEY, namespace);
return newNode;
});
return Pair.of(kubeObj, node);
}
@ApplicationScoped
static final class KubeConfig {
private static final String OWN_NAMESPACE = ".";
@Inject Logger logger;
@Inject FileSystem fs;
@ConfigProperty(name = "cryostat.discovery.kubernetes.namespaces")
Optional<List<String>> watchNamespaces;
@ConfigProperty(name = "kubernetes.service.host")
Optional<String> serviceHost;
@ConfigProperty(name = "cryostat.discovery.kubernetes.namespace-path")
String namespacePath;
private final LazyInitializer<KubernetesClient> kubeClient =
new LazyInitializer<KubernetesClient>() {
@Override
protected KubernetesClient initialize() throws ConcurrentException {
return new KubernetesClientBuilder()
.withTaskExecutor(Infrastructure.getDefaultWorkerPool())
.build();
}
};
Collection<String> getWatchNamespaces() {
return watchNamespaces.orElse(List.of()).stream()
.map(
n -> {
if (OWN_NAMESPACE.equals(n)) {
return getOwnNamespace();
}
return n;
})
.filter(StringUtils::isNotBlank)
.collect(Collectors.toSet());
}
String getOwnNamespace() {
try {
return fs.readString(Path.of(namespacePath));
} catch (Exception e) {
logger.trace(e);
return null;
}
}
boolean kubeApiAvailable() {
return StringUtils.isNotBlank(serviceHost.orElse(""));
}
KubernetesClient kubeClient() throws ConcurrentException {
return kubeClient.get();
}
}
private final class EndpointsHandler implements ResourceEventHandler<Endpoints> {
@Override
public void onAdd(Endpoints endpoints) {
logger.debugv(
"Endpoint {0} created in namespace {1}",
endpoints.getMetadata().getName(), endpoints.getMetadata().getNamespace());
QuarkusTransaction.joiningExisting()
.run(() -> handleObservedEndpoints(endpoints.getMetadata().getNamespace()));
}
@Override
public void onUpdate(Endpoints oldEndpoints, Endpoints newEndpoints) {
logger.debugv(
"Endpoint {0} modified in namespace {1}",
newEndpoints.getMetadata().getName(),
newEndpoints.getMetadata().getNamespace());
QuarkusTransaction.joiningExisting()
.run(() -> handleObservedEndpoints(newEndpoints.getMetadata().getNamespace()));
}
@Override
public void onDelete(Endpoints endpoints, boolean deletedFinalStateUnknown) {
logger.debugv(
"Endpoint {0} deleted in namespace {1}",
endpoints.getMetadata().getName(), endpoints.getMetadata().getNamespace());
if (deletedFinalStateUnknown) {
logger.warnv("Deleted final state unknown: {0}", endpoints);
return;
}
QuarkusTransaction.joiningExisting()
.run(() -> handleObservedEndpoints(endpoints.getMetadata().getNamespace()));
}
}
private static record EndpointDiscoveryEvent(
String namespace, Target target, ObjectReference objRef, EventKind eventKind) {
static EndpointDiscoveryEvent from(
String namespace, Target target, ObjectReference objRef, EventKind eventKind) {
return new EndpointDiscoveryEvent(namespace, target, objRef, eventKind);
}
}
private class TargetTuple {
ObjectReference objRef;
EndpointAddress addr;
EndpointPort port;
TargetTuple(ObjectReference objRef, EndpointAddress addr, EndpointPort port) {
this.objRef = objRef;
this.addr = addr;
this.port = port;
}
public Target toTarget() {
try {
String ip = addr.getIp().replaceAll("\\.", "-");
String namespace = objRef.getNamespace();
boolean isPod = objRef.getKind().equals(KubeDiscoveryNodeType.POD.getKind());
String host = String.format("%s.%s", ip, namespace);
if (isPod) {
host = String.format("%s.pod", host);
}
JMXServiceURL jmxUrl =
new JMXServiceURL(
"rmi",
"",
0,
"/jndi/rmi://" + host + ':' + port.getPort() + "/jmxrmi");
URI connectUrl = URI.create(jmxUrl.toString());
Pair<HasMetadata, DiscoveryNode> pair =
queryForNode(namespace, objRef.getName(), objRef.getKind());
HasMetadata obj = pair.getLeft();
Target target = new Target();
target.activeRecordings = new ArrayList<>();
target.connectUrl = connectUrl;
target.alias = objRef.getName();
target.labels = obj != null ? obj.getMetadata().getLabels() : new HashMap<>();
target.annotations = new Annotations();
target.annotations
.platform()
.putAll(obj != null ? obj.getMetadata().getAnnotations() : Map.of());
target.annotations
.cryostat()
.putAll(
Map.of(
"REALM",
REALM,
"HOST",
addr.getIp(),
"PORT",
Integer.toString(port.getPort()),
"NAMESPACE",
objRef.getNamespace(),
isPod ? "POD_NAME" : "OBJECT_NAME",
objRef.getName()));
return target;
} catch (Exception e) {
logger.warn("Target conversion exception", e);
return null;
}
}
}
}
enum KubeDiscoveryNodeType implements NodeType {
NAMESPACE("Namespace"),
STATEFULSET(
"StatefulSet",
c -> ns -> n -> c.apps().statefulSets().inNamespace(ns).withName(n).get()),
DAEMONSET("DaemonSet", c -> ns -> n -> c.apps().daemonSets().inNamespace(ns).withName(n).get()),
DEPLOYMENT(
"Deployment", c -> ns -> n -> c.apps().deployments().inNamespace(ns).withName(n).get()),
REPLICASET(
"ReplicaSet", c -> ns -> n -> c.apps().replicaSets().inNamespace(ns).withName(n).get()),
REPLICATIONCONTROLLER(
"ReplicationController",
c -> ns -> n -> c.replicationControllers().inNamespace(ns).withName(n).get()),
POD("Pod", c -> ns -> n -> c.pods().inNamespace(ns).withName(n).get()),
ENDPOINT("Endpoint", c -> ns -> n -> c.endpoints().inNamespace(ns).withName(n).get()),
// OpenShift resources
DEPLOYMENTCONFIG("DeploymentConfig"),
;
private final String kubernetesKind;
private final transient Function<
KubernetesClient, Function<String, Function<String, ? extends HasMetadata>>>
getFn;
KubeDiscoveryNodeType(String kubernetesKind) {
this(kubernetesKind, client -> namespace -> name -> null);
}
KubeDiscoveryNodeType(
String kubernetesKind,
Function<KubernetesClient, Function<String, Function<String, ? extends HasMetadata>>>
getFn) {
this.kubernetesKind = kubernetesKind;
this.getFn = getFn;
}
@Override
public String getKind() {
return kubernetesKind;
}
public Function<KubernetesClient, Function<String, Function<String, ? extends HasMetadata>>>
getQueryFunction() {
return getFn;
}
public static KubeDiscoveryNodeType fromKubernetesKind(String kubernetesKind) {
if (kubernetesKind == null) {
return null;
}
for (KubeDiscoveryNodeType nt : values()) {
if (kubernetesKind.equalsIgnoreCase(nt.kubernetesKind)) {
return nt;
}
}
return null;
}
@Override
public String toString() {
return getKind();
}
}