Repository: GoogleCloudPlatform/spark-on-k8s-operator
Pull request #1209 (2021-03-24 13:14:04)

Fix the error of hostAliases when there are more than 2 hostnames
If there are more than 2 hostnames in `hostAliases` path, `spec/hostAliases` will only show the first and many last hostname. Like this:
```yaml
hostAliases:
- ip: "10.1.1.1"
  hostnames:
  - "hostname1"
- ip: "10.1.1.3"
  hostnames:
  - "hostname3"
- ip: "10.1.1.3"
  hostnames:
  - "hostname3"
```
Now fix this problem.

google-cla[bot]: 
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: **Please visit <https://cla.developers.google.com/> to sign.**

Once you've signed (or fixed any issues), please reply here with `@googlebot I signed it!` and we'll verify it.

----

#### What to do if you already signed the CLA

##### Individual signers

*   It's possible we don't have your GitHub username or you're using a different email address on your commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).

##### Corporate signers

*   Your company has a Point of Contact who decides which employees are authorized to participate. Ask your POC to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the Google project maintainer to [go/cla#troubleshoot](http://go/cla#troubleshoot) ([Public version](https://opensource.google/docs/cla/#troubleshoot)).
*   The email used to register you as an authorized contributor must be the email used for the Git commit. Check [your existing CLA data](https://cla.developers.google.com/clas) and verify that your [email is set on your git commits](https://help.github.com/articles/setting-your-email-in-git/).
*   The email used to register you as an authorized contributor must also be [attached to your GitHub account](https://github.com/settings/emails).
		

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fspark-on-k8s-operator%2Fpull%2F1209) for more info**.

<!-- need_sender_cla -->
cdmikechen: @googlebot I signed it!
google-cla[bot]: We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors.  If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)?  If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment `@googlebot I fixed it.`. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fspark-on-k8s-operator%2Fpull%2F1209) for more info**.

<!-- need_author_cla -->
cdmikechen: @googlebot I fixed it.
google-cla[bot]: We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors.  If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)?  If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment `@googlebot I fixed it.`. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fspark-on-k8s-operator%2Fpull%2F1209) for more info**.

<!-- need_author_cla -->
google-cla[bot]: We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for all the commit author(s) or Co-authors.  If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA ([login here](https://cla.developers.google.com/) to double check)?  If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google.
In order to pass this check, please resolve this problem and then comment `@googlebot I fixed it.`. If the bot doesn't comment, it means it doesn't think anything has changed.

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fspark-on-k8s-operator%2Fpull%2F1209) for more info**.

<!-- need_author_cla -->
cdmikechen: @liyinan926  Hi~ Thanks for your review.
I've changed my code, and run test in my computer. But I've seen CI build failed. Can you help me find the cause of the specific error, please ? I haven't found the connection between my code and the CI build failed.
cdmikechen: @liyinan926 Hi~ CI have passed by PR https://github.com/GoogleCloudPlatform/spark-on-k8s-operator/pull/1210
I've merge this change to codes.

Patch:
diff --git a/pkg/webhook/patch.go b/pkg/webhook/patch.go
index 986baf7..46933ce 100644
--- a/pkg/webhook/patch.go
+++ b/pkg/webhook/patch.go
@@ -801,12 +801,11 @@ func addHostAliases(pod *corev1.Pod, app *v1beta2.SparkApplication) []patchOpera
 	}
 
 	var ops []patchOperation
-	for _, v := range hostAliases {
+	if len(hostAliases) > 0 {
 		if first {
-			ops = append(ops, patchOperation{Op: "add", Path: "/spec/hostAliases", Value: []corev1.HostAlias{v}})
-			first = false
+			ops = append(ops, patchOperation{Op: "add", Path: "/spec/hostAliases", Value: hostAliases})
 		} else {
-			ops = append(ops, patchOperation{Op: "add", Path: "/spec/hostAliases/-", Value: &v})
+			ops = append(ops, patchOperation{Op: "add", Path: "/spec/hostAliases/-", Value: hostAliases})
 		}
 	}
 	return ops


Test patch:
diff --git a/pkg/webhook/patch_test.go b/pkg/webhook/patch_test.go
index 5b3b707..35ef392 100644
--- a/pkg/webhook/patch_test.go
+++ b/pkg/webhook/patch_test.go
@@ -1833,6 +1833,14 @@ func TestPatchSparkPod_HostAliases(t *testing.T) {
 							IP:        "192.168.0.1",
 							Hostnames: []string{"test.com", "test2.com"},
 						},
+						{
+							IP:        "192.168.0.2",
+							Hostnames: []string{"test3.com"},
+						},
+						{
+							IP:        "192.168.0.3",
+							Hostnames: []string{"test4.com"},
+						},
 					},
 				},
 			},
@@ -1875,9 +1883,11 @@ func TestPatchSparkPod_HostAliases(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	assert.Equal(t, 2, len(modifiedDriverPod.Spec.HostAliases))
+	assert.Equal(t, 4, len(modifiedDriverPod.Spec.HostAliases))
 	assert.Equal(t, "127.0.0.1", modifiedDriverPod.Spec.HostAliases[0].IP)
 	assert.Equal(t, "192.168.0.1", modifiedDriverPod.Spec.HostAliases[1].IP)
+	assert.Equal(t, "192.168.0.2", modifiedDriverPod.Spec.HostAliases[2].IP)
+	assert.Equal(t, "192.168.0.3", modifiedDriverPod.Spec.HostAliases[3].IP)
 
 	executorPod := &corev1.Pod{
 		ObjectMeta: metav1.ObjectMeta{