Skip to content

Commit

Permalink
fix(k8s): fix realm node not found and deletion constraint violation
Browse files Browse the repository at this point in the history
  • Loading branch information
tthvo committed Apr 15, 2024
1 parent 029b2fb commit bd4ad8e
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/main/java/io/cryostat/discovery/KubeApiDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import io.fabric8.kubernetes.client.informers.SharedIndexInformer;
import io.quarkus.runtime.ShutdownEvent;
import io.quarkus.runtime.StartupEvent;
import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
Expand Down Expand Up @@ -113,7 +114,7 @@ protected HashMap<String, SharedIndexInformer<Endpoints>> initialize()
};

@Transactional
void onStart(@Observes StartupEvent evt) {
void onStart(@Observes @Priority(1) StartupEvent evt) {
if (!(enabled())) {
return;
}
Expand All @@ -135,8 +136,14 @@ void onStart(@Observes StartupEvent evt) {
}

logger.infov("Starting {0} client", REALM);
}

safeGetInformers(); // trigger lazy init
void initializeInformers(@Observes StartupEvent evt) {
try {
nsInformers.get();
} catch (ConcurrentException e) {
throw new IllegalStateException(e);
}
}

void onStop(@Observes ShutdownEvent evt) {
Expand Down Expand Up @@ -165,16 +172,6 @@ KubernetesClient client() {
return kubeConfig.kubeClient();
}

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 isCompatiblePort(EndpointPort port) {
return JmxPortNames.orElse(List.of()).contains(port.getName())
|| JmxPortNumbers.orElse(List.of()).contains(port.getPort());
Expand Down Expand Up @@ -236,18 +233,26 @@ private void pruneOwnerChain(DiscoveryNode nsNode, TargetTuple targetTuple) {
Target target = Target.getTargetByConnectUrl(targetTuple.toTarget().connectUrl);
DiscoveryNode targetNode = target.discoveryNode;

Pair<HasMetadata, DiscoveryNode> node = Pair.of(null, targetNode);
while (true) {
Pair<HasMetadata, DiscoveryNode> owner = getOwnerNode(node);
if (owner == null) {
break;
}
DiscoveryNode ownerNode = owner.getRight();
if (node.getRight().children.isEmpty()) {
ownerNode.children.remove(node.getRight());
ownerNode.persist();
if (nsNode.children.contains(targetNode)) {
nsNode.children.remove(targetNode);
} else {
Pair<HasMetadata, DiscoveryNode> node =
queryForNode(
targetRef.getNamespace(), targetRef.getName(), targetRef.getKind());
node.getRight().children.remove(targetNode);
node.getRight().persist();
while (true) {
Pair<HasMetadata, DiscoveryNode> owner = getOwnerNode(node);
if (owner == null) {
break;
}
DiscoveryNode ownerNode = owner.getRight();
if (node.getRight().children.isEmpty()) {
ownerNode.children.remove(node.getRight());
ownerNode.persist();
}
node = owner;
}
node = owner;
}

target.delete();
Expand Down

0 comments on commit bd4ad8e

Please sign in to comment.