-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Service http checks data source for agentless proxies #14924
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e6afcc9
Service http checks data source for agentless proxies
jshahriddhi a8062c7
Add changelog
jshahriddhi 69df3fe
Address feedback
jshahriddhi 6532064
Include nodename in req for test
jshahriddhi ac79b85
Pass node name in proxy mgr config
jshahriddhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ```release-note:bug | ||
| server: fixes the error trying to source proxy configuration for http checks, in case of proxies using consul-dataplane. | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package proxycfgglue | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/consul/agent/cache" | ||
| cachetype "github.com/hashicorp/consul/agent/cache-types" | ||
| "github.com/hashicorp/consul/agent/local" | ||
| "github.com/hashicorp/consul/agent/proxycfg" | ||
| "github.com/hashicorp/consul/agent/structs" | ||
| ) | ||
|
|
||
| // CacheHTTPChecks satisifies the proxycfg.HTTPChecks interface by sourcing | ||
| // data from the agent cache. | ||
| func CacheHTTPChecks(c *cache.Cache) proxycfg.HTTPChecks { | ||
| return &cacheProxyDataSource[*cachetype.ServiceHTTPChecksRequest]{c, cachetype.ServiceHTTPChecksName} | ||
| } | ||
|
|
||
| // ServerHTTPChecks satisifies the proxycfg.HTTPChecks interface. | ||
| // It sources data from the server agent cache if the service exists in the local state, else | ||
| // it is a no-op since the checks can only be performed by local agents. | ||
| func ServerHTTPChecks(deps ServerDataSourceDeps, nodeName string, cacheSource proxycfg.HTTPChecks, localState *local.State) proxycfg.HTTPChecks { | ||
| return serverHTTPChecks{deps, nodeName, cacheSource, localState} | ||
| } | ||
|
|
||
| type serverHTTPChecks struct { | ||
| deps ServerDataSourceDeps | ||
| nodeName string | ||
| cacheSource proxycfg.HTTPChecks | ||
| localState *local.State | ||
| } | ||
|
|
||
| func (c serverHTTPChecks) Notify(ctx context.Context, req *cachetype.ServiceHTTPChecksRequest, correlationID string, ch chan<- proxycfg.UpdateEvent) error { | ||
| // if the service exists in the current server agent local state, delegate to the cache data source | ||
| if req.NodeName == c.nodeName && c.localState.ServiceExists(structs.ServiceID{ID: req.ServiceID, EnterpriseMeta: req.EnterpriseMeta}) { | ||
| return c.cacheSource.Notify(ctx, req, correlationID, ch) | ||
| } | ||
| l := c.deps.Logger.With("service_id", req.ServiceID) | ||
| l.Debug("service-http-checks: no-op") | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package proxycfgglue | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/go-hclog" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| cachetype "github.com/hashicorp/consul/agent/cache-types" | ||
| "github.com/hashicorp/consul/agent/local" | ||
| "github.com/hashicorp/consul/agent/proxycfg" | ||
| "github.com/hashicorp/consul/agent/structs" | ||
| "github.com/hashicorp/consul/agent/token" | ||
| ) | ||
|
|
||
| func TestServerHTTPChecks(t *testing.T) { | ||
| var ( | ||
| ctx = context.Background() | ||
| svcID = "web-sidecar-proxy-1" | ||
| correlationID = "correlation-id" | ||
| ch = make(chan<- proxycfg.UpdateEvent) | ||
| cacheResult = errors.New("KABOOM") | ||
| nodeName = "server-1" | ||
| ) | ||
|
|
||
| type testCase struct { | ||
| name string | ||
| serviceInLocalState bool | ||
| req *cachetype.ServiceHTTPChecksRequest | ||
| expectedResult error | ||
| } | ||
|
|
||
| run := func(t *testing.T, tc testCase) { | ||
| serviceID := structs.NewServiceID(svcID, nil) | ||
| localState := testLocalState(t) | ||
| mockCacheSource := newMockServiceHTTPChecks(t) | ||
| if tc.serviceInLocalState { | ||
| require.NoError(t, localState.AddServiceWithChecks(&structs.NodeService{ID: serviceID.ID}, nil, "")) | ||
| } | ||
| if tc.req.NodeName == nodeName && tc.serviceInLocalState { | ||
| mockCacheSource.On("Notify", ctx, tc.req, correlationID, ch).Return(cacheResult) | ||
| } else { | ||
| mockCacheSource.AssertNotCalled(t, "Notify") | ||
| } | ||
|
|
||
| dataSource := ServerHTTPChecks(ServerDataSourceDeps{Logger: hclog.NewNullLogger()}, nodeName, mockCacheSource, localState) | ||
| err := dataSource.Notify(ctx, tc.req, correlationID, ch) | ||
| require.Equal(t, tc.expectedResult, err) | ||
| } | ||
|
|
||
| testcases := []testCase{ | ||
| { | ||
| name: "delegate to cache source if service in local state of the server node", | ||
| serviceInLocalState: true, | ||
| req: &cachetype.ServiceHTTPChecksRequest{ServiceID: svcID, NodeName: nodeName}, | ||
| expectedResult: cacheResult, | ||
| }, | ||
| { | ||
| name: "no-op if service not in local state of server node", | ||
| serviceInLocalState: false, | ||
| req: &cachetype.ServiceHTTPChecksRequest{ServiceID: svcID, NodeName: nodeName}, | ||
| expectedResult: nil, | ||
| }, | ||
| { | ||
| name: "no-op if service with same ID in local state but belongs to different node", | ||
| serviceInLocalState: true, | ||
| req: &cachetype.ServiceHTTPChecksRequest{ServiceID: svcID, NodeName: "server-2"}, | ||
| expectedResult: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testcases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| run(t, tc) | ||
| }) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func newMockServiceHTTPChecks(t *testing.T) *mockServiceHTTPChecks { | ||
| mock := &mockServiceHTTPChecks{} | ||
| mock.Mock.Test(t) | ||
|
|
||
| t.Cleanup(func() { mock.AssertExpectations(t) }) | ||
|
|
||
| return mock | ||
| } | ||
|
|
||
| type mockServiceHTTPChecks struct { | ||
| mock.Mock | ||
| } | ||
|
|
||
| func (m *mockServiceHTTPChecks) Notify(ctx context.Context, req *cachetype.ServiceHTTPChecksRequest, correlationID string, ch chan<- proxycfg.UpdateEvent) error { | ||
| return m.Called(ctx, req, correlationID, ch).Error(0) | ||
| } | ||
|
|
||
| func testLocalState(t *testing.T) *local.State { | ||
| t.Helper() | ||
|
|
||
| l := local.NewState(local.Config{}, hclog.NewNullLogger(), &token.Store{}) | ||
| l.TriggerSyncChanges = func() {} | ||
| return l | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.