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 11, 2022
1 parent ccd525c commit 8fdf005
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 86 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
}
60 changes: 52 additions & 8 deletions pkg/cmd/get/cluster/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,34 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
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"
)

const (
clusterGroup = "cluster.open-cluster-management.io"
clusterAPIVersion = "v1"
clusterKind = "ManagedCluster"
)

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 @@ -50,8 +66,32 @@ func (o *Options) run() (err error) {
}

table := converToTable(clusters)
tree := convertToTree(clusters, o.printer.Tree())
clusters.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: clusterGroup,
Version: clusterAPIVersion,
Kind: clusterKind,
})

o.printer.WithTree(tree).WithTable(table).WithYaml(clusters)

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

func convertToTree(clusters *clusterapiv1.ManagedClusterList, tree *printer.TreePrinter) *printer.TreePrinter {
for _, cluster := range clusters.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(clusters *clusterapiv1.ManagedClusterList) *metav1.Table {
Expand All @@ -69,15 +109,22 @@ func converToTable(clusters *clusterapiv1.ManagedClusterList) *metav1.Table {
}

for _, cluster := range clusters.Items {
row := convertRow(cluster)
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 +143,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
}
56 changes: 46 additions & 10 deletions pkg/cmd/get/clusterset/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
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"
)

const (
clustersetGroup = "cluster.open-cluster-management.io"
clustersetAPIVersion = "v1beta2"
clustersetKind = "ManagedClusterSet"
)

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 @@ -55,8 +71,27 @@ func (o *Options) run() (err error) {
}

table := converToTable(clustersets, bindingMap)
tree := convertToTree(clustersets, bindingMap, o.printer.Tree())
clustersets.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{
Group: clustersetGroup,
Version: clustersetAPIVersion,
Kind: clustersetKind,
})

return o.printer.PrintObj(table, o.Streams.Out)
o.printer.WithTree(tree).WithTable(table).WithYaml(clustersets)

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

func convertToTree(clustersets *clusterapiv1beta1.ManagedClusterSetList, bindingMap map[string][]string, tree *printer.TreePrinter) *printer.TreePrinter {
for _, clusterset := range clustersets.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 tree
}

func converToTable(clustersets *clusterapiv1beta1.ManagedClusterSetList, bindingMap map[string][]string) *metav1.Table {
Expand All @@ -69,25 +104,26 @@ func converToTable(clustersets *clusterapiv1beta1.ManagedClusterSetList, binding
Rows: []metav1.TableRow{},
}

for _, cluster := range clustersets.Items {
bindings := bindingMap[cluster.Name]
row := convertRow(cluster, bindings)
for _, clusterset := range clustersets.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
}
33 changes: 18 additions & 15 deletions pkg/cmd/get/clusterset/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 @@ -14,26 +15,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: "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 8fdf005

Please sign in to comment.