Skip to content
Open
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
20 changes: 20 additions & 0 deletions internal/ansible/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -144,6 +145,8 @@ func Run(done chan error, o Options) error {

// Remove the authorization header so the proxy can correctly inject the header.
server.Handler = removeAuthorizationHeader(server.Handler)
// Set the host header so that it matches the host in kubeconfig.
server.Handler = setHostHeader(server.Handler, o.KubeConfig)

if o.OwnerInjection {
server.Handler = &injectOwnerReferenceHandler{
Expand Down Expand Up @@ -283,6 +286,23 @@ func removeAuthorizationHeader(h http.Handler) http.Handler {
})
}

func setHostHeader(next http.Handler, kubeConfig *rest.Config) http.Handler {
host := kubeConfig.Host
if host == "" {
return next
}

// If host is a URL, extract the host portion
if u, err := url.ParseRequestURI(host); err == nil {
host = u.Host
}
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
req.Header.Set("Host", host)
req.Host = host
next.ServeHTTP(w, req)
})
}

// Helper function used by recovering dependent watches and owner ref injection.
func getRequestOwnerRef(req *http.Request) (*kubeconfig.NamespacedOwnerReference, error) {
owner := kubeconfig.NamespacedOwnerReference{}
Expand Down