Skip to content

Commit 181df9c

Browse files
authored
Merge pull request #478 from replicatedhq/divolgin/outcomes
Ensure outcomes are optional in every case
2 parents b4c97e3 + 742ddc8 commit 181df9c

File tree

5 files changed

+106
-60
lines changed

5 files changed

+106
-60
lines changed

pkg/analyze/job_status.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,20 @@ func analyzeOneJobStatus(analyzer *troubleshootv1beta2.JobStatus, getFileContent
5050
IsFail: true,
5151
Message: fmt.Sprintf("The job %q was not found", analyzer.Name),
5252
}
53-
} else {
53+
} else if len(analyzer.Outcomes) > 0 {
5454
result, err = jobStatus(analyzer.Outcomes, job)
5555
if err != nil {
5656
return nil, errors.Wrap(err, "failed to process status")
5757
}
58+
} else {
59+
result = getDefaultJobResult(job)
5860
}
5961
}
6062

63+
if result == nil {
64+
return nil, nil
65+
}
66+
6167
return []*AnalyzeResult{result}, nil
6268
}
6369

@@ -82,27 +88,10 @@ func analyzeAllJobStatuses(analyzer *troubleshootv1beta2.JobStatus, getFileConte
8288
}
8389

8490
for _, job := range jobs {
85-
if job.Spec.Completions == nil && job.Status.Succeeded > 1 {
86-
continue
87-
}
88-
89-
if job.Spec.Completions != nil && *job.Spec.Completions == job.Status.Succeeded {
90-
continue
91-
}
92-
93-
if job.Status.Failed == 0 {
94-
continue
95-
}
96-
97-
result := &AnalyzeResult{
98-
Title: fmt.Sprintf("%s/%s Job Status", job.Namespace, job.Name),
99-
IconKey: "kubernetes_deployment_status",
100-
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
101-
IsFail: true,
102-
Message: fmt.Sprintf("The job %s/%s is not complete", job.Namespace, job.Name),
91+
result := getDefaultJobResult(&job)
92+
if result != nil {
93+
results = append(results, result)
10394
}
104-
105-
results = append(results, result)
10695
}
10796
}
10897

@@ -187,6 +176,28 @@ func jobStatus(outcomes []*troubleshootv1beta2.Outcome, job *batchv1.Job) (*Anal
187176
return result, nil
188177
}
189178

179+
func getDefaultJobResult(job *batchv1.Job) *AnalyzeResult {
180+
if job.Spec.Completions == nil && job.Status.Succeeded > 1 {
181+
return nil
182+
}
183+
184+
if job.Spec.Completions != nil && *job.Spec.Completions == job.Status.Succeeded {
185+
return nil
186+
}
187+
188+
if job.Status.Failed == 0 {
189+
return nil
190+
}
191+
192+
return &AnalyzeResult{
193+
Title: fmt.Sprintf("%s/%s Job Status", job.Namespace, job.Name),
194+
IconKey: "kubernetes_deployment_status",
195+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
196+
IsFail: true,
197+
Message: fmt.Sprintf("The job %s/%s is not complete", job.Namespace, job.Name),
198+
}
199+
}
200+
190201
func compareJobStatusToWhen(when string, job *batchv1.Job) (bool, error) {
191202
parts := strings.Split(strings.TrimSpace(when), " ")
192203

pkg/analyze/job_status_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,24 @@ func Test_JobStatus(t *testing.T) {
123123
"cluster-resources/jobs/test.json": []byte(collectedJobs),
124124
},
125125
},
126+
{
127+
name: "analyze all jobs",
128+
analyzer: troubleshootv1beta2.JobStatus{},
129+
expectResult: []*AnalyzeResult{
130+
{
131+
IsPass: false,
132+
IsWarn: false,
133+
IsFail: true,
134+
Title: "test/post-install-job Job Status",
135+
Message: "The job test/post-install-job is not complete",
136+
IconKey: "kubernetes_deployment_status",
137+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
138+
},
139+
},
140+
files: map[string][]byte{
141+
"cluster-resources/jobs/test.json": []byte(collectedJobs),
142+
},
143+
},
126144
}
127145

128146
for _, test := range tests {

pkg/analyze/replicaset_status.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ func analyzeOneReplicaSetStatus(analyzer *troubleshootv1beta2.ReplicaSetStatus,
5151
IsFail: true,
5252
Message: fmt.Sprintf("The replicaset %q was not found", analyzer.Name),
5353
}
54-
} else {
54+
} else if len(analyzer.Outcomes) > 0 {
5555
result, err = replicasetStatus(analyzer.Outcomes, replicaset)
5656
if err != nil {
5757
return nil, errors.Wrap(err, "failed to process status")
5858
}
59+
} else {
60+
result = getDefaultReplicaSetResult(replicaset)
5961
}
6062
}
6163

@@ -99,23 +101,12 @@ func analyzeAllReplicaSetStatuses(analyzer *troubleshootv1beta2.ReplicaSetStatus
99101
return nil, errors.Wrap(err, "failed to process status")
100102
}
101103
} else {
102-
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
103-
continue
104-
}
105-
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
106-
continue
107-
}
108-
109-
result = &AnalyzeResult{
110-
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
111-
IconKey: "kubernetes_deployment_status", // TODO: need new icon
112-
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
113-
IsFail: true,
114-
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
115-
}
104+
result = getDefaultReplicaSetResult(&replicaset)
116105
}
117106

