Skip to content

Commit 137b0ce

Browse files
authored
Merge pull request #515 from replicatedhq/divolgin/namesapces
Allow specifying namespaces when analyzing cluster resources
2 parents 46cfa14 + 007edd1 commit 137b0ce

20 files changed

+6025
-103
lines changed

pkg/analyze/data_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,32 @@ import (
44
_ "embed"
55
)
66

7-
//go:embed files/deployments.json
8-
var collectedDeployments string
7+
//go:embed files/deployments/default.json
8+
var defaultDeployments string
9+
10+
//go:embed files/deployments/monitoring.json
11+
var monitoringDeployments string
12+
13+
//go:embed files/deployments/kube-system.json
14+
var kubeSystemDeployments string
915

1016
//go:embed files/nodes.json
1117
var collectedNodes string
1218

13-
//go:embed files/jobs.json
14-
var collectedJobs string
19+
//go:embed files/jobs/test.json
20+
var testJobs string
21+
22+
//go:embed files/jobs/projectcontour.json
23+
var projectcontourJobs string
24+
25+
//go:embed files/replicasets/default.json
26+
var defaultReplicaSets string
27+
28+
//go:embed files/replicasets/rook-ceph.json
29+
var rookCephReplicaSets string
30+
31+
//go:embed files/statefulsets/default.json
32+
var defaultStatefulSets string
1533

16-
//go:embed files/replicasets.json
17-
var collectedReplicaSets string
34+
//go:embed files/statefulsets/monitoring.json
35+
var monitoringStatefulSets string

pkg/analyze/deployment_status.go

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,47 @@ func analyzeOneDeploymentStatus(analyzer *troubleshootv1beta2.DeploymentStatus,
5959
}
6060

