|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.deploy.k8s.integrationtest |
| 18 | + |
| 19 | +import java.net.{URI, URL} |
| 20 | +import java.nio.file.Paths |
| 21 | +import java.util.UUID |
| 22 | + |
| 23 | +import io.fabric8.kubernetes.api.model.{Endpoints, Pod, Service} |
| 24 | +import org.apache.http.client.utils.URIBuilder |
| 25 | + |
| 26 | +private[spark] object SparkExamplesFileServerRunner { |
| 27 | + |
| 28 | + private val fileServerImage = System.getProperty( |
| 29 | + "spark.docker.test.fileServerImage", "spark-examples-file-server:latest") |
| 30 | + private val fileServerExampleJarsDir = Paths.get("docker-file-server", "jars") |
| 31 | + require( |
| 32 | + fileServerExampleJarsDir |
| 33 | + .toFile |
| 34 | + .listFiles() |
| 35 | + .exists(file => file.getName.startsWith("spark-examples")), |
| 36 | + s"No spark-examples jar found in $fileServerExampleJarsDir.") |
| 37 | + require( |
| 38 | + fileServerExampleJarsDir |
| 39 | + .toFile |
| 40 | + .listFiles() |
| 41 | + .count(file => file.getName.startsWith("spark-examples")) == 1, |
| 42 | + s"Multiple spark-examples jars found in $fileServerExampleJarsDir.") |
| 43 | + private val fileServerExampleJar = Paths.get("docker-file-server", "jars") |
| 44 | + .toFile |
| 45 | + .listFiles() |
| 46 | + .filter(file => file.getName.startsWith("spark-examples"))(0) |
| 47 | + .getName |
| 48 | + private val fileServerPodLocatorLabelKey = "fileServerLocator" |
| 49 | + private val fileServerPodLocatorLabelValue = UUID.randomUUID().toString.replaceAll("-", "") |
| 50 | + private val fileServerName = "spark-examples-file-server" |
| 51 | + |
| 52 | + def launchServerAndGetUriForExamplesJar( |
| 53 | + kubernetesTestComponents: KubernetesTestComponents): URI = { |
| 54 | + val podReadinessWatcher = new SparkReadinessWatcher[Pod] |
| 55 | + Utils.tryWithResource( |
| 56 | + kubernetesTestComponents |
| 57 | + .kubernetesClient |
| 58 | + .pods() |
| 59 | + .withName(fileServerName) |
| 60 | + .watch(podReadinessWatcher)) { _ => |
| 61 | + kubernetesTestComponents.kubernetesClient.pods().createNew() |
| 62 | + .withNewMetadata() |
| 63 | + .withName(fileServerName) |
| 64 | + .addToLabels(fileServerPodLocatorLabelKey, fileServerPodLocatorLabelValue) |
| 65 | + .endMetadata() |
| 66 | + .withNewSpec() |
| 67 | + .addNewContainer() |
| 68 | + .withName("main") |
| 69 | + .withImage(fileServerImage) |
| 70 | + .withImagePullPolicy("Never") |
| 71 | + .withNewReadinessProbe() |
| 72 | + .withNewHttpGet() |
| 73 | + .withNewPort(80) |
| 74 | + .withPath("/ping") |
| 75 | + .endHttpGet() |
| 76 | + .endReadinessProbe() |
| 77 | + .endContainer() |
| 78 | + .endSpec() |
| 79 | + .done() |
| 80 | + podReadinessWatcher.waitUntilReady() |
| 81 | + } |
| 82 | + val endpointsReadinessWatcher = new SparkReadinessWatcher[Endpoints] |
| 83 | + Utils.tryWithResource( |
| 84 | + kubernetesTestComponents |
| 85 | + .kubernetesClient |
| 86 | + .endpoints() |
| 87 | + .withName(fileServerName) |
| 88 | + .watch(endpointsReadinessWatcher)) { _ => |
| 89 | + kubernetesTestComponents.kubernetesClient.services().createNew() |
| 90 | + .withNewMetadata() |
| 91 | + .withName(fileServerName) |
| 92 | + .endMetadata() |
| 93 | + .withNewSpec() |
| 94 | + .addToSelector(fileServerPodLocatorLabelKey, fileServerPodLocatorLabelValue) |
| 95 | + .addNewPort() |
| 96 | + .withName("file-server-port") |
| 97 | + .withNewTargetPort(80) |
| 98 | + .withPort(80) |
| 99 | + .endPort() |
| 100 | + .withType("NodePort") |
| 101 | + .endSpec() |
| 102 | + .done() |
| 103 | + endpointsReadinessWatcher.waitUntilReady() |
| 104 | + } |
| 105 | + val resolvedNodePort = kubernetesTestComponents |
| 106 | + .kubernetesClient |
| 107 | + .services() |
| 108 | + .withName(fileServerName) |
| 109 | + .get() |
| 110 | + .getSpec |
| 111 | + .getPorts |
| 112 | + .get(0) |
| 113 | + .getNodePort |
| 114 | + val masterHostname = URI.create(kubernetesTestComponents.clientConfig.getMasterUrl).getHost |
| 115 | + new URIBuilder() |
| 116 | + .setHost(masterHostname) |
| 117 | + .setPort(resolvedNodePort) |
| 118 | + .setScheme("http") |
| 119 | + .setPath(s"/$fileServerExampleJar") |
| 120 | + .build() |
| 121 | + } |
| 122 | +} |
0 commit comments