mirror of
https://github.com/kubevela/kubevela.git
synced 2026-05-14 05:16:49 +00:00
* Implement API `api/envs/default/apps/ implement API `api/envs/default/apps/ and refactor code * address comments from @wonderflow and @ryan * fix code rebase issue * Implement env APIs implemented APIs for env and make api-test and e2e-setup * fix ci issues * address comments
195 lines
5.4 KiB
Go
195 lines
5.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
|
|
"github.com/cloud-native-application/rudrx/api/types"
|
|
cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util"
|
|
"github.com/cloud-native-application/rudrx/pkg/oam"
|
|
"github.com/cloud-native-application/rudrx/pkg/utils/system"
|
|
|
|
"github.com/gosuri/uitable"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func EnvCommandGroup(parentCmd *cobra.Command, c types.Args, ioStream cmdutil.IOStreams) {
|
|
parentCmd.AddCommand(NewEnvInitCommand(c, ioStream),
|
|
NewEnvSwitchCommand(ioStream),
|
|
NewEnvDeleteCommand(ioStream),
|
|
NewEnvCommand(ioStream),
|
|
)
|
|
}
|
|
|
|
func NewEnvCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
|
|
ctx := context.Background()
|
|
cmd := &cobra.Command{
|
|
Use: "env",
|
|
DisableFlagsInUseLine: true,
|
|
Short: "List environments",
|
|
Long: "List all environments",
|
|
Example: `vela env [env-name]`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return ListEnvs(ctx, args, ioStreams)
|
|
},
|
|
Annotations: map[string]string{
|
|
types.TagCommandType: types.TypeStart,
|
|
},
|
|
}
|
|
cmd.SetOut(ioStreams.Out)
|
|
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
|
|
cmdutil.PrintUsageIntroduce(cmd, "Prepare environments for applications")
|
|
subcmds := []*cobra.Command{cmd, NewEnvInitCommand(types.Args{}, ioStreams), NewEnvSwitchCommand(ioStreams), NewEnvDeleteCommand(ioStreams)}
|
|
cmdutil.PrintUsage(cmd, subcmds)
|
|
cmdutil.PrintExample(cmd, subcmds)
|
|
cmdutil.PrintFlags(cmd, subcmds)
|
|
})
|
|
return cmd
|
|
}
|
|
|
|
func NewEnvInitCommand(c types.Args, ioStreams cmdutil.IOStreams) *cobra.Command {
|
|
var envArgs types.EnvMeta
|
|
ctx := context.Background()
|
|
cmd := &cobra.Command{
|
|
Use: "env:init <envName>",
|
|
DisableFlagsInUseLine: true,
|
|
Short: "Create environments",
|
|
Long: "Create environment and switch to it",
|
|
Example: `vela env:init test --namespace test`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
newClient, err := client.New(c.Config, client.Options{Scheme: c.Schema})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return CreateOrUpdateEnv(ctx, newClient, &envArgs, args, ioStreams)
|
|
},
|
|
Annotations: map[string]string{
|
|
types.TagCommandType: types.TypeStart,
|
|
},
|
|
}
|
|
cmd.SetOut(ioStreams.Out)
|
|
cmd.Flags().StringVar(&envArgs.Namespace, "namespace", "default", "specify K8s namespace for env")
|
|
return cmd
|
|
}
|
|
|
|
func NewEnvDeleteCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
|
|
ctx := context.Background()
|
|
cmd := &cobra.Command{
|
|
Use: "env:delete",
|
|
DisableFlagsInUseLine: true,
|
|
Short: "Delete environment",
|
|
Long: "Delete environment",
|
|
Example: `vela env:delete test`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return DeleteEnv(ctx, args, ioStreams)
|
|
},
|
|
Annotations: map[string]string{
|
|
types.TagCommandType: types.TypeStart,
|
|
},
|
|
}
|
|
cmd.SetOut(ioStreams.Out)
|
|
return cmd
|
|
}
|
|
|
|
func NewEnvSwitchCommand(ioStreams cmdutil.IOStreams) *cobra.Command {
|
|
ctx := context.Background()
|
|
cmd := &cobra.Command{
|
|
Use: "env:sw",
|
|
DisableFlagsInUseLine: true,
|
|
Short: "Switch environments",
|
|
Long: "switch to another environment",
|
|
Example: `vela env:sw test`,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return SwitchEnv(ctx, args, ioStreams)
|
|
},
|
|
Annotations: map[string]string{
|
|
types.TagCommandType: types.TypeStart,
|
|
},
|
|
}
|
|
cmd.SetOut(ioStreams.Out)
|
|
return cmd
|
|
}
|
|
|
|
func ListEnvs(ctx context.Context, args []string, ioStreams cmdutil.IOStreams) error {
|
|
table := uitable.New()
|
|
table.MaxColWidth = 60
|
|
table.AddRow("NAME", "CURRENT", "NAMESPACE")
|
|
var envName = ""
|
|
if len(args) > 0 {
|
|
envName = args[0]
|
|
}
|
|
envList, err := oam.ListEnvs(envName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, env := range envList {
|
|
table.AddRow(env.Name, env.Current, env.Namespace)
|
|
}
|
|
ioStreams.Info(table.String())
|
|
return nil
|
|
}
|
|
|
|
func DeleteEnv(ctx context.Context, args []string, ioStreams cmdutil.IOStreams) error {
|
|
if len(args) < 1 {
|
|
return fmt.Errorf("you must specify env name for vela env:delete command")
|
|
}
|
|
envName := args[0]
|
|
msg, err := oam.DeleteEnv(envName)
|
|
if err == nil {
|
|
ioStreams.Info(msg)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func CreateOrUpdateEnv(ctx context.Context, c client.Client, envArgs *types.EnvMeta, args []string, ioStreams cmdutil.IOStreams) error {
|
|
if len(args) < 1 {
|
|
return fmt.Errorf("you must specify env name for vela env:init command")
|
|
}
|
|
envName := args[0]
|
|
namespace := envArgs.Namespace
|
|
err, msg := oam.CreateOrUpdateEnv(ctx, c, envName, namespace)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ioStreams.Info(msg)
|
|
return nil
|
|
}
|
|
|
|
func SwitchEnv(ctx context.Context, args []string, ioStreams cmdutil.IOStreams) error {
|
|
if len(args) < 1 {
|
|
return fmt.Errorf("you must specify env name for vela env command")
|
|
}
|
|
envName := args[0]
|
|
msg, err := oam.SwitchEnv(envName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ioStreams.Info(msg)
|
|
return nil
|
|
}
|
|
|
|
func GetEnv(cmd *cobra.Command) (*types.EnvMeta, error) {
|
|
var envName string
|
|
var err error
|
|
if cmd != nil {
|
|
envName = cmd.Flag("env").Value.String()
|
|
}
|
|
if envName != "" {
|
|
return oam.GetEnvByName(envName)
|
|
}
|
|
envName, err = oam.GetCurrentEnvName()
|
|
if err != nil {
|
|
if !os.IsNotExist(err) {
|
|
return nil, err
|
|
}
|
|
if err = system.InitDefaultEnv(); err != nil {
|
|
return nil, err
|
|
}
|
|
envName = types.DefaultEnvName
|
|
}
|
|
return oam.GetEnvByName(envName)
|
|
}
|