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 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.5.1
github.com/v3io/scaler-types v1.5.0
github.com/v3io/scaler-types v1.6.0
go.uber.org/atomic v1.4.0 // indirect
go.uber.org/multierr v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/v3io/scaler-types v1.5.0 h1:3nA56ewksIWmcGPW45wXUBMT3AZ4m61HMInR33j4uvY=
github.com/v3io/scaler-types v1.5.0/go.mod h1:Q2xufg8/9sLUOHh8/ckhHaAzZy2M8AgEknMTJ5HxvFI=
github.com/v3io/scaler-types v1.6.0/go.mod h1:Q2xufg8/9sLUOHh8/ckhHaAzZy2M8AgEknMTJ5HxvFI=
go.uber.org/atomic v1.4.0 h1:cxzIVoETapQEqDhQu3QfnvXAV4AlzcvUCxkVUFw3+EU=
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
Expand Down
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
4 changes: 4 additions & 0 deletions pkg/resourcescaler/resourcescaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ func (r *NopResourceScaler) GetResources() ([]scaler_types.Resource, error) {
func (r *NopResourceScaler) GetConfig() (*scaler_types.ResourceScalerConfig, error) {
return nil, nil
}

func (r *NopResourceScaler) ResolveServiceName(resource scaler_types.Resource) (string, error) {
return resource.Name, nil
}