-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
214 lines (195 loc) · 5.25 KB
/
get.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cmd
import (
"context"
"fmt"
"net/http"
"os"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation"
"k8s.io/cli-runtime/pkg/printers"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
)
var watch bool
var getCmd = &cobra.Command{
Use: "get",
Short: "list resources",
RunE: func(c *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("not enough arguments to 'get'")
}
if len(args) > 1 {
return fmt.Errorf("too many arguments to 'get'")
}
d := newDoer(args[0])
rv, err := d.get(args[0])
if err != nil {
return err
}
if watch {
return d.watch(args[0], rv)
}
return nil
},
}
func init() {
getCmd.Flags().BoolVarP(&watch, "watch", "w", false, "watch for changes")
}
type doer struct {
client dynamic.ResourceInterface
printer printers.ResourcePrinter
}
func newDoer(resource string) doer {
client := getClient(resource)
printOpts := printers.PrintOptions{WithNamespace: true}
return doer{
client: client,
printer: printers.NewTypeSetter(scheme.Scheme).ToPrinter(printers.NewTablePrinter(printOpts)),
}
}
func getClient(resource string) dynamic.ResourceInterface {
gvr := GroupVersion.WithResource(resource)
gvr, _ = mapper.ResourceFor(gvr)
resourceClient := client.Resource(gvr)
if allNamespaces {
return resourceClient
}
return resourceClient.Namespace(namespace)
}
func (d doer) get(resource string) (string, error) {
unst, err := d.client.List(context.TODO(), metav1.ListOptions{})
if err != nil {
return "", err
}
table, err := toTable(unst.UnstructuredContent())
if err != nil {
return "", err
}
rv := unst.GetResourceVersion()
if err = d.printer.PrintObj(table, os.Stdout); err != nil {
return rv, err
}
return rv, nil
}
func (d doer) watch(resource, rv string) error {
if rv == "" {
rv = "0"
}
opts := metav1.ListOptions{
ResourceVersion: rv,
}
watcher, err := d.client.Watch(context.TODO(), opts)
if err != nil {
return err
}
for event := range watcher.ResultChan() {
table, err := toTable(event.Object.(*unstructured.Unstructured).UnstructuredContent())
if err != nil {
return err
}
if err = d.printer.PrintObj(table, os.Stdout); err != nil {
return err
}
}
return nil
}
func toTable(obj map[string]interface{}) (*metav1.Table, error) {
table := &metav1.Table{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj, table)
if err != nil {
return nil, err
}
for i := range table.Rows {
row := &table.Rows[i]
if row.Object.Raw == nil || row.Object.Object != nil {
continue
}
converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, row.Object.Raw)
if err != nil {
return nil, err
}
row.Object.Object = converted
}
return table, nil
}
// endpointRestrictions implements negotiation.EndpointRestrictions.
type endpointRestrictions struct{}
// AllowsMediaTypeTransform implements negotiation.EndpointRestrictions.AllowsMediaTypeTransform.
func (e endpointRestrictions) AllowsMediaTypeTransform(mimeType string, mimeSubType string, gvk *schema.GroupVersionKind) bool {
if mimeType != "application" {
return false
}
if mimeSubType != "json" {
return false
}
if gvk == nil || gvk.Kind == "" || gvk.Kind == "Table" {
return true
}
return false
}
// AllowsServerVersion implements negotiation.EndpointRestrictions.AllowsServerVersion.
func (e endpointRestrictions) AllowsServerVersion(string) bool {
return true
}
// AllowsStreamSchema implements negotiation.EndpointRestrictions.AllowsStreamSchema.
func (e endpointRestrictions) AllowsStreamSchema(string) bool {
return true
}
type addOptions struct {
accept string
query map[string]string
next http.RoundTripper
}
// RoundTrip adds parameters to request table formatting to the request object.
func (a *addOptions) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Accept", a.accept)
q := r.URL.Query()
for k, v := range a.query {
q.Set(k, v)
}
r.URL.RawQuery = q.Encode()
return a.next.RoundTrip(r)
}
func roundTripper(mediaType negotiation.MediaTypeOptions) func(http.RoundTripper) http.RoundTripper {
accept := mediaType.Accepted.MediaType
if mediaType.Convert != nil {
if mediaType.Convert.Kind != "" {
accept += ";as=" + mediaType.Convert.Kind
}
if mediaType.Convert.Version != "" {
accept += ";v=" + mediaType.Convert.Version
}
if mediaType.Convert.Group != "" {
accept += ";g=" + mediaType.Convert.Group
}
}
return func(rt http.RoundTripper) http.RoundTripper {
ao := addOptions{
accept: accept,
next: rt,
}
if mediaType.Convert != nil && mediaType.Convert.Kind == "Table" {
ao.query = map[string]string{
"includeObject": "Object",
}
}
return &ao
}
}
func setTableMediaType(cfg *rest.Config) {
acceptedTypes := []runtime.SerializerInfo{
{
MediaType: "application/json",
MediaTypeType: "application",
MediaTypeSubType: "json",
},
}
mediaType, _ := negotiation.NegotiateMediaTypeOptions("application/json;as=Table;v=v1;g=meta.k8s.io", acceptedTypes, endpointRestrictions{})
setOptions := roundTripper(mediaType)
cfg.Wrap(setOptions)
}