Skip to content

Commit e156b8c

Browse files
authored
chore: Refactor in cluster analysers (#999)
Have all in-cluster analysers implement the same interface. This will help with the implementation of code that requires making calls to all analysers Fixes #995
1 parent 1114902 commit e156b8c

39 files changed

+962
-585
lines changed

pkg/analyze/analyzer.go

Lines changed: 86 additions & 422 deletions
Large diffs are not rendered by default.

pkg/analyze/analyzer_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ func Test_GetExcludeFlag(t *testing.T) {
6565
})
6666
}
6767
}
68+
69+
func TestAnalyzeWithNilAnalyzer(t *testing.T) {
70+
got, err := Analyze(nil, nil, nil)
71+
assert.Error(t, err)
72+
assert.Nil(t, got)
73+
}

pkg/analyze/ceph.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,35 @@ type PgMap struct {
100100
TotalBytes uint64 `json:"bytes_total"`
101101
}
102102

103-
func cephStatus(analyzer *troubleshootv1beta2.CephStatusAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
103+
type AnalyzeCephStatus struct {
104+
analyzer *troubleshootv1beta2.CephStatusAnalyze
105+
}
106+
107+
func (a *AnalyzeCephStatus) Title() string {
108+
title := a.analyzer.CheckName
109+
if title == "" {
110+
title = "Ceph Status"
111+
}
112+
113+
return title
114+
}
115+
116+
func (a *AnalyzeCephStatus) IsExcluded() (bool, error) {
117+
return isExcluded(a.analyzer.Exclude)
118+
}
119+
120+
func (a *AnalyzeCephStatus) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
121+
result, err := a.cephStatus(a.analyzer, getFile)
122+
if err != nil {
123+
return nil, err
124+
}
125+
if result != nil {
126+
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
127+
}
128+
return []*AnalyzeResult{result}, nil
129+
}
130+
131+
func (a *AnalyzeCephStatus) cephStatus(analyzer *troubleshootv1beta2.CephStatusAnalyze, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
104132
fileName := path.Join(collect.GetCephCollectorFilepath(analyzer.CollectorName, analyzer.Namespace), "status.json")
105133
collected, err := getCollectedFileContents(fileName)
106134

@@ -111,13 +139,8 @@ func cephStatus(analyzer *troubleshootv1beta2.CephStatusAnalyze, getCollectedFil
111139
return nil, errors.Wrap(err, "failed to read collected ceph status")
112140
}
113141

114-
title := analyzer.CheckName
115-
if title == "" {
116-
title = "Ceph Status"
117-
}
118-
119142
analyzeResult := &AnalyzeResult{
120-
Title: title,
143+
Title: a.Title(),
121144
IconKey: "rook", // maybe this should be ceph?
122145
IconURI: "https://troubleshoot.sh/images/analyzer-icons/rook.svg?w=11&h=16",
123146
}

pkg/analyze/ceph_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,11 @@ func Test_cephStatus(t *testing.T) {
273273
}
274274
}
275275

276-
actual, err := cephStatus(&test.analyzer, test.getFile)
276+
a := AnalyzeCephStatus{
277+
analyzer: &test.analyzer,
278+
}
279+
280+
actual, err := a.cephStatus(&test.analyzer, test.getFile)
277281
req.NoError(err)
278282

279283
assert.Equal(t, test.expectResult, actual)

pkg/analyze/cluster_pod_statuses.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ import (
1515
corev1 "k8s.io/api/core/v1"
1616
)
1717

