-
Notifications
You must be signed in to change notification settings - Fork 683
/
main.go
90 lines (77 loc) · 1.86 KB
/
main.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
package kubestatus
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/spf13/cobra"
"github.com/datawire/ambassador/pkg/k8s"
)
func Main() {
var st = &cobra.Command{
Use: "kubestatus <kind>",
Short: "get and set status of kubernetes resources",
Args: cobra.ExactArgs(1),
SilenceErrors: true,
SilenceUsage: true,
}
info := k8s.NewKubeInfoFromFlags(st.Flags())
fields := st.Flags().StringP("field-selector", "f", "", "field selector")
labels := st.Flags().StringP("label-selector", "l", "", "label selector")
statusFile := st.Flags().StringP("update", "u", "", "update with new status from file (must be json)")
st.RunE = func(cmd *cobra.Command, args []string) error {
var status map[string]interface{}
if *statusFile != "" {
rawStatus, err := os.Open(*statusFile)
if err != nil {
return err
}
defer rawStatus.Close()
dec := json.NewDecoder(rawStatus)
err = dec.Decode(&status)
if err != nil {
return err
}
}
kind := args[0]
namespace, err := info.Namespace()
if err != nil {
return err
}
w := k8s.MustNewWatcher(info)
err = w.WatchQuery(k8s.Query{
Kind: kind,
Namespace: namespace,
FieldSelector: *fields,
LabelSelector: *labels,
}, func(w *k8s.Watcher) {
for _, rsrc := range w.List(kind) {
if *statusFile == "" {
fmt.Println("Status of", rsrc.QName())
fmt.Printf(" %v\n", rsrc["status"])
} else {
fmt.Println("Updating", rsrc.QName())
rsrc["status"] = status
_, err := w.UpdateStatus(rsrc)
if err != nil {
// log.Debugf doesn't exist.
if false {
log.Printf("error updating resource: %v", err)
}
}
}
}
w.Stop()
})
if err != nil {
return err
}
w.Wait()
return nil
}
err := st.Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}