github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyze.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 preflight
    21  
    22  import (
    23  	"context"
    24  	"fmt"
    25  	"path/filepath"
    26  	"strings"
    27  
    28  	analyze "github.com/replicatedhq/troubleshoot/pkg/analyze"
    29  	troubleshoot "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
    30  	"github.com/replicatedhq/troubleshoot/pkg/preflight"
    31  	"helm.sh/helm/v3/pkg/cli/values"
    32  
    33  	preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2"
    34  	kbanalyzer "github.com/1aal/kubeblocks/pkg/cli/preflight/analyzer"
    35  )
    36  
    37  type KBClusterCollectResult struct {
    38  	preflight.ClusterCollectResult
    39  	HelmOptions     *values.Options
    40  	AnalyzerSpecs   []*troubleshoot.Analyze
    41  	KbAnalyzerSpecs []*preflightv1beta2.ExtendAnalyze
    42  }
    43  
    44  type KBHostCollectResult struct {
    45  	preflight.HostCollectResult
    46  	AnalyzerSpecs   []*troubleshoot.HostAnalyze
    47  	KbAnalyzerSpecs []*preflightv1beta2.ExtendHostAnalyze
    48  }
    49  
    50  func (c KBClusterCollectResult) Analyze() []*analyze.AnalyzeResult {
    51  	return doAnalyze(c.Context, c.AllCollectedData, c.AnalyzerSpecs, c.KbAnalyzerSpecs, nil, nil, c.HelmOptions)
    52  }
    53  
    54  func (c KBHostCollectResult) Analyze() []*analyze.AnalyzeResult {
    55  	return doAnalyze(c.Context, c.AllCollectedData, nil, nil, c.AnalyzerSpecs, c.KbAnalyzerSpecs, nil)
    56  }
    57  
    58  func doAnalyze(ctx context.Context, allCollectedData map[string][]byte,
    59  	analyzers []*troubleshoot.Analyze,
    60  	kbAnalyzers []*preflightv1beta2.ExtendAnalyze,
    61  	hostAnalyzers []*troubleshoot.HostAnalyze,
    62  	kbhHostAnalyzers []*preflightv1beta2.ExtendHostAnalyze,
    63  	options *values.Options,
    64  ) []*analyze.AnalyzeResult {
    65  	getCollectedFileContents := func(fileName string) ([]byte, error) {
    66  		contents, ok := allCollectedData[fileName]
    67  		if !ok {
    68  			return nil, fmt.Errorf("file %s was not collected", fileName)
    69  		}
    70  
    71  		return contents, nil
    72  	}
    73  	getChildCollectedFileContents := func(prefix string, excludeFiles []string) (map[string][]byte, error) {
    74  		matching := make(map[string][]byte)
    75  		for k, v := range allCollectedData {
    76  			if strings.HasPrefix(k, prefix) {
    77  				matching[k] = v
    78  			}
    79  		}
    80  		for k, v := range allCollectedData {
    81  			if ok, _ := filepath.Match(prefix, k); ok {
    82  				matching[k] = v
    83  			}
    84  		}
    85  		if len(excludeFiles) > 0 {
    86  			for k := range matching {
    87  				for _, ex := range excludeFiles {
    88  					if ok, _ := filepath.Match(ex, k); ok {
    89  						delete(matching, k)
    90  					}
    91  				}
    92  			}
    93  		}
    94  		if len(matching) == 0 {
    95  			return nil, fmt.Errorf("file not found: %s", prefix)
    96  		}
    97  		return matching, nil
    98  	}
    99  	var analyzeResults []*analyze.AnalyzeResult
   100  	for _, analyzer := range analyzers {
   101  		analyzeResult, _ := analyze.Analyze(ctx, analyzer, getCollectedFileContents, getChildCollectedFileContents)
   102  		if analyzeResult != nil {
   103  			analyzeResults = append(analyzeResults, analyzeResult...)
   104  		}
   105  	}
   106  	for _, kbAnalyzer := range kbAnalyzers {
   107  		analyzeResult := kbanalyzer.KBAnalyze(ctx, kbAnalyzer, getCollectedFileContents, getChildCollectedFileContents, options)
   108  		analyzeResults = append(analyzeResults, analyzeResult...)
   109  	}
   110  	for _, hostAnalyzer := range hostAnalyzers {
   111  		analyzeResult := analyze.HostAnalyze(ctx, hostAnalyzer, getCollectedFileContents, getChildCollectedFileContents)
   112  		analyzeResults = append(analyzeResults, analyzeResult...)
   113  	}
   114  	for _, kbHostAnalyzer := range kbhHostAnalyzers {
   115  		analyzeResult := kbanalyzer.HostKBAnalyze(ctx, kbHostAnalyzer, getCollectedFileContents, getChildCollectedFileContents)
   116  		analyzeResults = append(analyzeResults, analyzeResult...)
   117  	}
   118  	return analyzeResults
   119  }