Skip to content

Commit

Permalink
* address code review
Browse files Browse the repository at this point in the history
* handle no when outcome
* fix Go lint G115
  • Loading branch information
nvanthao committed Dec 2, 2024
1 parent e363669 commit 51c678e
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 27 deletions.
1 change: 1 addition & 0 deletions config/crds/troubleshoot.sh_analyzers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ spec:
type: object
type: array
restartCount:
format: int32
type: integer
strict:
type: BoolString
Expand Down
1 change: 1 addition & 0 deletions config/crds/troubleshoot.sh_preflights.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ spec:
type: object
type: array
restartCount:
format: int32
type: integer
strict:
type: BoolString
Expand Down
1 change: 1 addition & 0 deletions config/crds/troubleshoot.sh_supportbundles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ spec:
type: object
type: array
restartCount:
format: int32
type: integer
strict:
type: BoolString
Expand Down
50 changes: 27 additions & 23 deletions pkg/analyze/cluster_container_statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ func (a *AnalyzeClusterContainerStatuses) Analyze(getFile getCollectedFileConten
}

func (a *AnalyzeClusterContainerStatuses) getPodsMatchingFilters(podListFiles map[string][]byte) (podsWithContainers, error) {
var podsMatchedNamespace []corev1.Pod
matchedPods := podsWithContainers{}

// filter pods matched namespace selector
for fileName, fileContent := range podListFiles {
// pod list fileName is the namespace name, e.g. default.json
currentNamespace := strings.TrimSuffix(filepath.Base(fileName), ".json")
Expand All @@ -82,7 +84,6 @@ func (a *AnalyzeClusterContainerStatuses) getPodsMatchingFilters(podListFiles ma
}

// filter pods by namespace
var podsMatchedNamespace []corev1.Pod
var podList corev1.PodList
if err := json.Unmarshal(fileContent, &podList); err != nil {
var pods []corev1.Pod
Expand All @@ -94,32 +95,33 @@ func (a *AnalyzeClusterContainerStatuses) getPodsMatchingFilters(podListFiles ma
} else {
podsMatchedNamespace = append(podsMatchedNamespace, podList.Items...)
}
}

// filter pods by container restart count
for _, pod := range podsMatchedNamespace {
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.RestartCount < int32(a.analyzer.RestartCount) {
continue
}
// check if the pod has already been matched
key := string(pod.UID)
if _, ok := matchedPods[key]; !ok {
matchedPods[key] = struct {
name string
namespace string
containerStatuses []corev1.ContainerStatus
}{
name: pod.Name,
namespace: pod.Namespace,
containerStatuses: []corev1.ContainerStatus{containerStatus},
}
continue
// filter pods by container criteria
for _, pod := range podsMatchedNamespace {
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.RestartCount < a.analyzer.RestartCount {
continue
}
// check if the pod has already been matched
key := string(pod.UID)
if _, ok := matchedPods[key]; !ok {
matchedPods[key] = struct {
name string
namespace string
containerStatuses []corev1.ContainerStatus
}{
name: pod.Name,
namespace: pod.Namespace,
containerStatuses: []corev1.ContainerStatus{containerStatus},
}
entry := matchedPods[key]
entry.containerStatuses = append(entry.containerStatuses, containerStatus)
continue
}
entry := matchedPods[key]
entry.containerStatuses = append(entry.containerStatuses, containerStatus)
}
}

return matchedPods, nil
}

Expand Down Expand Up @@ -156,10 +158,12 @@ func (a *AnalyzeClusterContainerStatuses) analyzeContainerStatuses(podContainers
continue
}

// empty when indicates final case, let's return the result
if when == "" {
continue
return []*AnalyzeResult{&r}, nil
}

// continue matching with when condition
reason, isEqualityOp, err := parseWhen(when)
if err != nil {
return nil, errors.Wrap(err, "failed to parse when")
Expand Down
36 changes: 36 additions & 0 deletions pkg/analyze/cluster_container_statuses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,42 @@ func Test_analyzeContainerStatuses(t *testing.T) {
"cluster-resources/pods/message-oomkill-pod.json": []byte(messageOOMKillPod),
},
},
{
name: "pass when there is no status detected",
analyzer: troubleshootv1beta2.ClusterContainerStatuses{
AnalyzeMeta: troubleshootv1beta2.AnalyzeMeta{
CheckName: "oomkilled-container",
},
Outcomes: []*troubleshootv1beta2.Outcome{
{
Fail: &troubleshootv1beta2.SingleOutcome{
When: "== OOMKilled",
Message: "Container {{ .ContainerName }} from pod {{ .Namespace }}/{{ .PodName }} has OOMKilled",
},
},
{
Pass: &troubleshootv1beta2.SingleOutcome{
Message: "No OOMKilled container found",
},
},
},
Namespaces: []string{"default"},
},
expectResult: []*AnalyzeResult{
{
IsFail: false,
IsWarn: false,
IsPass: true,
Title: "oomkilled-container",
Message: "No OOMKilled container found",
IconKey: "kubernetes_container_statuses",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/kubernetes.svg?w=16&h=16",
},
},
files: map[string][]byte{
"cluster-resources/pods/default.json": []byte(defaultPods),
},
},
}

for _, test := range tests {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/troubleshoot/v1beta2/analyzer_shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type ClusterContainerStatuses struct {
AnalyzeMeta `json:",inline" yaml:",inline"`
Outcomes []*Outcome `json:"outcomes" yaml:"outcomes"`
Namespaces []string `json:"namespaces,omitempty" yaml:"namespaces,omitempty"`
RestartCount int `json:"restartCount" yaml:"restartCount"`
RestartCount int32 `json:"restartCount" yaml:"restartCount"`
}

type ContainerRuntime struct {
Expand Down
3 changes: 2 additions & 1 deletion schemas/analyzer-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@
}
},
"restartCount": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"strict": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
Expand Down
3 changes: 2 additions & 1 deletion schemas/preflight-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@
}
},
"restartCount": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"strict": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
Expand Down
3 changes: 2 additions & 1 deletion schemas/supportbundle-troubleshoot-v1beta2.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@
}
},
"restartCount": {
"type": "integer"
"type": "integer",
"format": "int32"
},
"strict": {
"oneOf": [{"type": "string"},{"type": "boolean"}]
Expand Down

0 comments on commit 51c678e

Please sign in to comment.