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 3 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
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ require (
k8s.io/client-go v7.0.0+incompatible
k8s.io/metrics v0.0.0-20190116212108-c656f2547d11
)

replace (
github.com/v3io/scaler-types => github.com/hedingber/scaler-types v1.6.0
)
Hedingber marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ github.com/google/gofuzz v1.0.0 h1:A8PeW59pxE9IoFRqBp37U+mSNaQoZ46F1f0f863XSXw=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyycI+I=
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
github.com/hedingber/scaler-types v1.6.0/go.mod h1:3sfHduIpQo/uYnOkcnkGE8Xilm1rKYhjIvBeFcyfetw=
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c h1:kQWxfPIHVLbgLzphqk3QUflDy9QdksZR4ygR807bpy0=
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
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
}