refactor Obj to save more information about resources

Signed-off-by: lonelyCZ <531187475@qq.com>
This commit is contained in:
lonelyCZ 2022-03-29 19:48:12 +08:00
parent 1a35b099a4
commit 6a669fa962
2 changed files with 11 additions and 14 deletions

View File

@ -194,8 +194,7 @@ func (g *CommandGetOptions) Complete(cmd *cobra.Command, args []string) error {
// Obj cluster info // Obj cluster info
type Obj struct { type Obj struct {
Cluster string Cluster string
Infos runtime.Object Info *resource.Info
Mapping *meta.RESTMapping
} }
// RBInfo resourcebinding info and print info // RBInfo resourcebinding info and print info
@ -246,7 +245,7 @@ func (g *CommandGetOptions) Run(karmadaConfig KarmadaConfig, cmd *cobra.Command,
// sort objects by resource kind to classify them // sort objects by resource kind to classify them
sort.Slice(objs, func(i, j int) bool { 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 { if err := g.printObjs(objs, &allErrs, args); err != nil {
@ -274,7 +273,7 @@ func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []strin
w := printers.GetNewTabWriter(separatorWriter) w := printers.GetNewTabWriter(separatorWriter)
sameKind := make([]Obj, 0) sameKind := make([]Obj, 0)
for ix := range objs { for ix := range objs {
mapping := objs[ix].Mapping mapping := objs[ix].Info.Mapping
sameKind = append(sameKind, objs[ix]) sameKind = append(sameKind, objs[ix])
printWithNamespace := g.checkPrintWithNamespace(mapping) printWithNamespace := g.checkPrintWithNamespace(mapping)
@ -303,7 +302,7 @@ func (g *CommandGetOptions) printObjs(objs []Obj, allErrs *[]error, args []strin
lastMapping = mapping 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{} table := &metav1.Table{}
allTableRows, mapping, err := g.reconstructionRow(sameKind, table) allTableRows, mapping, err := g.reconstructionRow(sameKind, table)
if err != nil { if err != nil {
@ -399,8 +398,7 @@ func (g *CommandGetOptions) getObjInfo(wg *sync.WaitGroup, mux *sync.Mutex, f cm
for ix := range infos { for ix := range infos {
objInfo = Obj{ objInfo = Obj{
Cluster: cluster, Cluster: cluster,
Infos: infos[ix].Object, Info: infos[ix],
Mapping: infos[ix].Mapping,
} }
*objs = append(*objs, objInfo) *objs = append(*objs, objInfo)
} }
@ -412,8 +410,8 @@ func (g *CommandGetOptions) reconstructionRow(objs []Obj, table *metav1.Table) (
var allTableRows []metav1.TableRow var allTableRows []metav1.TableRow
var mapping *meta.RESTMapping var mapping *meta.RESTMapping
for ix := range objs { for ix := range objs {
mapping = objs[ix].Mapping mapping = objs[ix].Info.Mapping
unstr, ok := objs[ix].Infos.(*unstructured.Unstructured) unstr, ok := objs[ix].Info.Object.(*unstructured.Unstructured)
if !ok { if !ok {
return nil, nil, fmt.Errorf("attempt to decode non-Unstructured object") return nil, nil, fmt.Errorf("attempt to decode non-Unstructured object")
} }
@ -738,9 +736,9 @@ func multipleGVKsRequested(objs []Obj) bool {
if len(objs) < 2 { if len(objs) < 2 {
return false return false
} }
gvk := objs[0].Mapping.GroupVersionKind gvk := objs[0].Info.Mapping.GroupVersionKind
for _, obj := range objs { for _, obj := range objs {
if obj.Mapping.GroupVersionKind != gvk { if obj.Info.Mapping.GroupVersionKind != gvk {
return true 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) 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() opts.gvk = obj.GetObjectKind().GroupVersionKind()
@ -317,8 +317,7 @@ func (o *CommandPromoteOption) getObjInfo(f cmdutil.Factory, cluster string, arg
obj := &Obj{ obj := &Obj{
Cluster: cluster, Cluster: cluster,
Infos: infos[0].Object, Info: infos[0],
Mapping: infos[0].Mapping,
} }
return obj, nil return obj, nil