Skip to content

Commit e1bccd7

Browse files
author
Andrew Reed
committed
Analyze longhorn engine
1 parent 2f34561 commit e1bccd7

File tree

2 files changed

+106
-4
lines changed

2 files changed

+106
-4
lines changed

pkg/analyze/longhorn.go

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ func longhorn(analyzer *troubleshootv1beta2.LonghornAnalyze, getCollectedFileCon
5858
replicas = append(replicas, replica)
5959
}
6060

61+
// get engines.longhorn.io
62+
enginesDir := collect.GetLonghornEnginesDirectory(ns)
63+
enginesGlob := filepath.Join(enginesDir, "*")
64+
enginesYaml, err := findFiles(enginesGlob)
65+
if err != nil {
66+
return nil, errors.Wrapf(err, "failed to find longhorn engines files under %s", enginesDir)
67+
}
68+
engines := []*longhornv1beta1.Engine{}
69+
for key, engineYaml := range enginesYaml {
70+
engineYaml = stripRedactedLines(engineYaml)
71+
engine := &longhornv1beta1.Engine{}
72+
err := yaml.Unmarshal(engineYaml, engine)
73+
if err != nil {
74+
return nil, errors.Wrapf(err, "failed to unmarshal engine yaml from %s", key)
75+
}
76+
engines = append(engines, engine)
77+
}
78+
6179
results := []*AnalyzeResult{}
6280

6381
for _, node := range nodes {
@@ -68,6 +86,10 @@ func longhorn(analyzer *troubleshootv1beta2.LonghornAnalyze, getCollectedFileCon
6886
results = append(results, analyzeLonghornReplica(replica))
6987
}
7088

89+
for _, engine := range engines {
90+
results = append(results, analyzeLonghornEngine(engine))
91+
}
92+
7193
return results, nil
7294
}
7395

@@ -123,17 +145,35 @@ func analyzeLonghornReplica(replica *longhornv1beta1.Replica) *AnalyzeResult {
123145
return result
124146
}
125147

148+
func analyzeLonghornEngine(engine *longhornv1beta1.Engine) *AnalyzeResult {
149+
result := &AnalyzeResult{
150+
Title: fmt.Sprintf("Longhorn Engine: %s", engine.Name),
151+
}
152+
153+
desired := engine.Spec.InstanceSpec.DesireState
154+
actual := engine.Status.InstanceStatus.CurrentState
155+
156+
if desired != actual {
157+
result.IsWarn = true
158+
result.Message = fmt.Sprintf("Longhorn engine %s current status %q, should be %q", engine.Name, actual, desired)
159+
return result
160+
}
161+
162+
result.IsPass = true
163+
result.Message = fmt.Sprintf("Engine is %s", actual)
164+
165+
return result
166+
}
167+
126168
func stripRedactedLines(yaml []byte) []byte {
127169
buf := bytes.NewBuffer(yaml)
128170
scanner := bufio.NewScanner(buf)
129171

130172
out := []byte{}
131173

132174
for scanner.Scan() {
133-
if strings.Contains(scanner.Text(), redact.MASK_TEXT) {
134-
continue
135-
}
136-
out = append(out, scanner.Bytes()...)
175+
line := strings.ReplaceAll(scanner.Text(), redact.MASK_TEXT, "HIDDEN")
176+
out = append(out, []byte(line)...)
137177
out = append(out, '\n')
138178
}
139179

pkg/analyze/longhorn_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,65 @@ func TestAnalyzeLonghornReplica(t *testing.T) {
142142
})
143143
}
144144
}
145+
146+
func TestAnalyzeLonghornEngine(t *testing.T) {
147+
tests := []struct {
148+
name string
149+
engine *longhornv1beta1.Engine
150+
expect *AnalyzeResult
151+
}{
152+
{
153+
name: "running",
154+
engine: &longhornv1beta1.Engine{
155+
ObjectMeta: metav1.ObjectMeta{
156+
Name: "pvc-uuid-1",
157+
},
158+
Spec: longhorntypes.EngineSpec{
159+
InstanceSpec: longhorntypes.InstanceSpec{
160+
DesireState: longhorntypes.InstanceStateRunning,
161+
},
162+
},
163+
Status: longhorntypes.EngineStatus{
164+
InstanceStatus: longhorntypes.InstanceStatus{
165+
CurrentState: longhorntypes.InstanceStateRunning,
166+
},
167+
},
168+
},
169+
expect: &AnalyzeResult{
170+
Title: "Longhorn Engine: pvc-uuid-1",
171+
IsPass: true,
172+
Message: "Engine is running",
173+
},
174+
},
175+
{
176+
name: "stopped",
177+
engine: &longhornv1beta1.Engine{
178+
ObjectMeta: metav1.ObjectMeta{
179+
Name: "pvc-uuid-1",
180+
},
181+
Spec: longhorntypes.EngineSpec{
182+
InstanceSpec: longhorntypes.InstanceSpec{
183+
DesireState: longhorntypes.InstanceStateRunning,
184+
},
185+
},
186+
Status: longhorntypes.EngineStatus{
187+
InstanceStatus: longhorntypes.InstanceStatus{
188+
CurrentState: longhorntypes.InstanceStateStopped,
189+
},
190+
},
191+
},
192+
expect: &AnalyzeResult{
193+
Title: "Longhorn Engine: pvc-uuid-1",
194+
IsWarn: true,
195+
Message: `Longhorn engine pvc-uuid-1 current status "stopped", should be "running"`,
196+
},
197+
},
198+
}
199+
for _, test := range tests {
200+
t.Run(test.name, func(t *testing.T) {
201+
got := analyzeLonghornEngine(test.engine)
202+
203+
assert.Equal(t, test.expect, got)
204+
})
205+
}
206+
}

0 commit comments

Comments
 (0)