HwangDongJun commented on code in PR #58187:
URL: https://github.com/apache/spark/pull/58187#discussion_r4608820758


##########
resource-managers/kubernetes/core/src/test/scala/org/apache/spark/deploy/k8s/submit/ClientSuite.scala:
##########
@@ -223,22 +364,212 @@ class ClientSuite extends SparkFunSuite with 
BeforeAndAfter {
     val configMap = configMaps.head
     assert(configMap.getMetadata.getName ===
       KubernetesClientUtils.configMapNameDriver)
+    assert(configMap.getMetadata.getName === mountedConfigMapName)
     assert(configMap.getImmutable())
     assert(configMap.getData.containsKey(SPARK_CONF_FILE_NAME))
     
assert(configMap.getData.get(SPARK_CONF_FILE_NAME).contains("conf1key=conf1value"))
     
assert(configMap.getData.get(SPARK_CONF_FILE_NAME).contains("conf2key=conf2value"))
   }
 
+  test("SPARK-38079: the driver pod is created gated, and the gate is removed 
last") {
+    newClient().run()
+
+    assert(events.toSeq === submissionEvents())
+    assert(gateNames(createdPodArgumentCaptor.getValue) === 
Seq(DRIVER_RESOURCES_SCHEDULING_GATE))
+  }
+
+  test("SPARK-38079: pre resources are applied before the gated pod and owned 
after it") {
+    useSpec(BUILT_KUBERNETES_SPEC_WITH_PRERES)
+
+    newClient().run()
+
+    assert(events.toSeq ===
+      submissionEvents(preResource = Some("CustomResourceDefinition/preCRD")))
+  }
+
+  test("SPARK-38079: the gate is removed by name from the pod that create() 
returned") {
+    newClient().run()
+
+    verify(namedPods).patch(
+      ArgumentMatchers.any[PatchContext](), ArgumentMatchers.any[String]())
+    assert(patchContextCaptor.getValue.getPatchType === 
PatchType.STRATEGIC_MERGE)
+    assert(patchBodyCaptor.getValue ===
+      """{"spec":{"schedulingGates":[{"$patch":"delete",""" +
+        s""""name":"$DRIVER_RESOURCES_SCHEDULING_GATE"}]}}""")
+    assert(podTargets.map(_._1).toSeq === Seq("remove gate"))
+    assert(podTargets.map(_._2.getMetadata.getUid).toSet === 
Set(DRIVER_POD_UID))
+  }
+
+  test("SPARK-38079: gates already on the pod template are kept next to the 
Spark gate") {
+    useSpec(specWithPod(withGates(BUILT_DRIVER_POD, TEMPLATE_GATE)))
+
+    newClient().run()
+
+    assert(gateNames(createdPodArgumentCaptor.getValue) ===
+      Seq(TEMPLATE_GATE, DRIVER_RESOURCES_SCHEDULING_GATE))
+    // The patch is a strategic merge patch that names only Spark's gate.
+    assert(patchContextCaptor.getValue.getPatchType === 
PatchType.STRATEGIC_MERGE)
+    assert(!patchBodyCaptor.getValue.contains(TEMPLATE_GATE))
+  }
+
+  test("SPARK-38079: a Spark gate already on the pod is not added a second 
time") {
+    useSpec(specWithPod(
+      withGates(BUILT_DRIVER_POD, TEMPLATE_GATE, 
DRIVER_RESOURCES_SCHEDULING_GATE)))
+
+    newClient().run()
+
+    assert(gateNames(createdPodArgumentCaptor.getValue) ===
+      Seq(TEMPLATE_GATE, DRIVER_RESOURCES_SCHEDULING_GATE))
+    assert(events.last === "remove gate")
+  }
+
+  test("SPARK-38079: the gate is off by default, so the previous flow needs no 
patch") {
+    kconf = KubernetesTestConf.createDriverConf(
+      resourceNamePrefix = Some(KUBERNETES_RESOURCE_PREFIX))
+    assert(!kconf.get(USE_DRIVER_SCHEDULING_GATE))
+    useSpec(BUILT_KUBERNETES_SPEC)
+
+    newClient().run()
+
+    assert(createdPodArgumentCaptor.getValue === fullExpectedPod(gated = 
false))
+    assert(events.toSeq === submissionEvents(gateRemoved = false))
+  }
+
+  test("SPARK-38079: disabling the gate keeps the previous flow and the 
template's gates") {
+    kconf = driverConf(gate = false)
+    useSpec(specWithPod(withGates(BUILT_DRIVER_POD, TEMPLATE_GATE)))
+
+    newClient().run()
+
+    assert(createdPodArgumentCaptor.getValue ===
+      withGates(fullExpectedPod(gated = false), TEMPLATE_GATE))
+    assert(events.toSeq === submissionEvents(gateRemoved = false))
+  }
+
+  test("SPARK-38079: a pod pinned with spec.nodeName is neither gated nor 
patched") {
+    val pinnedPod = new PodBuilder(BUILT_DRIVER_POD)
+      .editSpec()
+        .withNodeName("some-node")
+        .endSpec()
+      .build()
+    useSpec(specWithPod(pinnedPod))
+
+    newClient().run()
+
+    val expected = new PodBuilder(fullExpectedPod(gated = false))
+      .editSpec().withNodeName("some-node").endSpec().build()
+    assert(createdPodArgumentCaptor.getValue === expected)
+    assert(events.toSeq === submissionEvents(gateRemoved = false))
+  }
+
+  test("SPARK-38079: a pinned pod keeps the gates of its template") {
+    val pinnedPod = withGates(new PodBuilder(BUILT_DRIVER_POD)
+      .editSpec().withNodeName("some-node").endSpec().build(), TEMPLATE_GATE)
+    useSpec(specWithPod(pinnedPod))
+
+    newClient().run()
+
+    assert(gateNames(createdPodArgumentCaptor.getValue) === Seq(TEMPLATE_GATE))
+    assert(events.toSeq === submissionEvents(gateRemoved = false))
+  }
+
+  test("SPARK-38079: a failure applying the pre resources deletes them and 
creates no pod") {
+    useSpec(BUILT_KUBERNETES_SPEC_WITH_PRERES)
+    val failure = new RuntimeException("apply failed")
+    failingWhen(failure)(_.exists(_.isInstanceOf[CustomResourceDefinition]))
+
+    val thrown = intercept[RuntimeException](newClient().run())
+
+    assert(thrown eq failure)
+    assert(events.toSeq === Seq(
+      "list(CustomResourceDefinition/preCRD)",
+      "list(CustomResourceDefinition/preCRD)", "delete resources"))
+    assert(podTargets.isEmpty)
+  }
+
+  test("SPARK-38079: a failure creating the pod deletes nothing but the pre 
resources") {
+    val failure = new RuntimeException("create failed")
+    failPodCall(failure)
+
+    val thrown = intercept[RuntimeException](newClient().run())
+
+    assert(thrown eq failure)
+    assert(events.toSeq === Seq("list()", "apply", "create pod", "list()", 
"delete resources"))
+    assert(podTargets.isEmpty)
+  }
+
+  test("SPARK-38079: a failure refreshing the pre resources' owners deletes 
the pod and them") {
+    useSpec(BUILT_KUBERNETES_SPEC_WITH_PRERES)
+    val failure = new RuntimeException("owner refresh failed")
+    failingWhen(failure) { resources =>
+      resources.exists(r => r.isInstanceOf[CustomResourceDefinition] &&
+        r.getMetadata.getOwnerReferences != null && 
!r.getMetadata.getOwnerReferences.isEmpty)
+    }
+
+    val thrown = intercept[RuntimeException](newClient().run())
+
+    assert(thrown eq failure)
+    val owned = "list(CustomResourceDefinition/preCRD+owner)"
+    assert(events.toSeq.drop(3) === Seq(owned, "delete pod", owned, "delete 
resources"))
+    assert(!events.contains("remove gate"))
+  }
+
+  test("SPARK-38079: a failure applying the post resources deletes only the 
pod") {
+    val failure = new RuntimeException("apply failed")
+    failingWhen(failure)(_.exists(_.isInstanceOf[Secret]))
+
+    val thrown = intercept[RuntimeException](newClient().run())
+
+    assert(thrown eq failure)
+    assert(events.last === "delete pod")
+    assert(!events.contains("remove gate"))
+    assert(!events.contains("delete resources"))
+    // The delete goes through the pod returned by create().
+    assert(podTargets.map(_._2.getMetadata.getUid).toSet === 
Set(DRIVER_POD_UID))
+  }
+
+  test("SPARK-38079: a failure deleting the pod does not hide the original 
failure") {
+    val failure = new RuntimeException("apply failed")
+    failingWhen(failure)(_.exists(_.isInstanceOf[Secret]))
+    failPodDelete(new RuntimeException("delete failed"))
+
+    val thrown = intercept[RuntimeException](newClient().run())
+
+    assert(thrown eq failure)
+    assert(events.last === "delete pod")
+  }
+
+  test("SPARK-38079: a failure removing the gate deletes the pod and 
propagates") {
+    val failure = new RuntimeException("patch failed")
+    failGatePatch(failure)
+
+    val thrown = intercept[RuntimeException](newClient().run())
+
+    assert(thrown eq failure)
+    assert(events.takeRight(2) === Seq("remove gate", "delete pod"))
+    assert(podTargets.map(_._1).toSeq === Seq("remove gate", "delete pod"))
+    assert(podTargets.map(_._2.getMetadata.getUid).toSet === 
Set(DRIVER_POD_UID))
+    verify(namedPods, never()).watch(loggingPodStatusWatcher)
+  }
+
+  test("SPARK-38079: a failure removing the gate is not hidden by a failure 
deleting the pod") {
+    val failure = new KubernetesClientException("forbidden", 403, null)

Review Comment:
   Added, using `withLogAppender` as the SPARK-42813 test does. 
`gatePatchHint(failure)` makes the gate patch fail with the given exception, 
runs `newClient().run()` inside `withLogAppender` at `ERROR`, and returns the 
logged messages that contain `can-i patch pod`.
   
   - `a forbidden gate patch logs the hint that names the config`: a 403 logs 
the hint exactly once, and the message contains 
`spark.kubernetes.submission.useDriverSchedulingGate`.
   - `a gate patch that fails for another reason does not log the hint`: 
neither a `RuntimeException` nor a 409 `KubernetesClientException` logs it.
   
   I ran these mutations against the new tests:
   
   - removing the 403 condition (`case ke: KubernetesClientException =>`) fails 
the second test;
   - removing the `logError` fails the first test;
   - keeping the message but dropping the config key from it fails the first 
test.
   
   Before the change, removing the 403 condition so that the hint is logged for 
every failure, and removing the `logError`, both left all 22 `ClientSuite` 
tests green, as you described.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to