-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
executable file
·112 lines (93 loc) · 2.7 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/http/httputil"
"os"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"
)
// Tracer implements http.RoundTripper. It prints each request and
// response/error to os.Stderr. WARNING: this may output sensitive information
// including bearer tokens.
type Tracer struct {
http.RoundTripper
}
// RoundTrip calls the nested RoundTripper while printing each request and
// response/error to os.Stderr on either side of the nested call. WARNING: this
// may output sensitive information including bearer tokens.
func (t *Tracer) RoundTrip(req *http.Request) (*http.Response, error) {
// Dump the request to os.Stderr.
b, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
os.Stderr.Write(b)
os.Stderr.Write([]byte{'\n'})
// Call the nested RoundTripper.
resp, err := t.RoundTripper.RoundTrip(req)
// If an error was returned, dump it to os.Stderr.
if err != nil {
fmt.Fprintln(os.Stderr, err)
return resp, err
}
// Dump the response to os.Stderr.
b, err = httputil.DumpResponse(resp, req.URL.Query().Get("watch") != "true")
if err != nil {
return nil, err
}
os.Stderr.Write(b)
os.Stderr.Write([]byte{'\n'})
return resp, err
}
// go run main.go -namespace kube-system
func main() {
defaultKubeconfig := os.Getenv(clientcmd.RecommendedConfigPathEnvVar)
if len(defaultKubeconfig) == 0 {
defaultKubeconfig = clientcmd.RecommendedHomeFile
}
kubeconfig := flag.String(clientcmd.RecommendedConfigPathFlag,
defaultKubeconfig, "absolute path to the kubeconfig file")
namespace := flag.String("namespace", metav1.NamespaceDefault,
"create the deployment in this namespace")
verbose := flag.Bool("verbose", false, "display HTTP calls")
flag.Parse()
rc, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
if *verbose {
// wrap the default RoundTripper with an instance of Tracer.
rc.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return &Tracer{rt}
}
}
// create a new dynamic client using the rest.Config
dc, err := dynamic.NewForConfig(rc)
if err != nil {
panic(err.Error())
}
// identify pods resource
gvr := schema.GroupVersionResource{
Version: "v1",
Resource: "pods",
}
// list all pods in the specified namespace
res, err := dc.Resource(gvr).
Namespace(*namespace).
List(context.TODO(), metav1.ListOptions{})
if err != nil {
if !errors.IsNotFound(err) {
panic(err)
}
}
// for each pod, print just the name
for _, el := range res.Items {
fmt.Printf("%v\n", el.GetName())
}
}