github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/host_utility.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 "encoding/json" 24 "fmt" 25 26 "github.com/pkg/errors" 27 analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze" 28 29 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 30 kbcollector "github.com/1aal/kubeblocks/pkg/cli/preflight/collector" 31 "github.com/1aal/kubeblocks/pkg/cli/preflight/util" 32 ) 33 34 type AnalyzeHostUtility struct { 35 hostAnalyzer *preflightv1beta2.HostUtilityAnalyze 36 } 37 38 func (a *AnalyzeHostUtility) Title() string { 39 return util.TitleOrDefault(a.hostAnalyzer.AnalyzeMeta, "Host "+a.hostAnalyzer.CollectorName+" Utility Info") 40 } 41 42 func (a *AnalyzeHostUtility) IsExcluded() (bool, error) { 43 return util.IsExcluded(a.hostAnalyzer.Exclude) 44 } 45 46 func (a *AnalyzeHostUtility) utilityPath() string { 47 if a.hostAnalyzer.CollectorName != "" { 48 return a.hostAnalyzer.CollectorName 49 } 50 return kbcollector.DefaultHostUtilityPath 51 } 52 53 func (a *AnalyzeHostUtility) Analyze(getCollectedFileContents func(string) ([]byte, error)) ([]*analyzer.AnalyzeResult, error) { 54 fullPath := fmt.Sprintf(kbcollector.UtilityPathFormat, a.utilityPath()) 55 collected, err := getCollectedFileContents(fullPath) 56 if err != nil { 57 return nil, errors.Wrapf(err, "failed to get collected file name: %s", fullPath) 58 } 59 60 utilityInfo := kbcollector.HostUtilityInfo{} 61 if err := json.Unmarshal(collected, &utilityInfo); err != nil { 62 return nil, errors.Wrap(err, "failed to unmarshal utility info") 63 } 64 // generate analyzer result 65 result := analyzer.AnalyzeResult{ 66 Title: a.Title(), 67 } 68 for _, outcome := range a.hostAnalyzer.Outcomes { 69 switch { 70 case outcome.Pass != nil && len(utilityInfo.Error) == 0 && len(utilityInfo.Path) > 0: 71 result.IsPass = true 72 result.Message = outcome.Pass.Message + fmt.Sprintf(". Utility %s Path is %s", utilityInfo.Name, utilityInfo.Path) 73 result.URI = outcome.Pass.URI 74 case outcome.Warn != nil && len(utilityInfo.Error) > 0 && len(utilityInfo.Path) == 0: 75 result.IsWarn = true 76 result.Message = outcome.Warn.Message 77 result.URI = outcome.Warn.URI 78 case outcome.Fail != nil && len(utilityInfo.Error) > 0 && len(utilityInfo.Path) == 0: 79 // return warning info even if outcome.Fail is set 80 result.IsWarn = true 81 result.Message = outcome.Fail.Message 82 result.URI = outcome.Fail.URI 83 default: 84 } 85 } 86 return []*analyzer.AnalyzeResult{&result}, nil 87 }