Skip to content

Commit

Permalink
add cpuArchitecture filter to nodeResources collector (#930)
Browse files Browse the repository at this point in the history
* filter on cpu architecture

* filter by cpu architecture

* fail if we dont have a label match too

* add tests for cpu arch filter

* update for make schemas
  • Loading branch information
adamancini authored Dec 29, 2022
1 parent df43c9f commit 0f2892c
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ spec:
type: BoolString
filters:
properties:
architecture:
type: string
cpuAllocatable:
type: string
cpuCapacity:
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ spec:
type: BoolString
filters:
properties:
architecture:
type: string
cpuAllocatable:
type: string
cpuCapacity:
Expand Down
2 changes: 2 additions & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,8 @@ spec:
type: BoolString
filters:
properties:
architecture:
type: string
cpuAllocatable:
type: string
cpuCapacity:
Expand Down
49 changes: 39 additions & 10 deletions examples/preflight/node-resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,68 @@ spec:
- pass:
message: This cluster has enough nodes.
- nodeResources:
checkName: Must have 3 nodes with at least 6 cores
checkName: Must have 3 nodes with at least 6 cores each
filters:
cpuCapacity: "6"
outcomes:
- fail:
when: "< 3"
when: "count() < 3"
message: This application requires at least 3 nodes with 6 cores each
- pass:
message: This cluster has enough nodes with enough codes
message: This cluster has enough nodes with enough cores
- nodeResources:
checkName: Must have 1 node with 16 GB (available) memory and 5 cores (on a single node)
checkName: Must have 1 node with 16 GB (available) memory and 10 cores (on a single node)
filters:
allocatableMemory: 16Gi
cpuCapacity: "5"
cpuCapacity: "10"
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.
- nodeResources:
checkName: There must be at least 3 nodes in the cluster
checkName: Must have 1 node with 16 GB (available) memory and 4 cores of amd64 arch (on a single node)
filters:
allocatableMemory: 16Gi
cpuArchitecture: amd64
cpuCapacity: "4"
outcomes:
- fail:
when: "count() < 3"
message: This application requires at least 3 nodes in the cluster
when: "< 1"
message: This application requires at least 1 node with amd64 arch and 16GB available memory
- pass:
message: This cluster has a node with the correct architecture and enough memory.
- nodeResources:
checkName: Must have 1 manager node with 16 GB (available) memory and 4 cores of amd64 arch
filters:
selector:
matchLabel:
node-role.kubernetes.io/master: ""
allocatableMemory: 16Gi
cpuArchitecture: amd64
cpuCapacity: "6"
outcomes:
- fail:
when: "< 1"
message: This application requires at least 1 manager node with amd64 arch, 16GB available memory, and the label `node-role.kubernetes.io/master=""`
- pass:
message: This cluster haas sufficient nodes
message: This cluster has a node with the correct architecture and enough memory.
- nodeResources:
checkName: There must be a total of at least 32Gi of memory on all nodes
outcomes:
- fail:
when: "sum(memoryCapacity) < 32Gi"
message: This application requires that 32Gi or more memory be available to the cluster
- pass:
message: This cluster haas sufficient memory
message: This cluster has sufficient memory
- nodeResources:
checkName: There must be a total of at least 32Gi of memory on amd64 nodes
filters:
cpuArchitecture: amd64
outcomes:
- fail:
when: "sum(memoryCapacity) < 32Gi"
message: This application requires that 32Gi or more memory on amd64 nodes be available to the cluster
- pass:
message: This cluster has sufficient memory
8 changes: 8 additions & 0 deletions pkg/analyze/node_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,14 @@ func nodeMatchesFilters(node corev1.Node, filters *troubleshootv1beta2.NodeResou
}
}

if filters.Architecture != "" {
parsed := filters.Architecture

if !strings.EqualFold(node.Status.NodeInfo.Architecture, parsed) {
return false, nil
}
}

if filters.CPUCapacity != "" {
parsed, err := resource.ParseQuantity(filters.CPUCapacity)
if err != nil {
Expand Down
56 changes: 56 additions & 0 deletions pkg/analyze/node_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ func Test_nodeMatchesFilters(t *testing.T) {
},
},
Status: corev1.NodeStatus{
NodeInfo: corev1.NodeSystemInfo{
Architecture: "amd64",
},
Capacity: corev1.ResourceList{
"attachable-volumes-aws-ebs": resource.MustParse("25"),
"cpu": resource.MustParse("2"),
Expand Down Expand Up @@ -438,6 +441,22 @@ func Test_nodeMatchesFilters(t *testing.T) {
},
expectResult: false,
},
{
name: "true when cpu arch is amd64",
node: node,
filters: &troubleshootv1beta2.NodeResourceFilters{
Architecture: "amd64",
},
expectResult: true,
},
{
name: "false when cpu arch is not amd64",
node: node,
filters: &troubleshootv1beta2.NodeResourceFilters{
Architecture: "armhf",
},
expectResult: false,
},
{
name: "true when allocatable memory is available",
node: node,
Expand Down Expand Up @@ -709,6 +728,43 @@ func Test_analyzeNodeResources(t *testing.T) {
IconURI: "https://troubleshoot.sh/images/analyzer-icons/node-resources.svg?w=16&h=18",
},
},
{
name: "at least 8 cores on amd64", // filter for a node with enough amd64 cores
analyzer: &troubleshootv1beta2.NodeResources{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "amd64-exists",
},
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
When: "max(cpuCapacity) < 8",
Message: "There isn't a node with 8 or more cores on amd64 arch",
URI: "",
},
},
{
Pass: &troubleshootv1beta2.SingleOutcome{
When: "max(cpuCapacity) >= 8",
Message: "There is a node with at least 8 cores on amd64 arch",
URI: "",
},
},
},
Filters: &troubleshootv1beta2.NodeResourceFilters{
Architecture: "amd64",
},
},
want: &AnalyzeResult{
IsPass: true,
IsFail: false,
IsWarn: false,
Title: "amd64-exists",
Message: "There is a node with at least 8 cores on amd64 arch",
URI: "",
IconKey: "kubernetes_node_resources",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/node-resources.svg?w=16&h=18",
},
},
{
name: "unfiltered CPU totals",
analyzer: &troubleshootv1beta2.NodeResources{
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/troubleshoot/v1beta2/analyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type NodeResources struct {
}

type NodeResourceFilters struct {
Architecture string `json:"architecture,omitempty" yaml:"cpuArchitecture,omitempty"`
CPUCapacity string `json:"cpuCapacity,omitempty" yaml:"cpuCapacity,omitempty"`
CPUAllocatable string `json:"cpuAllocatable,omitempty" yaml:"cpuAllocatable,omitempty"`
MemoryCapacity string `json:"memoryCapacity,omitempty" yaml:"memoryCapacity,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions schemas/analyzer-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,9 @@
"filters": {
"type": "object",
"properties": {
"architecture": {
"type": "string"
},
"cpuAllocatable": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions schemas/preflight-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,9 @@
"filters": {
"type": "object",
"properties": {
"architecture": {
"type": "string"
},
"cpuAllocatable": {
"type": "string"
},
Expand Down
3 changes: 3 additions & 0 deletions schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,9 @@
"filters": {
"type": "object",
"properties": {
"architecture": {
"type": "string"
},
"cpuAllocatable": {
"type": "string"
},
Expand Down

0 comments on commit 0f2892c

Please sign in to comment.