From b38aa1cdf06729d5b3469d851a0d5d2b59006beb Mon Sep 17 00:00:00 2001 From: qiaozp <47812250+chivalryq@users.noreply.github.com> Date: Fri, 19 Nov 2021 18:00:03 +0800 Subject: [PATCH] Fix: Align -n command argument (#2719) * add namespace flag fix fix test fix tests * try test * try test * fix tests --- e2e/application/vela.yaml | 12 +++++++ e2e/commonContext.go | 2 +- references/appfile/api/appfile.go | 5 ++- references/appfile/api/appfile_test.go | 4 +-- references/cli/common.go | 44 +++++++++++++++++++++++ references/cli/def.go | 36 ++++++------------- references/cli/delete.go | 14 ++++---- references/cli/dryrun.go | 5 +-- references/cli/exec.go | 2 +- references/cli/export.go | 8 ++--- references/cli/init.go | 48 +++++++++++++++++--------- references/cli/livediff.go | 5 +-- references/cli/ls.go | 11 ++---- references/cli/show.go | 7 ++-- references/cli/status.go | 33 +++++++++--------- references/cli/up.go | 11 +++--- references/cli/up_test.go | 10 ++---- references/cli/workflow.go | 44 +++++------------------ references/cli/workloads.go | 5 +-- references/common/application.go | 28 +++++++-------- references/common/common.go | 5 ++- references/common/workload.go | 24 +++---------- 22 files changed, 184 insertions(+), 179 deletions(-) create mode 100644 e2e/application/vela.yaml diff --git a/e2e/application/vela.yaml b/e2e/application/vela.yaml new file mode 100644 index 000000000..405eea3b9 --- /dev/null +++ b/e2e/application/vela.yaml @@ -0,0 +1,12 @@ +createTime: "0001-01-01T00:00:00Z" +name: initmyapp +services: + mysvc: + addRevisionLabel: false + cpu: "0.5" + image: nginx:latest + imagePullPolicy: Always + memory: 200M + port: 80 + type: webservice +updateTime: "0001-01-01T00:00:00Z" diff --git a/e2e/commonContext.go b/e2e/commonContext.go index b64d94e37..ea9f37932 100644 --- a/e2e/commonContext.go +++ b/e2e/commonContext.go @@ -127,7 +127,7 @@ var ( cli := fmt.Sprintf("vela delete %s", applicationName) output, err := Exec(cli) gomega.Expect(err).NotTo(gomega.HaveOccurred()) - gomega.Expect(output).To(gomega.ContainSubstring("deleted from env")) + gomega.Expect(output).To(gomega.ContainSubstring("already deleted")) }) }) } diff --git a/references/appfile/api/appfile.go b/references/appfile/api/appfile.go index 151de8557..cf6ece3c4 100644 --- a/references/appfile/api/appfile.go +++ b/references/appfile/api/appfile.go @@ -31,7 +31,6 @@ import ( "github.com/oam-dev/kubevela/apis/core.oam.dev/common" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1alpha2" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" - "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/builtin" "github.com/oam-dev/kubevela/pkg/oam" cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" @@ -142,7 +141,7 @@ func (app *AppFile) ExecuteAppfileTasks(io cmdutil.IOStreams) error { } // BuildOAMApplication renders Appfile into Application, Scopes and other K8s Resources. -func (app *AppFile) BuildOAMApplication(env *types.EnvMeta, io cmdutil.IOStreams, tm template.Manager, silence bool) (*v1beta1.Application, []oam.Object, error) { +func (app *AppFile) BuildOAMApplication(namespace string, io cmdutil.IOStreams, tm template.Manager, silence bool) (*v1beta1.Application, []oam.Object, error) { if err := app.ExecuteAppfileTasks(io); err != nil { if strings.Contains(err.Error(), "'image' : not found") { return nil, nil, ErrImageNotDefined @@ -152,7 +151,7 @@ func (app *AppFile) BuildOAMApplication(env *types.EnvMeta, io cmdutil.IOStreams // auxiliaryObjects currently include OAM Scope Custom Resources and ConfigMaps var auxiliaryObjects []oam.Object servApp := new(v1beta1.Application) - servApp.SetNamespace(env.Namespace) + servApp.SetNamespace(namespace) servApp.SetName(app.Name) servApp.Spec.Components = []common.ApplicationComponent{} if !silence { diff --git a/references/appfile/api/appfile_test.go b/references/appfile/api/appfile_test.go index 184413b99..b519a54d0 100644 --- a/references/appfile/api/appfile_test.go +++ b/references/appfile/api/appfile_test.go @@ -132,7 +132,7 @@ func TestBuildOAMApplication2(t *testing.T) { for _, tcase := range testCases { tcase.expectApp.Namespace = expectNs - o, _, err := tcase.appFile.BuildOAMApplication(&types.EnvMeta{Namespace: expectNs}, cmdutil.IOStreams{ + o, _, err := tcase.appFile.BuildOAMApplication(expectNs, cmdutil.IOStreams{ In: os.Stdin, Out: os.Stdout, }, tm, false) @@ -378,7 +378,7 @@ outputs: ingress: { } } - application, objects, err := app.BuildOAMApplication(&types.EnvMeta{Namespace: "default"}, io, tm, false) + application, objects, err := app.BuildOAMApplication("default", io, tm, false) if c.want.err != nil { assert.Equal(t, c.want.err, err) return diff --git a/references/cli/common.go b/references/cli/common.go index daa19648c..4e826dd0b 100644 --- a/references/cli/common.go +++ b/references/cli/common.go @@ -16,9 +16,53 @@ limitations under the License. package cli +import ( + "github.com/spf13/cobra" + + "github.com/oam-dev/kubevela/pkg/utils/common" +) + // constants used in `svc` command const ( App = "app" Service = "svc" Namespace = "namespace" + + // FlagDescription command flag to specify the description of the definition + FlagDescription = "desc" + // FlagDryRun command flag to disable actual changes and only display intend changes + FlagDryRun = "dry-run" + // FlagTemplateYAML command flag to specify which existing template YAML file to use + FlagTemplateYAML = "template-yaml" + // FlagOutput command flag to specify which file to save + FlagOutput = "output" + // FlagMessage command flag to specify which file to save + FlagMessage = "message" + // FlagType command flag to specify which definition type to use + FlagType = "type" + // FlagNamespace command flag to specify which namespace to use + FlagNamespace = "namespace" + // FlagInteractive command flag to specify the use of interactive process + FlagInteractive = "interactive" ) + +func addNamespaceArg(cmd *cobra.Command) { + cmd.Flags().StringP(Namespace, "n", "", "specify the namespace to use") +} + +// GetFlagNamespaceOrEnv will get env and namespace flag, namespace flag takes the priority +func GetFlagNamespaceOrEnv(cmd *cobra.Command, args common.Args) (string, error) { + namespace, err := cmd.Flags().GetString(Namespace) + if err != nil { + return "", err + } + if namespace != "" { + return namespace, nil + } + velaEnv, err := GetFlagEnvOrCurrent(cmd, args) + if err != nil { + return "", err + } + return velaEnv.Namespace, nil + +} diff --git a/references/cli/def.go b/references/cli/def.go index 12af3c17d..6fd29c142 100644 --- a/references/cli/def.go +++ b/references/cli/def.go @@ -47,22 +47,6 @@ import ( ) const ( - // FlagDescription command flag to specify the description of the definition - FlagDescription = "desc" - // FlagDryRun command flag to disable actual changes and only display intend changes - FlagDryRun = "dry-run" - // FlagTemplateYAML command flag to specify which existing template YAML file to use - FlagTemplateYAML = "template-yaml" - // FlagOutput command flag to specify which file to save - FlagOutput = "output" - // FlagMessage command flag to specify which file to save - FlagMessage = "message" - // FlagType command flag to specify which definition type to use - FlagType = "type" - // FlagNamespace command flag to specify which namespace to use - FlagNamespace = "namespace" - // FlagInteractive command flag to specify the use of interactive process - FlagInteractive = "interactive" // HelmChartNamespacePlaceholder is used as a placeholder for rendering definitions into helm chart format HelmChartNamespacePlaceholder = "###HELM_NAMESPACE###" // HelmChartFormatEnvName is the name of the environment variable to enable render helm chart format YAML @@ -315,7 +299,7 @@ func NewDefinitionGetCommand(c common.Args) *cobra.Command { } namespace, err := cmd.Flags().GetString(FlagNamespace) if err != nil { - return errors.Wrapf(err, "failed to get `%s`", FlagNamespace) + return errors.Wrapf(err, "failed to get `%s`", Namespace) } k8sClient, err := c.GetClient() if err != nil { @@ -336,7 +320,7 @@ func NewDefinitionGetCommand(c common.Args) *cobra.Command { }, } cmd.Flags().StringP(FlagType, "t", "", "Specify which definition type to get. If empty, all types will be searched. Valid types: "+strings.Join(common2.ValidDefinitionTypes(), ", ")) - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, all namespaces will be searched.") + cmd.Flags().StringP(Namespace, "n", "", "Specify which namespace to get. If empty, all namespaces will be searched.") return cmd } @@ -358,7 +342,7 @@ func NewDefinitionListCommand(c common.Args) *cobra.Command { } namespace, err := cmd.Flags().GetString(FlagNamespace) if err != nil { - return errors.Wrapf(err, "failed to get `%s`", FlagNamespace) + return errors.Wrapf(err, "failed to get `%s`", Namespace) } k8sClient, err := c.GetClient() if err != nil { @@ -386,7 +370,7 @@ func NewDefinitionListCommand(c common.Args) *cobra.Command { }, } cmd.Flags().StringP(FlagType, "t", "", "Specify which definition type to list. If empty, all types will be searched. Valid types: "+strings.Join(common2.ValidDefinitionTypes(), ", ")) - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to list. If empty, all namespaces will be searched.") + cmd.Flags().StringP(Namespace, "n", "", "Specify which namespace to list. If empty, all namespaces will be searched.") return cmd } @@ -409,7 +393,7 @@ func NewDefinitionEditCommand(c common.Args) *cobra.Command { } namespace, err := cmd.Flags().GetString(FlagNamespace) if err != nil { - return errors.Wrapf(err, "failed to get `%s`", FlagNamespace) + return errors.Wrapf(err, "failed to get `%s`", Namespace) } k8sClient, err := c.GetClient() if err != nil { @@ -470,7 +454,7 @@ func NewDefinitionEditCommand(c common.Args) *cobra.Command { }, } cmd.Flags().StringP(FlagType, "t", "", "Specify which definition type to get. If empty, all types will be searched. Valid types: "+strings.Join(common2.ValidDefinitionTypes(), ", ")) - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, all namespaces will be searched.") + cmd.Flags().StringP(Namespace, "n", "", "Specify which namespace to get. If empty, all namespaces will be searched.") return cmd } @@ -606,7 +590,7 @@ func NewDefinitionApplyCommand(c common.Args) *cobra.Command { } namespace, err := cmd.Flags().GetString(FlagNamespace) if err != nil { - return errors.Wrapf(err, "failed to get `%s`", FlagNamespace) + return errors.Wrapf(err, "failed to get `%s`", Namespace) } k8sClient, err := c.GetClient() if err != nil { @@ -660,7 +644,7 @@ func NewDefinitionApplyCommand(c common.Args) *cobra.Command { }, } cmd.Flags().BoolP(FlagDryRun, "", false, "only build definition from CUE into CRB object without applying it to kubernetes clusters") - cmd.Flags().StringP(FlagNamespace, "n", "vela-system", "Specify which namespace to apply.") + cmd.Flags().StringP(Namespace, "n", "vela-system", "Specify which namespace to apply.") return cmd } @@ -680,7 +664,7 @@ func NewDefinitionDelCommand(c common.Args) *cobra.Command { } namespace, err := cmd.Flags().GetString(FlagNamespace) if err != nil { - return errors.Wrapf(err, "failed to get `%s`", FlagNamespace) + return errors.Wrapf(err, "failed to get `%s`", Namespace) } k8sClient, err := c.GetClient() if err != nil { @@ -725,7 +709,7 @@ func NewDefinitionDelCommand(c common.Args) *cobra.Command { }, } cmd.Flags().StringP(FlagType, "t", "", "Specify the definition type of target. Valid types: "+strings.Join(common2.ValidDefinitionTypes(), ", ")) - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace the definition locates.") + cmd.Flags().StringP(Namespace, "n", "", "Specify which namespace the definition locates.") return cmd } diff --git a/references/cli/delete.go b/references/cli/delete.go index cfc99884d..ab7fccd25 100644 --- a/references/cli/delete.go +++ b/references/cli/delete.go @@ -46,17 +46,18 @@ func NewDeleteCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Comman cmd.SetOut(ioStreams.Out) cmd.RunE = func(cmd *cobra.Command, args []string) error { + namespace, err := GetFlagNamespaceOrEnv(cmd, c) + if err != nil { + return err + } newClient, err := c.GetClient() if err != nil { return err } o := &common.DeleteOptions{ - C: c, - } - o.Client = newClient - o.Env, err = GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err + C: c, + Namespace: namespace, + Client: newClient, } if len(args) < 1 { return errors.New("must specify name for the app") @@ -89,5 +90,6 @@ func NewDeleteCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Comman return nil } cmd.PersistentFlags().StringP(Service, "", "", "delete only the specified service in this app") + addNamespaceArg(cmd) return cmd } diff --git a/references/cli/dryrun.go b/references/cli/dryrun.go index 2971447ba..5932149f9 100644 --- a/references/cli/dryrun.go +++ b/references/cli/dryrun.go @@ -72,11 +72,11 @@ func NewDryRunCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Command return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { - velaEnv, err := GetFlagEnvOrCurrent(cmd, c) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - buff, err := DryRunApplication(o, c, velaEnv.Namespace) + buff, err := DryRunApplication(o, c, namespace) if err != nil { return err } @@ -87,6 +87,7 @@ func NewDryRunCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Command cmd.Flags().StringVarP(&o.ApplicationFile, "file", "f", "./app.yaml", "application file name") cmd.Flags().StringVarP(&o.DefinitionFile, "definition", "d", "", "specify a definition file or directory, it will only be used in dry-run rather than applied to K8s cluster") + addNamespaceArg(cmd) cmd.SetOut(ioStreams.Out) return cmd } diff --git a/references/cli/exec.go b/references/cli/exec.go index 75681f94e..396a28fa8 100644 --- a/references/cli/exec.go +++ b/references/cli/exec.go @@ -132,7 +132,7 @@ func NewExecCommand(c common.Args, ioStreams util.IOStreams) *cobra.Command { cmd.Flags().Duration(podRunningTimeoutFlag, defaultPodExecTimeout, "The length of time (like 5s, 2m, or 3h, higher than zero) to wait until at least one pod is running", ) - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, uses namespace in env.") + cmd.Flags().StringP(Namespace, "n", "", "Specify which namespace to get. If empty, uses namespace in env.") return cmd } diff --git a/references/cli/export.go b/references/cli/export.go index eb7a6f747..039c34d2a 100644 --- a/references/cli/export.go +++ b/references/cli/export.go @@ -37,15 +37,14 @@ func NewExportCommand(c common2.Args, ioStream cmdutil.IOStreams) *cobra.Command types.TagCommandType: types.TypeStart, }, RunE: func(cmd *cobra.Command, args []string) error { - velaEnv, err := GetFlagEnvOrCurrent(cmd, c) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } o := &common.AppfileOptions{ - IO: ioStream, - Env: velaEnv, + IO: ioStream, } - _, data, err := o.Export(*appFilePath, velaEnv.Namespace, true, c) + _, data, err := o.Export(*appFilePath, namespace, true, c) if err != nil { return err } @@ -55,6 +54,7 @@ func NewExportCommand(c common2.Args, ioStream cmdutil.IOStreams) *cobra.Command } cmd.SetOut(ioStream.Out) + addNamespaceArg(cmd) cmd.Flags().StringVarP(appFilePath, "file", "f", "", "specify file path for appfile") return cmd } diff --git a/references/cli/init.go b/references/cli/init.go index b6b427ae9..3c8b1b3a9 100644 --- a/references/cli/init.go +++ b/references/cli/init.go @@ -26,15 +26,15 @@ import ( "cuelang.org/go/cue" "github.com/AlecAivazis/survey/v2" "github.com/fatih/color" - "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" + v1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/yaml" "github.com/oam-dev/kubevela/apis/types" common2 "github.com/oam-dev/kubevela/pkg/utils/common" - "github.com/oam-dev/kubevela/pkg/utils/env" cmdutil "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/references/appfile" "github.com/oam-dev/kubevela/references/appfile/api" @@ -45,8 +45,8 @@ import ( type appInitOptions struct { client client.Client cmdutil.IOStreams - Env *types.EnvMeta - c common2.Args + Namespace string + c common2.Args app *api.Application appName string @@ -68,15 +68,17 @@ func NewInitCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Command return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { + var err error + o.Namespace, err = GetFlagNamespaceOrEnv(cmd, c) + if err != nil { + return err + } + newClient, err := c.GetClient() if err != nil { return err } o.client = newClient - o.Env, err = GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err - } o.IOStreams.Info("Welcome to use KubeVela CLI! Please describe your application.") o.IOStreams.Info() if err = o.CheckEnv(); err != nil { @@ -108,24 +110,25 @@ func NewInitCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Command } ctx := context.Background() - err = common.BuildRun(ctx, o.app, o.client, o.Env, o.IOStreams) + err = common.BuildRun(ctx, o.app, o.client, o.Namespace, o.IOStreams) if err != nil { return err } - deployStatus, err := printTrackingDeployStatus(c, o.IOStreams, o.appName, o.Env) + deployStatus, err := printTrackingDeployStatus(c, o.IOStreams, o.appName, o.Namespace) if err != nil { return err } if deployStatus != compStatusDeployed { return nil } - return printAppStatus(context.Background(), newClient, ioStreams, o.appName, o.Env, cmd, c) + return printAppStatus(context.Background(), newClient, ioStreams, o.appName, o.Namespace, cmd, c) }, Annotations: map[string]string{ types.TagCommandType: types.TypeStart, }, } cmd.Flags().BoolVar(&o.renderOnly, "render-only", false, "Rendering vela.yaml in current dir and do not deploy") + addNamespaceArg(cmd) cmd.SetOut(ioStreams.Out) return cmd } @@ -144,11 +147,22 @@ func (o *appInitOptions) Naming() error { // CheckEnv checks environment, e.g., domain and email. func (o *appInitOptions) CheckEnv() error { - if o.Env.Namespace == "" { - o.Env.Namespace = "default" + if o.Namespace == "" { + o.Namespace = "default" } - if err := env.CreateEnv(o.Env.Name, o.Env); err != nil { - return errors.Wrap(err, "app init create namespace err") + var ns v1.Namespace + ctx := context.Background() + err := o.client.Get(ctx, client.ObjectKey{ + Name: o.Namespace, + }, &ns) + if apierrors.IsNotFound(err) { + ns.Name = o.Namespace + err = o.client.Create(ctx, &ns) + if err != nil { + return err + } + } else if err != nil { + return err } return nil } @@ -181,7 +195,7 @@ func formatAndGetUsage(p *types.Parameter) string { // Workload asks user to choose workload type from installed workloads func (o *appInitOptions) Workload() error { - workloads, err := plugins.LoadInstalledCapabilityWithType(o.Env.Namespace, o.c, types.TypeComponentDefinition) + workloads, err := plugins.LoadInstalledCapabilityWithType(o.Namespace, o.c, types.TypeComponentDefinition) if err != nil { return err } @@ -303,7 +317,7 @@ func (o *appInitOptions) Workload() error { // other type not supported } } - o.app, err = common.BaseComplete(o.Env, o.c, o.workloadName, o.appName, fs, o.workloadType) + o.app, err = common.BaseComplete(o.Namespace, o.c, o.workloadName, o.appName, fs, o.workloadType) return err } diff --git a/references/cli/livediff.go b/references/cli/livediff.go index e8de9c47a..ea4de3afe 100644 --- a/references/cli/livediff.go +++ b/references/cli/livediff.go @@ -73,11 +73,11 @@ func NewLiveDiffCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Comma return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { - velaEnv, err := GetFlagEnvOrCurrent(cmd, c) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - buff, err := LiveDiffApplication(o, c, velaEnv.Namespace) + buff, err := LiveDiffApplication(o, c, namespace) if err != nil { return err } @@ -90,6 +90,7 @@ func NewLiveDiffCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Comma cmd.Flags().StringVarP(&o.DefinitionFile, "definition", "d", "", "specify a file or directory containing capability definitions, they will only be used in dry-run rather than applied to K8s cluster") cmd.Flags().StringVarP(&o.Revision, "Revision", "r", "", "specify an application Revision name, by default, it will compare with the latest Revision") cmd.Flags().IntVarP(&o.Context, "context", "c", -1, "output number lines of context around changes, by default show all unchanged lines") + addNamespaceArg(cmd) cmd.SetOut(ioStreams.Out) return cmd } diff --git a/references/cli/ls.go b/references/cli/ls.go index 7d2675e9c..3cf5d6baa 100644 --- a/references/cli/ls.go +++ b/references/cli/ls.go @@ -44,28 +44,21 @@ func NewListCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Command { return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { - env, err := GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err - } newClient, err := c.GetClient() if err != nil { return err } - namespace, err := cmd.Flags().GetString(Namespace) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - if namespace == "" { - namespace = env.Namespace - } return printApplicationList(ctx, newClient, namespace, ioStreams) }, Annotations: map[string]string{ types.TagCommandType: types.TypeApp, }, } - cmd.PersistentFlags().StringP(Namespace, "n", "", "specify the namespace the application want to list, default is the current env namespace") + addNamespaceArg(cmd) return cmd } diff --git a/references/cli/show.go b/references/cli/show.go index b8708d704..3b0935ced 100644 --- a/references/cli/show.go +++ b/references/cli/show.go @@ -74,14 +74,14 @@ func NewCapabilityShowCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra } ctx := context.Background() capabilityName := args[0] - velaEnv, err := GetFlagEnvOrCurrent(cmd, c) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } if webSite { - return startReferenceDocsSite(ctx, velaEnv.Namespace, c, ioStreams, capabilityName) + return startReferenceDocsSite(ctx, namespace, c, ioStreams, capabilityName) } - return ShowReferenceConsole(ctx, c, ioStreams, capabilityName, velaEnv.Namespace) + return ShowReferenceConsole(ctx, c, ioStreams, capabilityName, namespace) }, Annotations: map[string]string{ types.TagCommandType: types.TypeStart, @@ -89,6 +89,7 @@ func NewCapabilityShowCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra } cmd.Flags().BoolVarP(&webSite, "web", "", false, " start web doc site") + addNamespaceArg(cmd) cmd.SetOut(ioStreams.Out) return cmd } diff --git a/references/cli/status.go b/references/cli/status.go index a4a380f79..035b0f753 100644 --- a/references/cli/status.go +++ b/references/cli/status.go @@ -92,38 +92,37 @@ func NewAppStatusCommand(c common.Args, ioStreams cmdutil.IOStreams) *cobra.Comm return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { + namespace, err := GetFlagNamespaceOrEnv(cmd, c) + if err != nil { + return err + } argsLength := len(args) if argsLength == 0 { ioStreams.Errorf("Hint: please specify an application") os.Exit(1) } appName := args[0] - env, err := GetFlagEnvOrCurrent(cmd, c) - if err != nil { - ioStreams.Errorf("Error: failed to get Env: %s", err) - return err - } newClient, err := c.GetClient() if err != nil { return err } - return printAppStatus(ctx, newClient, ioStreams, appName, env, cmd, c) + return printAppStatus(ctx, newClient, ioStreams, appName, namespace, cmd, c) }, Annotations: map[string]string{ types.TagCommandType: types.TypeApp, }, } cmd.Flags().StringP("svc", "s", "", "service name") + addNamespaceArg(cmd) cmd.SetOut(ioStreams.Out) return cmd } -func printAppStatus(_ context.Context, c client.Client, ioStreams cmdutil.IOStreams, appName string, env *types.EnvMeta, cmd *cobra.Command, velaC common.Args) error { - app, err := appfile.LoadApplication(env.Namespace, appName, velaC) +func printAppStatus(_ context.Context, c client.Client, ioStreams cmdutil.IOStreams, appName string, namespace string, cmd *cobra.Command, velaC common.Args) error { + app, err := appfile.LoadApplication(namespace, appName, velaC) if err != nil { return err } - namespace := env.Namespace cmd.Printf("About:\n\n") table := newUITable() @@ -133,7 +132,7 @@ func printAppStatus(_ context.Context, c client.Client, ioStreams cmdutil.IOStre cmd.Printf("%s\n\n", table.String()) cmd.Printf("Services:\n\n") - return loopCheckStatus(c, ioStreams, appName, env) + return loopCheckStatus(c, ioStreams, appName, namespace) } func loadRemoteApplication(c client.Client, ns string, name string) (*v1beta1.Application, error) { @@ -154,8 +153,8 @@ func getComponentType(app *v1beta1.Application, name string) string { return "webservice" } -func loopCheckStatus(c client.Client, ioStreams cmdutil.IOStreams, appName string, env *types.EnvMeta) error { - remoteApp, err := loadRemoteApplication(c, env.Namespace, appName) +func loopCheckStatus(c client.Client, ioStreams cmdutil.IOStreams, appName string, namespace string) error { + remoteApp, err := loadRemoteApplication(c, namespace, appName) if err != nil { return err } @@ -173,7 +172,7 @@ func loopCheckStatus(c client.Client, ioStreams cmdutil.IOStreams, appName strin ioStreams.Infof(" %s %s\n", healthColor.Sprint(healthstats), healthColor.Sprint(healthInfo)) // load it again after health check - remoteApp, err = loadRemoteApplication(c, env.Namespace, appName) + remoteApp, err = loadRemoteApplication(c, namespace, appName) if err != nil { return err } @@ -194,14 +193,14 @@ func loopCheckStatus(c client.Client, ioStreams cmdutil.IOStreams, appName strin return nil } -func printTrackingDeployStatus(c common.Args, ioStreams cmdutil.IOStreams, appName string, env *types.EnvMeta) (CompStatus, error) { +func printTrackingDeployStatus(c common.Args, ioStreams cmdutil.IOStreams, appName string, namespace string) (CompStatus, error) { sDeploy := newTrackingSpinnerWithDelay("Checking Status ...", trackingInterval) sDeploy.Start() defer sDeploy.Stop() TrackDeployLoop: for { time.Sleep(trackingInterval) - deployStatus, failMsg, err := TrackDeployStatus(c, appName, env) + deployStatus, failMsg, err := TrackDeployStatus(c, appName, namespace) if err != nil { return compStatusUnknown, err } @@ -223,8 +222,8 @@ TrackDeployLoop: } // TrackDeployStatus will only check AppConfig is deployed successfully, -func TrackDeployStatus(c common.Args, appName string, env *types.EnvMeta) (CompStatus, string, error) { - appObj, err := appfile.LoadApplication(env.Namespace, appName, c) +func TrackDeployStatus(c common.Args, appName string, namespace string) (CompStatus, string, error) { + appObj, err := appfile.LoadApplication(namespace, appName, c) if err != nil { return compStatusUnknown, "", err } diff --git a/references/cli/up.go b/references/cli/up.go index 3ab14bce7..c70086e6d 100644 --- a/references/cli/up.go +++ b/references/cli/up.go @@ -46,7 +46,7 @@ func NewUpCommand(c common2.Args, ioStream cmdutil.IOStreams) *cobra.Command { return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { - velaEnv, err := GetFlagEnvOrCurrent(cmd, c) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } @@ -70,16 +70,17 @@ func NewUpCommand(c common2.Args, ioStream cmdutil.IOStreams) *cobra.Command { } } else { o := &common.AppfileOptions{ - Kubecli: kubecli, - IO: ioStream, - Env: velaEnv, + Kubecli: kubecli, + IO: ioStream, + Namespace: namespace, } - return o.Run(*appFilePath, velaEnv.Namespace, c) + return o.Run(*appFilePath, o.Namespace, c) } return nil }, } cmd.SetOut(ioStream.Out) cmd.Flags().StringVarP(appFilePath, "file", "f", "", "specify file path for appfile") + addNamespaceArg(cmd) return cmd } diff --git a/references/cli/up_test.go b/references/cli/up_test.go index 5f6fa4645..e17cc6f12 100644 --- a/references/cli/up_test.go +++ b/references/cli/up_test.go @@ -25,7 +25,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/oam-dev/kubevela/apis/core.oam.dev/v1beta1" - "github.com/oam-dev/kubevela/apis/types" common2 "github.com/oam-dev/kubevela/pkg/utils/common" "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/references/common" @@ -33,13 +32,10 @@ import ( func TestUp(t *testing.T) { ioStream := util.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr} - env := types.EnvMeta{ - Name: "up", - Namespace: "env-up", - } + namespace := "up-ns" o := common.AppfileOptions{ - IO: ioStream, - Env: &env, + IO: ioStream, + Namespace: namespace, } app := &v1beta1.Application{} app.Name = "app-up" diff --git a/references/cli/workflow.go b/references/cli/workflow.go index 769ee0068..ee67df8a5 100644 --- a/references/cli/workflow.go +++ b/references/cli/workflow.go @@ -60,17 +60,10 @@ func NewWorkflowSuspendCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra if len(args) < 1 { return fmt.Errorf("must specify application name") } - namespace, err := cmd.Flags().GetString(FlagNamespace) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - if namespace == "" { - env, err := GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err - } - namespace = env.Namespace - } app, err := appfile.LoadApplication(namespace, args[0], c) if err != nil { return err @@ -93,7 +86,7 @@ func NewWorkflowSuspendCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra return nil }, } - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, uses namespace in env.") + addNamespaceArg(cmd) return cmd } @@ -108,17 +101,10 @@ func NewWorkflowResumeCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra. if len(args) < 1 { return fmt.Errorf("must specify application name") } - namespace, err := cmd.Flags().GetString(FlagNamespace) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - if namespace == "" { - env, err := GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err - } - namespace = env.Namespace - } app, err := appfile.LoadApplication(namespace, args[0], c) if err != nil { return err @@ -151,7 +137,7 @@ func NewWorkflowResumeCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra. return nil }, } - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, uses namespace in env.") + addNamespaceArg(cmd) return cmd } @@ -166,17 +152,10 @@ func NewWorkflowTerminateCommand(c common.Args, ioStream cmdutil.IOStreams) *cob if len(args) < 1 { return fmt.Errorf("must specify application name") } - namespace, err := cmd.Flags().GetString(FlagNamespace) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - if namespace == "" { - env, err := GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err - } - namespace = env.Namespace - } app, err := appfile.LoadApplication(namespace, args[0], c) if err != nil { return err @@ -199,7 +178,7 @@ func NewWorkflowTerminateCommand(c common.Args, ioStream cmdutil.IOStreams) *cob return nil }, } - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, uses namespace in env.") + addNamespaceArg(cmd) return cmd } @@ -214,17 +193,10 @@ func NewWorkflowRestartCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra if len(args) < 1 { return fmt.Errorf("must specify application name") } - namespace, err := cmd.Flags().GetString(FlagNamespace) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - if namespace == "" { - env, err := GetFlagEnvOrCurrent(cmd, c) - if err != nil { - return err - } - namespace = env.Namespace - } app, err := appfile.LoadApplication(namespace, args[0], c) if err != nil { return err @@ -247,7 +219,7 @@ func NewWorkflowRestartCommand(c common.Args, ioStream cmdutil.IOStreams) *cobra return nil }, } - cmd.Flags().StringP(FlagNamespace, "n", "", "Specify which namespace to get. If empty, uses namespace in env.") + addNamespaceArg(cmd) return cmd } diff --git a/references/cli/workloads.go b/references/cli/workloads.go index b665ed77f..7857f1829 100644 --- a/references/cli/workloads.go +++ b/references/cli/workloads.go @@ -38,17 +38,18 @@ func NewWorkloadsCommand(c common2.Args, ioStreams cmdutil.IOStreams) *cobra.Com return c.SetConfig() }, RunE: func(cmd *cobra.Command, args []string) error { - env, err := GetFlagEnvOrCurrent(cmd, c) + namespace, err := GetFlagNamespaceOrEnv(cmd, c) if err != nil { return err } - return printWorkloadList(env.Namespace, c, ioStreams) + return printWorkloadList(namespace, c, ioStreams) }, Annotations: map[string]string{ types.TagCommandType: types.TypeCap, }, } cmd.SetOut(ioStreams.Out) + addNamespaceArg(cmd) return cmd } diff --git a/references/common/application.go b/references/common/application.go index 3d1498bae..3f4d7a6f1 100644 --- a/references/common/application.go +++ b/references/common/application.go @@ -46,9 +46,9 @@ import ( // AppfileOptions is some configuration that modify options for an Appfile type AppfileOptions struct { - Kubecli client.Client - IO cmdutil.IOStreams - Env *types.EnvMeta + Kubecli client.Client + IO cmdutil.IOStreams + Namespace string } // BuildResult is the export struct from AppFile yaml or AppFile object @@ -68,18 +68,18 @@ type Option struct { // DeleteOptions is options for delete type DeleteOptions struct { - AppName string - CompName string - Client client.Client - Env *types.EnvMeta - C common.Args + Namespace string + AppName string + CompName string + Client client.Client + C common.Args } // DeleteApp will delete app including server side func (o *DeleteOptions) DeleteApp() (string, error) { ctx := context.Background() var app = new(corev1beta1.Application) - err := o.Client.Get(ctx, client.ObjectKey{Name: o.AppName, Namespace: o.Env.Namespace}, app) + err := o.Client.Get(ctx, client.ObjectKey{Name: o.AppName, Namespace: o.Namespace}, app) if err != nil { if apierrors.IsNotFound(err) { return fmt.Sprintf("app \"%s\" already deleted", o.AppName), nil @@ -96,7 +96,7 @@ func (o *DeleteOptions) DeleteApp() (string, error) { healthScopeName, ok := cmp.Scopes[api.DefaultHealthScopeKey] if ok { var healthScope corev1alpha2.HealthScope - if err := o.Client.Get(ctx, client.ObjectKey{Namespace: o.Env.Namespace, Name: healthScopeName}, &healthScope); err != nil { + if err := o.Client.Get(ctx, client.ObjectKey{Namespace: o.Namespace, Name: healthScopeName}, &healthScope); err != nil { if apierrors.IsNotFound(err) { continue } @@ -107,7 +107,7 @@ func (o *DeleteOptions) DeleteApp() (string, error) { } } } - return fmt.Sprintf("app \"%s\" deleted from env \"%s\"", o.AppName, o.Env.Name), nil + return fmt.Sprintf("app \"%s\" already deleted from namespace \"%s\"", o.AppName, o.Namespace), nil } // DeleteComponent will delete one component including server side. @@ -116,7 +116,7 @@ func (o *DeleteOptions) DeleteComponent(io cmdutil.IOStreams) (string, error) { if o.AppName == "" { return "", errors.New("app name is required") } - app, err := appfile.LoadApplication(o.Env.Namespace, o.AppName, o.C) + app, err := appfile.LoadApplication(o.Namespace, o.AppName, o.C) if err != nil { return "", err } @@ -181,7 +181,7 @@ func (o *AppfileOptions) ExportFromAppFile(app *api.AppFile, namespace string, q appHandler := appfile.NewApplication(app, tm) // new - retApplication, scopes, err := appHandler.BuildOAMApplication(o.Env, o.IO, appHandler.Tm, quiet) + retApplication, scopes, err := appHandler.BuildOAMApplication(o.Namespace, o.IO, appHandler.Tm, quiet) if err != nil { return nil, nil, err } @@ -251,7 +251,7 @@ func (o *AppfileOptions) Run(filePath, namespace string, c common.Args) error { // BaseAppFileRun starts an application according to Appfile func (o *AppfileOptions) BaseAppFileRun(result *BuildResult, args common.Args) error { - kubernetesComponent, err := appfile.ApplyTerraform(result.application, o.Kubecli, o.IO, o.Env.Namespace, args) + kubernetesComponent, err := appfile.ApplyTerraform(result.application, o.Kubecli, o.IO, o.Namespace, args) if err != nil { return err } diff --git a/references/common/common.go b/references/common/common.go index 4fca3e7ce..1fbfdcaa3 100644 --- a/references/common/common.go +++ b/references/common/common.go @@ -21,15 +21,14 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/oam-dev/kubevela/apis/types" "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/references/appfile" "github.com/oam-dev/kubevela/references/appfile/api" ) // BuildRun will build application and deploy from Appfile -func BuildRun(ctx context.Context, app *api.Application, client client.Client, env *types.EnvMeta, io util.IOStreams) error { - o, scopes, err := app.BuildOAMApplication(env, io, app.Tm, true) +func BuildRun(ctx context.Context, app *api.Application, client client.Client, namespace string, io util.IOStreams) error { + o, scopes, err := app.BuildOAMApplication(namespace, io, app.Tm, true) if err != nil { return err } diff --git a/references/common/workload.go b/references/common/workload.go index fe06f9260..f77065fd7 100644 --- a/references/common/workload.go +++ b/references/common/workload.go @@ -25,28 +25,14 @@ import ( "cuelang.org/go/cue" "github.com/spf13/pflag" - "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/oam-dev/kubevela/apis/types" - "github.com/oam-dev/kubevela/pkg/utils/util" "github.com/oam-dev/kubevela/references/appfile" "github.com/oam-dev/kubevela/references/appfile/api" "github.com/oam-dev/kubevela/references/plugins" ) -// RunOptions include all options for run -type RunOptions struct { - Env *types.EnvMeta - WorkloadName string - KubeClient client.Client - App *api.Application - AppName string - Staging bool - util.IOStreams -} - // InitApplication will load Application from cluster -func InitApplication(env *types.EnvMeta, c common.Args, workloadName string, appGroup string) (*api.Application, error) { +func InitApplication(namespace string, c common.Args, workloadName string, appGroup string) (*api.Application, error) { var appName string if appGroup != "" { appName = appGroup @@ -56,7 +42,7 @@ func InitApplication(env *types.EnvMeta, c common.Args, workloadName string, app // TODO(wonderflow): we should load the existing application from cluster and convert to appfile // app, err := appfile.LoadApplication(env.Namespace, appName, c) // compatible application not found - app, err := appfile.NewEmptyApplication(env.Namespace, c) + app, err := appfile.NewEmptyApplication(namespace, c) if err != nil { return nil, err } @@ -66,8 +52,8 @@ func InitApplication(env *types.EnvMeta, c common.Args, workloadName string, app } // BaseComplete will construct an Application from cli parameters. -func BaseComplete(env *types.EnvMeta, c common.Args, workloadName string, appName string, flagSet *pflag.FlagSet, workloadType string) (*api.Application, error) { - app, err := InitApplication(env, c, workloadName, appName) +func BaseComplete(namespace string, c common.Args, workloadName string, appName string, flagSet *pflag.FlagSet, workloadType string) (*api.Application, error) { + app, err := InitApplication(namespace, c, workloadName, appName) if err != nil { return nil, err } @@ -79,7 +65,7 @@ func BaseComplete(env *types.EnvMeta, c common.Args, workloadName string, appNam // Not exist tp = workloadType } - template, err := plugins.LoadCapabilityByName(tp, env.Namespace, c) + template, err := plugins.LoadCapabilityByName(tp, namespace, c) if err != nil { return nil, err }