From 860bd909b3aa6d0bd804f3034c562082489a6771 Mon Sep 17 00:00:00 2001 From: Andrej Vano Date: Wed, 8 Jan 2025 14:15:56 +0100 Subject: [PATCH] [IBMMQ] Wait until openshift namespace annotations are present --- .../mq/resource/openshift/OpenshiftIBMMQ.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/resource/openshift/OpenshiftIBMMQ.java b/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/resource/openshift/OpenshiftIBMMQ.java index cd3da9eb2..caacb5bb9 100644 --- a/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/resource/openshift/OpenshiftIBMMQ.java +++ b/system-x/services/jms/ibm-mq/src/main/java/software/tnb/jms/ibm/mq/resource/openshift/OpenshiftIBMMQ.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ThreadLocalRandom; +import java.util.function.BooleanSupplier; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -113,10 +114,9 @@ public void create() { * What this does it gets the uid interval from the namespace annotation and generates a random uid from that range and "hardcodes" that id * in the deployment config and changes the auth configuration */ - final List uidRange = - Arrays.stream(OpenshiftClient.get().namespaces().withName(OpenshiftClient.get().getNamespace()).get().getMetadata() - .getAnnotations().get("openshift.io/sa.scc.uid-range").split("/")).map(Long::parseLong).toList(); - uid = ThreadLocalRandom.current().nextLong(uidRange.get(0), uidRange.get(0) + uidRange.get(1)); + UidSupplier supplier = new UidSupplier(); + WaitUtils.waitFor(supplier, "Waiting for uid-range openshift annotation"); + uid = supplier.getUid(); createMqscConfigMap(); @@ -261,4 +261,29 @@ private int microshiftClientPort() { .filter(servicePort -> name().equals(servicePort.getName())) .findFirst().get().getNodePort(); } + + /** + * In some cases the code was quick enough and the annotations were empty, so moved the uid-range logic into a separate supplier. + */ + private static final class UidSupplier implements BooleanSupplier { + private long uid; + + @Override + public boolean getAsBoolean() { + Map annotations = OpenshiftClient.get().namespaces().withName(OpenshiftClient.get().getNamespace()).get().getMetadata() + .getAnnotations(); + + if (annotations == null || annotations.isEmpty()) { + return false; + } + + final List uidRange = Arrays.stream(annotations.get("openshift.io/sa.scc.uid-range").split("/")).map(Long::parseLong).toList(); + uid = ThreadLocalRandom.current().nextLong(uidRange.get(0), uidRange.get(0) + uidRange.get(1)); + return true; + } + + public long getUid() { + return uid; + } + } }