-
Notifications
You must be signed in to change notification settings - Fork 2.1k
check_restart: move feature to task level hook #27750
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
Open
mismithhisler
wants to merge
6
commits into
b-check-restart-refactor
Choose a base branch
from
b-fix-check-restart-block
base: b-check-restart-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
193082c
check_restart: move feature to task level hook
mismithhisler f2927e8
adds proper service interpolation and restarting
mismithhisler be240e2
add tests
mismithhisler d45080c
fix tests
mismithhisler 354c491
more test fixes
mismithhisler 642fda7
add idempotent failure restarts and update watcher tests
mismithhisler 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
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,135 @@ | ||
| // Copyright IBM Corp. 2015, 2025 | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package taskrunner | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/nomad/client/allocrunner/interfaces" | ||
| "github.com/hashicorp/nomad/client/serviceregistration" | ||
| "github.com/hashicorp/nomad/client/serviceregistration/wrapper" | ||
| "github.com/hashicorp/nomad/client/taskenv" | ||
| "github.com/hashicorp/nomad/command/agent/consul" | ||
| "github.com/hashicorp/nomad/nomad/structs" | ||
| ) | ||
|
|
||
| type hookCheck struct { | ||
| // providerType defines the backend service that is checking | ||
| // the service. This is currently either Nomad or Consul. | ||
| providerType string | ||
|
|
||
| // providerNS is the providers namespace that the service is | ||
| // registered. When a provider implements namespaces (i.e. Consul), | ||
| // Nomad runs a single check watcher per namespace. | ||
| providerNS string | ||
|
|
||
| // checkID is the ID of the check used to register a check_restart watch | ||
| checkID string | ||
|
|
||
| // check is the actual Nomad service check configuration | ||
| check *structs.ServiceCheck | ||
| } | ||
|
|
||
| // The checkRestartHook is responsible for registering/deregistering _both_ group and task | ||
| // check_start blocks with the appropriate CheckWatcher. This is a standalone hook and not part | ||
| // of the service hook because restarting checks is task specific, even though check_restart | ||
| // can be defined at the group level. Therefore this task will look at both TG and task services. | ||
| type checkRestartHook struct { | ||
| checks []*hookCheck | ||
| handler *wrapper.HandlerWrapper | ||
| wr serviceregistration.WorkloadRestarter | ||
| allocID string | ||
| taskName string | ||
| taskEnv *taskenv.TaskEnv | ||
|
|
||
| tgName string | ||
| tgServices []*structs.Service | ||
| taskServices []*structs.Service | ||
| } | ||
|
|
||
| func newCheckRestartHook(alloc *structs.Allocation, task *structs.Task, handler *wrapper.HandlerWrapper, restarter serviceregistration.WorkloadRestarter) *checkRestartHook { | ||
| tg := alloc.Job.LookupTaskGroup(alloc.TaskGroup) | ||
| return &checkRestartHook{ | ||
| handler: handler, | ||
| allocID: alloc.ID, | ||
| taskName: task.Name, | ||
| tgName: tg.Name, | ||
| tgServices: tg.Services, | ||
| taskServices: task.Services, | ||
| wr: restarter, | ||
| } | ||
| } | ||
|
|
||
| func (h *checkRestartHook) Name() string { | ||
| return "check_restart" | ||
| } | ||
|
|
||
| func (h *checkRestartHook) Prestart(ctx context.Context, req *interfaces.TaskPrestartRequest, _ *interfaces.TaskPrestartResponse) error { | ||
| var checks []*hookCheck | ||
| for _, s := range taskenv.InterpolateServices(req.TaskEnv, h.tgServices) { | ||
| for _, c := range s.Checks { | ||
| if c.TriggersRestarts() && c.TaskName == h.taskName { | ||
| checks = append(checks, &hookCheck{ | ||
| providerType: s.Provider, | ||
| providerNS: s.Cluster, | ||
| check: c, | ||
| // TODO: does this work for Nomad? | ||
| checkID: checkID(h.allocID, h.tgName, fmt.Sprintf("group-%s", h.tgName), s.Provider, c, s), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, s := range taskenv.InterpolateServices(req.TaskEnv, h.taskServices) { | ||
| for _, c := range s.Checks { | ||
| if c.TriggersRestarts() { | ||
| checks = append(checks, &hookCheck{ | ||
| providerType: s.Provider, | ||
| providerNS: s.Cluster, | ||
| check: c, | ||
| // TODO: does this work for Nomad? | ||
| checkID: checkID(h.allocID, h.tgName, h.taskName, s.Provider, c, s), | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| h.checks = checks | ||
|
|
||
| for _, c := range h.checks { | ||
| watcher := h.handler.CheckWatcher(c.providerType, c.providerNS) | ||
| watcher.Watch(c.checkID, c.check, h.wr) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (h *checkRestartHook) Exited(ctx context.Context, req *interfaces.TaskExitedRequest, resp *interfaces.TaskExitedResponse) error { | ||
| for _, c := range h.checks { | ||
| watcher := h.handler.CheckWatcher(c.providerType, c.providerNS) | ||
| watcher.Unwatch(c.checkID) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (h *checkRestartHook) Stop(ctx context.Context, req *interfaces.TaskStopRequest, resp *interfaces.TaskStopResponse) error { | ||
| for _, c := range h.checks { | ||
| watcher := h.handler.CheckWatcher(c.providerType, c.providerNS) | ||
| watcher.Unwatch(c.checkID) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // checkID returns a provider specific checkID for the workload. Unfortunately nomad and consul use different | ||
| // methods for creating checkID's so these are quite different. Consul distinguishes between group and task | ||
| // checkID's, but Nomad seems to just always use the task group name? | ||
| func checkID(allocID, tg, task, ptype string, check *structs.ServiceCheck, service *structs.Service) string { | ||
| switch ptype { | ||
| case "nomad": | ||
| return string(structs.NomadCheckID(allocID, tg, check)) | ||
| case "consul": | ||
| return consul.MakeCheckID(serviceregistration.MakeAllocServiceID(allocID, task, service), check) | ||
| default: | ||
| return "" | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
client/allocrunner/taskrunner/check_restart_hook_test.go
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,123 @@ | ||
| // Copyright IBM Corp. 2015, 2025 | ||
| // SPDX-License-Identifier: BUSL-1.1 | ||
|
|
||
| package taskrunner | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/nomad/client/allocrunner/interfaces" | ||
| regMock "github.com/hashicorp/nomad/client/serviceregistration/mock" | ||
| "github.com/hashicorp/nomad/client/serviceregistration/wrapper" | ||
| "github.com/hashicorp/nomad/client/taskenv" | ||
| "github.com/hashicorp/nomad/command/agent/consul" | ||
| "github.com/hashicorp/nomad/helper/testlog" | ||
| "github.com/hashicorp/nomad/nomad/mock" | ||
| "github.com/hashicorp/nomad/nomad/structs" | ||
| "github.com/shoenig/test/must" | ||
| tmock "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| func TestCheckRestartHook_Prestart(t *testing.T) { | ||
| logger := testlog.HCLogger(t) | ||
|
|
||
| alloc := mock.Alloc() | ||
| alloc.Job.Canonicalize() | ||
|
|
||
| handler := regMock.NewServiceRegistrationHandler(logger) | ||
|
|
||
| mockWatcher := ®Mock.MockUniversalWatcher{} | ||
| mockWatcher.On("Watch", tmock.Anything, tmock.Anything, tmock.Anything) | ||
| handler.UniversalWatcher = mockWatcher | ||
|
|
||
| regWrap := wrapper.NewHandlerWrapper(logger, handler, handler) | ||
|
|
||
| service := &structs.Service{ | ||
| Provider: "nomad", | ||
| Checks: []*structs.ServiceCheck{ | ||
| { | ||
| TaskName: "web", | ||
| CheckRestart: &structs.CheckRestart{ | ||
| Limit: 1, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // group level service | ||
| alloc.Job.LookupTaskGroup("web").Services = []*structs.Service{service} | ||
|
|
||
| // task level service | ||
| task := alloc.LookupTask("web") | ||
| task.Services = []*structs.Service{service} | ||
|
|
||
| testHook := newCheckRestartHook(alloc, task, regWrap, consul.NoopRestarter()) | ||
|
|
||
| err := testHook.Prestart( | ||
| t.Context(), | ||
| &interfaces.TaskPrestartRequest{TaskEnv: taskenv.NewEmptyTaskEnv()}, | ||
| &interfaces.TaskPrestartResponse{}, | ||
| ) | ||
| must.NoError(t, err) | ||
| must.True(t, mockWatcher.AssertNumberOfCalls(t, "Watch", 2)) | ||
| } | ||
|
|
||
| func TestCheckRestartHook_Exited(t *testing.T) { | ||
| logger := testlog.HCLogger(t) | ||
|
|
||
| alloc := mock.Alloc() | ||
| alloc.Job.Canonicalize() | ||
|
|
||
| mockWatcher := ®Mock.MockUniversalWatcher{} | ||
| mockWatcher.On("Unwatch", tmock.Anything) | ||
|
|
||
| handler := regMock.NewServiceRegistrationHandler(logger) | ||
| handler.UniversalWatcher = mockWatcher | ||
|
|
||
| regWrap := wrapper.NewHandlerWrapper(logger, handler, handler) | ||
|
|
||
| testHook := newCheckRestartHook(alloc, alloc.LookupTask("web"), regWrap, consul.NoopRestarter()) | ||
| testHook.checks = []*hookCheck{ | ||
| { | ||
| providerType: "nomad", | ||
| }, | ||
| } | ||
|
|
||
| err := testHook.Exited( | ||
| t.Context(), | ||
| &interfaces.TaskExitedRequest{}, | ||
| &interfaces.TaskExitedResponse{}, | ||
| ) | ||
| must.NoError(t, err) | ||
| must.True(t, mockWatcher.AssertNumberOfCalls(t, "Unwatch", 1)) | ||
| } | ||
|
|
||
| func TestCheckRestartHook_Stop(t *testing.T) { | ||
| logger := testlog.HCLogger(t) | ||
|
|
||
| alloc := mock.Alloc() | ||
| alloc.Job.Canonicalize() | ||
|
|
||
| mockWatcher := ®Mock.MockUniversalWatcher{} | ||
| mockWatcher.On("Unwatch", tmock.Anything) | ||
|
|
||
| handler := regMock.NewServiceRegistrationHandler(logger) | ||
| handler.UniversalWatcher = mockWatcher | ||
|
|
||
| regWrap := wrapper.NewHandlerWrapper(logger, handler, handler) | ||
|
|
||
| testHook := newCheckRestartHook(alloc, alloc.LookupTask("web"), regWrap, consul.NoopRestarter()) | ||
| testHook.checks = []*hookCheck{ | ||
| { | ||
| providerType: "nomad", | ||
| }, | ||
| } | ||
|
|
||
| err := testHook.Stop( | ||
| t.Context(), | ||
| &interfaces.TaskStopRequest{}, | ||
| &interfaces.TaskStopResponse{}, | ||
| ) | ||
| must.NoError(t, err) | ||
| must.True(t, mockWatcher.AssertNumberOfCalls(t, "Unwatch", 1)) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
c.TaskName == h.taskNamemeans we would only register that for group level checks, only the task specified in the service/check will be restarted. Also if the task is not specified the check won't even be registered.