Skip to content

Commit 733422b

Browse files
committed
Implement new logic resolving before BC logic
1 parent 68a35f1 commit 733422b

File tree

1 file changed

+41
-6
lines changed

1 file changed

+41
-6
lines changed

pkg/dlx/handler.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
101101
resourceNames = append(resourceNames, resourceName)
102102
resourceTargetURLMap[resourceName] = targetURL
103103
} else {
104-
targetNameHeaderValue := req.Header.Get(h.targetNameHeader)
105-
path := req.Header.Get(h.targetPathHeader)
106-
if targetNameHeaderValue == "" {
107-
h.logger.WarnWith("When ingress not set, must pass header value",
108-
"missingHeader", h.targetNameHeader)
104+
path, resourceNames, err := h.getResourceNameAndPath(req)
105+
if err != nil {
106+
h.logger.WarnWith("Failed to get resource names and path from request",
107+
"error", err.Error(),
108+
"host", req.Host,
109+
"path", req.URL.Path)
109110
res.WriteHeader(http.StatusBadRequest)
110111
return
111112
}
112-
resourceNames = strings.Split(targetNameHeaderValue, ",")
113113
for _, resourceName := range resourceNames {
114114
targetURL, status := h.parseTargetURL(resourceName, path)
115115
if targetURL == nil {
@@ -167,6 +167,41 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) {
167167
proxy.ServeHTTP(res, req)
168168
}
169169

170+
func (h *Handler) getResourceNameAndPath(req *http.Request) (string, []string, error) {
171+
if path, resourceNames, err := h.extractValuesFromIngress(req); err == nil {
172+
return path, resourceNames, nil
173+
} else {
174+
h.logger.DebugWith("Failed to get target name from ingress cache, try to extract from the request headers",
175+
"host", req.Host,
176+
"path", req.URL.Path,
177+
"error", err.Error())
178+
}
179+
180+
// old implementation for backward compatibility
181+
targetNameHeaderValue := req.Header.Get(h.targetNameHeader)
182+
path := req.Header.Get(h.targetPathHeader)
183+
if targetNameHeaderValue == "" {
184+
return "", nil, errors.New("No target name header found")
185+
}
186+
resourceNames := strings.Split(targetNameHeaderValue, ",")
187+
return path, resourceNames, nil
188+
}
189+
190+
func (h *Handler) extractValuesFromIngress(req *http.Request) (string, []string, error) {
191+
host := req.Host
192+
path := req.URL.Path
193+
resourceNames, err := h.ingressCache.Get(host, path)
194+
if err != nil {
195+
return "", nil, errors.New("Failed to get target name from ingress cache")
196+
}
197+
198+
if len(resourceNames) == 0 {
199+
return "", nil, errors.New("No resourceNames found in ingress cache")
200+
}
201+
202+
return path, resourceNames, nil
203+
}
204+
170205
func (h *Handler) parseTargetURL(resourceName, path string) (*url.URL, int) {
171206
serviceName, err := h.resourceScaler.ResolveServiceName(scalertypes.Resource{Name: resourceName})
172207
if err != nil {

0 commit comments

Comments
 (0)