Skip to content

Commit

Permalink
Merge pull request #478 from replicatedhq/divolgin/outcomes
Browse files Browse the repository at this point in the history
Ensure outcomes are optional in every case
  • Loading branch information
divolgin committed Oct 29, 2021
2 parents b4c97e3 + 742ddc8 commit 181df9c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 60 deletions.
53 changes: 32 additions & 21 deletions pkg/analyze/job_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ func analyzeOneJobStatus(analyzer *troubleshootv1beta2.JobStatus, getFileContent
IsFail: true,
Message: fmt.Sprintf("The job %q was not found", analyzer.Name),
}
} else {
} else if len(analyzer.Outcomes) > 0 {
result, err = jobStatus(analyzer.Outcomes, job)
if err != nil {
return nil, errors.Wrap(err, "failed to process status")
}
} else {
result = getDefaultJobResult(job)
}
}

if result == nil {
return nil, nil
}

return []*AnalyzeResult{result}, nil
}

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

for _, job := range jobs {
if job.Spec.Completions == nil && job.Status.Succeeded > 1 {
continue
}

if job.Spec.Completions != nil && *job.Spec.Completions == job.Status.Succeeded {
continue
}

if job.Status.Failed == 0 {
continue
}

result := &AnalyzeResult{
Title: fmt.Sprintf("%s/%s Job Status", job.Namespace, job.Name),
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
IsFail: true,
Message: fmt.Sprintf("The job %s/%s is not complete", job.Namespace, job.Name),
result := getDefaultJobResult(&job)
if result != nil {
results = append(results, result)
}

results = append(results, result)
}
}

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

func getDefaultJobResult(job *batchv1.Job) *AnalyzeResult {
if job.Spec.Completions == nil && job.Status.Succeeded > 1 {
return nil
}

if job.Spec.Completions != nil && *job.Spec.Completions == job.Status.Succeeded {
return nil
}

if job.Status.Failed == 0 {
return nil
}

return &AnalyzeResult{
Title: fmt.Sprintf("%s/%s Job Status", job.Namespace, job.Name),
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
IsFail: true,
Message: fmt.Sprintf("The job %s/%s is not complete", job.Namespace, job.Name),
}
}

func compareJobStatusToWhen(when string, job *batchv1.Job) (bool, error) {
parts := strings.Split(strings.TrimSpace(when), " ")

Expand Down
18 changes: 18 additions & 0 deletions pkg/analyze/job_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ func Test_JobStatus(t *testing.T) {
"cluster-resources/jobs/test.json": []byte(collectedJobs),
},
},
{
name: "analyze all jobs",
analyzer: troubleshootv1beta2.JobStatus{},
expectResult: []*AnalyzeResult{
{
IsPass: false,
IsWarn: false,
IsFail: true,
Title: "test/post-install-job Job Status",
Message: "The job test/post-install-job is not complete",
IconKey: "kubernetes_deployment_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17",
},
},
files: map[string][]byte{
"cluster-resources/jobs/test.json": []byte(collectedJobs),
},
},
}

for _, test := range tests {
Expand Down
40 changes: 24 additions & 16 deletions pkg/analyze/replicaset_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ func analyzeOneReplicaSetStatus(analyzer *troubleshootv1beta2.ReplicaSetStatus,
IsFail: true,
Message: fmt.Sprintf("The replicaset %q was not found", analyzer.Name),
}
} else {
} else if len(analyzer.Outcomes) > 0 {
result, err = replicasetStatus(analyzer.Outcomes, replicaset)
if err != nil {
return nil, errors.Wrap(err, "failed to process status")
}
} else {
result = getDefaultReplicaSetResult(replicaset)
}
}

Expand Down Expand Up @@ -99,23 +101,12 @@ func analyzeAllReplicaSetStatuses(analyzer *troubleshootv1beta2.ReplicaSetStatus
return nil, errors.Wrap(err, "failed to process status")
}
} else {
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
continue
}
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
continue
}

result = &AnalyzeResult{
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
IconKey: "kubernetes_deployment_status", // TODO: need new icon
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
IsFail: true,
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
}
result = getDefaultReplicaSetResult(&replicaset)
}

results = append(results, result)
if result != nil {
results = append(results, result)
}
}
}

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

