Skip to content

Commit

Permalink
Supporting service name which is different then the resource name [0.…
Browse files Browse the repository at this point in the history
…2.x] (#35)
  • Loading branch information
Hedingber authored Apr 18, 2020
1 parent 5e5378c commit 1f7d243
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
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
14 changes: 13 additions & 1 deletion 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 Down Expand Up @@ -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
10 changes: 7 additions & 3 deletions pkg/dlx/resourcestarter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type mocker struct {
mock.Mock
}

func (suite *resourceStarterTest) SetScale(resourceName scaler_types.Resource, scale int) error {
suite.mocker.Called(resourceName)
func (suite *resourceStarterTest) SetScale(resources []scaler_types.Resource, scale int) error {
suite.mocker.Called(resources)
return nil
}

Expand All @@ -38,13 +38,17 @@ func (suite *resourceStarterTest) GetConfig() (*scaler_types.ResourceScalerConfi
return nil, nil
}

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

func (suite *resourceStarterTest) SetupTest() {
suite.functionStarter = &ResourceStarter{
logger: suite.logger,
resourceSinksMap: make(resourceSinksMap),
namespace: "default",
resourceReadinessTimeout: time.Duration(1 * time.Second),
scaler: suite,
scaler: suite,
}
suite.mocker = new(mocker)
}
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
}

0 comments on commit 1f7d243

Please sign in to comment.