github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/preflight/analyzer/host_region.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 "strings" 26 27 "github.com/pkg/errors" 28 analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze" 29 30 preflightv1beta2 "github.com/1aal/kubeblocks/externalapis/preflight/v1beta2" 31 kbcollector "github.com/1aal/kubeblocks/pkg/cli/preflight/collector" 32 "github.com/1aal/kubeblocks/pkg/cli/preflight/util" 33 ) 34 35 type AnalyzeClusterRegion struct { 36 analyzer *preflightv1beta2.ClusterRegionAnalyze 37 } 38 39 func (a *AnalyzeClusterRegion) Title() string { 40 return util.TitleOrDefault(a.analyzer.AnalyzeMeta, "Cluster Region") 41 } 42 43 func (a *AnalyzeClusterRegion) IsExcluded() (bool, error) { 44 return util.IsExcluded(a.analyzer.Exclude) 45 } 46 47 func (a *AnalyzeClusterRegion) Analyze(getFile func(string) ([]byte, error)) ([]*analyzer.AnalyzeResult, error) { 48 collected, err := getFile(kbcollector.ClusterRegionPath) 49 if err != nil { 50 return nil, errors.Wrap(err, "failed to get contents of region_name.json") 51 } 52 regionInfo := kbcollector.ClusterRegionInfo{} 53 if err := json.Unmarshal(collected, ®ionInfo); err != nil { 54 return nil, errors.Wrap(err, "failed to unmarshal region info") 55 } 56 57 isMatched := false 58 regionName := regionInfo.RegionName 59 for _, expectedRegionName := range a.analyzer.RegionNames { 60 if strings.EqualFold(regionName, expectedRegionName) { 61 isMatched = true 62 break 63 } 64 } 65 66 strRegionInfo := fmt.Sprintf(" target cluster region is %s.", regionName) 67 result := &analyzer.AnalyzeResult{ 68 Title: a.Title(), 69 } 70 for _, outcome := range a.analyzer.Outcomes { 71 switch { 72 case isMatched && outcome.Pass != nil: 73 result.IsPass = true 74 result.Message = outcome.Pass.Message + strRegionInfo 75 result.URI = outcome.Pass.URI 76 case !isMatched && outcome.Warn != nil: 77 result.IsWarn = true 78 result.Message = outcome.Warn.Message + strRegionInfo 79 result.URI = outcome.Warn.URI 80 case !isMatched && outcome.Fail != nil: 81 // just return warning info even if outcome.Fail is set 82 result.IsWarn = true 83 result.Message = outcome.Fail.Message + strRegionInfo 84 result.URI = outcome.Fail.URI 85 default: 86 } 87 } 88 result.Strict = a.analyzer.Strict.BoolOrDefaultFalse() 89 return []*analyzer.AnalyzeResult{result}, nil 90 }