-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add Node health controller #17214
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
Add Node health controller #17214
Changes from all commits
Commits
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
120 changes: 120 additions & 0 deletions
120
internal/catalog/internal/controllers/nodehealth/controller.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,120 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package nodehealth | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/consul/internal/catalog/internal/types" | ||
| "github.com/hashicorp/consul/internal/controller" | ||
| "github.com/hashicorp/consul/internal/resource" | ||
| pbcatalog "github.com/hashicorp/consul/proto-public/pbcatalog/v1alpha1" | ||
| "github.com/hashicorp/consul/proto-public/pbresource" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| func NodeHealthController() controller.Controller { | ||
| return controller.ForType(types.NodeType). | ||
| WithWatch(types.HealthStatusType, controller.MapOwnerFiltered(types.NodeType)). | ||
| WithReconciler(&nodeHealthReconciler{}) | ||
| } | ||
|
|
||
| type nodeHealthReconciler struct{} | ||
|
|
||
| func (r *nodeHealthReconciler) Reconcile(ctx context.Context, rt controller.Runtime, req controller.Request) error { | ||
| // The runtime is passed by value so replacing it here for the remaineder of this | ||
| // reconciliation request processing will not affect future invocations. | ||
| rt.Logger = rt.Logger.With("resource-id", req.ID) | ||
|
|
||
| rt.Logger.Trace("reconciling node health") | ||
|
|
||
| // read the node | ||
| rsp, err := rt.Client.Read(ctx, &pbresource.ReadRequest{Id: req.ID}) | ||
| switch { | ||
| case status.Code(err) == codes.NotFound: | ||
| rt.Logger.Trace("node has been deleted") | ||
| return nil | ||
| case err != nil: | ||
| rt.Logger.Error("the resource service has returned an unexpected error", "error", err) | ||
| return err | ||
| } | ||
|
|
||
| res := rsp.Resource | ||
|
|
||
| health, err := getNodeHealth(ctx, rt, req.ID) | ||
| if err != nil { | ||
| rt.Logger.Error("failed to calculate the nodes health", "error", err) | ||
| return err | ||
| } | ||
|
|
||
| message := NodeHealthyMessage | ||
| statusState := pbresource.Condition_STATE_TRUE | ||
| if health != pbcatalog.Health_HEALTH_PASSING { | ||
| statusState = pbresource.Condition_STATE_FALSE | ||
| message = NodeUnhealthyMessage | ||
| } | ||
|
|
||
| newStatus := &pbresource.Status{ | ||
| ObservedGeneration: res.Generation, | ||
| Conditions: []*pbresource.Condition{ | ||
| { | ||
| Type: StatusConditionHealthy, | ||
| State: statusState, | ||
| Reason: health.String(), | ||
| Message: message, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| if resource.EqualStatus(res.Status[StatusKey], newStatus, false) { | ||
| rt.Logger.Trace("resources node health status is unchanged", "health", health.String()) | ||
| return nil | ||
| } | ||
|
|
||
| _, err = rt.Client.WriteStatus(ctx, &pbresource.WriteStatusRequest{ | ||
| Id: res.Id, | ||
| Key: StatusKey, | ||
| Status: newStatus, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| rt.Logger.Error("error encountered when attempting to update the resources node health status", "error", err) | ||
| return err | ||
| } | ||
|
|
||
| rt.Logger.Trace("resources node health status was updated", "health", health.String()) | ||
| return nil | ||
| } | ||
|
|
||
| func getNodeHealth(ctx context.Context, rt controller.Runtime, nodeRef *pbresource.ID) (pbcatalog.Health, error) { | ||
| rsp, err := rt.Client.ListByOwner(ctx, &pbresource.ListByOwnerRequest{ | ||
| Owner: nodeRef, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return pbcatalog.Health_HEALTH_CRITICAL, err | ||
| } | ||
|
|
||
| health := pbcatalog.Health_HEALTH_PASSING | ||
|
|
||
| for _, res := range rsp.Resources { | ||
| if resource.EqualType(res.Id.Type, types.HealthStatusType) { | ||
| var hs pbcatalog.HealthStatus | ||
| if err := res.Data.UnmarshalTo(&hs); err != nil { | ||
| // This should be impossible as the resource service + type validations the | ||
| // catalog is performing will ensure that no data gets written where unmarshalling | ||
| // to this type will error. | ||
| return pbcatalog.Health_HEALTH_CRITICAL, fmt.Errorf("error unmarshalling health status data: %w", err) | ||
| } | ||
|
|
||
| if hs.Status > health { | ||
| health = hs.Status | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return health, nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.