118-
results = append(results, result)
107+
if result != nil {
108+
results = append(results, result)
109+
}
119110
}
120111
}
121112

@@ -200,6 +191,23 @@ func replicasetStatus(outcomes []*troubleshootv1beta2.Outcome, replicaset *appsv
200191
return result, nil
201192
}
202193

194+
func getDefaultReplicaSetResult(replicaset *appsv1.ReplicaSet) *AnalyzeResult {
195+
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
196+
return nil
197+
}
198+
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
199+
return nil
200+
}
201+
202+
return &AnalyzeResult{
203+
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
204+
IconKey: "kubernetes_deployment_status", // TODO: need new icon
205+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
206+
IsFail: true,
207+
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
208+
}
209+
}
210+
203211
func compareReplicaSetStatusToWhen(when string, job *appsv1.ReplicaSet) (bool, error) {
204212
parts := strings.Split(strings.TrimSpace(when), " ")
205213

pkg/analyze/replicaset_status_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,8 @@ func Test_analyzeReplicaSetStatus(t *testing.T) {
8484
},
8585
},
8686
{
87-
name: "find the one failing replicaset",
88-
analyzer: troubleshootv1beta2.ReplicaSetStatus{
89-
Namespace: "rook-ceph",
90-
},
87+
name: "analyze all replicasets",
88+
analyzer: troubleshootv1beta2.ReplicaSetStatus{},
9189
expectResult: []*AnalyzeResult{
9290
{
9391
IsPass: false,

pkg/analyze/statefulset_status.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,36 @@ func analyzeOneStatefulsetStatus(analyzer *troubleshootv1beta2.StatefulsetStatus
3131
return nil, errors.Wrap(err, "failed to unmarshal statefulset list")
3232
}
3333

34-
var status *appsv1.StatefulSetStatus
35-
for _, statefulset := range statefulsets {
36-
if statefulset.Name == analyzer.Name {
37-
status = statefulset.Status.DeepCopy()
34+
var statefulset *appsv1.StatefulSet
35+
for _, s := range statefulsets {
36+
if s.Name == analyzer.Name {
37+
statefulset = s.DeepCopy()
3838
break
3939
}
4040
}
4141

42-
if status == nil {
42+
if statefulset == nil {
4343
result = &AnalyzeResult{
4444
Title: fmt.Sprintf("%s Statefulset Status", analyzer.Name),
4545
IconKey: "kubernetes_statefulset_status",
4646
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
4747
IsFail: true,
4848
Message: fmt.Sprintf("The statefulset %q was not found", analyzer.Name),
4949
}
50-
} else {
51-
result, err = commonStatus(analyzer.Outcomes, fmt.Sprintf("%s Status", analyzer.Name), "kubernetes_statefulset_status", "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14", int(status.ReadyReplicas))
50+
} else if len(analyzer.Outcomes) > 0 {
51+
result, err = commonStatus(analyzer.Outcomes, fmt.Sprintf("%s Status", analyzer.Name), "kubernetes_statefulset_status", "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14", int(statefulset.Status.ReadyReplicas))
5252
if err != nil {
5353
return nil, errors.Wrap(err, "failed to process status")
5454
}
55+
} else {
56+
result = getDefaultStatefulSetResult(statefulset)
5557
}
5658
}
5759

60+
if result == nil {
61+
return nil, nil
62+
}
63+
5864
return []*AnalyzeResult{result}, nil
5965
}
6066

@@ -79,21 +85,26 @@ func analyzeAllStatefulsetStatuses(analyzer *troubleshootv1beta2.StatefulsetStat
7985
}
8086

8187
for _, statefulset := range statefulsets {
82-
if statefulset.Status.Replicas == statefulset.Status.ReadyReplicas {
83-
continue
84-
}
85-
86-
result := &AnalyzeResult{
87-
Title: fmt.Sprintf("%s/%s Statefulset Status", statefulset.Namespace, statefulset.Name),
88-
IconKey: "kubernetes_statefulset_status",
89-
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
90-
IsFail: true,
91-
Message: fmt.Sprintf("The statefulset %s/%s has %d/%d replicas", statefulset.Namespace, statefulset.Name, statefulset.Status.ReadyReplicas, statefulset.Status.Replicas),
88+
result := getDefaultStatefulSetResult(&statefulset)
89+
if result != nil {
90+
results = append(results, result)
9291
}
93-
94-
results = append(results, result)
9592
}
9693
}
9794

9895
return results, nil
9996
}
97+
98+
func getDefaultStatefulSetResult(statefulset *appsv1.StatefulSet) *AnalyzeResult {
99+
if statefulset.Status.Replicas == statefulset.Status.ReadyReplicas {
100+
return nil
101+
}
102+
103+
return &AnalyzeResult{
104+
Title: fmt.Sprintf("%s/%s Statefulset Status", statefulset.Namespace, statefulset.Name),
105+
IconKey: "kubernetes_statefulset_status",
106+
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
107+
IsFail: true,
108+
Message: fmt.Sprintf("The statefulset %s/%s has %d/%d replicas", statefulset.Namespace, statefulset.Name, statefulset.Status.ReadyReplicas, statefulset.Status.Replicas),
109+
}
110+
}

0 commit comments

Comments
 (0)