-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpdb.go
61 lines (55 loc) · 1.36 KB
/
pdb.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
package main
import (
"encoding/json"
"log"
"os/exec"
)
// PdbItems a list of Pod Disruption Budget
type PdbItems struct {
Items []Pdb
}
// Pdb Pod Disruption Budget
type Pdb struct {
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
} `json:"metadata"`
Spec struct {
MinAvailable int `json:"minAvailable"`
MaxUnavailable int `json:"maxUnavailable"`
Selector struct {
MatchLabels map[string]string `json:"matchLabels"`
} `json:"selector"`
} `json:"spec"`
Status struct {
CurrentHealthy int `json:"currentHealthy"`
DesiredHealthy int `json:"desiredHealthy"`
DisruptionsAllowed int `json:"disruptionsAllowed"`
ExpectedPods int `json:"expectedPods"`
} `json:"status"`
}
func (p Pdb) match(labels map[string]string) bool {
for k, v := range p.Spec.Selector.MatchLabels {
if labels[k] != v {
return false
}
}
return true
}
// RetrievePdbs executes kubectl get pdb command
func RetrievePdbs() []Pdb {
cmd := "kubectl get pdb --all-namespaces -o json"
out, err := exec.Command("bash", "-c", cmd).CombinedOutput()
if err != nil {
log.Fatalf("Failed to execute command: %s", cmd)
}
json := string(out)
return buildPdbItems(json).Items
}
func buildPdbItems(str string) (pdbs PdbItems) {
err := json.Unmarshal([]byte(str), &pdbs)
if err != nil {
log.Fatalf("%+v", err)
}
return pdbs
}