mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Remove import of k8sdeps from create command
This commit is contained in:
@@ -45,7 +45,7 @@ See https://sigs.k8s.io/kustomize
|
|||||||
stdOut, fSys, v,
|
stdOut, fSys, v,
|
||||||
rf, pf),
|
rf, pf),
|
||||||
edit.NewCmdEdit(fSys, v, uf),
|
edit.NewCmdEdit(fSys, v, uf),
|
||||||
create.NewCmdCreate(fSys),
|
create.NewCmdCreate(fSys, uf),
|
||||||
misc.NewCmdConfig(fSys),
|
misc.NewCmdConfig(fSys),
|
||||||
misc.NewCmdVersion(stdOut),
|
misc.NewCmdVersion(stdOut),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,18 +7,19 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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/kustfile"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/commands/util"
|
"sigs.k8s.io/kustomize/v3/pkg/commands/util"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
||||||
|
"sigs.k8s.io/kustomize/v3/pkg/ifc"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
|
||||||
)
|
)
|
||||||
|
|
||||||
type createFlags struct {
|
type createFlags struct {
|
||||||
resources []string
|
resources string
|
||||||
namespace string
|
namespace string
|
||||||
annotations string
|
annotations string
|
||||||
labels string
|
labels string
|
||||||
@@ -30,7 +31,7 @@ type createFlags struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewCmdCreate returns an instance of 'create' subcommand.
|
// 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: "."}
|
opts := createFlags{path: "."}
|
||||||
c := &cobra.Command{
|
c := &cobra.Command{
|
||||||
Use: "create",
|
Use: "create",
|
||||||
@@ -38,22 +39,22 @@ func NewCmdCreate(fSys fs.FileSystem) *cobra.Command {
|
|||||||
Long: "",
|
Long: "",
|
||||||
Example: `
|
Example: `
|
||||||
# Create a new overlay from the base '../base".
|
# 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.
|
# Create a new kustomization detecting resources in the current directory.
|
||||||
kustomize create --autodetect
|
kustomize create --autodetect
|
||||||
|
|
||||||
# Create a new kustomization with multiple resources and fields set.
|
# 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 {
|
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,
|
&opts.resources,
|
||||||
"resource",
|
"resources",
|
||||||
[]string{},
|
"",
|
||||||
"Name of a file containing a file to add to the kustomization file.")
|
"Name of a file containing a file to add to the kustomization file.")
|
||||||
c.Flags().StringVar(
|
c.Flags().StringVar(
|
||||||
&opts.namespace,
|
&opts.namespace,
|
||||||
@@ -93,8 +94,8 @@ func NewCmdCreate(fSys fs.FileSystem) *cobra.Command {
|
|||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCreate(opts createFlags, fSys fs.FileSystem) error {
|
func runCreate(opts createFlags, fSys fs.FileSystem, uf ifc.KunstructuredFactory) error {
|
||||||
resources, err := util.GlobPatterns(fSys, opts.resources)
|
resources, err := util.GlobPatterns(fSys, strings.Split(opts.resources, ","))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -102,7 +103,7 @@ func runCreate(opts createFlags, fSys fs.FileSystem) error {
|
|||||||
return fmt.Errorf("kustomization file already exists")
|
return fmt.Errorf("kustomization file already exists")
|
||||||
}
|
}
|
||||||
if opts.detectResources {
|
if opts.detectResources {
|
||||||
detected, err := detectResources(fSys, opts.path, opts.detectRecursive)
|
detected, err := detectResources(fSys, uf, opts.path, opts.detectRecursive)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -143,9 +144,8 @@ func runCreate(opts createFlags, fSys fs.FileSystem) error {
|
|||||||
return mf.Write(m)
|
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
|
var paths []string
|
||||||
factory := kunstruct.NewKunstructuredFactoryImpl()
|
|
||||||
err := fSys.Walk(base, func(path string, info os.FileInfo, err error) error {
|
err := fSys.Walk(base, func(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -171,7 +171,7 @@ func detectResources(fSys fs.FileSystem, base string, recursive bool) ([]string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := factory.SliceFromBytes(fContents); err != nil {
|
if _, err := uf.SliceFromBytes(fContents); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
paths = append(paths, path)
|
paths = append(paths, path)
|
||||||
|
|||||||
@@ -7,11 +7,14 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/commands/kustfile"
|
"sigs.k8s.io/kustomize/v3/pkg/commands/kustfile"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
"sigs.k8s.io/kustomize/v3/pkg/fs"
|
||||||
"sigs.k8s.io/kustomize/v3/pkg/types"
|
"sigs.k8s.io/kustomize/v3/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var factory = kunstruct.NewKunstructuredFactoryImpl()
|
||||||
|
|
||||||
func readKustomizationFS(t *testing.T, fakeFS fs.FileSystem) *types.Kustomization {
|
func readKustomizationFS(t *testing.T, fakeFS fs.FileSystem) *types.Kustomization {
|
||||||
kf, err := kustfile.NewKustomizationFile(fakeFS)
|
kf, err := kustfile.NewKustomizationFile(fakeFS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -25,7 +28,7 @@ func readKustomizationFS(t *testing.T, fakeFS fs.FileSystem) *types.Kustomizatio
|
|||||||
}
|
}
|
||||||
func TestCreateNoArgs(t *testing.T) {
|
func TestCreateNoArgs(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
cmd := NewCmdCreate(fakeFS)
|
cmd := NewCmdCreate(fakeFS, factory)
|
||||||
err := cmd.RunE(cmd, []string{})
|
err := cmd.RunE(cmd, []string{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
@@ -36,13 +39,14 @@ func TestCreateNoArgs(t *testing.T) {
|
|||||||
func TestCreateWithResources(t *testing.T) {
|
func TestCreateWithResources(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
fakeFS.WriteFile("foo.yaml", []byte(""))
|
fakeFS.WriteFile("foo.yaml", []byte(""))
|
||||||
opts := createFlags{resources: []string{"foo.yaml"}}
|
fakeFS.WriteFile("bar.yaml", []byte(""))
|
||||||
err := runCreate(opts, fakeFS)
|
opts := createFlags{resources: "foo.yaml,bar.yaml"}
|
||||||
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
m := readKustomizationFS(t, fakeFS)
|
m := readKustomizationFS(t, fakeFS)
|
||||||
expected := []string{"foo.yaml"}
|
expected := []string{"foo.yaml", "bar.yaml"}
|
||||||
if !reflect.DeepEqual(m.Resources, expected) {
|
if !reflect.DeepEqual(m.Resources, expected) {
|
||||||
t.Fatalf("expected %+v but got %+v", expected, m.Resources)
|
t.Fatalf("expected %+v but got %+v", expected, m.Resources)
|
||||||
}
|
}
|
||||||
@@ -52,7 +56,7 @@ func TestCreateWithNamespace(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
want := "foo"
|
want := "foo"
|
||||||
opts := createFlags{namespace: want}
|
opts := createFlags{namespace: want}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -66,7 +70,7 @@ func TestCreateWithNamespace(t *testing.T) {
|
|||||||
func TestCreateWithLabels(t *testing.T) {
|
func TestCreateWithLabels(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
opts := createFlags{labels: "foo:bar"}
|
opts := createFlags{labels: "foo:bar"}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -80,7 +84,7 @@ func TestCreateWithLabels(t *testing.T) {
|
|||||||
func TestCreateWithAnnotations(t *testing.T) {
|
func TestCreateWithAnnotations(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
opts := createFlags{annotations: "foo:bar"}
|
opts := createFlags{annotations: "foo:bar"}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -95,7 +99,7 @@ func TestCreateWithNamePrefix(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
want := "foo-"
|
want := "foo-"
|
||||||
opts := createFlags{prefix: want}
|
opts := createFlags{prefix: want}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -109,7 +113,7 @@ func TestCreateWithNamePrefix(t *testing.T) {
|
|||||||
func TestCreateWithNameSuffix(t *testing.T) {
|
func TestCreateWithNameSuffix(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
opts := createFlags{suffix: "-foo"}
|
opts := createFlags{suffix: "-foo"}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected cmd error: %v", err)
|
t.Errorf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -152,7 +156,7 @@ func TestCreateWithDetect(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
writeDetectContent(fakeFS)
|
writeDetectContent(fakeFS)
|
||||||
opts := createFlags{path: "/", detectResources: true}
|
opts := createFlags{path: "/", detectResources: true}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected cmd error: %v", err)
|
t.Fatalf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -167,7 +171,7 @@ func TestCreateWithDetectRecursive(t *testing.T) {
|
|||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
writeDetectContent(fakeFS)
|
writeDetectContent(fakeFS)
|
||||||
opts := createFlags{path: "/", detectResources: true, detectRecursive: true}
|
opts := createFlags{path: "/", detectResources: true, detectRecursive: true}
|
||||||
err := runCreate(opts, fakeFS)
|
err := runCreate(opts, fakeFS, factory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unexpected cmd error: %v", err)
|
t.Fatalf("unexpected cmd error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user