Skip to content

Commit

Permalink
add new print format for get cluster/clusterset/work
Browse files Browse the repository at this point in the history
Signed-off-by: ycyaoxdu <[email protected]>
  • Loading branch information
ycyaoxdu committed Oct 12, 2022
1 parent cfc493b commit 1d4e300
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 109 deletions.
2 changes: 2 additions & 0 deletions pkg/cmd/get/cluster/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream

cmd.Flags().StringVar(&o.Clusterset, "clusterset", "", "ClusterSet of the clusters")

o.printer.AddFlag(cmd.Flags())

return cmd
}
58 changes: 45 additions & 13 deletions pkg/cmd/get/cluster/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterapiv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/clusteradm/pkg/helpers/printer"
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
o.printer.Competele()

return nil
}

func (o *Options) validate(args []string) (err error) {
if len(args) != 0 {
return fmt.Errorf("there should be no argument")
}

err = o.printer.Validate()
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -49,12 +58,30 @@ func (o *Options) run() (err error) {
return err
}

table := converToTable(clusters)
o.printer.WithTreeConverter(convertToTree).WithTableConverter(converToTable)

return o.printer.PrintObj(table, o.Streams.Out)
return o.printer.Print(o.Streams, clusters)
}

func converToTable(clusters *clusterapiv1.ManagedClusterList) *metav1.Table {
func convertToTree(obj runtime.Object, tree *printer.TreePrinter) *printer.TreePrinter {
if mclList, ok := obj.(*clusterapiv1.ManagedClusterList); ok {
for _, cluster := range mclList.Items {
accepted, available, version, cpu, memory, clusterset := getFileds(cluster)
mp := make(map[string]interface{})
mp[".Accepted"] = accepted
mp[".Available"] = available
mp[".ClusterSet"] = clusterset
mp[".KubernetesVersion"] = version
mp[".Capacity.Cpu"] = cpu
mp[".Capacity.Memory"] = memory

tree.AddFileds(cluster.Name, &mp)
}
}
return tree
}

func converToTable(obj runtime.Object) *metav1.Table {
table := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
Expand All @@ -68,16 +95,24 @@ func converToTable(clusters *clusterapiv1.ManagedClusterList) *metav1.Table {
Rows: []metav1.TableRow{},
}

for _, cluster := range clusters.Items {
row := convertRow(cluster)
table.Rows = append(table.Rows, row)
}
if mclList, ok := obj.(*clusterapiv1.ManagedClusterList); ok {
for _, cluster := range mclList.Items {
accepted, available, version, cpu, memory, clusterset := getFileds(cluster)
row := metav1.TableRow{
Cells: []interface{}{cluster.Name, accepted, available, clusterset, cpu, memory, version},
Object: runtime.RawExtension{Object: &cluster},
}

table.Rows = append(table.Rows, row)
}
}
return table
}

func convertRow(cluster clusterapiv1.ManagedCluster) metav1.TableRow {
var available, cpu, memory, clusterset string
func getFileds(cluster clusterapiv1.ManagedCluster) (accepted bool, available, version, cpu, memory, clusterset string) {
accepted = cluster.Spec.HubAcceptsClient

version = cluster.Status.Version.Kubernetes

availableCond := meta.FindStatusCondition(cluster.Status.Conditions, clusterapiv1.ManagedClusterConditionAvailable)
if availableCond != nil {
Expand All @@ -96,8 +131,5 @@ func convertRow(cluster clusterapiv1.ManagedCluster) metav1.TableRow {
clusterset = cluster.Labels["cluster.open-cluster-management.io/clusterset"]
}

return metav1.TableRow{
Cells: []interface{}{cluster.Name, cluster.Spec.HubAcceptsClient, available, clusterset, cpu, memory, cluster.Status.Version.Kubernetes},
Object: runtime.RawExtension{Object: &cluster},
}
return
}
33 changes: 18 additions & 15 deletions pkg/cmd/get/cluster/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/helpers/printer"
)

type Options struct {
Expand All @@ -16,26 +17,28 @@ type Options struct {

Streams genericclioptions.IOStreams

printer printers.ResourcePrinter
printer *printer.PrinterOption
}

func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options {
return &Options{
ClusteradmFlags: clusteradmFlags,
Streams: streams,
printer: printers.NewTablePrinter(printers.PrintOptions{
NoHeaders: false,
WithNamespace: false,
WithKind: false,
Wide: false,
ShowLabels: false,
Kind: schema.GroupKind{
Group: "cluster.open-cluster-management.io",
Kind: "ManagedCluster",
},
ColumnLabels: []string{},
SortBy: "",
AllowMissingKeys: true,
}),
printer: printer.NewPrinterOption(pntOpt),
}
}

var pntOpt = printers.PrintOptions{
NoHeaders: false,
WithNamespace: false,
WithKind: false,
Wide: false,
ShowLabels: false,
Kind: schema.GroupKind{
Group: "cluster.open-cluster-management.io",
Kind: "ManagedCluster",
},
ColumnLabels: []string{},
SortBy: "",
AllowMissingKeys: true,
}
2 changes: 2 additions & 0 deletions pkg/cmd/get/clusterset/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
},
}

o.printer.AddFlag(cmd.Flags())

return cmd
}
89 changes: 64 additions & 25 deletions pkg/cmd/get/clusterset/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,54 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
clusterapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
"open-cluster-management.io/clusteradm/pkg/helpers/printer"
)

