-
Notifications
You must be signed in to change notification settings - Fork 43
/
qos.go
101 lines (84 loc) · 2.31 KB
/
qos.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package qos
import (
"fmt"
"github.com/vmware-tanzu/sonobuoy-plugins/reliability-scanner/internal"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
var (
checkName string = "qos"
)
// QuerierSpec defines the Specification for a Querier
type QuerierSpec struct {
MinimumDesiredQOSClass string `yaml:"minimum_desired_qos_class"`
IncludeDetail bool `yaml:"include_detail"`
}
// Querier defines the query and set of checks
type Querier struct {
client *kubernetes.Clientset
Spec *QuerierSpec `yaml:"spec"`
}
// NewQuerier returns a new configured Querier
func NewQuerier(spec *QuerierSpec) (Querier, error) {
out := Querier{
Spec: spec,
}
config, err := rest.InClusterConfig()
if err != nil {
return out, err
}
out.client, err = kubernetes.NewForConfig(config)
if err != nil {
return out, err
}
return out, nil
}
// AddtoRunner configures a runner with the Querier for this check.
func (querier *Querier) AddtoRunner(runner *internal.Runner) {
runner.Queriers = append(runner.Queriers, querier)
runner.Logger.WithFields(log.Fields{
"check_name": checkName,
"phase": "add",
}).Info("complete")
}
// Start runs the Querier
func (q Querier) Start(cfg *internal.QuerierConfig) {
cfg.Logger.WithFields(log.Fields{
"check": checkName,
"phase": "starting",
}).Info(internal.CheckStartMsg)
checkItem := internal.ReportItem{
Name: checkName,
Status: "passed",
}
pods, err := q.client.CoreV1().Pods("").List(cfg.Context, metav1.ListOptions{})
if err != nil {
checkItem.Status = "failed"
}
for _, pod := range pods.Items {
details := make(map[string]interface{})
item := internal.Item{
Name: fmt.Sprintf("%s/%s", pod.Namespace, pod.Name),
Status: "failed",
Details: details,
}
if meetsMinimum(string(pod.Status.QOSClass), q.Spec.MinimumDesiredQOSClass) {
item.Status = "passed"
}
if q.Spec.IncludeDetail {
details["qos_class"] = pod.Status.QOSClass
}
checkItem.Items = append(checkItem.Items, item)
}
cfg.Logger.WithFields(log.Fields{
"check_name": checkName,
"phase": "complete",
}).Info(internal.CheckCompleteMsg)
cfg.Results <- checkItem
cfg.Logger.WithFields(log.Fields{
"check_name": checkName,
"phase": "write",
}).Info(internal.CheckWriteMsg)
}