Skip to content

Commit

Permalink
Remove import of k8sdeps from create command
Browse files Browse the repository at this point in the history
  • Loading branch information
richardmarshall committed Jul 31, 2019
1 parent a68f95b commit eeafd43
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/commands.go
Expand Up @@ -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),
)
Expand Down
30 changes: 15 additions & 15 deletions pkg/commands/create/create.go
Expand Up @@ -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
Expand All @@ -30,30 +31,30 @@ 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",
Short: "Create a new kustomization in the current directory",
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,
Expand Down Expand Up @@ -93,16 +94,16 @@ 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
}
if _, err := kustfile.NewKustomizationFile(fSys); err == nil {
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
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
26 changes: 15 additions & 11 deletions pkg/commands/create/create_test.go
Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit eeafd43

Please sign in to comment.