-
Notifications
You must be signed in to change notification settings - Fork 14
[Handler] Integrate the watcher's cache into request handler #78
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
[Handler] Integrate the watcher's cache into request handler #78
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, added some comments and suggestions
pkg/dlx/handler.go
Outdated
@@ -163,6 +166,43 @@ func (h *Handler) handleRequest(res http.ResponseWriter, req *http.Request) { | |||
proxy.ServeHTTP(res, req) | |||
} | |||
|
|||
func (h *Handler) getResourceNameAndPath(req *http.Request) (string, []string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
align to the return order to make it clearer
func (h *Handler) getResourceNameAndPath(req *http.Request) (string, []string, error) { | |
func (h *Handler) getPathAndResourceNames(req *http.Request) (string, []string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - align namings
pkg/dlx/handler.go
Outdated
return path, resourceNames, nil | ||
} | ||
|
||
func (h *Handler) extractValuesFromIngress(req *http.Request) (string, []string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func (h *Handler) extractValuesFromIngress(req *http.Request) (string, []string, error) { | |
func (h *Handler) getValuesFromCache(req *http.Request) (string, []string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - align namings
pkg/dlx/handler.go
Outdated
return path, resourceNames, nil | ||
} | ||
|
||
h.logger.DebugWith("Failed to get target name from ingress cache, try to extract from the request headers", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
h.logger.DebugWith("Failed to get target name from ingress cache, try to extract from the request headers", | |
h.logger.DebugWith("Failed to get target from ingress cache, trying to extract from the request headers", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - align namings
pkg/dlx/handler.go
Outdated
path := req.URL.Path | ||
resourceNames, err := h.ingressCache.Get(host, path) | ||
if err != nil { | ||
return "", nil, errors.New("Failed to get target name from ingress cache") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return "", nil, errors.New("Failed to get target name from ingress cache") | |
return "", nil, errors.New("Failed to get target names from ingress cache") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - align namings
pkg/dlx/handler.go
Outdated
resourceNames, err := h.ingressCache.Get(host, path) | ||
if err != nil { | ||
return "", nil, errors.New("Failed to get target name from ingress cache") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mix resourceNames
and target name
in the variables and logs - choose one and stick with it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I’ll keep using resourceNames
to stay consistent with the naming convention used in the handler context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - align namings
pkg/dlx/handler_test.go
Outdated
} | ||
|
||
func (suite *HandlerTestSuite) TestHandleRequest() { | ||
for _, tc := range []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for _, tc := range []struct { | |
for _, testCase := range []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - unit test comments
pkg/dlx/handler_test.go
Outdated
reqPath: "/test/path", | ||
expectedStatus: http.StatusOK, | ||
}, { | ||
name: "No ingress headers,multiple targets found in ingress cache", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: "No ingress headers,multiple targets found in ingress cache", | |
name: "No ingress headers, multiple targets found in ingress cache", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - unit test comments
pkg/dlx/handler_test.go
Outdated
} | ||
|
||
func (suite *HandlerTestSuite) TestGetResourceNameAndPath() { | ||
for _, tc := range []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for _, tc := range []struct { | |
for _, testCase := range []struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - unit test comments
pkg/dlx/handler_test.go
Outdated
}, { | ||
name: "Missing both ingress headers and host and path did not found in ingress cache", | ||
reqHost: "www.example.com", | ||
reqPath: "/test/path", | ||
expectErr: true, | ||
errMsg: "No target name header found", | ||
}, | ||
} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a test case for existing both cache and headers - cache should take
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch of missing test case, adding
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comment - unit test comments
pkg/dlx/handler_test.go
Outdated
testIngressCache := ingresscache.NewIngressCache(suite.logger) | ||
if tc.initialCachedData != nil { | ||
err := testIngressCache.Set(tc.initialCachedData.host, tc.initialCachedData.path, tc.initialCachedData.targets) | ||
suite.Require().NoError(err) | ||
} | ||
|
||
testHandler := suite.createTestHandler(suite.backendPort, testIngressCache) | ||
testRequest := suite.createTestHTTPRequest(tc.reqHeaders, tc.reqHost, tc.reqPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate from previous test - create a helper function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replaced suite.createTestHandler
with suite.createTestHandlerAndInitTestCache
to reduce the duplication.
Refactoring the rest of the variables is a bit tedious, so I prefer to keep the test as-is for now.
Done here - CR comment - unit test comments
pkg/dlx/handler.go
Outdated
path := req.URL.Path | ||
resourceNames, err := h.ingressCache.Get(host, path) | ||
if err != nil { | ||
return "", nil, errors.New("Failed to get resourceNames from ingress cache") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return "", nil, errors.New("Failed to get resourceNames from ingress cache") | |
return "", nil, errors.New("Failed to get resource names from ingress cache") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name string | ||
resolveServiceNameErr error | ||
initialCachedData *kube.IngressValue | ||
reqHeaders map[string]string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have no test case for existing request headers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I didn't add coverage for the existing flow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added coverage here - CR comments - Unit tests - add request header flow coverage
pkg/dlx/handler_test.go
Outdated
expectedStatus int | ||
}{ | ||
{ | ||
name: "No ingress headers, host and path found in ingress cache", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name: "No ingress headers, host and path found in ingress cache", | |
name: "No request headers, host and path found in ingress cache", |
Same comment for all test case names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/dlx/handler_test.go
Outdated
expectedResourceNames []string | ||
}{ | ||
{ | ||
name: "No ingress headers, host and path found in ingress cache", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for the cases in this test:
name: "No ingress headers, host and path found in ingress cache", | |
name: "No request headers, host and path found in ingress cache", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/dlx/handler.go
Outdated
h.logger.WarnWith("Failed to get resource names and path from request", | ||
"error", err.Error(), | ||
"host", req.Host, | ||
"path", req.URL.Path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that req.URL is pointer, so might technically be nil, do we check it anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I will cover it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed here - CR comments - handler req.URL is nil
### Description This PR integrates the new DLX from the Scaler package, which introduces a revised internal flow. The changes align the DLX initialization with the new Scaler initialization and configuration patterns by updating the `dlxOptions` and related logic. ### Changes Made - Extracted hardcoded configuration variables - Implemented the `scalertype.ResolveTargetsFromIngressCallback` for extracting Nuclio targets - Added changes to the DLX init (as described in the Jira) ### Testing - Unit tests for the new `resolveCallback` - Manual integration tests to verify the functionalities ### References - Jira ticket link - https://iguazio.atlassian.net/browse/NUC-509 - External links - the related scaler PRs -- v3io/scaler#76 -- v3io/scaler#78 ### Additional Notes - Before merging this PR, the `go.mod` must be modified since it currently relies on a feature branch --------- Co-authored-by: Katerina Molchanova <[email protected]>
Description
This PR integrates the cache into the handler in order to resolve the request target based on the new cache.
Changes Made
IngressHostCacheReader
(the cache read-only access) to the handlerTesting
References
Additional Notes
targetURL
, the handler appends the path twice. This fix is relatively minor and has been tested manually, but since the current code still works, it's worth deciding whether we want to address it now.