Skip to content

Commit 52efd16

Browse files
authored
feat: allow users to check cpu arch (#1644)
1 parent 402d111 commit 52efd16

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

pkg/analyze/host_cpu.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (a *AnalyzeHostCPU) Analyze(
6363
return []*AnalyzeResult{&result}, nil
6464
}
6565

66-
isMatch, err := compareHostCPUConditionalToActual(outcome.Fail.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags)
66+
isMatch, err := compareHostCPUConditionalToActual(outcome.Fail.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
6767
if err != nil {
6868
return nil, errors.Wrap(err, "failed to compare")
6969
}
@@ -84,7 +84,7 @@ func (a *AnalyzeHostCPU) Analyze(
8484
return []*AnalyzeResult{&result}, nil
8585
}
8686

87-
isMatch, err := compareHostCPUConditionalToActual(outcome.Warn.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags)
87+
isMatch, err := compareHostCPUConditionalToActual(outcome.Warn.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
8888
if err != nil {
8989
return nil, errors.Wrap(err, "failed to compare")
9090
}
@@ -105,7 +105,7 @@ func (a *AnalyzeHostCPU) Analyze(
105105
return []*AnalyzeResult{&result}, nil
106106
}
107107

108-
isMatch, err := compareHostCPUConditionalToActual(outcome.Pass.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags)
108+
isMatch, err := compareHostCPUConditionalToActual(outcome.Pass.When, cpuInfo.LogicalCount, cpuInfo.PhysicalCount, cpuInfo.Flags, cpuInfo.MachineArch)
109109
if err != nil {
110110
return nil, errors.Wrap(err, "failed to compare")
111111
}
@@ -164,14 +164,19 @@ func doCompareHostCPUFlags(expected string, flags []string) (res bool, err error
164164
return true, nil
165165
}
166166

167-
func compareHostCPUConditionalToActual(conditional string, logicalCount int, physicalCount int, flags []string) (res bool, err error) {
167+
func compareHostCPUConditionalToActual(conditional string, logicalCount int, physicalCount int, flags []string, machineArch string) (res bool, err error) {
168168
compareLogical := false
169169
comparePhysical := false
170170
compareUnspecified := false
171+
compareMachineArch := false
171172

172173
comparator := ""
173174
desired := ""
174175

176+
/* When the conditional is in the format of "logical <comparator> <desired>"
177+
example: when: "count < 2"
178+
*/
179+
175180
parts := strings.Split(conditional, " ")
176181
if len(parts) == 3 {
177182
comparator = parts[1]
@@ -182,6 +187,8 @@ func compareHostCPUConditionalToActual(conditional string, logicalCount int, phy
182187
comparePhysical = true
183188
} else if strings.ToLower(parts[0]) == "count" {
184189
compareUnspecified = true
190+
} else if strings.ToLower(parts[0]) == "machinearch" {
191+
compareMachineArch = true
185192
}
186193
} else if len(parts) == 2 {
187194
compareUnspecified = true
@@ -199,14 +206,16 @@ func compareHostCPUConditionalToActual(conditional string, logicalCount int, phy
199206
return doCompareHostCPUFlags(desired, flags)
200207
}
201208

202-
if !compareLogical && !comparePhysical && !compareUnspecified {
209+
if !compareLogical && !comparePhysical && !compareUnspecified && !compareMachineArch {
203210
return false, errors.New("unable to parse conditional")
204211
}
205212

206213
if compareLogical {
207214
return doCompareHostCPU(comparator, desired, logicalCount)
208215
} else if comparePhysical {
209216
return doCompareHostCPU(comparator, desired, physicalCount)
217+
} else if compareMachineArch {
218+
return doCompareMachineArch(comparator, desired, machineArch)
210219
} else {
211220
actual := logicalCount
212221
if physicalCount > logicalCount {
@@ -217,6 +226,16 @@ func compareHostCPUConditionalToActual(conditional string, logicalCount int, phy
217226
}
218227
}
219228

229+
func doCompareMachineArch(operator string, desired string, actual string) (bool, error) {
230+
switch operator {
231+
case "=", "==", "===":
232+
return actual == desired, nil
233+
case "!=", "!==":
234+
return actual != desired, nil
235+
}
236+
return false, errors.New("unknown operator")
237+
}
238+
220239
func doCompareHostCPU(operator string, desired string, actual int) (bool, error) {
221240
desiredInt, err := strconv.ParseInt(desired, 10, 64)
222241
if err != nil {
@@ -234,6 +253,8 @@ func doCompareHostCPU(operator string, desired string, actual int) (bool, error)
234253
return actual >= int(desiredInt), nil
235254
case "=", "==", "===":
236255
return actual == int(desiredInt), nil
256+
case "!=", "!==":
257+
return actual != int(desiredInt), nil
237258
}
238259

239260
return false, errors.New("unknown operator")

pkg/analyze/host_cpu_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ func Test_compareHostCPUConditionalToActual(t *testing.T) {
8282
logicalCount int
8383
physicalCount int
8484
flags []string
85+
machineArch string
8586
expected bool
8687
}{
8788
{
@@ -164,12 +165,18 @@ func Test_compareHostCPUConditionalToActual(t *testing.T) {
164165
flags: []string{"a", "b", "c", "d", "e"},
165166
expected: true,
166167
},
168+
{
169+
name: "machine arch matches",
170+
when: "machineArch == x86_64",
171+
machineArch: "x86_64",
172+
expected: true,
173+
},
167174
}
168175
for _, test := range tests {
169176
t.Run(test.name, func(t *testing.T) {
170177
req := require.New(t)
171178

172-
actual, err := compareHostCPUConditionalToActual(test.when, test.logicalCount, test.physicalCount, test.flags)
179+
actual, err := compareHostCPUConditionalToActual(test.when, test.logicalCount, test.physicalCount, test.flags, test.machineArch)
173180
req.NoError(err)
174181

175182
assert.Equal(t, test.expected, actual)

pkg/collect/host_cpu.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"github.com/pkg/errors"
88
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
99
"github.com/shirou/gopsutil/v3/cpu"
10+
"github.com/shirou/gopsutil/v3/host"
1011
)
1112

1213
type CPUInfo struct {
1314
LogicalCount int `json:"logicalCount"`
1415
PhysicalCount int `json:"physicalCount"`
1516
Flags []string `json:"flags"`
17+
MachineArch string `json:"machineArch"`
1618
}
1719

1820
const HostCPUPath = `host-collectors/system/cpu.json`
@@ -45,6 +47,11 @@ func (c *CollectHostCPU) Collect(progressChan chan<- interface{}) (map[string][]
4547
}
4648
cpuInfo.PhysicalCount = physicalCount
4749

50+
cpuInfo.MachineArch, err = host.KernelArch()
51+
if err != nil {
52+
return nil, errors.Wrap(err, "failed to fetch cpu architecture")
53+
}
54+
4855
// XXX even though the cpu.Info() returns a slice per CPU it is way
4956
// common to have the same flags for all CPUs. We consolidate them here
5057
// so the output is a list of all different flags present in all CPUs.

pkg/collect/host_cpu_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func TestCollectHostCPU_Collect(t *testing.T) {
2525
require.NoError(t, err)
2626

2727
// Check if values exist. They will be different on different machines.
28-
assert.Equal(t, 3, len(m))
28+
assert.Equal(t, 4, len(m))
2929
assert.Contains(t, m, "logicalCount")
3030
assert.Contains(t, m, "physicalCount")
3131
assert.Contains(t, m, "flags")
32+
assert.Contains(t, m, "machineArch")
3233
}

0 commit comments

Comments
 (0)