Skip to content

Commit

Permalink
add add secret command
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Pollack committed Jan 25, 2019
1 parent 2c1be17 commit a0c22b8
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 44 deletions.
6 changes: 5 additions & 1 deletion pkg/commands/edit/add/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import (
func NewCmdAdd(fsys fs.FileSystem, v ifc.Validator, kf ifc.KunstructuredFactory) *cobra.Command {
c := &cobra.Command{
Use: "add",
Short: "Adds configmap/resource/patch/base to the kustomization file.",
Short: "Adds an item to the kustomization file.",
Long: "",
Example: `
# Adds a secret to the kustomization file
kustomize edit add secret NAME --from-literal=k=v
# Adds a configmap to the kustomization file
kustomize edit add configmap NAME --from-literal=k=v
Expand All @@ -53,6 +56,7 @@ func NewCmdAdd(fsys fs.FileSystem, v ifc.Validator, kf ifc.KunstructuredFactory)
c.AddCommand(
newCmdAddResource(fsys),
newCmdAddPatch(fsys),
newCmdAddSecret(fsys, kf),
newCmdAddConfigMap(fsys, kf),
newCmdAddBase(fsys),
newCmdAddLabel(fsys, v.MakeLabelValidator()),
Expand Down
22 changes: 11 additions & 11 deletions pkg/commands/edit/add/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (

// newCmdAddConfigMap returns a new command.
func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.Command {
var flagsAndArgs cMapFlagsAndArgs
var flags flagsAndArgs
cmd := &cobra.Command{
Use: "configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1]",
Short: "Adds a configmap to the kustomization file.",
Expand All @@ -45,12 +45,12 @@ func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.
kustomize edit add configmap my-configmap --from-env-file=env/path.env
`,
RunE: func(_ *cobra.Command, args []string) error {
err := flagsAndArgs.ExpandFileSource(fSys)
err := flags.ExpandFileSource(fSys)
if err != nil {
return err
}

err = flagsAndArgs.Validate(args)
err = flags.Validate(args)
if err != nil {
return err
}
Expand All @@ -68,7 +68,7 @@ func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.

// Add the flagsAndArgs map to the kustomization file.
kf.Set(loader.NewFileLoaderAtCwd(fSys))
err = addConfigMap(kustomization, flagsAndArgs, kf)
err = addConfigMap(kustomization, flags, kf)
if err != nil {
return err
}
Expand All @@ -79,19 +79,19 @@ func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.
}

cmd.Flags().StringSliceVar(
&flagsAndArgs.FileSources,
&flags.FileSources,
"from-file",
[]string{},
"Key file can be specified using its file path, in which case file basename will be used as configmap "+
"key, or optionally with a key and file path, in which case the given key will be used. Specifying a "+
"directory will iterate each named file in the directory whose basename is a valid configmap key.")
cmd.Flags().StringArrayVar(
&flagsAndArgs.LiteralSources,
&flags.LiteralSources,
"from-literal",
[]string{},
"Specify a key and literal value to insert in configmap (i.e. mykey=somevalue)")
cmd.Flags().StringVar(
&flagsAndArgs.EnvFileSource,
&flags.EnvFileSource,
"from-env-file",
"",
"Specify the path to a file to read lines of key=val pairs to create a configmap (i.e. a Docker .env file).")
Expand All @@ -104,9 +104,9 @@ func newCmdAddConfigMap(fSys fs.FileSystem, kf ifc.KunstructuredFactory) *cobra.
// Suggest passing a copy of kustomization file.
func addConfigMap(
k *types.Kustomization,
flagsAndArgs cMapFlagsAndArgs, kf ifc.KunstructuredFactory) error {
cmArgs := makeConfigMapArgs(k, flagsAndArgs.Name)
err := mergeFlagsIntoCmArgs(&cmArgs.DataSources, flagsAndArgs)
flags flagsAndArgs, kf ifc.KunstructuredFactory) error {
cmArgs := makeConfigMapArgs(k, flags.Name)
err := mergeFlagsIntoCmArgs(&cmArgs.DataSources, flags)
if err != nil {
return err
}
Expand All @@ -130,7 +130,7 @@ func makeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs
return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1]
}

func mergeFlagsIntoCmArgs(src *types.DataSources, flags cMapFlagsAndArgs) error {
func mergeFlagsIntoCmArgs(src *types.DataSources, flags flagsAndArgs) error {
src.LiteralSources = append(src.LiteralSources, flags.LiteralSources...)
src.FileSources = append(src.FileSources, flags.FileSources...)
if src.EnvSource != "" && src.EnvSource != flags.EnvFileSource {
Expand Down
12 changes: 6 additions & 6 deletions pkg/commands/edit/add/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestMakeConfigMapArgs(t *testing.T) {
func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) {
ds := &types.DataSources{}

err := mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{LiteralSources: []string{"k1=v1"}})
err := mergeFlagsIntoCmArgs(ds, flagsAndArgs{LiteralSources: []string{"k1=v1"}})
if err != nil {
t.Fatalf("Merge initial literal source should not return error")
}
Expand All @@ -76,7 +76,7 @@ func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) {
t.Fatalf("Initial literal source should have been added")
}

err = mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{LiteralSources: []string{"k2=v2"}})
err = mergeFlagsIntoCmArgs(ds, flagsAndArgs{LiteralSources: []string{"k2=v2"}})
if err != nil {
t.Fatalf("Merge second literal source should not return error")
}
Expand All @@ -89,7 +89,7 @@ func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) {
func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) {
ds := &types.DataSources{}

err := mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{FileSources: []string{"file1"}})
err := mergeFlagsIntoCmArgs(ds, flagsAndArgs{FileSources: []string{"file1"}})
if err != nil {
t.Fatalf("Merge initial file source should not return error")
}
Expand All @@ -98,7 +98,7 @@ func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) {
t.Fatalf("Initial file source should have been added")
}

err = mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{FileSources: []string{"file2"}})
err = mergeFlagsIntoCmArgs(ds, flagsAndArgs{FileSources: []string{"file2"}})
if err != nil {
t.Fatalf("Merge second file source should not return error")
}
Expand All @@ -113,7 +113,7 @@ func TestMergeFlagsIntoCmArgs_EnvSource(t *testing.T) {
envFileName2 := "env2"
ds := &types.DataSources{}

err := mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{EnvFileSource: envFileName})
err := mergeFlagsIntoCmArgs(ds, flagsAndArgs{EnvFileSource: envFileName})
if err != nil {
t.Fatalf("Merge initial env source should not return error")
}
Expand All @@ -122,7 +122,7 @@ func TestMergeFlagsIntoCmArgs_EnvSource(t *testing.T) {
t.Fatalf("Initial env source filename should have been added")
}

