This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
status.go
67 lines (56 loc) · 1.71 KB
/
status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2018 Oath, Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package kubernetes
import (
"strings"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
"github.com/yahoo/yfuzz/pkg/types"
corev1 "k8s.io/api/core/v1"
)
// GetJobStatus retrieves the status of all pods belonging to a given job.
func (k API) GetJobStatus(job string) (map[types.PodStatus]int, error) {
pods, err := k.GetPods(job)
if err != nil {
return nil, err
}
count := make(map[types.PodStatus]int)
for _, pod := range pods.Items {
if pod.Status.Phase == corev1.PodSucceeded {
// Job finished normally, check if we found a crash
status := k.getFinishedPodStatus(pod.GetName())
count[status]++
} else {
status := podPhaseToPodStatus(pod.Status.Phase)
count[status]++
}
}
return count, nil
}
func (k API) getFinishedPodStatus(podName string) types.PodStatus {
log, err := k.GetPodLogs(podName, viper.GetInt("kubernetes.job-config.log.tail-lines"))
if err != nil {
jww.WARN.Println(err.Error())
return types.StatusUnknown
}
if strings.Contains(log, viper.GetString("kubernetes.job-config.log.crash-found-message")) {
return types.StatusCrashFound
}
return types.StatusNoCrash
}
// Convert a kubernetes pod phase into a yFuzz pod status
func podPhaseToPodStatus(podPhase corev1.PodPhase) types.PodStatus {
switch podPhase {
case corev1.PodPending:
return types.StatusPending
case corev1.PodRunning:
return types.StatusRunning
case corev1.PodFailed:
return types.StatusFailed
case corev1.PodUnknown:
return types.StatusUnknown
default:
jww.WARN.Printf("Faield to convert %s to yFuzz Job Status.\n", podPhase)
return types.StatusUnknown
}
}