Skip to content

Commit

Permalink
add hostAliases for SparkPodSpec (kubeflow#1133)
Browse files Browse the repository at this point in the history
* add hostAliases for SparkPodSpec and update codegen files

* add webhook patcher for HostAliases + add tests

* update chart CRDs

* update api doc
  • Loading branch information
ImpSy committed Jan 20, 2021
1 parent 6bbb2a0 commit 2c50fea
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,17 @@ spec:
- name
- quantity
type: object
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
hostNetwork:
type: boolean
image:
Expand Down Expand Up @@ -2307,6 +2318,17 @@ spec:
- name
- quantity
type: object
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
hostNetwork:
type: boolean
image:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,17 @@ spec:
- name
- quantity
type: object
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
hostNetwork:
type: boolean
image:
Expand Down Expand Up @@ -2293,6 +2304,17 @@ spec:
- name
- quantity
type: object
hostAliases:
items:
properties:
hostnames:
items:
type: string
type: array
ip:
type: string
type: object
type: array
hostNetwork:
type: boolean
image:
Expand Down
14 changes: 14 additions & 0 deletions docs/api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2780,6 +2780,20 @@ string
<p>ServiceAccount is the name of the custom Kubernetes service account used by the pod.</p>
</td>
</tr>
<tr>
<td>
<code>hostAliases</code></br>
<em>
<a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.13/#hostalias-v1-core">
[]Kubernetes core/v1.HostAlias
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>HostAliases settings for the pod, following the Kubernetes specifications.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="sparkoperator.k8s.io/v1beta2.SparkUIConfiguration">SparkUIConfiguration
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/sparkoperator.k8s.io/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ type SparkPodSpec struct {
// ServiceAccount is the name of the custom Kubernetes service account used by the pod.
// +optional
ServiceAccount *string `json:"serviceAccount,omitempty"`
// HostAliases settings for the pod, following the Kubernetes specifications.
// +optional
HostAliases []apiv1.HostAlias `json:"hostAliases,omitempty"`
}

// DriverSpec is specification of the driver.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions pkg/webhook/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func patchSparkPod(pod *corev1.Pod, app *v1beta2.SparkApplication) []patchOperat
patchOps = append(patchOps, addDNSConfig(pod, app)...)
patchOps = append(patchOps, addEnvVars(pod, app)...)
patchOps = append(patchOps, addEnvFrom(pod, app)...)
patchOps = append(patchOps, addHostAliases(pod, app)...)

op := addSchedulerName(pod, app)
if op != nil {
Expand Down Expand Up @@ -783,3 +784,28 @@ func findContainer(pod *corev1.Pod) int {
}
return -1
}

func addHostAliases(pod *corev1.Pod, app *v1beta2.SparkApplication) []patchOperation {
var hostAliases []corev1.HostAlias
if util.IsDriverPod(pod) {
hostAliases = app.Spec.Driver.HostAliases
} else if util.IsExecutorPod(pod) {
hostAliases = app.Spec.Executor.HostAliases
}

first := false
if len(pod.Spec.HostAliases) == 0 {
first = true
}

var ops []patchOperation
for _, v := range hostAliases {
if first {
ops = append(ops, patchOperation{Op: "add", Path: "/spec/hostAliases", Value: []corev1.HostAlias{v}})
first = false
} else {
ops = append(ops, patchOperation{Op: "add", Path: "/spec/hostAliases/-", Value: &v})
}
}
return ops
}
91 changes: 91 additions & 0 deletions pkg/webhook/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1811,3 +1811,94 @@ func getModifiedPod(pod *corev1.Pod, app *v1beta2.SparkApplication) (*corev1.Pod

return modifiedPod, nil
}

func TestPatchSparkPod_HostAliases(t *testing.T) {
app := &v1beta2.SparkApplication{
ObjectMeta: metav1.ObjectMeta{
Name: "spark-test",
UID: "spark-test-1",
},
Spec: v1beta2.SparkApplicationSpec{
Driver: v1beta2.DriverSpec{
SparkPodSpec: v1beta2.SparkPodSpec{
HostAliases: []corev1.HostAlias{
{
IP: "127.0.0.1",
Hostnames: []string{"localhost"},
},
{
IP: "192.168.0.1",
Hostnames: []string{"test.com", "test2.com"},
},
},
},
},
Executor: v1beta2.ExecutorSpec{
SparkPodSpec: v1beta2.SparkPodSpec{
HostAliases: []corev1.HostAlias{
{
IP: "127.0.0.1",
Hostnames: []string{"localhost"},
},
{
IP: "192.168.0.1",
Hostnames: []string{"test.com", "test2.com"},
},
},
},
},
},
}

driverPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "spark-driver",
Labels: map[string]string{
config.SparkRoleLabel: config.SparkDriverRole,
config.LaunchedBySparkOperatorLabel: "true",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: config.SparkDriverContainerName,
Image: "spark-driver:latest",
},
},
},
}

modifiedDriverPod, err := getModifiedPod(driverPod, app)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 2, 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)

executorPod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "spark-executor",
Labels: map[string]string{
config.SparkRoleLabel: config.SparkExecutorRole,
config.LaunchedBySparkOperatorLabel: "true",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: config.SparkExecutorContainerName,
Image: "spark-executor:latest",
},
},
},
}

modifiedExecutorPod, err := getModifiedPod(executorPod, app)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 2, len(modifiedExecutorPod.Spec.HostAliases))
assert.Equal(t, "127.0.0.1", modifiedExecutorPod.Spec.HostAliases[0].IP)
assert.Equal(t, "192.168.0.1", modifiedExecutorPod.Spec.HostAliases[1].IP)
}

0 comments on commit 2c50fea

Please sign in to comment.