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

Supporting service name which is different then the resource name #36

Merged
merged 8 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Supporting service name which is different then the resource name
  • Loading branch information
Hedingber committed Apr 16, 2020
commit 7905cd368c792bdc2b01f51860e3ceb1ef0d972e
1 change: 1 addition & 0 deletions pkg/dlx/dlx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func NewDLX(parentLogger logger.Logger,

handler, err := NewHandler(childLogger,
resourceStarter,
resourceScaler,
options.TargetNameHeader,
options.TargetPathHeader,
options.TargetPort)
Expand Down
16 changes: 14 additions & 2 deletions pkg/dlx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@ import (

"github.com/nuclio/errors"
"github.com/nuclio/logger"
"github.com/v3io/scaler-types"
)

type Handler struct {
logger logger.Logger
HandleFunc func(http.ResponseWriter, *http.Request)
resourceStarter *ResourceStarter
resourceScaler scaler_types.ResourceScaler
targetNameHeader string
targetPathHeader string
targetPort int
}

func NewHandler(parentLogger logger.Logger,
resourceStarter *ResourceStarter,
resourceScaler scaler_types.ResourceScaler,
targetNameHeader string,
targetPathHeader string,
targetPort int) (Handler, error) {
h := Handler{
logger: parentLogger.GetChild("handler"),
resourceStarter: resourceStarter,
resourceScaler: resourceScaler,
targetNameHeader: targetNameHeader,
targetPathHeader: targetPathHeader,
targetPort: targetPort,
Expand All @@ -47,7 +51,7 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
forwardedHost := req.Header.Get("X-Forwarded-Host")
forwardedPort := req.Header.Get("X-Forwarded-Port")
originalURI := req.Header.Get("X-Original-Uri")
resourceName = req.Header.Get("X-Service-Name")
resourceName = req.Header.Get("X-Resource-Name")

if forwardedHost != "" && forwardedPort != "" && resourceName != "" {
targetURL, err = url.Parse(fmt.Sprintf("http://%s:%s/%s", forwardedHost, forwardedPort, originalURI))
Expand All @@ -63,7 +67,14 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusBadRequest)
return
}
targetURL, err = url.Parse(fmt.Sprintf("http://%s:%d/%s", resourceName, h.targetPort, path))
serviceName, err := h.resourceScaler.ResolveServiceName(scaler_types.Resource{Name: resourceName})
if err != nil {
h.logger.WarnWith("Failed resolving service name",
"err", errors.GetErrorStackString(err, 10))
res.WriteHeader(http.StatusInternalServerError)
return
}
targetURL, err = url.Parse(fmt.Sprintf("http://%s:%d/%s", serviceName, h.targetPort, path))
if err != nil {
res.WriteHeader(h.URLBadParse(resourceName, err))
return
Expand All @@ -81,6 +92,7 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
return
}

h.logger.DebugWith("Creating reverse proxy", "targetURL", targetURL)
proxy := httputil.NewSingleHostReverseProxy(targetURL)
proxy.ServeHTTP(res, req)
}
Expand Down