Merge pull request #437 from captainroy-hy/app-centric

refact cmds from comp-centric into app-centric style
This commit is contained in:
Jianbo Sun
2020-10-26 15:29:35 +08:00
committed by GitHub
7 changed files with 67 additions and 47 deletions
+12 -3
View File
@@ -14,6 +14,7 @@ var (
applicationName = "app-trait-basic"
applicationNotExistedName = "app-trait-basic-NOT-EXISTED"
traitAlias = "scale"
serviceNameNotExisting = "svc-not-existing"
)
var _ = ginkgo.Describe("Trait", func() {
@@ -25,15 +26,23 @@ var _ = ginkgo.Describe("Trait", func() {
e2e.TraitManualScalerAttachContext("vela attach trait", traitAlias, applicationName)
// Trait
ginkgo.Context("vela attach trait to a not existed app", func() {
ginkgo.It("should print successful attached information", func() {
ginkgo.Context("vela attach trait to a not existing app", func() {
ginkgo.It("should alert app not exist", func() {
cli := fmt.Sprintf("vela %s %s", traitAlias, applicationNotExistedName)
output, err := e2e.Exec(cli)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(output).To(gomega.ContainSubstring("component name (" + applicationNotExistedName + ") doesn't exist"))
gomega.Expect(output).To(gomega.ContainSubstring("the application " + applicationNotExistedName + " doesn't exist in current env " + envName))
})
})
ginkgo.Context("vela attach trait to a not existing service", func() {
ginkgo.It("should alert service not exist", func() {
cli := fmt.Sprintf("vela %s %s --svc %s", traitAlias, applicationName, serviceNameNotExisting)
output, err := e2e.Exec(cli)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(output).To(gomega.ContainSubstring("the service " + serviceNameNotExisting + " doesn't exist in the application " + applicationName))
})
})
//ginkgo.Context("vela detach trait", func() {
// ginkgo.It("should print successful detached information", func() {
// cli := fmt.Sprintf("vela %s --detach %s", traitAlias, applicationName)
+1
View File
@@ -19,6 +19,7 @@ const (
App = "app"
WorkloadType = "type"
TraitDetach = "detach"
Service = "svc"
)
type runOptions oam.RunOptions
+2 -19
View File
@@ -6,7 +6,6 @@ import (
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/crossplane/oam-kubernetes-runtime/pkg/oam"
"github.com/oam-dev/kubevela/api/types"
"github.com/spf13/cobra"
@@ -16,6 +15,7 @@ import (
"k8s.io/client-go/kubernetes"
"github.com/oam-dev/kubevela/pkg/application"
"github.com/oam-dev/kubevela/pkg/commands/util"
velacmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
cmdexec "k8s.io/kubectl/pkg/cmd/exec"
k8scmdutil "k8s.io/kubectl/pkg/cmd/util"
@@ -127,7 +127,7 @@ func (o *VelaExecOptions) Init(ctx context.Context, c *cobra.Command, argsIn []s
}
func (o *VelaExecOptions) Complete() error {
compName, err := o.askComponent()
compName, err := util.AskToChooseOneService(o.App.GetComponents())
if err != nil {
return err
}
@@ -146,23 +146,6 @@ func (o *VelaExecOptions) Complete() error {
return o.kcExecOptions.Complete(o.f, o.Cmd, args, 1)
}
func (o *VelaExecOptions) askComponent() (string, error) {
comps := o.App.GetComponents()
if len(comps) == 1 {
return comps[0], nil
}
prompt := &survey.Select{
Message: "You have multiple Components in your app. Please choose one component for logs: ",
Options: comps,
}
var compName string
err := survey.AskOne(prompt, &compName)
if err != nil {
return "", fmt.Errorf("choosing component name err %v", err)
}
return compName, nil
}
func (o *VelaExecOptions) getPodName(compName string) (string, error) {
podList, err := o.ClientSet.CoreV1().Pods(o.Env.Namespace).List(o.Context, v1.ListOptions{
LabelSelector: labels.Set(map[string]string{
+2 -19
View File
@@ -10,9 +10,9 @@ import (
"github.com/oam-dev/kubevela/api/types"
"github.com/oam-dev/kubevela/pkg/application"
"github.com/oam-dev/kubevela/pkg/commands/util"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/color"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -62,23 +62,6 @@ type Args struct {
App *application.Application
}
func (l *Args) AskComponent() (string, error) {
comps := l.App.GetComponents()
if len(comps) == 1 {
return comps[0], nil
}
prompt := &survey.Select{
Message: "You have multiple Components in your app. Please choose one component for logs: ",
Options: comps,
}
var compName string
err := survey.AskOne(prompt, &compName)
if err != nil {
return "", fmt.Errorf("choosing component name err %v", err)
}
return compName, nil
}
// Run refer to the implementation at https://github.com/oam-dev/stern/blob/master/stern/main.go
func (l *Args) Run(ctx context.Context, ioStreams cmdutil.IOStreams) error {
@@ -86,7 +69,7 @@ func (l *Args) Run(ctx context.Context, ioStreams cmdutil.IOStreams) error {
if err != nil {
return err
}
compName, err := l.AskComponent()
compName, err := util.AskToChooseOneService(l.App.GetComponents())
if err != nil {
return err
}
+28 -6
View File
@@ -3,9 +3,11 @@ package commands
import (
"context"
"errors"
"fmt"
"github.com/oam-dev/kubevela/api/types"
"github.com/oam-dev/kubevela/pkg/application"
"github.com/oam-dev/kubevela/pkg/commands/util"
cmdutil "github.com/oam-dev/kubevela/pkg/commands/util"
"github.com/oam-dev/kubevela/pkg/oam"
"github.com/oam-dev/kubevela/pkg/plugins"
@@ -76,7 +78,7 @@ func AddTraitCommands(parentCmd *cobra.Command, c types.Args, ioStreams cmdutil.
for _, v := range tmp.Parameters {
types.SetFlagBy(pluginCmd.Flags(), v)
}
pluginCmd.Flags().StringP(App, "a", "", "create or add into an existing application group")
pluginCmd.Flags().StringP(Service, "", "", "specify one service belonging to the application")
pluginCmd.Flags().BoolP(Staging, "s", false, "only save changes locally without real update application")
pluginCmd.Flags().BoolP(TraitDetach, "", false, "detach trait from component")
@@ -89,12 +91,32 @@ func (o *commandOptions) Prepare(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("please specify the name of the app")
}
o.workloadName = args[0]
if app := cmd.Flag(App).Value.String(); app != "" {
o.appName = app
} else {
o.appName = o.workloadName
o.appName = args[0]
// get application
app, err := application.Load(o.Env.Name, o.appName)
if err != nil {
return err
}
if len(app.Name) == 0 {
return fmt.Errorf("the application %s doesn't exist in current env %s", o.appName, o.Env.Name)
}
// get service name
serviceNames := app.GetComponents()
if svcName := cmd.Flag(Service).Value.String(); svcName != "" {
for _, v := range serviceNames {
if v == svcName {
o.workloadName = svcName
return nil
}
}
return fmt.Errorf("the service %s doesn't exist in the application %s", svcName, o.appName)
}
svcName, err := util.AskToChooseOneService(serviceNames)
if err != nil {
return err
}
o.workloadName = svcName
return nil
}
+21
View File
@@ -9,6 +9,7 @@ import (
"github.com/oam-dev/kubevela/api/types"
"github.com/AlecAivazis/survey/v2"
corev1alpha2 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/spf13/cobra"
"k8s.io/klog"
@@ -172,3 +173,23 @@ func PrintFlags(cmd *cobra.Command, subcmds []*cobra.Command) {
}
cmd.Println()
}
// AskToChooseOneService will ask users to select one service of the application if more than one exidi
func AskToChooseOneService(svcNames []string) (string, error) {
if len(svcNames) == 0 {
return "", fmt.Errorf("no service exist in the application")
}
if len(svcNames) == 1 {
return svcNames[0], nil
}
prompt := &survey.Select{
Message: "You have multiple Services in your app. Please choose one Service: ",
Options: svcNames,
}
var svcName string
err := survey.AskOne(prompt, &svcName)
if err != nil {
return "", fmt.Errorf("choosing service err %v", err)
}
return svcName, nil
}
+1
View File
@@ -155,6 +155,7 @@ var _ = AfterSuite(func() {
_ = k8sClient.Delete(context.Background(), &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: DefinitionNamespace}})
_ = k8sClient.Delete(context.Background(), &td)
_ = k8sClient.Delete(context.Background(), &wd)
_ = k8sClient.Delete(context.Background(), &websvcWD)
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})