Merge pull request #3910 from XiShanYongYe-Chang/karmadactl-get-without-rb-info
[karmadactl get] judge with label on the resources determine whether resource is adopted by Karmada
This commit is contained in:
commit
27eb29cd31
|
@ -17,7 +17,6 @@ import (
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
|
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
|
||||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
|
@ -26,19 +25,19 @@ import (
|
||||||
"k8s.io/cli-runtime/pkg/resource"
|
"k8s.io/cli-runtime/pkg/resource"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
watchtools "k8s.io/client-go/tools/watch"
|
watchtools "k8s.io/client-go/tools/watch"
|
||||||
|
"k8s.io/klog/v2"
|
||||||
"k8s.io/kubectl/pkg/cmd/get"
|
"k8s.io/kubectl/pkg/cmd/get"
|
||||||
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
cmdutil "k8s.io/kubectl/pkg/cmd/util"
|
||||||
"k8s.io/kubectl/pkg/util/interrupt"
|
"k8s.io/kubectl/pkg/util/interrupt"
|
||||||
"k8s.io/kubectl/pkg/util/templates"
|
"k8s.io/kubectl/pkg/util/templates"
|
||||||
utilpointer "k8s.io/utils/pointer"
|
utilpointer "k8s.io/utils/pointer"
|
||||||
|
|
||||||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
|
|
||||||
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
|
karmadaclientset "github.com/karmada-io/karmada/pkg/generated/clientset/versioned"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
"github.com/karmada-io/karmada/pkg/karmadactl/options"
|
||||||
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
"github.com/karmada-io/karmada/pkg/karmadactl/util"
|
||||||
|
karmadautil "github.com/karmada-io/karmada/pkg/util"
|
||||||
"github.com/karmada-io/karmada/pkg/util/gclient"
|
"github.com/karmada-io/karmada/pkg/util/gclient"
|
||||||
"github.com/karmada-io/karmada/pkg/util/helper"
|
"github.com/karmada-io/karmada/pkg/util/helper"
|
||||||
"github.com/karmada-io/karmada/pkg/util/names"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -285,14 +284,6 @@ type WatchObj struct {
|
||||||
r *resource.Result
|
r *resource.Result
|
||||||
}
|
}
|
||||||
|
|
||||||
// RBInfo resourcebinding info and print info
|
|
||||||
var RBInfo map[string]*OtherPrint
|
|
||||||
|
|
||||||
// OtherPrint applied is used in the display column
|
|
||||||
type OtherPrint struct {
|
|
||||||
Applied interface{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run performs the get operation.
|
// Run performs the get operation.
|
||||||
func (g *CommandGetOptions) Run(f util.Factory, cmd *cobra.Command, args []string) error {
|
func (g *CommandGetOptions) Run(f util.Factory, cmd *cobra.Command, args []string) error {
|
||||||
mux := sync.Mutex{}
|
mux := sync.Mutex{}
|
||||||
|
@ -311,12 +302,6 @@ func (g *CommandGetOptions) Run(f util.Factory, cmd *cobra.Command, args []strin
|
||||||
g.ServerPrint = false
|
g.ServerPrint = false
|
||||||
}
|
}
|
||||||
|
|
||||||
RBInfo = make(map[string]*OtherPrint)
|
|
||||||
|
|
||||||
if err := g.getRBInKarmada(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(g.Clusters) == 0 {
|
if len(g.Clusters) == 0 {
|
||||||
clusterList, err := g.karmadaClient.ClusterV1alpha1().Clusters().List(context.TODO(), metav1.ListOptions{})
|
clusterList, err := g.karmadaClient.ClusterV1alpha1().Clusters().List(context.TODO(), metav1.ListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -548,16 +533,25 @@ func (g *CommandGetOptions) reconstructionRow(objs []Obj, table *metav1.Table) (
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
for rowIdx := range table.Rows {
|
for rowIdx := range table.Rows {
|
||||||
var tempRow metav1.TableRow
|
var cells []interface{}
|
||||||
rbKey := getRBKey(mapping.GroupVersionKind, table.Rows[rowIdx], objs[ix].Cluster)
|
cells = append(cells, table.Rows[rowIdx].Cells[0])
|
||||||
|
cells = append(cells, objs[ix].Cluster)
|
||||||
|
cells = append(cells, table.Rows[rowIdx].Cells[1:]...)
|
||||||
|
table.Rows[rowIdx].Cells = cells
|
||||||
|
|
||||||
tempRow.Cells = append(append(tempRow.Cells, table.Rows[rowIdx].Cells[0], objs[ix].Cluster), table.Rows[rowIdx].Cells[1:]...)
|
unObj := &unstructured.Unstructured{}
|
||||||
if _, ok := RBInfo[rbKey]; ok {
|
err := unObj.UnmarshalJSON(table.Rows[rowIdx].Object.Raw)
|
||||||
tempRow.Cells = append(tempRow.Cells, "Y")
|
if err != nil {
|
||||||
} else {
|
klog.Errorf("Failed to unmarshal unObj, error is: %v", err)
|
||||||
tempRow.Cells = append(tempRow.Cells, "N")
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
v, exist := unObj.GetLabels()[karmadautil.ManagedByKarmadaLabel]
|
||||||
|
if exist && v == karmadautil.ManagedByKarmadaLabelValue {
|
||||||
|
table.Rows[rowIdx].Cells = append(table.Rows[rowIdx].Cells, "Y")
|
||||||
|
} else {
|
||||||
|
table.Rows[rowIdx].Cells = append(table.Rows[rowIdx].Cells, "N")
|
||||||
}
|
}
|
||||||
table.Rows[rowIdx].Cells = tempRow.Cells
|
|
||||||
}
|
}
|
||||||
allTableRows = append(allTableRows, table.Rows...)
|
allTableRows = append(allTableRows, table.Rows...)
|
||||||
}
|
}
|
||||||
|
@ -578,20 +572,26 @@ func (g *CommandGetOptions) reconstructObj(obj runtime.Object, mapping *meta.RES
|
||||||
}
|
}
|
||||||
|
|
||||||
for rowIdx := range table.Rows {
|
for rowIdx := range table.Rows {
|
||||||
var tempRow metav1.TableRow
|
var cells []interface{}
|
||||||
rbKey := getRBKey(mapping.GroupVersionKind, table.Rows[rowIdx], cluster)
|
|
||||||
|
|
||||||
if g.OutputWatchEvents {
|
if g.OutputWatchEvents {
|
||||||
tempRow.Cells = append(append(tempRow.Cells, event, table.Rows[rowIdx].Cells[0], cluster), table.Rows[rowIdx].Cells[1:]...)
|
cells = append(append(cells, event, table.Rows[rowIdx].Cells[0], cluster), table.Rows[rowIdx].Cells[1:]...)
|
||||||
} else {
|
} else {
|
||||||
tempRow.Cells = append(append(tempRow.Cells, table.Rows[rowIdx].Cells[0], cluster), table.Rows[rowIdx].Cells[1:]...)
|
cells = append(append(cells, table.Rows[rowIdx].Cells[0], cluster), table.Rows[rowIdx].Cells[1:]...)
|
||||||
}
|
}
|
||||||
if _, ok := RBInfo[rbKey]; ok {
|
table.Rows[rowIdx].Cells = cells
|
||||||
tempRow.Cells = append(tempRow.Cells, "Y")
|
|
||||||
|
unObj := &unstructured.Unstructured{}
|
||||||
|
err := unObj.UnmarshalJSON(table.Rows[rowIdx].Object.Raw)
|
||||||
|
if err != nil {
|
||||||
|
klog.Errorf("Failed to unmarshal unObj, error is: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
v, exist := unObj.GetLabels()[karmadautil.ManagedByKarmadaLabel]
|
||||||
|
if exist && v == karmadautil.ManagedByKarmadaLabelValue {
|
||||||
|
table.Rows[rowIdx].Cells = append(table.Rows[rowIdx].Cells, "Y")
|
||||||
} else {
|
} else {
|
||||||
tempRow.Cells = append(tempRow.Cells, "N")
|
table.Rows[rowIdx].Cells = append(table.Rows[rowIdx].Cells, "N")
|
||||||
}
|
}
|
||||||
table.Rows[rowIdx].Cells = tempRow.Cells
|
|
||||||
}
|
}
|
||||||
allTableRows = append(allTableRows, table.Rows...)
|
allTableRows = append(allTableRows, table.Rows...)
|
||||||
|
|
||||||
|
@ -896,58 +896,6 @@ func (g *CommandGetOptions) transformRequests(req *rest.Request) {
|
||||||
}, ","))
|
}, ","))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *CommandGetOptions) getRBInKarmada() error {
|
|
||||||
var rbList *workv1alpha2.ResourceBindingList
|
|
||||||
var crbList *workv1alpha2.ClusterResourceBindingList
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if !g.AllNamespaces {
|
|
||||||
rbList, err = g.karmadaClient.WorkV1alpha2().ResourceBindings(*options.DefaultConfigFlags.Namespace).List(context.TODO(), metav1.ListOptions{})
|
|
||||||
} else {
|
|
||||||
rbList, err = g.karmadaClient.WorkV1alpha2().ResourceBindings(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if crbList, err = g.karmadaClient.WorkV1alpha2().ClusterResourceBindings().List(context.TODO(), metav1.ListOptions{}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for idx := range rbList.Items {
|
|
||||||
rbKey := rbList.Items[idx].GetName()
|
|
||||||
val := rbList.Items[idx].Status.AggregatedStatus
|
|
||||||
for i := range val {
|
|
||||||
if val[i].Applied && val[i].ClusterName != "" {
|
|
||||||
newRBKey := fmt.Sprintf("%s-%s", val[i].ClusterName, rbKey)
|
|
||||||
RBInfo[newRBKey] = &OtherPrint{
|
|
||||||
Applied: val[i].Applied,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for idx := range crbList.Items {
|
|
||||||
rbKey := crbList.Items[idx].GetName()
|
|
||||||
val := crbList.Items[idx].Status.AggregatedStatus
|
|
||||||
for i := range val {
|
|
||||||
if val[i].Applied && val[i].ClusterName != "" {
|
|
||||||
newRBKey := fmt.Sprintf("%s-%s", val[i].ClusterName, rbKey)
|
|
||||||
RBInfo[newRBKey] = &OtherPrint{
|
|
||||||
Applied: val[i].Applied,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getRBKey(gvk schema.GroupVersionKind, row metav1.TableRow, cluster string) string {
|
|
||||||
resourceName, _ := row.Cells[0].(string)
|
|
||||||
rbKey := names.GenerateBindingName(gvk.Kind, resourceName)
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s-%s", cluster, rbKey)
|
|
||||||
}
|
|
||||||
|
|
||||||
func multipleGVKsRequested(objs []Obj) bool {
|
func multipleGVKsRequested(objs []Obj) bool {
|
||||||
if len(objs) < 2 {
|
if len(objs) < 2 {
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in New Issue