Skip to content

Commit

Permalink
feat: dbctl describe more cluster information (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
L.DongMing authored and sophon-zt committed Oct 26, 2022
1 parent f8527ed commit c56beb9
Show file tree
Hide file tree
Showing 36 changed files with 1,271 additions and 464 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ENABLE_WEBHOOKS ?= false
APP_NAME = kubeblocks


VERSION ?= 0.1.0-alpha.6
VERSION ?= 0.1.0-alpha.8
CHART_PATH = deploy/helm


Expand Down
1 change: 1 addition & 0 deletions apis/dbaas/v1alpha1/appversion_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
1 change: 1 addition & 0 deletions apis/dbaas/v1alpha1/cluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down
1 change: 1 addition & 0 deletions apis/dbaas/v1alpha1/clusterdefinition_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down
1 change: 1 addition & 0 deletions apis/dbaas/v1alpha1/opsrequest_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/util/yaml"
)

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ require (
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/text v0.3.7
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
Expand All @@ -311,7 +311,7 @@ require (
k8s.io/apiextensions-apiserver v0.24.2
k8s.io/apiserver v0.24.2 // indirect
k8s.io/component-base v0.24.3
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/klog/v2 v2.60.1
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
oras.land/oras-go v1.2.0 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/dbctl/cmd/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/pingcap/go-tpc/tpcc"
)

Expand Down
1 change: 1 addition & 0 deletions internal/dbctl/cmd/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cluster
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/cli-runtime/pkg/genericclioptions"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"

Expand Down
1 change: 1 addition & 0 deletions internal/dbctl/cmd/cluster/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down
14 changes: 4 additions & 10 deletions internal/dbctl/cmd/cluster/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ import (
)

func NewDescribeCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
cmd := describe.Command{
Factory: f,
Streams: streams,
Short: "Describe database cluster info.",
GroupKind: []schema.GroupKind{
{Group: types.Group, Kind: types.KindCluster},
},
Template: []string{"cluster.tmpl"},
}
return cmd.Build()
o := describe.NewOptions(f, streams,
"Describe database cluster info",
schema.GroupKind{Group: types.Group, Kind: types.KindCluster})
return o.Build()
}
4 changes: 2 additions & 2 deletions internal/dbctl/cmd/cluster/expose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package cluster
import (
"fmt"

"k8s.io/cli-runtime/pkg/genericclioptions"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic/fake"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"

Expand Down
1 change: 1 addition & 0 deletions internal/dbctl/cmd/cluster/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down
1 change: 1 addition & 0 deletions internal/dbctl/cmd/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down
1 change: 1 addition & 0 deletions internal/dbctl/cmd/create/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down
155 changes: 101 additions & 54 deletions internal/dbctl/cmd/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,102 +17,149 @@ limitations under the License.
package describe

import (
"embed"
"fmt"

"github.com/leaanthony/debme"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/cli-runtime/pkg/resource"
cmdutil "k8s.io/kubectl/pkg/cmd/util"

"github.com/apecloud/kubeblocks/internal/dbctl/cmd/get"
"k8s.io/kubectl/pkg/describe"
)

var (
//go:embed template/*
template embed.FS
)
// Options used to construct a describe command
type Options struct {
Factory cmdutil.Factory
Short string
Namespace string
Selector string

type PrintExtra func() error
Describer func(*meta.RESTMapping) (describe.ResourceDescriber, error)
Builder func() *resource.Builder

// Command used to construct a describe command
type Command struct {
Factory cmdutil.Factory
Short string
GroupKind schema.GroupKind
Name string

GroupKind []schema.GroupKind
Template []string
Name string
PrintExtra PrintExtra
EnforceNamespace bool
AllNamespaces bool

Streams genericclioptions.IOStreams
DescriberSettings *describe.DescriberSettings
genericclioptions.IOStreams
}

// Build return a describe command
func (c *Command) Build() *cobra.Command {
o := get.NewOptions(c.Streams, []string{})
func NewOptions(f cmdutil.Factory, streams genericclioptions.IOStreams, short string, gk schema.GroupKind) *Options {
return &Options{
Factory: f,
IOStreams: streams,
Short: short,
GroupKind: gk,
DescriberSettings: &describe.DescriberSettings{
ShowEvents: true,
ChunkSize: cmdutil.DefaultChunkSize,
},
}
}

// Build return a describe command
func (o *Options) Build() *cobra.Command {
cmd := &cobra.Command{
Use: "describe",
Short: c.Short,
Short: o.Short,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(c.complete(args))
cmdutil.CheckErr(c.run(o, c.Factory))
cmdutil.CheckErr(o.complete(args))
cmdutil.CheckErr(o.run())
},
}

cmd.Flags().BoolVarP(&o.AllNamespaces, "all-namespace", "A", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
cmdutil.AddLabelSelectorFlagVar(cmd, &o.LabelSelector)
cmd.Flags().StringVarP(&o.Selector, "selector", "l", o.Selector, "Selector (label query) to filter on, supports '=', '==', and '!='.(e.g. -l key1=value1,key2=value2)")
cmd.Flags().BoolVar(&o.AllNamespaces, "all-namespaces", o.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
cmd.Flags().BoolVar(&o.DescriberSettings.ShowEvents, "show-events", o.DescriberSettings.ShowEvents, "If true, display events related to the described object.")

return cmd
}

func (c *Command) complete(args []string) error {
if len(args) == 0 {
func (o *Options) complete(args []string) error {
var err error
if len(args) == 0 || o.GroupKind.Empty() {
return errors.New("You must specify the name of resource to describe.")
}

if len(c.GroupKind) == 0 {
return errors.New("You must specify the resource type to describe.")
o.Name = args[0]
if o.Namespace, o.EnforceNamespace, err = o.Factory.ToRawKubeConfigLoader().Namespace(); err != nil {
return err
}
if o.AllNamespaces {
o.EnforceNamespace = false
}

if len(c.GroupKind) != len(c.Template) {
return errors.New("The number of resource type is not equal to template.")
o.Describer = func(mapping *meta.RESTMapping) (describe.ResourceDescriber, error) {
return DescriberFn(o.Factory, mapping)
}
c.Name = args[0]
o.Builder = o.Factory.NewBuilder
return nil
}

func (c *Command) run(o *get.Options, f cmdutil.Factory) error {
if err := o.Complete(f); err != nil {
func (o *Options) run() error {
r := o.Builder().
Unstructured().
ContinueOnError().
NamespaceParam(o.Namespace).DefaultNamespace().AllNamespaces(o.AllNamespaces).
LabelSelectorParam(o.Selector).
ResourceTypeOrNameArgs(true, o.GroupKind.String(), o.Name).
Flatten().
Do()
err := r.Err()
if err != nil {
return err
}

// Get object from k8s and render the template
tmplFs, _ := debme.FS(template, "template")
for i := 0; i < len(c.GroupKind); i++ {
tmplBytes, err := tmplFs.ReadFile(c.Template[i])
var allErrs []error
infos, err := r.Infos()
if err != nil {
allErrs = append(allErrs, err)
}

errs := sets.NewString()
first := true
for _, info := range infos {
mapping := info.ResourceMapping()
describer, err := o.Describer(mapping)
if err != nil {
fmt.Fprintln(o.ErrOut, "build describe command error")
return nil
if errs.Has(err.Error()) {
continue
}
allErrs = append(allErrs, err)
errs.Insert(err.Error())
continue
}
tmplStr := fmt.Sprintf("go-template=%s", string(tmplBytes))
o.PrintFlags.OutputFormat = &tmplStr
o.BuildArgs = []string{c.GroupKind[i].String(), c.Name}

if err = o.Run(f); err != nil {
return err
s, err := describer.Describe(info.Namespace, info.Name, *o.DescriberSettings)
if err != nil {
if errs.Has(err.Error()) {
continue
}
allErrs = append(allErrs, err)
errs.Insert(err.Error())
continue
}
if first {
first = false
fmt.Fprint(o.Out, s)
} else {
fmt.Fprintf(o.Out, "\n\n%s", s)
}
}

// execute the custom print function to print other infos
if c.PrintExtra != nil {
err := c.PrintExtra()
if err != nil {
return err
if len(infos) == 0 && len(allErrs) == 0 {
// if we wrote no output, and had no errors, be sure we output something.
if o.AllNamespaces {
fmt.Fprintln(o.ErrOut, "No resources found")
} else {
fmt.Fprintf(o.ErrOut, "No resources found in %s namespace.\n", o.Namespace)
}
}

return nil
return utilerrors.NewAggregate(allErrs)
}
Loading

0 comments on commit c56beb9

Please sign in to comment.