Repository navigation
Expand file tree
/
Copy pathhost_region.go
More file actions
91 lines (76 loc) · 2.82 KB
/
Copy pathhost_region.go
File metadata and controls
91 lines (76 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package analyzer
import (
"encoding/json"
"fmt"
"strings"
"github.com/pkg/errors"
analyzer "github.com/replicatedhq/troubleshoot/pkg/analyze"
preflightv1beta2 "github.com/apecloud/kubeblocks/externalapis/preflight/v1beta2"
kbcollector "github.com/apecloud/kbcli/pkg/preflight/collector"
"github.com/apecloud/kbcli/pkg/preflight/util"
)
type AnalyzeClusterRegion struct {
analyzer *preflightv1beta2.ClusterRegionAnalyze
}
func (a *AnalyzeClusterRegion) Title() string {
return util.TitleOrDefault(a.analyzer.AnalyzeMeta, "Cluster Region")
}
func (a *AnalyzeClusterRegion) IsExcluded() (bool, error) {
return util.IsExcluded(a.analyzer.Exclude)
}
func (a *AnalyzeClusterRegion) Analyze(getFile func(string) ([]byte, error)) ([]*analyzer.AnalyzeResult, error) {
collected, err := getFile(kbcollector.ClusterRegionPath)
if err != nil {
return nil, errors.Wrap(err, "failed to get contents of region_name.json")
}
regionInfo := kbcollector.ClusterRegionInfo{}
if err := json.Unmarshal(collected, ®ionInfo); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal region info")
}
isMatched := false
regionName := regionInfo.RegionName
for _, expectedRegionName := range a.analyzer.RegionNames {
if strings.EqualFold(regionName, expectedRegionName) {
isMatched = true
break
}
}
strRegionInfo := fmt.Sprintf(" target cluster region is %s.", regionName)
result := &analyzer.AnalyzeResult{
Title: a.Title(),
}
for _, outcome := range a.analyzer.Outcomes {
switch {
case isMatched && outcome.Pass != nil:
result.IsPass = true
result.Message = outcome.Pass.Message + strRegionInfo
result.URI = outcome.Pass.URI
case !isMatched && outcome.Warn != nil:
result.IsWarn = true
result.Message = outcome.Warn.Message + strRegionInfo
result.URI = outcome.Warn.URI
case !isMatched && outcome.Fail != nil:
// just return warning info even if outcome.Fail is set
result.IsWarn = true
result.Message = outcome.Fail.Message + strRegionInfo
result.URI = outcome.Fail.URI
default:
}
}
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
return []*analyzer.AnalyzeResult{result}, nil
}