github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/analyzer.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package analyzer
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  
    26  	"github.com/pkg/errors"
    27  	analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
    28  	"helm.sh/helm/v3/pkg/cli/values"
    29  
    30  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    31  )
    32  
    33  type KBAnalyzer interface {
    34  	Title() string
    35  	IsExcluded() (bool, error)
    36  	Analyze(getFile GetCollectedFileContents, findFiles GetChildCollectedFileContents) ([]*analyze.AnalyzeResult, error)
    37  }
    38  
    39  type GetCollectedFileContents func(string) ([]byte, error)
    40  type GetChildCollectedFileContents func(string, []string) (map[string][]byte, error)
    41  
    42  func GetAnalyzer(analyzer *preflightv1beta2.ExtendAnalyze, options *values.Options) (KBAnalyzer, bool) {
    43  	switch {
    44  	case analyzer.ClusterAccess != nil:
    45  		return &AnalyzeClusterAccess{analyzer: analyzer.ClusterAccess}, true
    46  	case analyzer.StorageClass != nil:
    47  		return &AnalyzeStorageClassByKb{analyzer: analyzer.StorageClass}, true
    48  	case analyzer.Taint != nil:
    49  		return &AnalyzeTaintClassByKb{analyzer: analyzer.Taint, HelmOpts: options}, true
    50  	default:
    51  		return nil, false
    52  	}
    53  }
    54  
    55  func KBAnalyze(ctx context.Context, kbAnalyzer *preflightv1beta2.ExtendAnalyze, getFile func(string) ([]byte, error), findFiles func(string, []string) (map[string][]byte, error), options *values.Options) []*analyze.AnalyzeResult {
    56  	analyzer, ok := GetAnalyzer(kbAnalyzer, options)
    57  	if !ok {
    58  		return NewAnalyzeResultError(analyzer, errors.New("invalid analyzer"))
    59  	}
    60  	isExcluded, _ := analyzer.IsExcluded()
    61  	if isExcluded {
    62  		// logger.Printf("Excluding %q analyzer", analyzer.Title())
    63  		return nil
    64  	}
    65  	results, err := analyzer.Analyze(getFile, findFiles)
    66  	if err != nil {
    67  		return NewAnalyzeResultError(analyzer, errors.Wrap(err, "analyze"))
    68  	}
    69  	return results
    70  }
    71  
    72  func HostKBAnalyze(ctx context.Context, kbHostAnalyzer *preflightv1beta2.ExtendHostAnalyze, getFile func(string) ([]byte, error), findFiles func(string, []string) (map[string][]byte, error)) []*analyze.AnalyzeResult {
    73  	hostAnalyzer, ok := GetHostAnalyzer(kbHostAnalyzer)
    74  	if !ok {
    75  		return analyze.NewAnalyzeResultError(hostAnalyzer, errors.New("invalid host analyzer"))
    76  	}
    77  	isExcluded, _ := hostAnalyzer.IsExcluded()
    78  	if isExcluded {
    79  		// logger.Printf("Excluding %q analyzer", hostAnalyzer.Title())
    80  		return nil
    81  	}
    82  	results, err := hostAnalyzer.Analyze(getFile)
    83  	if err != nil {
    84  		return analyze.NewAnalyzeResultError(hostAnalyzer, errors.Wrap(err, "analyze"))
    85  	}
    86  	return results
    87  }
    88  
    89  func NewAnalyzeResultError(analyzer KBAnalyzer, err error) []*analyze.AnalyzeResult {
    90  	if analyzer != nil {
    91  		return []*analyze.AnalyzeResult{{
    92  			IsFail:  true,
    93  			Title:   analyzer.Title(),
    94  			Message: fmt.Sprintf("Analyzer Failed: %v", err),
    95  		}}
    96  	}
    97  	return []*analyze.AnalyzeResult{{
    98  		IsFail:  true,
    99  		Title:   "nil analyzer",
   100  		Message: fmt.Sprintf("Analyzer Failed: %v", err),
   101  	}}
   102  }