/
run.go
482 lines (426 loc) · 14.7 KB
/
run.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Copyright 2024 Upbound Inc
// Copyright 2014-2024 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Please note: As of March 2023, the `upbound` commands have been disabled.
// We're keeping the code here for now, so they're easily resurrected.
// The upbound commands were meant to support the Upbound self-hosted option.
package query
import (
"context"
"fmt"
"strings"
"github.com/alecthomas/kong"
"github.com/pterm/pterm"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
kruntime "k8s.io/apimachinery/pkg/runtime"
runtimeschema "k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/json"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/printers"
cliresource "k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/kubectl/pkg/cmd/get"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"
queryv1alpha1 "github.com/upbound/up-sdk-go/apis/query/v1alpha1"
"github.com/upbound/up/cmd/up/query/resource"
"github.com/upbound/up/internal/upbound"
)
// afterApply constructs and binds Upbound-specific context to any subcommands
// that have Run() methods that receive it.
func (c *cmd) afterApply() error {
if c.ShowLabels && c.OutputFormat != "" && c.OutputFormat != "wide" {
return fmt.Errorf("--show-labels option cannot be used with %s printer", c.OutputFormat)
}
c.printFlags = get.NewGetPrintFlags()
c.printFlags.NoHeaders = &c.NoHeaders
c.printFlags.OutputFormat = ptr.To(strings.TrimPrefix(c.OutputFormat, "="))
c.printFlags.CustomColumnsFlags.NoHeaders = c.NoHeaders
c.printFlags.HumanReadableFlags.ColumnLabels = &c.ColumnLabels
c.printFlags.HumanReadableFlags.ShowKind = &c.ShowKind
c.printFlags.HumanReadableFlags.ShowLabels = &c.ShowLabels
c.printFlags.HumanReadableFlags.SortBy = &c.SortBy
c.printFlags.TemplateFlags.TemplateArgument = &c.Template
c.printFlags.TemplateFlags.AllowMissingKeys = &c.AllowMissingKeys
c.printFlags.JSONYamlPrintFlags.ShowManagedFields = c.ShowManagedFields
return nil
}
func (c *cmd) Run(ctx context.Context, kongCtx *kong.Context, upCtx *upbound.Context, queryTemplate resource.QueryObject, kubeconfig *rest.Config, notFound NotFound) error { // nolint:gocyclo // mostly taken from kubectl get. We don't want to divert.
tgns, errs := ParseTypesAndNames(c.Resources...)
if len(errs) > 0 {
return kerrors.NewAggregate(errs)
}
gkNames, categoryNames := SplitGroupKindAndCategories(tgns)
if upCtx.WrapTransport != nil {
kubeconfig.Wrap(upCtx.WrapTransport)
}
kc, err := client.New(kubeconfig, client.Options{Scheme: queryScheme})
if err != nil {
return fmt.Errorf("failed to create client: %w", err)
}
// create queries
var querySpecs []*queryv1alpha1.QuerySpec
for gk, names := range gkNames {
if len(names) == 0 {
query := createQuerySpec(types.NamespacedName{Namespace: c.namespace}, gk, nil, c.OutputFormat)
querySpecs = append(querySpecs, query)
continue
}
for _, name := range names {
query := createQuerySpec(types.NamespacedName{Namespace: c.namespace, Name: name}, gk, nil, c.OutputFormat)
querySpecs = append(querySpecs, query)
}
}
for cat, names := range categoryNames {
catList := []string{cat}
if cat == AllCategory {
catList = nil
}
if len(names) == 0 {
query := createQuerySpec(types.NamespacedName{Namespace: c.namespace}, metav1.GroupKind{}, catList, c.OutputFormat)
querySpecs = append(querySpecs, query)
continue
}
for _, name := range names {
query := createQuerySpec(types.NamespacedName{Namespace: c.namespace, Name: name}, metav1.GroupKind{}, catList, c.OutputFormat)
querySpecs = append(querySpecs, query)
}
}
// send queries and collect objects
var infos []*cliresource.Info
gks := sets.New[runtimeschema.GroupKind]()
for _, spec := range querySpecs {
var cursor string
var page int
for {
spec := spec.DeepCopy()
spec.QueryTopLevelResources.QueryResources.Page.Cursor = cursor
query := queryTemplate.DeepCopyQueryObject().SetSpec(spec)
// print query for debugging
if c.Flags.Debug > 0 {
kinds, _, err := queryScheme.ObjectKinds(query)
if err != nil {
return fmt.Errorf("failed to get object kinds: %w", err)
}
if len(kinds) != 1 {
return fmt.Errorf("expected exactly one kind, got %d", len(kinds))
}
query := query.DeepCopyQueryObject()
query.GetObjectKind().SetGroupVersionKind(queryv1alpha1.SchemeGroupVersion.WithKind(kinds[0].Kind))
bs, err := yaml.Marshal(query)
if err != nil {
return fmt.Errorf("failed to marshal query: %w", err)
}
fmt.Fprintf(kongCtx.Stderr, "Sending query:\n\n%s\n", string(bs)) // nolint:errcheck // just debug output
}
// send request
if err := kc.Create(ctx, query); err != nil {
return fmt.Errorf("SpaceQuery request failed: %w", err)
}
resp := query.GetResponse()
for _, w := range resp.Warnings {
pterm.Warning.Printfln("Warning: %s", w)
}
// collect objects
for _, obj := range resp.Objects {
if obj.Object == nil {
return fmt.Errorf("received unexpected nil object in response")
}
u := &unstructured.Unstructured{Object: obj.Object.Object}
infos = append(infos, &cliresource.Info{
Client: nil,
Mapping: &meta.RESTMapping{
Resource: runtimeschema.GroupVersionResource{
Group: u.GroupVersionKind().Group,
},
GroupVersionKind: u.GroupVersionKind(),
Scope: RESTScopeNameFunc(u.GetNamespace()),
},
Namespace: u.GetNamespace(),
Name: u.GetName(),
Source: types.NamespacedName{Namespace: obj.ControlPlane.Namespace, Name: obj.ControlPlane.Name}.String(),
Object: u,
ResourceVersion: u.GetResourceVersion(),
})
gks.Insert(u.GroupVersionKind().GroupKind())
}
// do paging
cursor = resp.Cursor.Next
page++
if cursor == "" {
break
}
if c.Flags.Debug > 0 {
fmt.Fprintf(kongCtx.Stderr, "Fetching page %d\n", page) // nolint:errcheck // just debug output
}
}
}
// print objects
showKind := c.ShowKind || gks.Len() > 1 || len(categoryNames)+len(gkNames) > 1
humanReadableOutput := (c.OutputFormat == "" && c.Template == "") || c.OutputFormat == "wide"
if humanReadableOutput {
return c.humanReadablePrintObjects(kongCtx, infos, showKind, notFound)
}
return c.printGeneric(kongCtx, infos)
}
func (c *cmd) humanReadablePrintObjects(kongCtx *kong.Context, infos []*cliresource.Info, printWithKind bool, notFound NotFound) error { // nolint:gocyclo // mostly taken from kubectl get. We don't want to divert.
objs := make([]kruntime.Object, len(infos))
for i, info := range infos {
objs[i] = info.Object
}
var positioner get.OriginalPositioner
if len(c.SortBy) > 0 {
robjs := make([]kruntime.Object, len(objs))
copy(robjs, objs)
sorter := get.NewRuntimeSorter(robjs, c.SortBy)
if err := sorter.Sort(); err != nil {
return err
}
positioner = sorter
}
trackingWriter := &trackingWriterWrapper{Delegate: kongCtx.Stdout} // track if we write any output
separatorWriter := &separatorWriterWrapper{Delegate: trackingWriter} // output an empty line separating output
w := printers.GetNewTabWriter(separatorWriter)
var (
allErrs []error
errs = sets.NewString()
printer printers.ResourcePrinter
lastMapping *meta.RESTMapping
allResourcesNamespaced = c.namespace != ""
)
for ix := range objs {
obj := objs[ix]
var mapping *meta.RESTMapping
var info *cliresource.Info
if positioner != nil {
info = infos[positioner.OriginalPosition(ix)]
mapping = info.Mapping
} else {
info = infos[ix]
mapping = info.Mapping
}
allResourcesNamespaced = allResourcesNamespaced && info.Namespace != ""
printWithNamespace := c.namespace == ""
if mapping != nil && mapping.Scope.Name() == meta.RESTScopeNameRoot {
printWithNamespace = false
}
if shouldGetNewPrinterForMapping(printer, lastMapping, mapping) {
if err := w.Flush(); err != nil {
return err
}
w.SetRememberedWidths(nil)
// add linebreaks between resource groups (if there is more than one)
// when it satisfies all following 3 conditions:
// 1) it's not the first resource group
// 2) it has row header
// 3) we've written output since the last time we started a new set of headers
if lastMapping != nil && !c.NoHeaders && trackingWriter.Written > 0 {
separatorWriter.SetReady(true)
}
var err error
printer, err = c.createPrinter(mapping, printWithNamespace, printWithKind)
if err != nil {
if !errs.Has(err.Error()) {
errs.Insert(err.Error())
allErrs = append(allErrs, err)
}
continue
}
lastMapping = mapping
}
if err := printer.PrintObj(obj, w); err != nil {
return err
}
}
if err := w.Flush(); err != nil {
return err
}
if trackingWriter.Written == 0 && len(allErrs) == 0 {
if err := notFound.PrintMessage(); err != nil {
return err
}
}
return kerrors.NewAggregate(allErrs)
}
func (c *cmd) printGeneric(kongCtx *kong.Context, infos []*cliresource.Info) error { // nolint:gocyclo // mostly taken from kubectl get. We don't want to divert.
// we flattened the data from the builder, so we have individual items, but now we'd like to either:
// 1. if there is more than one item, combine them all into a single list
// 2. if there is a single item and that item is a list, leave it as its specific list
// 3. if there is a single item and it is not a list, leave it as a single item
printer, err := c.createPrinter(nil, false, false)
if err != nil {
return err
}
var obj kruntime.Object
var errs []error
if len(infos) != 1 {
// we have zero or multple items, so coerce all items into a list.
// we don't want an *unstructured.Unstructured list yet, as we
// may be dealing with non-unstructured objects. Compose all items
// into an corev1.List, and then decode using an unstructured scheme.
list := corev1.List{
TypeMeta: metav1.TypeMeta{
Kind: "List",
APIVersion: "v1",
},
ListMeta: metav1.ListMeta{},
}
for _, info := range infos {
list.Items = append(list.Items, kruntime.RawExtension{Object: info.Object})
}
listData, err := json.Marshal(list)
if err != nil {
return err
}
converted, err := kruntime.Decode(unstructured.UnstructuredJSONScheme, listData)
if err != nil {
return err
}
obj = converted
} else {
obj = infos[0].Object
}
isList := meta.IsListType(obj)
if isList {
items, err := meta.ExtractList(obj)
if err != nil {
return err
}
// take the items and create a new list for display
list := &unstructured.UnstructuredList{
Object: map[string]interface{}{
"kind": "List",
"apiVersion": "v1",
"metadata": map[string]interface{}{},
},
}
if listMeta, err := meta.ListAccessor(obj); err == nil {
list.Object["metadata"] = map[string]interface{}{
"resourceVersion": listMeta.GetResourceVersion(),
}
}
for _, item := range items {
list.Items = append(list.Items, *item.(*unstructured.Unstructured))
}
if err := printer.PrintObj(list, kongCtx.Stdout); err != nil {
errs = append(errs, err)
}
return kerrors.Reduce(kerrors.Flatten(kerrors.NewAggregate(errs)))
}
if printErr := printer.PrintObj(obj, kongCtx.Stdout); printErr != nil {
errs = append(errs, printErr)
}
return kerrors.Reduce(kerrors.Flatten(kerrors.NewAggregate(errs)))
}
func (c *cmd) createPrinter(mapping *meta.RESTMapping, withNamespace bool, withKind bool) (printers.ResourcePrinterFunc, error) { // nolint:gocyclo // mostly taken from kubectl get. We don't want to divert.
// make a new copy of current flags / opts before mutating
printFlags := c.printFlags.Copy()
if mapping != nil {
printFlags.SetKind(mapping.GroupVersionKind.GroupKind())
}
if withNamespace {
if err := printFlags.EnsureWithNamespace(); err != nil {
return nil, err
}
}
if withKind {
if err := printFlags.EnsureWithKind(); err != nil {
return nil, err
}
}
printer, err := printFlags.ToPrinter()
if err != nil {
return nil, err
}
printer, err = printers.NewTypeSetter(scheme.Scheme).WrapToPrinter(printer, nil)
if err != nil {
return nil, err
}
if len(c.SortBy) > 0 {
printer = &get.SortingPrinter{Delegate: printer, SortField: c.SortBy}
}
return printer.PrintObj, nil
}
func createQuerySpec(obj types.NamespacedName, gk metav1.GroupKind, categories []string, outputFormat string) *queryv1alpha1.QuerySpec {
// retrieve minimal schema for the given output format
var schema interface{}
switch outputFormat {
case "", "wide":
schema = map[string]interface{}{
"kind": true,
"apiVersion": true,
"metadata": map[string]interface{}{
"creationTimestamp": true,
"deletionTimestamp": true,
"name": true,
"namespace": true,
},
"status": map[string]interface{}{
"conditions": true,
},
}
case "name":
schema = map[string]interface{}{
"kind": true,
"apiVersion": true,
"metadata": map[string]interface{}{
"name": true,
"namespace": true,
},
}
default:
// TODO: would be good to have a way to exclude managed fields
schema = true // everything
}
return &queryv1alpha1.QuerySpec{
QueryTopLevelResources: queryv1alpha1.QueryTopLevelResources{
Filter: queryv1alpha1.QueryTopLevelFilter{
QueryFilter: queryv1alpha1.QueryFilter{
Namespace: obj.Namespace,
Name: obj.Name,
Categories: categories,
Group: gk.Group,
Kind: gk.Kind,
},
},
QueryResources: queryv1alpha1.QueryResources{
Limit: 500,
Cursor: true,
Objects: &queryv1alpha1.QueryObjects{
ControlPlane: true,
Object: &queryv1alpha1.JSON{
Object: schema,
},
},
},
},
}
}
func shouldGetNewPrinterForMapping(printer printers.ResourcePrinter, lastMapping, mapping *meta.RESTMapping) bool {
return printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource
}
type RESTScopeNameFunc string
func (f RESTScopeNameFunc) Name() meta.RESTScopeName {
if f == "" {
return meta.RESTScopeNameRoot
}
return meta.RESTScopeNameNamespace
}