err = mergeFlagsIntoCmArgs(ds, cMapFlagsAndArgs{EnvFileSource: envFileName2})
err = mergeFlagsIntoCmArgs(ds, flagsAndArgs{EnvFileSource: envFileName2})
if err == nil {
t.Fatalf("Updating env source should return an error")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"sigs.k8s.io/kustomize/pkg/fs"
)

// cMapFlagsAndArgs encapsulates the options for add configmap commands.
type cMapFlagsAndArgs struct {
// flagsAndArgs encapsulates the options for add secret/configmap commands.
type flagsAndArgs struct {
// Name of configMap/Secret (required)
Name string
// FileSources to derive the configMap/Secret from (optional)
Expand All @@ -33,10 +33,12 @@ type cMapFlagsAndArgs struct {
// EnvFileSource to derive the configMap/Secret from (optional)
// TODO: Rationalize this name with Generic.EnvSource
EnvFileSource string
// Type of secret to create
Type string
}

// Validate validates required fields are set to support structured generation.
func (a *cMapFlagsAndArgs) Validate(args []string) error {
func (a *flagsAndArgs) Validate(args []string) error {
if len(args) != 1 {
return fmt.Errorf("name must be specified once")
}
Expand All @@ -51,7 +53,7 @@ func (a *cMapFlagsAndArgs) Validate(args []string) error {
return nil
}

func (a *cMapFlagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error {
func (a *flagsAndArgs) ExpandFileSource(fSys fs.FileSystem) error {
result, err := globPatterns(fSys, a.FileSources)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,52 +23,52 @@ import (
"sigs.k8s.io/kustomize/pkg/fs"
)

func TestDataConfigValidation_NoName(t *testing.T) {
config := cMapFlagsAndArgs{}
func TestDataValidation_NoName(t *testing.T) {
fa := flagsAndArgs{}

if config.Validate([]string{}) == nil {
if fa.Validate([]string{}) == nil {
t.Fatal("Validation should fail if no name is specified")
}
}

func TestDataConfigValidation_MoreThanOneName(t *testing.T) {
config := cMapFlagsAndArgs{}
func TestDataValidation_MoreThanOneName(t *testing.T) {
fa := flagsAndArgs{}

if config.Validate([]string{"name", "othername"}) == nil {
if fa.Validate([]string{"name", "othername"}) == nil {
t.Fatal("Validation should fail if more than one name is specified")
}
}

func TestDataConfigValidation_Flags(t *testing.T) {
tests := []struct {
name string
config cMapFlagsAndArgs
fa flagsAndArgs
shouldFail bool
}{
{
name: "env-file-source and literal are both set",
config: cMapFlagsAndArgs{
fa: flagsAndArgs{
LiteralSources: []string{"one", "two"},
EnvFileSource: "three",
},
shouldFail: true,
},
{
name: "env-file-source and from-file are both set",
config: cMapFlagsAndArgs{
fa: flagsAndArgs{
FileSources: []string{"one", "two"},
EnvFileSource: "three",
},
shouldFail: true,
},
{
name: "we don't have any option set",
config: cMapFlagsAndArgs{},
fa: flagsAndArgs{},
shouldFail: true,
},
{
name: "we have from-file and literal ",
config: cMapFlagsAndArgs{
fa: flagsAndArgs{
LiteralSources: []string{"one", "two"},
FileSources: []string{"three", "four"},
},
Expand All @@ -77,28 +77,28 @@ func TestDataConfigValidation_Flags(t *testing.T) {
}

for _, test := range tests {
if test.config.Validate([]string{"name"}) == nil && test.shouldFail {
if test.fa.Validate([]string{"name"}) == nil && test.shouldFail {
t.Fatalf("Validation should fail if %s", test.name)
} else if test.config.Validate([]string{"name"}) != nil && !test.shouldFail {
} else if test.fa.Validate([]string{"name"}) != nil && !test.shouldFail {
t.Fatalf("Validation should succeed if %s", test.name)
}
}
}

func TestExpandFileSource(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.Create("dir/config1")
fakeFS.Create("dir/config2")
fakeFS.Create("dir/fa1")
fakeFS.Create("dir/fa2")
fakeFS.Create("dir/reademe")
config := cMapFlagsAndArgs{
FileSources: []string{"dir/config*"},
fa := flagsAndArgs{
FileSources: []string{"dir/fa*"},
}
config.ExpandFileSource(fakeFS)
fa.ExpandFileSource(fakeFS)
expected := []string{
"dir/config1",
"dir/config2",
"dir/fa1",
"dir/fa2",
}
if !reflect.DeepEqual(config.FileSources, expected) {
t.Fatalf("FileSources is not correctly expanded: %v", config.FileSources)
if !reflect.DeepEqual(fa.FileSources, expected) {
t.Fatalf("FileSources is not correctly expanded: %v", fa.FileSources)
}
}
Loading

0 comments on commit a0c22b8

Please sign in to comment.