|
| 1 | +package analyzers |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "text/tabwriter" |
| 8 | + |
| 9 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 10 | + "k8s.io/apimachinery/pkg/runtime" |
| 11 | +) |
| 12 | + |
| 13 | +type PodsAnalyzer struct{} |
| 14 | + |
| 15 | +func (*PodsAnalyzer) Analyze(content []byte) (string, error) { |
| 16 | + manifestObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, content) |
| 17 | + if err != nil { |
| 18 | + return "", err |
| 19 | + } |
| 20 | + manifestUnstructured := manifestObj.(*unstructured.UnstructuredList) |
| 21 | + |
| 22 | + writer := &bytes.Buffer{} |
| 23 | + w := tabwriter.NewWriter(writer, 70, 0, 0, ' ', tabwriter.DiscardEmptyColumns) |
| 24 | + |
| 25 | + err = manifestUnstructured.EachListItem(func(object runtime.Object) error { |
| 26 | + u := object.(*unstructured.Unstructured) |
| 27 | + conditions, _, err := unstructured.NestedSlice(u.Object, "status", "conditions") |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + resultConditions := []string{} |
| 32 | + for _, condition := range conditions { |
| 33 | + condType, _, err := unstructured.NestedString(condition.(map[string]interface{}), "type") |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + condStatus, _, err := unstructured.NestedString(condition.(map[string]interface{}), "status") |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + resultConditions = append(resultConditions, fmt.Sprintf("%s=%s", condType, condStatus)) |
| 42 | + } |
| 43 | + |
| 44 | + resultContainers := []string{} |
| 45 | + containerStatuses, _, err := unstructured.NestedSlice(u.Object, "status", "containerStatuses") |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + for _, status := range containerStatuses { |
| 50 | + exitCode, exists, err := unstructured.NestedInt64(status.(map[string]interface{}), "lastState", "terminated", "exitCode") |
| 51 | + if !exists { |
| 52 | + continue |
| 53 | + } |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + if exitCode != 0 { |
| 58 | + message, _, err := unstructured.NestedString(status.(map[string]interface{}), "lastState", "terminated", "message") |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + containerName, _, err := unstructured.NestedString(status.(map[string]interface{}), "name") |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + resultContainers = append(resultContainers, fmt.Sprintf(" [!] Container %q exited with %d:\n %s\n", containerName, exitCode, message)) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fmt.Fprintf(w, "%s\t%s\n", u.GetName(), strings.Join(resultConditions, ", ")) |
| 71 | + |
| 72 | + if len(resultContainers) > 0 { |
| 73 | + fmt.Fprintf(w, "%s\n", strings.Join(resultContainers, ", ")) |
| 74 | + } |
| 75 | + return nil |
| 76 | + }) |
| 77 | + |
| 78 | + w.Flush() |
| 79 | + |
| 80 | + return writer.String(), err |
| 81 | +} |
0 commit comments