Skip to content

Commit 8c31e61

Browse files
authored
fix(collectors): Fix logs collection in longhorn collector (#886)
* fix(collectors): Fix logs collection in longhorn collector * Small typo * Run go fmt on added changes
1 parent 8e174fb commit 8c31e61

File tree

5 files changed

+56
-23
lines changed

5 files changed

+56
-23
lines changed

pkg/collect/ceph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func cephCommandExec(ctx context.Context, progressChan chan<- interface{}, c *Co
172172
}
173173

174174
pathPrefix := GetCephCollectorFilepath(cephCollector.CollectorName, cephCollector.Namespace)
175-
for srcFilename, _ := range results {
175+
for srcFilename := range results {
176176
var dstFileName string
177177
switch {
178178
case strings.HasSuffix(srcFilename, "-stdout.txt"):

pkg/collect/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
type CollectLogs struct {
2121
Collector *troubleshootv1beta2.Logs
2222
BundlePath string
23-
Namespace string
23+
Namespace string // There is a Namespace parameter in troubleshootv1beta2.Logs. Should we remove this?
2424
ClientConfig *rest.Config
2525
Client kubernetes.Interface
2626
Context context.Context

pkg/collect/longhorn.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"bytes"
66
"context"
77
"fmt"
8-
"path"
98
"path/filepath"
109
"regexp"
1110
"sync"
@@ -214,27 +213,10 @@ func (c *CollectLonghorn) Collect(progressChan chan<- interface{}) (CollectorRes
214213
}
215214
output.SaveResult(c.BundlePath, settingsKey, bytes.NewBuffer(settingsB))
216215

217-
// logs of all pods in namespace
218-
logsCollectorSpec := &troubleshootv1beta2.Logs{
219-
Selector: []string{""},
220-
Namespace: ns,
221-
}
222-
223-
rbacErrors := c.GetRBACErrors()
224-
logsCollector := &CollectLogs{logsCollectorSpec, c.BundlePath, c.Namespace, c.ClientConfig, c.Client, c.Context, nil, rbacErrors}
225-
226-
logs, err := logsCollector.Collect(progressChan)
216+
err = c.collectLonghornLogs(ns, output, progressChan)
227217
if err != nil {
228218
return nil, errors.Wrap(err, "collect longhorn logs")
229219
}
230-
logsDir := GetLonghornLogsDirectory(ns)
231-
for srcFilename, _ := range logs {
232-
dstFileName := path.Join(logsDir, srcFilename)
233-
err := copyResult(logs, output, c.BundlePath, srcFilename, dstFileName)
234-
if err != nil {
235-
logger.Printf("Failed to copy file %s; %v", srcFilename, err)
236-
}
237-
}
238220

239221
// https://longhorn.io/docs/1.1.1/advanced-resources/data-recovery/corrupted-replica/
240222

@@ -307,6 +289,29 @@ func (c *CollectLonghorn) Collect(progressChan chan<- interface{}) (CollectorRes
307289
return output, nil
308290
}
309291

292+
func (c *CollectLonghorn) collectLonghornLogs(namespace string, results CollectorResult, progressChan chan<- interface{}) error {
293+
// logs of all pods in namespace
294+
logsCollectorSpec := &troubleshootv1beta2.Logs{
295+
Selector: []string{""},
296+
Name: GetLonghornLogsDirectory(namespace), // Logs (symlinks) will be stored in this directory
297+
Namespace: namespace,
298+
}
299+
300+
rbacErrors := c.GetRBACErrors()
301+
logsCollector := &CollectLogs{logsCollectorSpec, c.BundlePath, namespace, c.ClientConfig, c.Client, c.Context, nil, rbacErrors}
302+
303+
logs, err := logsCollector.Collect(progressChan)
304+
if err != nil {
305+
return err
306+
}
307+
308+
// Add logs collector results to the rest of
309+
// the longhorn collector results for later consumption
310+
results.AddResult(logs)
311+
312+
return nil
313+
}
314+
310315
func GetLonghornNodesDirectory(namespace string) string {
311316
return fmt.Sprintf("longhorn/%s/nodes", namespace)
312317
}

pkg/collect/result.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func NewResult() CollectorResult {
2222
// is empty, no symlink is created. The relativeLinkPath is always saved in the result map.
2323
func (r CollectorResult) SymLinkResult(bundlePath, relativeLinkPath, relativeFilePath string) error {
2424
// We should have saved the result this symlink is pointing to prior to creating it
25-
klog.Info("Creating symlink ", relativeLinkPath, " -> ", relativeFilePath)
25+
klog.V(2).Info("Creating symlink ", relativeLinkPath, " -> ", relativeFilePath)
2626
data, ok := r[relativeFilePath]
2727
if !ok {
2828
return errors.Errorf("cannot create symlink, result in %q not found", relativeFilePath)
@@ -72,13 +72,23 @@ func (r CollectorResult) SymLinkResult(bundlePath, relativeLinkPath, relativeFil
7272
return errors.Wrap(err, "failed to create symlink")
7373
}
7474

75-
klog.V(2).Infof("Created '%s' symlink of '%s'", relativeLinkPath, relativeFilePath)
75+
klog.V(2).Infof("Created %q symlink of %q", relativeLinkPath, relativeFilePath)
7676
// store the file name referencing the symlink to have archived
7777
r[relativeLinkPath] = nil
7878

7979
return nil
8080
}
8181

82+
// AddResult combines another results object into this collector result.
83+
// This ensures when archiving a bundle from the result, all files are included.
84+
// It also ensures that when operating on the results in memory (e.g preflights),
85+
// all files are included.
86+
func (r CollectorResult) AddResult(other CollectorResult) {
87+
for k, v := range other {
88+
r[k] = v
89+
}
90+
}
91+
8292
// SaveResult saves the collector result to relativePath file on disk. If bundlePath is
8393
// empty, no file is created on disk. The relativePath is always saved in the result map.
8494
func (r CollectorResult) SaveResult(bundlePath string, relativePath string, reader io.Reader) error {

pkg/collect/result_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package collect
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCollectorResult_AddResult(t *testing.T) {
10+
r := CollectorResult{"a": []byte("a")}
11+
12+
other := CollectorResult{"b": []byte("b")}
13+
r.AddResult(other)
14+
15+
assert.Equal(t, 2, len(r))
16+
assert.Equal(t, []byte("a"), r["a"])
17+
assert.Equal(t, []byte("b"), r["b"])
18+
}

0 commit comments

Comments
 (0)