6161
func analyzeAllDeploymentStatuses(analyzer *troubleshootv1beta2.DeploymentStatus, getFileContents func(string) (map[string][]byte, error)) ([]*AnalyzeResult, error) {
62-
var fileName string
62+
fileNames := make([]string, 0)
6363
if analyzer.Namespace != "" {
64-
fileName = filepath.Join("cluster-resources", "deployments", fmt.Sprintf("%s.json", analyzer.Namespace))
65-
} else {
66-
fileName = filepath.Join("cluster-resources", "deployments", "*.json")
64+
fileNames = append(fileNames, filepath.Join("cluster-resources", "deployments", fmt.Sprintf("%s.json", analyzer.Namespace)))
65+
}
66+
for _, ns := range analyzer.Namespaces {
67+
fileNames = append(fileNames, filepath.Join("cluster-resources", "deployments", fmt.Sprintf("%s.json", ns)))
6768
}
6869

69-
files, err := getFileContents(fileName)
70-
if err != nil {
71-
return nil, errors.Wrap(err, "failed to read collected deployments from file")
70+
// no namespace specified, so we need to analyze all deployments
71+
if len(fileNames) == 0 {
72+
fileNames = append(fileNames, filepath.Join("cluster-resources", "deployments", "*.json"))
7273
}
7374

7475
results := []*AnalyzeResult{}
75-
for _, collected := range files {
76-
var deployments []appsv1.Deployment
77-
if err := json.Unmarshal(collected, &deployments); err != nil {
78-
return nil, errors.Wrap(err, "failed to unmarshal deployment list")
76+
for _, fileName := range fileNames {
77+
files, err := getFileContents(fileName)
78+
if err != nil {
79+
return nil, errors.Wrap(err, "failed to read collected deployments from file")
7980
}
8081

81-
for _, deployment := range deployments {
82-
if deployment.Status.Replicas == deployment.Status.AvailableReplicas {
83-
continue
82+
for _, collected := range files {
83+
var deployments []appsv1.Deployment
84+
if err := json.Unmarshal(collected, &deployments); err != nil {
85+
return nil, errors.Wrap(err, "failed to unmarshal deployment list")
8486
}
8587

86-
result := &AnalyzeResult{
87-
Title: fmt.Sprintf("%s/%s Deployment Status", deployment.Namespace, deployment.Name),
88-
IconKey: "kubernetes_deployment_status",
89-
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
90-
IsFail: true,
91-
Message: fmt.Sprintf("The deployment %s/%s has %d/%d replicas", deployment.Namespace, deployment.Name, deployment.Status.ReadyReplicas, deployment.Status.Replicas),
92-
}
88+
for _, deployment := range deployments {
89+
if deployment.Status.Replicas == deployment.Status.AvailableReplicas {
90+
continue
91+
}
92+
93+
result := &AnalyzeResult{
94+
Title: fmt.Sprintf("%s/%s Deployment Status", deployment.Namespace, deployment.Name),
95+
IconKey: "kubernetes_deployment_status",
96+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
97+
IsFail: true,
98+
Message: fmt.Sprintf("The deployment %s/%s has %d/%d replicas", deployment.Namespace, deployment.Name, deployment.Status.ReadyReplicas, deployment.Status.Replicas),
99+
}
93100

94-
results = append(results, result)
101+
results = append(results, result)
102+
}
95103
}
96104
}
97105

pkg/analyze/deployment_status_test.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ func Test_deploymentStatus(t *testing.T) {
4646
},
4747
},
4848
files: map[string][]byte{
49-
"cluster-resources/deployments/default.json": []byte(collectedDeployments),
49+
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
50+
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
51+
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
5052
},
5153
},
5254
{
@@ -80,7 +82,9 @@ func Test_deploymentStatus(t *testing.T) {
8082
},
8183
},
8284
files: map[string][]byte{
83-
"cluster-resources/deployments/default.json": []byte(collectedDeployments),
85+
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
86+
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
87+
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
8488
},
8589
},
8690
{
@@ -120,7 +124,40 @@ func Test_deploymentStatus(t *testing.T) {
120124
},
121125
},
122126
files: map[string][]byte{
123-
"cluster-resources/deployments/default.json": []byte(collectedDeployments),
127+
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
128+
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
129+
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
130+
},
131+
},
132+
{
133+
name: "multiple namespaces, 2/3",
134+
analyzer: troubleshootv1beta2.DeploymentStatus{
135+
Namespaces: []string{"default", "monitoring"},
136+
},
137+
expectResult: []*AnalyzeResult{
138+
{
139+
IsPass: false,
140+
IsWarn: false,
141+
IsFail: true,
142+
Title: "default/kotsadm-web Deployment Status",
143+
Message: "The deployment default/kotsadm-web has 1/2 replicas",
144+
IconKey: "kubernetes_deployment_status",
145+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
146+
},
147+
{
148+
IsPass: false,
149+
IsWarn: false,
150+
IsFail: true,
151+
Title: "monitoring/prometheus-operator Deployment Status",
152+
Message: "The deployment monitoring/prometheus-operator has 1/2 replicas",
153+
IconKey: "kubernetes_deployment_status",
154+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
155+
},
156+
},
157+
files: map[string][]byte{
158+
"cluster-resources/deployments/default.json": []byte(defaultDeployments),
159+
"cluster-resources/deployments/monitoring.json": []byte(monitoringDeployments),
160+
"cluster-resources/deployments/kube-system.json": []byte(kubeSystemDeployments),
124161
},
125162
},
126163
}
@@ -130,14 +167,19 @@ func Test_deploymentStatus(t *testing.T) {
130167
req := require.New(t)
131168

132169
getFiles := func(n string) (map[string][]byte, error) {
170+
if file, ok := test.files[n]; ok {
171+
return map[string][]byte{n: file}, nil
172+
}
133173
return test.files, nil
134174
}
135175

136176
actual, err := analyzeDeploymentStatus(&test.analyzer, getFiles)
137177
req.NoError(err)
138178

139-
assert.Equal(t, test.expectResult, actual)
140-
179+
req.Equal(len(test.expectResult), len(actual))
180+
for _, a := range actual {
181+
assert.Contains(t, test.expectResult, a)
182+
}
141183
})
142184
}
143185
}

pkg/analyze/files/deployments.json renamed to pkg/analyze/files/deployments/default.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@
425425
},
426426
"status": {
427427
"observedGeneration": 1,
428-
"replicas": 1,
428+
"replicas": 2,
429429
"updatedReplicas": 1,
430430
"readyReplicas": 1,
431431
"availableReplicas": 1,

0 commit comments

Comments
 (0)