18+
type AnalyzeClusterPodStatuses struct {
19+
analyzer *troubleshootv1beta2.ClusterPodStatuses
20+
}
21+
22+
func (a *AnalyzeClusterPodStatuses) Title() string {
23+
if a.analyzer.CheckName != "" {
24+
return a.analyzer.CheckName
25+
}
26+
27+
return "Cluster Pod Status"
28+
}
29+
30+
func (a *AnalyzeClusterPodStatuses) IsExcluded() (bool, error) {
31+
return isExcluded(a.analyzer.Exclude)
32+
}
33+
34+
func (a *AnalyzeClusterPodStatuses) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
35+
results, err := clusterPodStatuses(a.analyzer, findFiles)
36+
if err != nil {
37+
return nil, err
38+
}
39+
for i := range results {
40+
results[i].Strict = a.analyzer.Strict.BoolOrDefaultFalse()
41+
}
42+
return results, nil
43+
}
44+
1845
func clusterPodStatuses(analyzer *troubleshootv1beta2.ClusterPodStatuses, getChildCollectedFileContents getChildCollectedFileContents) ([]*AnalyzeResult, error) {
1946
excludeFiles := []string{}
2047
collected, err := getChildCollectedFileContents(filepath.Join(constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_PODS, "*.json"), excludeFiles)

pkg/analyze/cluster_version.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ import (
1010
"github.com/replicatedhq/troubleshoot/pkg/collect"
1111
)
1212

13+
type AnalyzeClusterVersion struct {
14+
analyzer *troubleshootv1beta2.ClusterVersion
15+
}
16+
17+
func (a *AnalyzeClusterVersion) Title() string {
18+
return title(a.analyzer.CheckName)
19+
}
20+
21+
func (a *AnalyzeClusterVersion) IsExcluded() (bool, error) {
22+
return isExcluded(a.analyzer.Exclude)
23+
}
24+
25+
func (a *AnalyzeClusterVersion) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
26+
result, err := analyzeClusterVersion(a.analyzer, getFile)
27+
if err != nil {
28+
return nil, err
29+
}
30+
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
31+
return []*AnalyzeResult{result}, nil
32+
}
33+
1334
func analyzeClusterVersion(analyzer *troubleshootv1beta2.ClusterVersion, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
1435
clusterInfo, err := getCollectedFileContents("cluster-info/cluster_version.json")
1536
if err != nil {
@@ -29,19 +50,22 @@ func analyzeClusterVersion(analyzer *troubleshootv1beta2.ClusterVersion, getColl
2950
return analyzeClusterVersionResult(k8sVersion, analyzer.Outcomes, analyzer.CheckName)
3051
}
3152

53+
func title(checkName string) string {
54+
if checkName == "" {
55+
return "Required Kubernetes Version"
56+
}
57+
58+
return checkName
59+
}
60+
3261
func analyzeClusterVersionResult(k8sVersion semver.Version, outcomes []*troubleshootv1beta2.Outcome, checkName string) (*AnalyzeResult, error) {
3362
for _, outcome := range outcomes {
3463
when := ""
3564
message := ""
3665
uri := ""
3766

38-
title := checkName
39-
if title == "" {
40-
title = "Required Kubernetes Version"
41-
}
42-
4367
result := AnalyzeResult{
44-
Title: title,
68+
Title: title(checkName),
4569
IconKey: "kubernetes_cluster_version",
4670
IconURI: "https://troubleshoot.sh/images/analyzer-icons/kubernetes.svg?w=16&h=16",
4771
}

pkg/analyze/configmap.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,33 @@ import (
88
"github.com/replicatedhq/troubleshoot/pkg/collect"
99
)
1010

11-
func analyzeConfigMap(analyzer *troubleshootv1beta2.AnalyzeConfigMap, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
11+
type AnalyzeConfigMap struct {
12+
analyzer *troubleshootv1beta2.AnalyzeConfigMap
13+
}
14+
15+
func (a *AnalyzeConfigMap) Title() string {
16+
title := a.analyzer.CheckName
17+
if title == "" {
18+
title = fmt.Sprintf("ConfigMap %s", a.analyzer.ConfigMapName)
19+
}
20+
21+
return title
22+
}
23+
24+
func (a *AnalyzeConfigMap) IsExcluded() (bool, error) {
25+
return isExcluded(a.analyzer.Exclude)
26+
}
27+
28+
func (a *AnalyzeConfigMap) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
29+
result, err := a.analyzeConfigMap(a.analyzer, getFile)
30+
if err != nil {
31+
return nil, err
32+
}
33+
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
34+
return []*AnalyzeResult{result}, nil
35+
}
36+
37+
func (a *AnalyzeConfigMap) analyzeConfigMap(analyzer *troubleshootv1beta2.AnalyzeConfigMap, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
1238
filename := collect.GetConfigMapFileName(
1339
&troubleshootv1beta2.ConfigMap{
1440
Namespace: analyzer.Namespace,
@@ -28,13 +54,8 @@ func analyzeConfigMap(analyzer *troubleshootv1beta2.AnalyzeConfigMap, getCollect
2854
return nil, err
2955
}
3056

31-
title := analyzer.CheckName
32-
if title == "" {
33-
title = fmt.Sprintf("ConfigMap %s", analyzer.ConfigMapName)
34-
}
35-
3657
result := AnalyzeResult{
37-
Title: title,
58+
Title: a.Title(),
3859
IconKey: "kubernetes_analyze_secret", // TODO: icon
3960
IconURI: "https://troubleshoot.sh/images/analyzer-icons/secret.svg?w=13&h=16",
4061
}

pkg/analyze/configmap_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,12 @@ func Test_analyzeConfigMap(t *testing.T) {
199199
}
200200
return contents, nil
201201
}
202-
got, err := analyzeConfigMap(tt.analyzer, getCollectedFileContents)
202+
203+
a := AnalyzeConfigMap{
204+
analyzer: tt.analyzer,
205+
}
206+
207+
got, err := a.analyzeConfigMap(tt.analyzer, getCollectedFileContents)
203208
if tt.wantErr {
204209
assert.Error(t, err)
205210
} else {

pkg/analyze/container_runtime.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,33 @@ import (
1212
corev1 "k8s.io/api/core/v1"
1313
)
1414

15-
func analyzeContainerRuntime(analyzer *troubleshootv1beta2.ContainerRuntime, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
15+
type AnalyzeContainerRuntime struct {
16+
analyzer *troubleshootv1beta2.ContainerRuntime
17+
}
18+
19+
func (a *AnalyzeContainerRuntime) Title() string {
20+
title := a.analyzer.CheckName
21+
if title == "" {
22+
title = "Container Runtime"
23+
}
24+
25+
return title
26+
}
27+
28+
func (a *AnalyzeContainerRuntime) IsExcluded() (bool, error) {
29+
return isExcluded(a.analyzer.Exclude)
30+
}
31+
32+
func (a *AnalyzeContainerRuntime) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
33+
result, err := a.analyzeContainerRuntime(a.analyzer, getFile)
34+
if err != nil {
35+
return nil, err
36+
}
37+
result.Strict = a.analyzer.Strict.BoolOrDefaultFalse()
38+
return []*AnalyzeResult{result}, nil
39+
}
40+
41+
func (a *AnalyzeContainerRuntime) analyzeContainerRuntime(analyzer *troubleshootv1beta2.ContainerRuntime, getCollectedFileContents func(string) ([]byte, error)) (*AnalyzeResult, error) {
1642
collected, err := getCollectedFileContents(fmt.Sprintf("%s/%s.json", constants.CLUSTER_RESOURCES_DIR, constants.CLUSTER_RESOURCES_NODES))
1743
if err != nil {
1844
return nil, errors.Wrap(err, "failed to get contents of nodes.json")
@@ -28,12 +54,8 @@ func analyzeContainerRuntime(analyzer *troubleshootv1beta2.ContainerRuntime, get
2854
foundRuntimes = append(foundRuntimes, node.Status.NodeInfo.ContainerRuntimeVersion)
2955
}
3056

31-
title := analyzer.CheckName
32-
if title == "" {
33-
title = "Container Runtime"
34-
}
3557
result := &AnalyzeResult{
36-
Title: title,
58+
Title: a.Title(),
3759
IconKey: "kubernetes_container_runtime",
3860
IconURI: "https://troubleshoot.sh/images/analyzer-icons/container-runtime.svg?w=23&h=16",
3961
}

pkg/analyze/container_runtime_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ func Test_containerRuntime(t *testing.T) {
113113
return test.files[n], nil
114114
}
115115

116-
actual, err := analyzeContainerRuntime(&test.analyzer, getFiles)
116+
a := AnalyzeContainerRuntime{
117+
analyzer: &test.analyzer,
118+
}
119+
120+
actual, err := a.analyzeContainerRuntime(&test.analyzer, getFiles)
117121
req.NoError(err)
118122

119123
assert.Equal(t, &test.expectResult, actual)

0 commit comments

Comments
 (0)