|
| 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.io.{File, PrintWriter} |
| 20 | + |
| 21 | +import scala.collection.JavaConverters._ |
| 22 | + |
| 23 | +import io.fabric8.kubernetes.api.model._ |
| 24 | +import io.fabric8.kubernetes.api.model.storage.StorageClassBuilder |
| 25 | +import org.scalatest.Tag |
| 26 | +import org.scalatest.concurrent.{Eventually, PatienceConfiguration} |
| 27 | +import org.scalatest.time.{Milliseconds, Span} |
| 28 | + |
| 29 | +import org.apache.spark.deploy.k8s.integrationtest.KubernetesSuite._ |
| 30 | + |
| 31 | +private[spark] trait PVTestsSuite { k8sSuite: KubernetesSuite => |
| 32 | + import PVTestsSuite._ |
| 33 | + |
| 34 | + private def setupLocalStorage(): Unit = { |
| 35 | + val scBuilder = new StorageClassBuilder() |
| 36 | + .withKind("StorageClass") |
| 37 | + .withApiVersion("storage.k8s.io/v1") |
| 38 | + .withNewMetadata() |
| 39 | + .withName(STORAGE_NAME) |
| 40 | + .endMetadata() |
| 41 | + .withProvisioner("kubernetes.io/no-provisioner") |
| 42 | + .withVolumeBindingMode("WaitForFirstConsumer") |
| 43 | + |
| 44 | + val pvBuilder = new PersistentVolumeBuilder() |
| 45 | + .withKind("PersistentVolume") |
| 46 | + .withApiVersion("v1") |
| 47 | + .withNewMetadata() |
| 48 | + .withName("test-local-pv") |
| 49 | + .endMetadata() |
| 50 | + .withNewSpec() |
| 51 | + .withCapacity(Map("storage" -> new QuantityBuilder().withAmount("1Gi").build()).asJava) |
| 52 | + .withAccessModes("ReadWriteOnce") |
| 53 | + .withPersistentVolumeReclaimPolicy("Retain") |
| 54 | + .withStorageClassName("test-local-storage") |
| 55 | + .withLocal(new LocalVolumeSourceBuilder().withPath(VM_PATH).build()) |
| 56 | + .withNewNodeAffinity() |
| 57 | + .withNewRequired() |
| 58 | + .withNodeSelectorTerms(new NodeSelectorTermBuilder() |
| 59 | + .withMatchExpressions(new NodeSelectorRequirementBuilder() |
| 60 | + .withKey("kubernetes.io/hostname") |
| 61 | + .withOperator("In") |
| 62 | + .withValues("minikube").build()).build()) |
| 63 | + .endRequired() |
| 64 | + .endNodeAffinity() |
| 65 | + .endSpec() |
| 66 | + |
| 67 | + val pvcBuilder = new PersistentVolumeClaimBuilder() |
| 68 | + .withKind("PersistentVolumeClaim") |
| 69 | + .withApiVersion("v1") |
| 70 | + .withNewMetadata() |
| 71 | + .withName(PVC_NAME) |
| 72 | + .endMetadata() |
| 73 | + .withNewSpec() |
| 74 | + .withAccessModes("ReadWriteOnce") |
| 75 | + .withStorageClassName("test-local-storage") |
| 76 | + .withResources(new ResourceRequirementsBuilder() |
| 77 | + .withRequests(Map("storage" -> new QuantityBuilder() |
| 78 | + .withAmount("1Gi").build()).asJava).build()) |
| 79 | + .endSpec() |
| 80 | + |
| 81 | + kubernetesTestComponents |
| 82 | + .kubernetesClient |
| 83 | + .storage() |
| 84 | + .storageClasses() |
| 85 | + .create(scBuilder.build()) |
| 86 | + |
| 87 | + kubernetesTestComponents |
| 88 | + .kubernetesClient |
| 89 | + .persistentVolumes() |
| 90 | + .create(pvBuilder.build()) |
| 91 | + |
| 92 | + kubernetesTestComponents |
| 93 | + .kubernetesClient |
| 94 | + .persistentVolumeClaims() |
| 95 | + .create(pvcBuilder.build()) |
| 96 | + } |
| 97 | + |
| 98 | + private def deleteLocalStorage(): Unit = { |
| 99 | + kubernetesTestComponents |
| 100 | + .kubernetesClient |
| 101 | + .persistentVolumeClaims() |
| 102 | + .withName(PVC_NAME) |
| 103 | + .delete() |
| 104 | + |
| 105 | + kubernetesTestComponents |
| 106 | + .kubernetesClient |
| 107 | + .persistentVolumes() |
| 108 | + .withName(PV_NAME) |
| 109 | + .delete() |
| 110 | + |
| 111 | + kubernetesTestComponents |
| 112 | + .kubernetesClient |
| 113 | + .storage() |
| 114 | + .storageClasses() |
| 115 | + .withName(STORAGE_NAME) |
| 116 | + .delete() |
| 117 | + } |
| 118 | + |
| 119 | + private def checkPVs(pod: Pod, file: String) = { |
| 120 | + Eventually.eventually(TIMEOUT, INTERVAL) { |
| 121 | + implicit val podName: String = pod.getMetadata.getName |
| 122 | + implicit val components: KubernetesTestComponents = kubernetesTestComponents |
| 123 | + val contents = Utils.executeCommand("cat", s"$CONTAINER_MOUNT_PATH/$file") |
| 124 | + assert(contents.toString.trim.equals(FILE_CONTENTS)) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private def createTempFile(): String = { |
| 129 | + val filename = try { |
| 130 | + val f = File.createTempFile("tmp", ".txt", new File(HOST_PATH)) |
| 131 | + f.deleteOnExit() |
| 132 | + new PrintWriter(f) { |
| 133 | + try { |
| 134 | + write(FILE_CONTENTS) |
| 135 | + } finally { |
| 136 | + close() |
| 137 | + } |
| 138 | + } |
| 139 | + f.getName |
| 140 | + } catch { |
| 141 | + case e: Exception => e.printStackTrace(); throw e; |
| 142 | + } |
| 143 | + filename |
| 144 | + } |
| 145 | + |
| 146 | + test("Test PVs with local storage", k8sTestTag, MinikubeTag) { |
| 147 | + sparkAppConf |
| 148 | + .set(s"spark.kubernetes.driver.volumes.persistentVolumeClaim.data.mount.path", |
| 149 | + CONTAINER_MOUNT_PATH) |
| 150 | + .set(s"spark.kubernetes.driver.volumes.persistentVolumeClaim.data.options.claimName", |
| 151 | + PVC_NAME) |
| 152 | + .set(s"spark.kubernetes.executor.volumes.persistentVolumeClaim.data.mount.path", |
| 153 | + CONTAINER_MOUNT_PATH) |
| 154 | + .set(s"spark.kubernetes.executor.volumes.persistentVolumeClaim.data.options.claimName", |
| 155 | + PVC_NAME) |
| 156 | + val file = createTempFile() |
| 157 | + try { |
| 158 | + setupLocalStorage() |
| 159 | + runDFSReadWriteAndVerifyCompletion( |
| 160 | + FILE_CONTENTS.split(" ").length, |
| 161 | + driverPodChecker = (driverPod: Pod) => { |
| 162 | + doBasicDriverPodCheck(driverPod) |
| 163 | + checkPVs(driverPod, file) |
| 164 | + }, |
| 165 | + executorPodChecker = (executorPod: Pod) => { |
| 166 | + doBasicExecutorPodCheck(executorPod) |
| 167 | + checkPVs(executorPod, file) |
| 168 | + }, |
| 169 | + appArgs = Array(s"$CONTAINER_MOUNT_PATH/$file", s"$CONTAINER_MOUNT_PATH"), |
| 170 | + interval = Some(PV_TESTS_INTERVAL) |
| 171 | + ) |
| 172 | + } finally { |
| 173 | + // make sure this always run |
| 174 | + deleteLocalStorage() |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +private[spark] object PVTestsSuite { |
| 180 | + val MinikubeTag = Tag("minikube") |
| 181 | + val STORAGE_NAME = "test-local-storage" |
| 182 | + val PV_NAME = "test-local-pv" |
| 183 | + val PVC_NAME = "test-local-pvc" |
| 184 | + val CONTAINER_MOUNT_PATH = "/opt/spark/pv-tests" |
| 185 | + val HOST_PATH = sys.env.getOrElse("PVC_TESTS_HOST_PATH", "/tmp") |
| 186 | + val VM_PATH = sys.env.getOrElse("PVC_TESTS_VM_PATH", "/tmp") |
| 187 | + val FILE_CONTENTS = "test PVs" |
| 188 | + val PV_TESTS_INTERVAL = PatienceConfiguration.Interval(Span(10, Milliseconds)) |
| 189 | +} |
0 commit comments