Skip to content

Commit 9a457f7

Browse files
fix/clusterPodStatuses: only process when conditional if specified (#1088)
* only process when conditional if specified * adding tests for cluster pod status analyzer * use klog instead of fmt for logging * add additional tests for warn and more operators --------- Co-authored-by: Camila Macedo <[email protected]>
1 parent c99e34f commit 9a457f7

23 files changed

+1516
-109
lines changed

pkg/analyze/cluster_pod_statuses.go

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package analyzer
33
import (
44
"bytes"
55
"encoding/json"
6-
"fmt"
76
"path/filepath"
87
"strings"
98
"text/template"
@@ -13,6 +12,7 @@ import (
1312
"github.com/replicatedhq/troubleshoot/pkg/constants"
1413
"github.com/replicatedhq/troubleshoot/pkg/k8sutil"
1514
corev1 "k8s.io/api/core/v1"
15+
"k8s.io/klog/v2"
1616
)
1717

1818
type AnalyzeClusterPodStatuses struct {
@@ -100,37 +100,40 @@ func clusterPodStatuses(analyzer *troubleshootv1beta2.ClusterPodStatuses, getChi
100100
r.URI = outcome.Pass.URI
101101
when = outcome.Pass.When
102102
} else {
103-
println("error: found an empty outcome in a clusterPodStatuses analyzer") // don't stop
103+
klog.Error("error: found an empty outcome in a clusterPodStatuses analyzer\n")
104104
continue
105105
}
106106

107-
parts := strings.Split(strings.TrimSpace(when), " ")
108-
if len(parts) < 2 {
109-
println(fmt.Sprintf("invalid 'when' format: %s\n", when)) // don't stop
110-
continue
111-
}
112-
113-
operator := parts[0]
114-
reason := parts[1]
107+
operator := ""
108+
reason := ""
115109
match := false
116-
117-
switch operator {
118-
case "=", "==", "===":
119-
if reason == "Healthy" {
120-
match = !k8sutil.IsPodUnhealthy(&pod)
121-
} else {
122-
match = reason == string(pod.Status.Phase) || reason == string(pod.Status.Reason)
110+
if when != "" {
111+
parts := strings.Split(strings.TrimSpace(when), " ")
112+
if len(parts) < 2 {
113+
klog.Errorf("invalid 'when' format: %s\n", when)
114+
continue
123115
}
124-
case "!=", "!==":
125-
if reason == "Healthy" {
126-
match = k8sutil.IsPodUnhealthy(&pod)
127-
} else {
128-
match = reason != string(pod.Status.Phase) && reason != string(pod.Status.Reason)
116+
operator = parts[0]
117+
reason = parts[1]
118+
119+
switch operator {
120+
case "=", "==", "===":
121+
if reason == "Healthy" {
122+
match = !k8sutil.IsPodUnhealthy(&pod)
123+
} else {
124+
match = reason == string(pod.Status.Phase) || reason == string(pod.Status.Reason)
125+
}
126+
case "!=", "!==":
127+
if reason == "Healthy" {
128+
match = k8sutil.IsPodUnhealthy(&pod)
129+
} else {
130+
match = reason != string(pod.Status.Phase) && reason != string(pod.Status.Reason)
131+
}
129132
}
130-
}
131133

132-
if !match {
133-
continue
134+
if !match {
135+
continue
136+
}
134137
}
135138

136139
r.InvolvedObject = &corev1.ObjectReference{

0 commit comments

Comments
 (0)