Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve getMasterURL() to add [] to IPv6 if needed #1825

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/controller/sparkapplication/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ func getMasterURL() (string, error) {
if kubernetesServicePort == "" {
return "", fmt.Errorf("environment variable %s is not found", kubernetesServicePortEnvVar)
}
// check if the host is IPv6 address
if strings.Contains(kubernetesServiceHost, ":") && !strings.HasPrefix(kubernetesServiceHost, "[") {
return fmt.Sprintf("k8s://https://[%s]:%s", kubernetesServiceHost, kubernetesServicePort), nil
}
return fmt.Sprintf("k8s://https://%s:%s", kubernetesServiceHost, kubernetesServicePort), nil
}

Expand Down
51 changes: 51 additions & 0 deletions pkg/controller/sparkapplication/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,54 @@ func TestProxyUserArg(t *testing.T) {
assert.Equal(t, "--proxy-user", args[4])
assert.Equal(t, "foo", args[5])
}

func Test_getMasterURL(t *testing.T) {
setEnv := func(host string, port string) {
if err := os.Setenv(kubernetesServiceHostEnvVar, host); err != nil {
t.Fatal(err)
}
if err := os.Setenv(kubernetesServicePortEnvVar, port); err != nil {
t.Fatal(err)
}
}

tests := []struct {
name string
host string
port string
want string
wantErr assert.ErrorAssertionFunc
}{
{
name: "should return a valid master url when IPv4 address is used",
host: "localhost",
port: "6443",
want: "k8s://https://localhost:6443",
wantErr: assert.NoError,
},
{
name: "should return a valid master url when IPv6 address is used",
host: "::1",
port: "6443",
want: "k8s://https://[::1]:6443",
wantErr: assert.NoError,
},
{
name: "should throw an error when the host is empty",
host: "",
port: "6443",
want: "",
wantErr: assert.Error,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
setEnv(tt.host, tt.port)
got, err := getMasterURL()
if !tt.wantErr(t, err, fmt.Sprintf("getMasterURL()")) {
return
}
assert.Equalf(t, tt.want, got, "getMasterURL()")
})
}
}