-
Notifications
You must be signed in to change notification settings - Fork 106
Node analyzers #127
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
Node analyzers #127
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,38 @@ | ||
| apiVersion: troubleshoot.replicated.com/v1beta1 | ||
| kind: Preflight | ||
| metadata: | ||
| name: sample | ||
| spec: | ||
| analyzers: | ||
| - nodeResources: | ||
| checkName: Must have at least 3 nodes in the cluster | ||
| outcomes: | ||
| - fail: | ||
| when: "< 3" | ||
| message: This application requires at least 3 nodes | ||
| - warn: | ||
| when: "< 5" | ||
| message: This application recommends at last 5 nodes. | ||
| - pass: | ||
| message: This cluster has enough nodes. | ||
| - nodeResources: | ||
| checkName: Must have 3 nodes with at least 6 cores | ||
| filters: | ||
| cpuCapacity: "6" | ||
| outcomes: | ||
| - fail: | ||
| when: "< 3" | ||
| message: This application requires at least 3 nodes with 6 cores each | ||
| - pass: | ||
| message: This cluster has enough nodes with enough codes | ||
| - nodeResources: | ||
| checkName: Must have 1 node with 16 GB (available) memory and 5 cores (on a single node) | ||
| filters: | ||
| allocatableMemory: 16Gi | ||
| cpuCapacity: "5" | ||
| outcomes: | ||
| - fail: | ||
| when: "< 1" | ||
| message: This application requires at least 1 node with 16GB available memory | ||
| - pass: | ||
| message: This cluster has a node with enough memory. |
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
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,218 @@ | ||
| package analyzer | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/resource" | ||
| ) | ||
|
|
||
| func analyzeNodeResources(analyzer *troubleshootv1beta1.NodeResources, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) { | ||
| collected, err := getCollectedFileContents("cluster-resources/nodes.json") | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to get contents of nodes.json") | ||
| } | ||
|
|
||
| var nodes []corev1.Node | ||
| if err := json.Unmarshal(collected, &nodes); err != nil { | ||
| return nil, errors.Wrap(err, "failed to unmarshal node list") | ||
| } | ||
|
|
||
| matchingNodeCount := 0 | ||
|
|
||
| for _, node := range nodes { | ||
| isMatch, err := nodeMatchesFilters(node, analyzer.Filters) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to check if node matches filter") | ||
| } | ||
|
|
||
| if isMatch { | ||
| matchingNodeCount++ | ||
| } | ||
| } | ||
|
|
||
| title := analyzer.CheckName | ||
| if title == "" { | ||
| title = "Node Resources" | ||
| } | ||
|
|
||
| result := &AnalyzeResult{ | ||
| Title: title, | ||
| } | ||
|
|
||
| for _, outcome := range analyzer.Outcomes { | ||
| if outcome.Fail != nil { | ||
| isWhenMatch, err := compareNodeResourceConditionalToActual(outcome.Fail.When, matchingNodeCount) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to parse when") | ||
| } | ||
|
|
||
| if isWhenMatch { | ||
| result.IsFail = true | ||
| result.Message = outcome.Fail.Message | ||
| result.URI = outcome.Fail.URI | ||
|
|
||
| return result, nil | ||
| } | ||
| } else if outcome.Warn != nil { | ||
| isWhenMatch, err := compareNodeResourceConditionalToActual(outcome.Warn.When, matchingNodeCount) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to parse when") | ||
| } | ||
|
|
||
| if isWhenMatch { | ||
| result.IsWarn = true | ||
| result.Message = outcome.Warn.Message | ||
| result.URI = outcome.Warn.URI | ||
|
|
||
| return result, nil | ||
| } | ||
| } else if outcome.Pass != nil { | ||
| isWhenMatch, err := compareNodeResourceConditionalToActual(outcome.Pass.When, matchingNodeCount) | ||
| if err != nil { | ||
| return nil, errors.Wrap(err, "failed to parse when") | ||
| } | ||
|
|
||
| if isWhenMatch { | ||
| result.IsPass = true | ||
| result.Message = outcome.Pass.Message | ||
| result.URI = outcome.Pass.URI | ||
|
|
||
| return result, nil | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func compareNodeResourceConditionalToActual(conditional string, actual int) (bool, error) { | ||
| if conditional == "" { | ||
| return true, nil | ||
| } | ||
|
|
||
| parts := strings.Split(strings.TrimSpace(conditional), " ") | ||
|
|
||
| if len(parts) != 2 { | ||
| return false, errors.New("unable to parse nodeResources conditional") | ||
| } | ||
|
|
||
| operator := parts[0] | ||
| desiredValue, err := strconv.Atoi(parts[1]) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse nodeResource value") | ||
| } | ||
|
|
||
| switch operator { | ||
| case "=", "==", "===": | ||
| return desiredValue == actual, nil | ||
| case "<": | ||
| return actual < desiredValue, nil | ||
| case "<=": | ||
| return actual <= desiredValue, nil | ||
| case ">": | ||
| return actual > desiredValue, nil | ||
| case ">=": | ||
| return actual >= desiredValue, nil | ||
| } | ||
|
|
||
| return false, errors.New("unexpected conditional in nodeResources") | ||
| } | ||
|
|
||
| func nodeMatchesFilters(node corev1.Node, filters *troubleshootv1beta1.NodeResourceFilters) (bool, error) { | ||
| if filters == nil { | ||
| return true, nil | ||
| } | ||
|
|
||
| // all filters must pass for this to pass | ||
|
|
||
| if filters.CPUCapacity != "" { | ||
| parsed, err := resource.ParseQuantity(filters.CPUCapacity) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse cpu capacity") | ||
| } | ||
|
|
||
| if node.Status.Capacity.Cpu().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
| if filters.CPUAllocatable != "" { | ||
| parsed, err := resource.ParseQuantity(filters.CPUAllocatable) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse cpu allocatable") | ||
| } | ||
|
|
||
| if node.Status.Allocatable.Cpu().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| if filters.MemoryCapacity != "" { | ||
| parsed, err := resource.ParseQuantity(filters.MemoryCapacity) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse memory capacity") | ||
| } | ||
|
|
||
| if node.Status.Capacity.Memory().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
| if filters.MemoryAllocatable != "" { | ||
| parsed, err := resource.ParseQuantity(filters.MemoryAllocatable) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse memory allocatable") | ||
| } | ||
|
|
||
| if node.Status.Allocatable.Memory().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| if filters.PodCapacity != "" { | ||
| parsed, err := resource.ParseQuantity(filters.PodCapacity) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse pod capacity") | ||
| } | ||
|
|
||
| if node.Status.Capacity.Pods().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
| if filters.PodAllocatable != "" { | ||
| parsed, err := resource.ParseQuantity(filters.PodAllocatable) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse pod allocatable") | ||
| } | ||
|
|
||
| if node.Status.Allocatable.Pods().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| if filters.EphemeralStorageCapacity != "" { | ||
| parsed, err := resource.ParseQuantity(filters.EphemeralStorageCapacity) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse ephemeralstorage capacity") | ||
| } | ||
|
|
||
| if node.Status.Capacity.StorageEphemeral().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
| if filters.EphemeralStorageAllocatable != "" { | ||
| parsed, err := resource.ParseQuantity(filters.EphemeralStorageAllocatable) | ||
| if err != nil { | ||
| return false, errors.Wrap(err, "failed to parse ephemeralstorage allocatable") | ||
| } | ||
|
|
||
| if node.Status.Allocatable.StorageEphemeral().Cmp(parsed) == -1 { | ||
| return false, nil | ||
| } | ||
| } | ||
|
|
||
| return true, 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.
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.
how about
!=too?