Skip to content

Commit 2ac44e8

Browse files
committed
Start webhooks on the same address as the server
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent c57137b commit 2ac44e8

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

test/e2e/apibinding/apibinding_webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func TestAPIBindingMutatingWebhook(t *testing.T) {
157157
port, err := kcptestingserver.GetFreePort(t)
158158
require.NoError(t, err, "failed to get free port for test webhook")
159159
dirPath := filepath.Dir(server.KubeconfigPath())
160-
testWebhooks[cluster].StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), port)
160+
testWebhooks[cluster].StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), cfg.Host, port)
161161

162162
sideEffect := admissionregistrationv1.SideEffectClassNone
163163
url := testWebhooks[cluster].GetURL()
@@ -312,7 +312,7 @@ func TestAPIBindingValidatingWebhook(t *testing.T) {
312312
port, err := kcptestingserver.GetFreePort(t)
313313
require.NoError(t, err, "failed to get free port for test webhook")
314314
dirPath := filepath.Dir(server.KubeconfigPath())
315-
testWebhooks[cluster].StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), port)
315+
testWebhooks[cluster].StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), cfg.Host, port)
316316

317317
kcptestinghelpers.Eventually(t, func() (bool, string) {
318318
cl := gohttp.Client{Transport: &gohttp.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}}

test/e2e/conformance/webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestMutatingWebhookInWorkspace(t *testing.T) {
9494
port, err := kcptestingserver.GetFreePort(t)
9595
require.NoError(t, err, "failed to get free port for test webhook")
9696
dirPath := filepath.Dir(server.KubeconfigPath())
97-
testWebhook.StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), port)
97+
testWebhook.StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), cfg.Host, port)
9898

9999
orgPath, _ := framework.NewOrganizationFixture(t, server) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
100100
ws1Path, ws1 := kcptesting.NewWorkspaceFixture(t, server, orgPath)
@@ -212,7 +212,7 @@ func TestValidatingWebhookInWorkspace(t *testing.T) {
212212
port, err := kcptestingserver.GetFreePort(t)
213213
require.NoError(t, err, "failed to get free port for test webhook")
214214
dirPath := filepath.Dir(server.KubeconfigPath())
215-
testWebhook.StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), port)
215+
testWebhook.StartTLS(t, filepath.Join(dirPath, "apiserver.crt"), filepath.Join(dirPath, "apiserver.key"), cfg.Host, port)
216216

217217
orgPath, _ := framework.NewOrganizationFixture(t, server) //nolint:staticcheck // TODO: switch to NewWorkspaceFixture.
218218
ws1, _ := kcptesting.NewWorkspaceFixture(t, server, orgPath)

test/e2e/fixtures/webhook/webhook.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222
"errors"
2323
"fmt"
2424
"io"
25+
"net"
2526
"net/http"
27+
"net/url"
2628
"sync"
2729
"testing"
2830

@@ -38,18 +40,29 @@ type AdmissionWebhookServer struct {
3840

3941
t *testing.T
4042

41-
port string
42-
lock sync.Mutex
43-
calls int
43+
host, port string
44+
lock sync.Mutex
45+
calls int
4446
}
4547

46-
func (s *AdmissionWebhookServer) StartTLS(t *testing.T, certFile, keyFile string, port string) {
48+
func (s *AdmissionWebhookServer) StartTLS(t *testing.T, certFile, keyFile, host, port string) {
4749
t.Helper()
4850

4951
s.t = t
52+
// The host passed to StartTLS is the Host of the rest.Config, which
53+
// can be just host, host:port or a full URL.
54+
u, err := url.Parse(host)
55+
if err != nil {
56+
t.Fatalf("error parsing host %q: %v", host, err)
57+
}
58+
host, _, err = net.SplitHostPort(u.Host)
59+
if err != nil {
60+
t.Fatalf("error splitting host %q: %v", u.Host, err)
61+
}
62+
s.host = host
5063
s.port = port
5164

52-
serv := &http.Server{Addr: fmt.Sprintf(":%v", port), Handler: s}
65+
serv := &http.Server{Addr: net.JoinHostPort(s.host, s.port), Handler: s}
5366
t.Cleanup(func() {
5467
t.Log("Shutting down the HTTP server")
5568
err := serv.Shutdown(context.TODO())
@@ -67,7 +80,12 @@ func (s *AdmissionWebhookServer) StartTLS(t *testing.T, certFile, keyFile string
6780
}
6881

6982
func (s *AdmissionWebhookServer) GetURL() string {
70-
return fmt.Sprintf("https://localhost:%v/hello", s.port)
83+
u := &url.URL{
84+
Scheme: "https",
85+
Host: net.JoinHostPort(s.host, s.port),
86+
Path: "/hello",
87+
}
88+
return u.String()
7189
}
7290

7391
func (s *AdmissionWebhookServer) ServeHTTP(resp http.ResponseWriter, req *http.Request) {

0 commit comments

Comments
 (0)