Merge pull request #1559 from lonelyCZ/pr-get-output

Add output information when no resource is found
This commit is contained in:
karmada-bot 2022-03-30 09:02:00 +08:00 committed by GitHub
commit 989f3fe8fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 24 deletions

View File

@ -194,8 +194,7 @@ func (g *CommandGetOptions) Complete(cmd *cobra.Command, args []string) error {
// Obj cluster info
type Obj struct {
Cluster string
Infos runtime.Object
Mapping *meta.RESTMapping
Info *resource.Info
}
// RBInfo resourcebinding info and print info
@ -246,18 +245,16 @@ func (g *CommandGetOptions) Run(karmadaConfig KarmadaConfig, cmd *cobra.Command,
// sort objects by resource kind to classify them
sort.Slice(objs, func(i, j int) bool {
return objs[i].Mapping.Resource.String() < objs[j].Mapping.Resource.String()
return objs[i].Info.Mapping.Resource.String() < objs[j].Info.Mapping.Resource.String()
})
if err := g.printObjs(objs, &allErrs, args); err != nil {
return err
}
g.printObjs(objs, &allErrs, args)
return utilerrors.NewAggregate(allErrs)
}
// printObjs print objects in multi clusters
func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []string) error {
func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []string) {
var err error
errs := sets.NewString()
@ -272,11 +269,13 @@ func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []strin
separatorWriter := &separatorWriterWrapper{Delegate: trackingWriter}
w := printers.GetNewTabWriter(separatorWriter)
allResourcesNamespaced := !g.AllNamespaces
sameKind := make([]Obj, 0)
for ix := range objs {
mapping := objs[ix].Mapping
mapping := objs[ix].Info.Mapping
sameKind = append(sameKind, objs[ix])
allResourcesNamespaced = allResourcesNamespaced && objs[ix].Info.Namespaced()
printWithNamespace := g.checkPrintWithNamespace(mapping)
if shouldGetNewPrinterForMapping(printer, lastMapping, mapping) {
@ -303,11 +302,12 @@ func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []strin
lastMapping = mapping
}
if ix == len(objs)-1 || objs[ix].Mapping.Resource != objs[ix+1].Mapping.Resource {
if ix == len(objs)-1 || objs[ix].Info.Mapping.Resource != objs[ix+1].Info.Mapping.Resource {
table := &metav1.Table{}
allTableRows, mapping, err := g.reconstructionRow(sameKind, table)
if err != nil {
return err
*allErrs = append(*allErrs, err)
return
}
table.Rows = allTableRows
@ -316,21 +316,33 @@ func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []strin
printObj, err := helper.ToUnstructured(table)
if err != nil {
return err
*allErrs = append(*allErrs, err)
return
}
err = printer.PrintObj(printObj, w)
if err != nil {
return err
*allErrs = append(*allErrs, err)
return
}
sameKind = make([]Obj, 0)
}
}
w.Flush()
return nil
g.printIfNotFindResource(trackingWriter.Written, allErrs, allResourcesNamespaced)
}
// printIfNotFindResource is sure we output something if we wrote no output, and had no errors, and are not ignoring NotFound
func (g *CommandGetOptions) printIfNotFindResource(written int, allErrs *[]error, allResourcesNamespaced bool) {
if written == 0 && !g.IgnoreNotFound && len(*allErrs) == 0 {
if allResourcesNamespaced {
fmt.Fprintf(g.ErrOut, "No resources found in %s namespace.\n", g.Namespace)
} else {
fmt.Fprintln(g.ErrOut, "No resources found")
}
}
}
// checkPrintWithNamespace check if print objects with namespace
@ -374,7 +386,9 @@ func (g *CommandGetOptions) getObjInfo(wg *sync.WaitGroup, mux *sync.Mutex, f cm
TransformRequests(g.transformRequests).
Do()
r.IgnoreErrors(apierrors.IsNotFound)
if g.IgnoreNotFound {
r.IgnoreErrors(apierrors.IsNotFound)
}
if err := r.Err(); err != nil {
*allErrs = append(*allErrs, fmt.Errorf("cluster(%s): %s", cluster, err))
@ -399,8 +413,7 @@ func (g *CommandGetOptions) getObjInfo(wg *sync.WaitGroup, mux *sync.Mutex, f cm
for ix := range infos {
objInfo = Obj{
Cluster: cluster,
Infos: infos[ix].Object,
Mapping: infos[ix].Mapping,
Info: infos[ix],
}
*objs = append(*objs, objInfo)
}
@ -412,8 +425,8 @@ func (g *CommandGetOptions) reconstructionRow(objs []Obj, table *metav1.Table) (
var allTableRows []metav1.TableRow
var mapping *meta.RESTMapping
for ix := range objs {
mapping = objs[ix].Mapping
unstr, ok := objs[ix].Infos.(*unstructured.Unstructured)
mapping = objs[ix].Info.Mapping
unstr, ok := objs[ix].Info.Object.(*unstructured.Unstructured)
if !ok {
return nil, nil, fmt.Errorf("attempt to decode non-Unstructured object")
}
@ -738,9 +751,9 @@ func multipleGVKsRequested(objs []Obj) bool {
if len(objs) < 2 {
return false
}
gvk := objs[0].Mapping.GroupVersionKind
gvk := objs[0].Info.Mapping.GroupVersionKind
for _, obj := range objs {
if obj.Mapping.GroupVersionKind != gvk {
if obj.Info.Mapping.GroupVersionKind != gvk {
return true
}
}

View File

@ -222,7 +222,7 @@ func RunPromote(_ io.Writer, karmadaConfig KarmadaConfig, opts CommandPromoteOpt
return fmt.Errorf("failed to get resource in cluster(%s). err: %v", opts.Cluster, err)
}
obj := objInfo.Infos.(*unstructured.Unstructured)
obj := objInfo.Info.Object.(*unstructured.Unstructured)
opts.gvk = obj.GetObjectKind().GroupVersionKind()
@ -317,8 +317,7 @@ func (o *CommandPromoteOption) getObjInfo(f cmdutil.Factory, cluster string, arg
obj := &Obj{
Cluster: cluster,
Infos: infos[0].Object,
Mapping: infos[0].Mapping,
Info: infos[0],
}
return obj, nil