From eeafd43513cbacb11c58c50b8ff0ab04c564b545 Mon Sep 17 00:00:00 2001 From: Richard Marshall Date: Tue, 30 Jul 2019 20:52:06 -0700 Subject: [PATCH] Remove import of k8sdeps from create command --- pkg/commands/commands.go | 2 +- pkg/commands/create/create.go | 30 +++++++++++++++--------------- pkg/commands/create/create_test.go | 26 +++++++++++++++----------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 7eeadf3d1..6fe5848de 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -45,7 +45,7 @@ See https://sigs.k8s.io/kustomize stdOut, fSys, v, rf, pf), edit.NewCmdEdit(fSys, v, uf), - create.NewCmdCreate(fSys), + create.NewCmdCreate(fSys, uf), misc.NewCmdConfig(fSys), misc.NewCmdVersion(stdOut), ) diff --git a/pkg/commands/create/create.go b/pkg/commands/create/create.go index d38e8c293..4dcbb516d 100644 --- a/pkg/commands/create/create.go +++ b/pkg/commands/create/create.go @@ -7,18 +7,19 @@ import ( "fmt" "os" "path/filepath" + "strings" "github.com/spf13/cobra" - "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" "sigs.k8s.io/kustomize/v3/pkg/commands/util" "sigs.k8s.io/kustomize/v3/pkg/fs" + "sigs.k8s.io/kustomize/v3/pkg/ifc" "sigs.k8s.io/kustomize/v3/pkg/pgmconfig" ) type createFlags struct { - resources []string + resources string namespace string annotations string labels string @@ -30,7 +31,7 @@ type createFlags struct { } // NewCmdCreate returns an instance of 'create' subcommand. -func NewCmdCreate(fSys fs.FileSystem) *cobra.Command { +func NewCmdCreate(fSys fs.FileSystem, uf ifc.KunstructuredFactory) *cobra.Command { opts := createFlags{path: "."} c := &cobra.Command{ Use: "create", @@ -38,22 +39,22 @@ func NewCmdCreate(fSys fs.FileSystem) *cobra.Command { Long: "", Example: ` # Create a new overlay from the base '../base". - kustomize create --resource ../base + kustomize create --resources ../base # Create a new kustomization detecting resources in the current directory. kustomize create --autodetect # Create a new kustomization with multiple resources and fields set. - kustomize create --resource depoyment.yaml --resource service.yaml --namespace staging --nameprefix acme- + kustomize create --resources deployment.yaml,service.yaml,../base --namespace staging --nameprefix acme- `, RunE: func(cmd *cobra.Command, args []string) error { - return runCreate(opts, fSys) + return runCreate(opts, fSys, uf) }, } - c.Flags().StringSliceVar( + c.Flags().StringVar( &opts.resources, - "resource", - []string{}, + "resources", + "", "Name of a file containing a file to add to the kustomization file.") c.Flags().StringVar( &opts.namespace, @@ -93,8 +94,8 @@ func NewCmdCreate(fSys fs.FileSystem) *cobra.Command { return c } -func runCreate(opts createFlags, fSys fs.FileSystem) error { - resources, err := util.GlobPatterns(fSys, opts.resources) +func runCreate(opts createFlags, fSys fs.FileSystem, uf ifc.KunstructuredFactory) error { + resources, err := util.GlobPatterns(fSys, strings.Split(opts.resources, ",")) if err != nil { return err } @@ -102,7 +103,7 @@ func runCreate(opts createFlags, fSys fs.FileSystem) error { return fmt.Errorf("kustomization file already exists") } if opts.detectResources { - detected, err := detectResources(fSys, opts.path, opts.detectRecursive) + detected, err := detectResources(fSys, uf, opts.path, opts.detectRecursive) if err != nil { return err } @@ -143,9 +144,8 @@ func runCreate(opts createFlags, fSys fs.FileSystem) error { return mf.Write(m) } -func detectResources(fSys fs.FileSystem, base string, recursive bool) ([]string, error) { +func detectResources(fSys fs.FileSystem, uf ifc.KunstructuredFactory, base string, recursive bool) ([]string, error) { var paths []string - factory := kunstruct.NewKunstructuredFactoryImpl() err := fSys.Walk(base, func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -171,7 +171,7 @@ func detectResources(fSys fs.FileSystem, base string, recursive bool) ([]string, if err != nil { return err } - if _, err := factory.SliceFromBytes(fContents); err != nil { + if _, err := uf.SliceFromBytes(fContents); err != nil { return nil } paths = append(paths, path) diff --git a/pkg/commands/create/create_test.go b/pkg/commands/create/create_test.go index 9ced074a9..6bf1840db 100644 --- a/pkg/commands/create/create_test.go +++ b/pkg/commands/create/create_test.go @@ -7,11 +7,14 @@ import ( "reflect" "testing" + "sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct" "sigs.k8s.io/kustomize/v3/pkg/commands/kustfile" "sigs.k8s.io/kustomize/v3/pkg/fs" "sigs.k8s.io/kustomize/v3/pkg/types" ) +var factory = kunstruct.NewKunstructuredFactoryImpl() + func readKustomizationFS(t *testing.T, fakeFS fs.FileSystem) *types.Kustomization { kf, err := kustfile.NewKustomizationFile(fakeFS) if err != nil { @@ -25,7 +28,7 @@ func readKustomizationFS(t *testing.T, fakeFS fs.FileSystem) *types.Kustomizatio } func TestCreateNoArgs(t *testing.T) { fakeFS := fs.MakeFakeFS() - cmd := NewCmdCreate(fakeFS) + cmd := NewCmdCreate(fakeFS, factory) err := cmd.RunE(cmd, []string{}) if err != nil { t.Errorf("unexpected cmd error: %v", err) @@ -36,13 +39,14 @@ func TestCreateNoArgs(t *testing.T) { func TestCreateWithResources(t *testing.T) { fakeFS := fs.MakeFakeFS() fakeFS.WriteFile("foo.yaml", []byte("")) - opts := createFlags{resources: []string{"foo.yaml"}} - err := runCreate(opts, fakeFS) + fakeFS.WriteFile("bar.yaml", []byte("")) + opts := createFlags{resources: "foo.yaml,bar.yaml"} + err := runCreate(opts, fakeFS, factory) if err != nil { t.Errorf("unexpected cmd error: %v", err) } m := readKustomizationFS(t, fakeFS) - expected := []string{"foo.yaml"} + expected := []string{"foo.yaml", "bar.yaml"} if !reflect.DeepEqual(m.Resources, expected) { t.Fatalf("expected %+v but got %+v", expected, m.Resources) } @@ -52,7 +56,7 @@ func TestCreateWithNamespace(t *testing.T) { fakeFS := fs.MakeFakeFS() want := "foo" opts := createFlags{namespace: want} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Errorf("unexpected cmd error: %v", err) } @@ -66,7 +70,7 @@ func TestCreateWithNamespace(t *testing.T) { func TestCreateWithLabels(t *testing.T) { fakeFS := fs.MakeFakeFS() opts := createFlags{labels: "foo:bar"} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Errorf("unexpected cmd error: %v", err) } @@ -80,7 +84,7 @@ func TestCreateWithLabels(t *testing.T) { func TestCreateWithAnnotations(t *testing.T) { fakeFS := fs.MakeFakeFS() opts := createFlags{annotations: "foo:bar"} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Errorf("unexpected cmd error: %v", err) } @@ -95,7 +99,7 @@ func TestCreateWithNamePrefix(t *testing.T) { fakeFS := fs.MakeFakeFS() want := "foo-" opts := createFlags{prefix: want} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Errorf("unexpected cmd error: %v", err) } @@ -109,7 +113,7 @@ func TestCreateWithNamePrefix(t *testing.T) { func TestCreateWithNameSuffix(t *testing.T) { fakeFS := fs.MakeFakeFS() opts := createFlags{suffix: "-foo"} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Errorf("unexpected cmd error: %v", err) } @@ -152,7 +156,7 @@ func TestCreateWithDetect(t *testing.T) { fakeFS := fs.MakeFakeFS() writeDetectContent(fakeFS) opts := createFlags{path: "/", detectResources: true} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Fatalf("unexpected cmd error: %v", err) } @@ -167,7 +171,7 @@ func TestCreateWithDetectRecursive(t *testing.T) { fakeFS := fs.MakeFakeFS() writeDetectContent(fakeFS) opts := createFlags{path: "/", detectResources: true, detectRecursive: true} - err := runCreate(opts, fakeFS) + err := runCreate(opts, fakeFS, factory) if err != nil { t.Fatalf("unexpected cmd error: %v", err) }