func getDefaultReplicaSetResult(replicaset *appsv1.ReplicaSet) *AnalyzeResult {
if replicaset.Spec.Replicas == nil && replicaset.Status.AvailableReplicas == 1 { // default is 1
return nil
}
if replicaset.Spec.Replicas != nil && *replicaset.Spec.Replicas == replicaset.Status.AvailableReplicas {
return nil
}

return &AnalyzeResult{
Title: fmt.Sprintf("%s/%s ReplicaSet Status", replicaset.Namespace, replicaset.Name),
IconKey: "kubernetes_deployment_status", // TODO: need new icon
IconURI: "https://troubleshoot.sh/images/analyzer-icons/deployment-status.svg?w=17&h=17", // TODO: need new icon
IsFail: true,
Message: fmt.Sprintf("The replicaset %s/%s is not ready", replicaset.Namespace, replicaset.Name),
}
}

func compareReplicaSetStatusToWhen(when string, job *appsv1.ReplicaSet) (bool, error) {
parts := strings.Split(strings.TrimSpace(when), " ")

Expand Down
6 changes: 2 additions & 4 deletions pkg/analyze/replicaset_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,8 @@ func Test_analyzeReplicaSetStatus(t *testing.T) {
},
},
{
name: "find the one failing replicaset",
analyzer: troubleshootv1beta2.ReplicaSetStatus{
Namespace: "rook-ceph",
},
name: "analyze all replicasets",
analyzer: troubleshootv1beta2.ReplicaSetStatus{},
expectResult: []*AnalyzeResult{
{
IsPass: false,
Expand Down
49 changes: 30 additions & 19 deletions pkg/analyze/statefulset_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,36 @@ func analyzeOneStatefulsetStatus(analyzer *troubleshootv1beta2.StatefulsetStatus
return nil, errors.Wrap(err, "failed to unmarshal statefulset list")
}

var status *appsv1.StatefulSetStatus
for _, statefulset := range statefulsets {
if statefulset.Name == analyzer.Name {
status = statefulset.Status.DeepCopy()
var statefulset *appsv1.StatefulSet
for _, s := range statefulsets {
if s.Name == analyzer.Name {
statefulset = s.DeepCopy()
break
}
}

if status == nil {
if statefulset == nil {
result = &AnalyzeResult{
Title: fmt.Sprintf("%s Statefulset Status", analyzer.Name),
IconKey: "kubernetes_statefulset_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
IsFail: true,
Message: fmt.Sprintf("The statefulset %q was not found", analyzer.Name),
}
} else {
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))
} else if len(analyzer.Outcomes) > 0 {
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))
if err != nil {
return nil, errors.Wrap(err, "failed to process status")
}
} else {
result = getDefaultStatefulSetResult(statefulset)
}
}

if result == nil {
return nil, nil
}

return []*AnalyzeResult{result}, nil
}

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

for _, statefulset := range statefulsets {
if statefulset.Status.Replicas == statefulset.Status.ReadyReplicas {
continue
}

result := &AnalyzeResult{
Title: fmt.Sprintf("%s/%s Statefulset Status", statefulset.Namespace, statefulset.Name),
IconKey: "kubernetes_statefulset_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
IsFail: true,
Message: fmt.Sprintf("The statefulset %s/%s has %d/%d replicas", statefulset.Namespace, statefulset.Name, statefulset.Status.ReadyReplicas, statefulset.Status.Replicas),
result := getDefaultStatefulSetResult(&statefulset)
if result != nil {
results = append(results, result)
}

results = append(results, result)
}
}

return results, nil
}

func getDefaultStatefulSetResult(statefulset *appsv1.StatefulSet) *AnalyzeResult {
if statefulset.Status.Replicas == statefulset.Status.ReadyReplicas {
return nil
}

return &AnalyzeResult{
Title: fmt.Sprintf("%s/%s Statefulset Status", statefulset.Namespace, statefulset.Name),
IconKey: "kubernetes_statefulset_status",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/statefulset-status.svg?w=23&h=14",
IsFail: true,
Message: fmt.Sprintf("The statefulset %s/%s has %d/%d replicas", statefulset.Namespace, statefulset.Name, statefulset.Status.ReadyReplicas, statefulset.Status.Replicas),
}
}

0 comments on commit 181df9c

Please sign in to comment.