Skip to content

Commit

Permalink
Fix using webhooks without proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <[email protected]>
  • Loading branch information
maksim-paskal committed Nov 14, 2024
1 parent 028f7fc commit 3018bd8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func NewInstrumenter(subsystemIdentifier string) *Instrumenter {
}

func (i *Instrumenter) WithProxy(proxyURL string) *Instrumenter {
if proxyURL == "" {
return i
}

proxy, err := url.Parse(proxyURL)
if err != nil {
log.WithError(err).Errorf("error parsing proxy url %s for %s", proxyURL, i.subsystemIdentifier)
Expand Down
41 changes: 37 additions & 4 deletions pkg/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/http/httptest"
"testing"

"github.com/maksim-paskal/aks-node-termination-handler/pkg/metrics"
"github.com/maksim-paskal/aks-node-termination-handler/pkg/template"
"github.com/maksim-paskal/aks-node-termination-handler/pkg/webhook"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -61,11 +62,27 @@ func testWebhookRequest(r *http.Request) error {
func TestWebHook(t *testing.T) { //nolint:funlen,tparallel
t.Parallel()

webhookClient := &http.Client{
Transport: metrics.NewInstrumenter("TestWebHook").
WithProxy("").
WithInsecureSkipVerify(true).
InstrumentedRoundTripper(),
}

webhookClientProxy := &http.Client{
Transport: metrics.NewInstrumenter("TestWebHookWithProxy").
WithProxy("http://someproxy").
WithInsecureSkipVerify(true).
InstrumentedRoundTripper(),
}

type Test struct {
Name string
Args map[string]string
Error bool
NodeName string
Name string
Args map[string]string
Error bool
ErrorMessage string
NodeName string
HTTPClient *http.Client
}

tests := []Test{
Expand Down Expand Up @@ -139,6 +156,15 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel
},
NodeName: "!!invalid!!GetNodeLabels",
},
{
Error: true,
ErrorMessage: "error making roundtrip: proxyconnect tcp: dial tcp",
Name: "HTTPClientProxy",
Args: map[string]string{
"webhook.url": getWebhookURL(),
},
HTTPClient: webhookClientProxy,
},
}

// clear flags
Expand Down Expand Up @@ -166,9 +192,16 @@ func TestWebHook(t *testing.T) { //nolint:funlen,tparallel
messageType.NodeName = tc.NodeName
}

if httpClient := tc.HTTPClient; httpClient != nil {
webhook.SetHTTPClient(httpClient)
} else {
webhook.SetHTTPClient(webhookClient)
}

err := webhook.SendWebHook(context.TODO(), messageType)
if tc.Error {
require.Error(t, err)
require.Contains(t, err.Error(), tc.ErrorMessage)
} else {
require.NoError(t, err)
}
Expand Down

0 comments on commit 3018bd8

Please sign in to comment.