func (o *Options) complete(cmd *cobra.Command, args []string) (err error) {
restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
clusterClient, err := clusterclientset.NewForConfig(restConfig)
if err != nil {
return err
}
o.Client = clusterClient

o.printer.Competele()

return nil
}

func (o *Options) validate(args []string) (err error) {
if len(args) != 0 {
return fmt.Errorf("there should be no argument")
}
return nil
}

func (o *Options) run() (err error) {
restConfig, err := o.ClusteradmFlags.KubectlFactory.ToRESTConfig()
if err != nil {
return err
}
clusterClient, err := clusterclientset.NewForConfig(restConfig)
err = o.printer.Validate()
if err != nil {
return err
}

clustersets, err := clusterClient.ClusterV1beta1().ManagedClusterSets().List(context.TODO(), metav1.ListOptions{})
return nil
}

func (o *Options) run() (err error) {
clustersets, err := o.Client.ClusterV1beta1().ManagedClusterSets().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
}

bindingMap := map[string][]string{}
o.printer.WithTreeConverter(o.convertToTree).WithTableConverter(o.converToTable)

return o.printer.Print(o.Streams, clustersets)
}

bindings, err := clusterClient.ClusterV1beta1().ManagedClusterSetBindings(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
func (o *Options) convertToTree(obj runtime.Object, tree *printer.TreePrinter) *printer.TreePrinter {
bindingMap := map[string][]string{}
bindings, err := o.Client.ClusterV1beta1().ManagedClusterSetBindings(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return err
panic(err)
}
for _, binding := range bindings.Items {
if _, ok := bindingMap[binding.Spec.ClusterSet]; !ok {
Expand All @@ -54,12 +69,33 @@ func (o *Options) run() (err error) {
bindingMap[binding.Spec.ClusterSet] = append(bindingMap[binding.Spec.ClusterSet], binding.Namespace)
}

table := converToTable(clustersets, bindingMap)
if csList, ok := obj.(*clusterapiv1beta1.ManagedClusterSetList); ok {
for _, clusterset := range csList.Items {
boundNs, status := getFileds(clusterset, bindingMap[clusterset.Name])
mp := make(map[string]interface{})
mp[".BoundNamespace"] = boundNs
mp[".Status"] = status
tree.AddFileds(clusterset.Name, &mp)
}
}

return o.printer.PrintObj(table, o.Streams.Out)
return tree
}

func converToTable(clustersets *clusterapiv1beta1.ManagedClusterSetList, bindingMap map[string][]string) *metav1.Table {
func (o *Options) converToTable(obj runtime.Object) *metav1.Table {
bindingMap := map[string][]string{}
bindings, err := o.Client.ClusterV1beta1().ManagedClusterSetBindings(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err)
}
for _, binding := range bindings.Items {
if _, ok := bindingMap[binding.Spec.ClusterSet]; !ok {
bindingMap[binding.Spec.ClusterSet] = []string{}
}

bindingMap[binding.Spec.ClusterSet] = append(bindingMap[binding.Spec.ClusterSet], binding.Namespace)
}

table := &metav1.Table{
ColumnDefinitions: []metav1.TableColumnDefinition{
{Name: "Name", Type: "string"},
Expand All @@ -69,25 +105,28 @@ func converToTable(clustersets *clusterapiv1beta1.ManagedClusterSetList, binding
Rows: []metav1.TableRow{},
}

for _, cluster := range clustersets.Items {
bindings := bindingMap[cluster.Name]
row := convertRow(cluster, bindings)
table.Rows = append(table.Rows, row)
if csList, ok := obj.(*clusterapiv1beta1.ManagedClusterSetList); ok {
for _, clusterset := range csList.Items {
boundNs, status := getFileds(clusterset, bindingMap[clusterset.Name])
row := metav1.TableRow{
Cells: []interface{}{clusterset.Name, boundNs, status},
Object: runtime.RawExtension{Object: &clusterset},
}

table.Rows = append(table.Rows, row)
}
}

return table
}

func convertRow(clusterset clusterapiv1beta1.ManagedClusterSet, bindings []string) metav1.TableRow {
var status string
func getFileds(clusterset clusterapiv1beta1.ManagedClusterSet, bindings []string) (boundNs, status string) {
boundNs = strings.Join(bindings, ",")

emptyCond := meta.FindStatusCondition(clusterset.Status.Conditions, clusterapiv1beta1.ManagedClusterSetConditionEmpty)
if emptyCond != nil {
status = string(emptyCond.Message)
}

return metav1.TableRow{
Cells: []interface{}{clusterset.Name, strings.Join(bindings, ","), status},
Object: runtime.RawExtension{Object: &clusterset},
}
return
}
36 changes: 21 additions & 15 deletions pkg/cmd/get/clusterset/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/printers"
clusterclientset "open-cluster-management.io/api/client/cluster/clientset/versioned"
genericclioptionsclusteradm "open-cluster-management.io/clusteradm/pkg/genericclioptions"
"open-cluster-management.io/clusteradm/pkg/helpers/printer"
)

type Options struct {
Expand All @@ -14,26 +16,30 @@ type Options struct {

Streams genericclioptions.IOStreams

printer printers.ResourcePrinter
printer *printer.PrinterOption

Client *clusterclientset.Clientset
}

func newOptions(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, streams genericclioptions.IOStreams) *Options {
return &Options{
ClusteradmFlags: clusteradmFlags,
Streams: streams,
printer: printers.NewTablePrinter(printers.PrintOptions{
NoHeaders: false,
WithNamespace: false,
WithKind: false,
Wide: false,
ShowLabels: false,
Kind: schema.GroupKind{
Group: "cluster.open-cluster-management.io",
Kind: "ManagedCluster",
},
ColumnLabels: []string{},
SortBy: "",
AllowMissingKeys: true,
}),
printer: printer.NewPrinterOption(pntOpt),
}
}

var pntOpt = printers.PrintOptions{
NoHeaders: false,
WithNamespace: false,
WithKind: false,
Wide: false,
ShowLabels: false,
Kind: schema.GroupKind{
Group: "cluster.open-cluster-management.io",
Kind: "ManagedClusterSet",
},
ColumnLabels: []string{},
SortBy: "",
AllowMissingKeys: true,
}
4 changes: 3 additions & 1 deletion pkg/cmd/get/work/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func NewCmd(clusteradmFlags *genericclioptionsclusteradm.ClusteradmFlags, stream
},
}

cmd.Flags().StringVar(&o.cluster, "cluster", "", "Name of the managed cluster")
cmd.Flags().StringVar(&o.cluster, "cluster", "", "Names of the managed cluster")

o.printer.AddFlag(cmd.Flags())

return cmd
}
Loading

0 comments on commit 1d4e300

Please sign in to comment.