From 84d18e43afdcd496588fe025207e8d3d9440969f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A9=E5=85=83?= Date: Thu, 6 Aug 2020 16:03:53 +0800 Subject: [PATCH] fix env init don't create namespace --- cmd/rudrx/main.go | 8 ++++---- pkg/cmd/env.go | 24 ++++++++++++++++-------- pkg/cmd/env_test.go | 6 ++++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/rudrx/main.go b/cmd/rudrx/main.go index 0a30af4a7..25c5a6cc9 100644 --- a/cmd/rudrx/main.go +++ b/cmd/rudrx/main.go @@ -94,10 +94,10 @@ func newCommand() *cobra.Command { cmd.NewInitCommand(f, client, ioStream), cmd.NewDeleteCommand(f, client, ioStream, os.Args[1:]), cmd.NewAppsCommand(f, client, ioStream), - cmd.NewEnvInitCommand(f, ioStream), - cmd.NewEnvSwitchCommand(f, ioStream), - cmd.NewEnvDeleteCommand(f, ioStream), - cmd.NewEnvCommand(f, ioStream), + cmd.NewEnvInitCommand(client, ioStream), + cmd.NewEnvSwitchCommand(ioStream), + cmd.NewEnvDeleteCommand(ioStream), + cmd.NewEnvCommand(ioStream), NewVersionCommand(), cmd.NewAppStatusCommand(client, ioStream), ) diff --git a/pkg/cmd/env.go b/pkg/cmd/env.go index ed72bde40..768aa89b8 100644 --- a/pkg/cmd/env.go +++ b/pkg/cmd/env.go @@ -8,16 +8,20 @@ import ( "os" "path/filepath" - "github.com/cloud-native-application/rudrx/api/types" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "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/utils/system" - cmdutil "github.com/cloud-native-application/rudrx/pkg/cmd/util" "github.com/gosuri/uitable" "github.com/spf13/cobra" ) -func NewEnvInitCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { +func NewEnvInitCommand(c client.Client, ioStreams cmdutil.IOStreams) *cobra.Command { var envArgs types.EnvMeta ctx := context.Background() cmd := &cobra.Command{ @@ -27,7 +31,7 @@ func NewEnvInitCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Co Long: "Create environment and switch to it", Example: `rudr env:init test --namespace test`, RunE: func(cmd *cobra.Command, args []string) error { - return CreateOrUpdateEnv(ctx, &envArgs, args, ioStreams) + return CreateOrUpdateEnv(ctx, c, &envArgs, args, ioStreams) }, } cmd.SetOut(ioStreams.Out) @@ -35,7 +39,7 @@ func NewEnvInitCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Co return cmd } -func NewEnvDeleteCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { +func NewEnvDeleteCommand(ioStreams cmdutil.IOStreams) *cobra.Command { ctx := context.Background() cmd := &cobra.Command{ Use: "env:delete", @@ -51,7 +55,7 @@ func NewEnvDeleteCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra. return cmd } -func NewEnvCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { +func NewEnvCommand(ioStreams cmdutil.IOStreams) *cobra.Command { ctx := context.Background() cmd := &cobra.Command{ Use: "env", @@ -67,7 +71,7 @@ func NewEnvCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Comman return cmd } -func NewEnvSwitchCommand(f cmdutil.Factory, ioStreams cmdutil.IOStreams) *cobra.Command { +func NewEnvSwitchCommand(ioStreams cmdutil.IOStreams) *cobra.Command { ctx := context.Background() cmd := &cobra.Command{ Use: "env:sw", @@ -146,7 +150,7 @@ func DeleteEnv(ctx context.Context, args []string, ioStreams cmdutil.IOStreams) return nil } -func CreateOrUpdateEnv(ctx context.Context, envArgs *types.EnvMeta, args []string, ioStreams cmdutil.IOStreams) error { +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 rudr env:init command") } @@ -166,6 +170,10 @@ func CreateOrUpdateEnv(ctx context.Context, envArgs *types.EnvMeta, args []strin if err != nil { return err } + if err := c.Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: envArgs.Namespace}}); err != nil && !apierrors.IsAlreadyExists(err) { + return err + } + if err = ioutil.WriteFile(curEnvPath, []byte(envname), 0644); err != nil { return err } diff --git a/pkg/cmd/env_test.go b/pkg/cmd/env_test.go index 52a350637..1d27e211b 100644 --- a/pkg/cmd/env_test.go +++ b/pkg/cmd/env_test.go @@ -6,6 +6,8 @@ import ( "os" "testing" + "github.com/crossplane/crossplane-runtime/pkg/test" + "github.com/cloud-native-application/rudrx/api/types" "github.com/cloud-native-application/rudrx/pkg/utils/system" @@ -35,9 +37,9 @@ func TestENV(t *testing.T) { exp := &types.EnvMeta{ Namespace: "test1", } - + client := test.NewMockClient() // Create env1 - err = CreateOrUpdateEnv(ctx, exp, []string{"env1"}, ioStream) + err = CreateOrUpdateEnv(ctx, client, exp, []string{"env1"}, ioStream) assert.NoError(t, err) // check and compare create env success