Ensure outcomes are optional in every case

This commit is contained in:
divolgin
2021-10-29 00:23:32 +00:00
parent b4c97e377e
commit 742ddc8c06
5 changed files with 106 additions and 60 deletions

View File

@@ -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
}
@@ -82,27 +88,10 @@ func analyzeAllJobStatuses(analyzer *troubleshootv1beta2.JobStatus, getFileConte
}
for _, job := range jobs {
if job.Spec.Completions == nil && job.Status.Succeeded > 1 {
continue
result := getDefaultJobResult(&job)
if result != nil {
results = append(results, result)
}
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),
}
results = append(results, result)
}
}
@@ -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), " ")

View File

@@ -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 {

View File

@@ -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)
}
}
@@ -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)
}
}
}
@@ -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), " ")

View File

@@ -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,

View File

@@ -31,15 +31,15 @@ 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",
@@ -47,14 +47,20 @@ func analyzeOneStatefulsetStatus(analyzer *troubleshootv1beta2.StatefulsetStatus
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
}
@@ -79,21 +85,26 @@ func analyzeAllStatefulsetStatuses(analyzer *troubleshootv1beta2.StatefulsetStat
}
for _, statefulset := range statefulsets {
if statefulset.Status.Replicas == statefulset.Status.ReadyReplicas {
continue
result := getDefaultStatefulSetResult(&statefulset)
if result != nil {
results = append(results, result)
}
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),
}
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),
}
}