Skip to content

Commit 762027a

Browse files
committed
Apply manifests from URL and await resources with readiness
1 parent cbc2cde commit 762027a

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

extensions/kubernetes-client/deployment/src/main/java/io/quarkus/kubernetes/client/deployment/DevServicesKubernetesProcessor.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
import java.io.ByteArrayOutputStream;
1212
import java.io.IOException;
1313
import java.io.InputStream;
14+
import java.net.URL;
1415
import java.time.Duration;
15-
import java.util.Base64;
16-
import java.util.List;
17-
import java.util.Map;
18-
import java.util.Objects;
19-
import java.util.Optional;
16+
import java.util.*;
17+
import java.util.concurrent.TimeUnit;
2018
import java.util.function.Function;
2119
import java.util.function.Supplier;
2220
import java.util.stream.Collectors;
@@ -45,8 +43,12 @@
4543
import com.github.dockerjava.api.DockerClient;
4644
import com.github.dockerjava.api.command.InspectContainerResponse;
4745

48-
import io.fabric8.kubernetes.api.model.HasMetadata;
46+
import io.fabric8.kubernetes.api.model.*;
47+
import io.fabric8.kubernetes.api.model.apps.Deployment;
48+
import io.fabric8.kubernetes.api.model.apps.ReplicaSet;
49+
import io.fabric8.kubernetes.api.model.apps.StatefulSet;
4950
import io.fabric8.kubernetes.client.*;
51+
import io.fabric8.kubernetes.client.Config;
5052
import io.quarkus.deployment.Feature;
5153
import io.quarkus.deployment.IsDevServicesSupportedByLaunchMode;
5254
import io.quarkus.deployment.annotations.BuildProducer;
@@ -196,10 +198,7 @@ public void applyManifests(
196198
.withConfig(Config.fromKubeconfig(kubernetesDevServiceInfoBuildItem.getKubeConfig()))
197199
.build()) {
198200
for (String manifestPath : manifests.get()) {
199-
// Load the manifest from the resources directory
200-
InputStream manifestStream = Thread.currentThread()
201-
.getContextClassLoader()
202-
.getResourceAsStream(manifestPath);
201+
InputStream manifestStream = getManifestStream(manifestPath);
203202

204203
if (manifestStream == null) {
205204
log.errorf("Could not find manifest file in resources: %s", manifestPath);
@@ -210,11 +209,25 @@ public void applyManifests(
210209
try {
211210
// A single manifest file may contain multiple resources to deploy
212211
List<HasMetadata> resources = client.load(manifestStream).items();
212+
List<HasMetadata> resourcesWithReadiness = new ArrayList<>();
213213

214214
if (resources.isEmpty()) {
215215
log.warnf("No resources found in manifest: %s", manifestPath);
216216
} else {
217-
resources.forEach(resource -> client.resource(resource).create());
217+
resources.forEach(resource -> {
218+
client.resource(resource).create();
219+
220+
if (isReadinessApplicable(resource)) {
221+
resourcesWithReadiness.add(resource);
222+
}
223+
});
224+
225+
resourcesWithReadiness.forEach(resource -> {
226+
log.info("Waiting for " + resource.getClass().getSimpleName() + " "
227+
+ resource.getMetadata().getName()
228+
+ " to be ready...");
229+
client.resource(resource).waitUntilReady(60, TimeUnit.SECONDS);
230+
});
218231
}
219232
} catch (Exception ex) {
220233
log.errorf("Failed to deploy manifest %s: %s", manifestPath, ex.getMessage());
@@ -228,6 +241,29 @@ public void applyManifests(
228241
}
229242
}
230243

244+
private boolean isReadinessApplicable(HasMetadata item) {
245+
return (item instanceof Deployment ||
246+
item instanceof io.fabric8.kubernetes.api.model.extensions.Deployment ||
247+
item instanceof ReplicaSet ||
248+
item instanceof Pod ||
249+
item instanceof ReplicationController ||
250+
item instanceof Endpoints ||
251+
item instanceof Node ||
252+
item instanceof StatefulSet);
253+
}
254+
255+
private InputStream getManifestStream(String manifestPath) throws IOException {
256+
if (manifestPath.startsWith("http://") || manifestPath.startsWith("https://")) {
257+
// Option 1: The manifest is a URL, in which case we download it
258+
return new URL(manifestPath).openStream();
259+
} else {
260+
// Option 2: The manifest is a file in the resources directory
261+
return Thread.currentThread()
262+
.getContextClassLoader()
263+
.getResourceAsStream(manifestPath);
264+
}
265+
}
266+
231267
private void shutdownCluster() {
232268
if (devService != null && devService.isOwner()) {
233269
try {

0 commit comments

Comments
 (0)