feat(support-bundle): optimize the error log of ceph and longhorn when kURL add-on were not enabled (#943)

This commit is contained in:
Dexter Yan
2023-01-10 09:37:42 +13:00
committed by GitHub
parent f1066f5d76
commit 962e2c7d7e
8 changed files with 76 additions and 25 deletions

View File

@@ -35,7 +35,7 @@ from a server that can be used to assist when troubleshooting a Kubernetes clust
if !v.GetBool("debug") {
klog.SetLogger(logr.Discard())
}
logger.SetQuiet(v.GetBool("quiet"))
logger.SetQuiet(!v.GetBool("debug"))
},
RunE: func(cmd *cobra.Command, args []string) error {
v := viper.GetViper()

View File

@@ -441,7 +441,9 @@ func Analyze(analyzer *troubleshootv1beta2.Analyze, getFile getCollectedFileCont
if err != nil {
return nil, err
}
result.Strict = analyzer.CephStatus.Strict.BoolOrDefaultFalse()
if result != nil {
result.Strict = analyzer.CephStatus.Strict.BoolOrDefaultFalse()
}
return []*AnalyzeResult{result}, nil
}
if analyzer.Longhorn != nil {

View File

@@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/collect"
"github.com/replicatedhq/troubleshoot/pkg/types"
)
type CephHealth string
@@ -102,7 +103,11 @@ type PgMap struct {
func cephStatus(analyzer *troubleshootv1beta2.CephStatusAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
fileName := path.Join(collect.GetCephCollectorFilepath(analyzer.CollectorName, analyzer.Namespace), "status.json")
collected, err := getCollectedFileContents(fileName)
if err != nil {
if _, ok := err.(*types.NotFoundError); ok {
return nil, nil
}
return nil, errors.Wrap(err, "failed to read collected ceph status")
}

View File

@@ -5,21 +5,25 @@ import (
"testing"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type getFile func(n string) ([]byte, error)
func Test_cephStatus(t *testing.T) {
tests := []struct {
name string
analyzer troubleshootv1beta2.CephStatusAnalyze
expectResult AnalyzeResult
expectResult *AnalyzeResult
getFile getFile
filePath, file string
}{
{
name: "pass case",
analyzer: troubleshootv1beta2.CephStatusAnalyze{},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: true,
IsWarn: false,
IsFail: false,
@@ -51,7 +55,7 @@ func Test_cephStatus(t *testing.T) {
{
name: "warn case",
analyzer: troubleshootv1beta2.CephStatusAnalyze{},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: false,
IsWarn: true,
IsFail: false,
@@ -84,7 +88,7 @@ func Test_cephStatus(t *testing.T) {
{
name: "fail case",
analyzer: troubleshootv1beta2.CephStatusAnalyze{},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: false,
IsWarn: false,
IsFail: true,
@@ -120,7 +124,7 @@ func Test_cephStatus(t *testing.T) {
CollectorName: "custom-namespace",
Namespace: "namespace",
},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: true,
IsWarn: false,
IsFail: false,
@@ -167,7 +171,7 @@ func Test_cephStatus(t *testing.T) {
},
},
},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: false,
IsWarn: false,
IsFail: true,
@@ -200,7 +204,7 @@ func Test_cephStatus(t *testing.T) {
{
name: "warn case with missing osd/pg data",
analyzer: troubleshootv1beta2.CephStatusAnalyze{},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: false,
IsWarn: true,
IsFail: false,
@@ -221,7 +225,7 @@ func Test_cephStatus(t *testing.T) {
{
name: "warn case with health status message and summary",
analyzer: troubleshootv1beta2.CephStatusAnalyze{},
expectResult: AnalyzeResult{
expectResult: &AnalyzeResult{
IsPass: false,
IsWarn: true,
IsFail: false,
@@ -249,21 +253,30 @@ func Test_cephStatus(t *testing.T) {
}
}`,
},
{
name: "pass case when get file returns not found error",
analyzer: troubleshootv1beta2.CephStatusAnalyze{},
expectResult: nil,
getFile: func(n string) ([]byte, error) {
return nil, &types.NotFoundError{}
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req := require.New(t)
getFile := func(n string) ([]byte, error) {
assert.Equal(t, n, test.filePath)
return []byte(test.file), nil
if test.getFile == nil {
test.getFile = func(n string) ([]byte, error) {
assert.Equal(t, n, test.filePath)
return []byte(test.file), nil
}
}
actual, err := cephStatus(&test.analyzer, getFile)
actual, err := cephStatus(&test.analyzer, test.getFile)
req.NoError(err)
assert.Equal(t, test.expectResult, *actual)
assert.Equal(t, test.expectResult, actual)
})
}
}

View File

@@ -4,6 +4,7 @@ import (
"archive/tar"
"compress/gzip"
"io"
"io/fs"
"os"
"path/filepath"
@@ -14,6 +15,7 @@ import (
"github.com/replicatedhq/troubleshoot/pkg/constants"
"github.com/replicatedhq/troubleshoot/pkg/docrewrite"
"github.com/replicatedhq/troubleshoot/pkg/logger"
"github.com/replicatedhq/troubleshoot/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
)
@@ -295,7 +297,14 @@ func FindBundleRootDir(localBundlePath string) (string, error) {
}
func (f fileContentProvider) getFileContents(fileName string) ([]byte, error) {
return os.ReadFile(filepath.Join(f.rootDir, fileName))
contents, err := os.ReadFile(filepath.Join(f.rootDir, fileName))
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
return nil, &types.NotFoundError{Name: fileName}
}
return nil, err
}
return contents, nil
}
func excludeFilePaths(files, excludeFiles []string) []string {

View File

@@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
"github.com/replicatedhq/troubleshoot/pkg/logger"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
@@ -137,15 +138,16 @@ func (c *CollectCeph) Collect(progressChan chan<- interface{}) (CollectorResult,
}
output := NewResult()
for _, command := range CephCommands {
err := cephCommandExec(ctx, progressChan, c, c.Collector, pod, command, output)
if err != nil {
pathPrefix := GetCephCollectorFilepath(c.Collector.CollectorName, c.Namespace)
dstFileName := path.Join(pathPrefix, fmt.Sprintf("%s.%s-error", command.ID, command.Format))
output.SaveResult(c.BundlePath, dstFileName, strings.NewReader(err.Error()))
if pod != nil {
for _, command := range CephCommands {
err := cephCommandExec(ctx, progressChan, c, c.Collector, pod, command, output)
if err != nil {
pathPrefix := GetCephCollectorFilepath(c.Collector.CollectorName, c.Namespace)
dstFileName := path.Join(pathPrefix, fmt.Sprintf("%s.%s-error", command.ID, command.Format))
output.SaveResult(c.BundlePath, dstFileName, strings.NewReader(err.Error()))
}
}
}
return output, nil
}
@@ -196,6 +198,7 @@ func cephCommandExec(ctx context.Context, progressChan chan<- interface{}, c *Co
func findRookCephToolsPod(ctx context.Context, c *CollectCeph, namespace string) (*corev1.Pod, error) {
client, err := kubernetes.NewForConfig(c.ClientConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to create kubernetes client")
}
@@ -210,7 +213,9 @@ func findRookCephToolsPod(ctx context.Context, c *CollectCeph, namespace string)
return &pods[0], nil
}
return nil, errors.New("rook ceph tools pod not found")
logger.Printf("rook ceph tools pod not found")
return nil, nil
}
func GetCephCollectorFilepath(name, namespace string) string {

View File

@@ -5,6 +5,7 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"path/filepath"
"regexp"
"sync"
@@ -16,6 +17,7 @@ import (
longhornv1beta1 "github.com/replicatedhq/troubleshoot/pkg/longhorn/client/clientset/versioned/typed/longhorn/v1beta1"
longhorntypes "github.com/replicatedhq/troubleshoot/pkg/longhorn/types"
"gopkg.in/yaml.v2"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
@@ -65,6 +67,12 @@ func (c *CollectLonghorn) Collect(progressChan chan<- interface{}) (CollectorRes
// collect nodes.longhorn.io
nodes, err := client.Nodes(ns).List(ctx, metav1.ListOptions{})
if err != nil {
if apiErr, ok := err.(*apiErrors.StatusError); ok {
if apiErr.ErrStatus.Code == http.StatusNotFound {
logger.Printf("list nodes.longhorn.io not found")
return nil, nil
}
}
return nil, errors.Wrap(err, "list nodes.longhorn.io")
}
dir := GetLonghornNodesDirectory(ns)

9
pkg/types/types.go Normal file
View File

@@ -0,0 +1,9 @@
package types
type NotFoundError struct {
Name string
}
func (e *NotFoundError) Error() string {
return e.Name + ": not found"
}