Merge pull request #889 from sethpollack/edit_add_plugins

update edit add secrets/configmaps to use plugins
This commit is contained in:
Kubernetes Prow Robot
2019-03-21 13:51:37 -07:00
committed by GitHub
4 changed files with 76 additions and 94 deletions

View File

@@ -17,8 +17,6 @@ limitations under the License.
package add package add
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
@@ -107,12 +105,9 @@ func addConfigMap(
k *types.Kustomization, k *types.Kustomization,
flags flagsAndArgs, kf ifc.KunstructuredFactory) error { flags flagsAndArgs, kf ifc.KunstructuredFactory) error {
cmArgs := makeConfigMapArgs(k, flags.Name) cmArgs := makeConfigMapArgs(k, flags.Name)
err := mergeFlagsIntoCmArgs(&cmArgs.DataSources, flags) mergeFlagsIntoCmArgs(&cmArgs.KVSources, flags)
if err != nil {
return err
}
// Validate by trying to create corev1.configmap. // Validate by trying to create corev1.configmap.
_, err = kf.MakeConfigMap(ldr, k.GeneratorOptions, cmArgs) _, err := kf.MakeConfigMap(ldr, k.GeneratorOptions, cmArgs)
if err != nil { if err != nil {
return err return err
} }
@@ -131,12 +126,23 @@ func makeConfigMapArgs(m *types.Kustomization, name string) *types.ConfigMapArgs
return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1] return &m.ConfigMapGenerator[len(m.ConfigMapGenerator)-1]
} }
func mergeFlagsIntoCmArgs(src *types.DataSources, flags flagsAndArgs) error { func mergeFlagsIntoCmArgs(src *[]types.KVSource, flags flagsAndArgs) {
src.LiteralSources = append(src.LiteralSources, flags.LiteralSources...) if len(flags.LiteralSources) > 0 {
src.FileSources = append(src.FileSources, flags.FileSources...) *src = append(*src, types.KVSource{
if src.EnvSource != "" && src.EnvSource != flags.EnvFileSource { Name: "literals",
return fmt.Errorf("updating existing env source '%s' not allowed", src.EnvSource) Args: flags.LiteralSources,
})
}
if len(flags.FileSources) > 0 {
*src = append(*src, types.KVSource{
Name: "files",
Args: flags.FileSources,
})
}
if flags.EnvFileSource != "" {
*src = append(*src, types.KVSource{
Name: "envfiles",
Args: []string{flags.EnvFileSource},
})
} }
src.EnvSource = flags.EnvFileSource
return nil
} }

View File

@@ -65,45 +65,33 @@ func TestMakeConfigMapArgs(t *testing.T) {
} }
func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) { func TestMergeFlagsIntoCmArgs_LiteralSources(t *testing.T) {
ds := &types.DataSources{} kv := []types.KVSource{}
err := mergeFlagsIntoCmArgs(ds, flagsAndArgs{LiteralSources: []string{"k1=v1"}}) mergeFlagsIntoCmArgs(&kv, flagsAndArgs{LiteralSources: []string{"k1=v1"}})
if err != nil {
t.Fatalf("Merge initial literal source should not return error")
}
if len(ds.LiteralSources) != 1 { if len(kv) != 1 {
t.Fatalf("Initial literal source should have been added") t.Fatalf("Initial literal source should have been added")
} }
err = mergeFlagsIntoCmArgs(ds, flagsAndArgs{LiteralSources: []string{"k2=v2"}}) mergeFlagsIntoCmArgs(&kv, flagsAndArgs{LiteralSources: []string{"k2=v2"}})
if err != nil {
t.Fatalf("Merge second literal source should not return error")
}
if len(ds.LiteralSources) != 2 { if len(kv) != 2 {
t.Fatalf("Second literal source should have been added") t.Fatalf("Second literal source should have been added")
} }
} }
func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) { func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) {
ds := &types.DataSources{} kv := []types.KVSource{}
err := mergeFlagsIntoCmArgs(ds, flagsAndArgs{FileSources: []string{"file1"}}) mergeFlagsIntoCmArgs(&kv, flagsAndArgs{FileSources: []string{"file1"}})
if err != nil {
t.Fatalf("Merge initial file source should not return error")
}
if len(ds.FileSources) != 1 { if len(kv) != 1 {
t.Fatalf("Initial file source should have been added") t.Fatalf("Initial file source should have been added")
} }
err = mergeFlagsIntoCmArgs(ds, flagsAndArgs{FileSources: []string{"file2"}}) mergeFlagsIntoCmArgs(&kv, flagsAndArgs{FileSources: []string{"file2"}})
if err != nil {
t.Fatalf("Merge second file source should not return error")
}
if len(ds.FileSources) != 2 { if len(kv) != 2 {
t.Fatalf("Second file source should have been added") t.Fatalf("Second file source should have been added")
} }
} }
@@ -111,19 +99,16 @@ func TestMergeFlagsIntoCmArgs_FileSources(t *testing.T) {
func TestMergeFlagsIntoCmArgs_EnvSource(t *testing.T) { func TestMergeFlagsIntoCmArgs_EnvSource(t *testing.T) {
envFileName := "env1" envFileName := "env1"
envFileName2 := "env2" envFileName2 := "env2"
ds := &types.DataSources{} kv := []types.KVSource{}
err := mergeFlagsIntoCmArgs(ds, flagsAndArgs{EnvFileSource: envFileName}) mergeFlagsIntoCmArgs(&kv, flagsAndArgs{EnvFileSource: envFileName})
if err != nil {
t.Fatalf("Merge initial env source should not return error") if len(kv) != 1 {
t.Fatalf("Initial env source should have been added")
} }
if ds.EnvSource != envFileName { mergeFlagsIntoCmArgs(&kv, flagsAndArgs{EnvFileSource: envFileName2})
t.Fatalf("Initial env source filename should have been added") if len(kv) != 2 {
} t.Fatalf("Second env source should have been added")
err = mergeFlagsIntoCmArgs(ds, flagsAndArgs{EnvFileSource: envFileName2})
if err == nil {
t.Fatalf("Updating env source should return an error")
} }
} }

View File

@@ -17,8 +17,6 @@ limitations under the License.
package add package add
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/commands/kustfile" "sigs.k8s.io/kustomize/pkg/commands/kustfile"
"sigs.k8s.io/kustomize/pkg/fs" "sigs.k8s.io/kustomize/pkg/fs"
@@ -112,12 +110,9 @@ func addSecret(
k *types.Kustomization, k *types.Kustomization,
flags flagsAndArgs, kf ifc.KunstructuredFactory) error { flags flagsAndArgs, kf ifc.KunstructuredFactory) error {
secretArgs := makeSecretArgs(k, flags.Name, flags.Type) secretArgs := makeSecretArgs(k, flags.Name, flags.Type)
err := mergeFlagsIntoSecretArgs(&secretArgs.DataSources, flags) mergeFlagsIntoSecretArgs(&secretArgs.KVSources, flags)
if err != nil {
return err
}
// Validate by trying to create corev1.secret. // Validate by trying to create corev1.secret.
_, err = kf.MakeSecret(ldr, k.GeneratorOptions, secretArgs) _, err := kf.MakeSecret(ldr, k.GeneratorOptions, secretArgs)
if err != nil { if err != nil {
return err return err
} }
@@ -136,12 +131,23 @@ func makeSecretArgs(m *types.Kustomization, name, secretType string) *types.Secr
return &m.SecretGenerator[len(m.SecretGenerator)-1] return &m.SecretGenerator[len(m.SecretGenerator)-1]
} }
func mergeFlagsIntoSecretArgs(src *types.DataSources, flags flagsAndArgs) error { func mergeFlagsIntoSecretArgs(src *[]types.KVSource, flags flagsAndArgs) {
src.LiteralSources = append(src.LiteralSources, flags.LiteralSources...) if len(flags.LiteralSources) > 0 {
src.FileSources = append(src.FileSources, flags.FileSources...) *src = append(*src, types.KVSource{
if src.EnvSource != "" && src.EnvSource != flags.EnvFileSource { Name: "literals",
return fmt.Errorf("updating existing env source '%s' not allowed", src.EnvSource) Args: flags.LiteralSources,
})
}
if len(flags.FileSources) > 0 {
*src = append(*src, types.KVSource{
Name: "files",
Args: flags.FileSources,
})
}
if flags.EnvFileSource != "" {
*src = append(*src, types.KVSource{
Name: "envfiles",
Args: []string{flags.EnvFileSource},
})
} }
src.EnvSource = flags.EnvFileSource
return nil
} }

View File

@@ -67,45 +67,33 @@ func TestMakeSecretArgs(t *testing.T) {
} }
func TestMergeFlagsIntoSecretArgs_LiteralSources(t *testing.T) { func TestMergeFlagsIntoSecretArgs_LiteralSources(t *testing.T) {
ds := &types.DataSources{} kv := []types.KVSource{}
err := mergeFlagsIntoSecretArgs(ds, flagsAndArgs{LiteralSources: []string{"k1=v1"}}) mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{LiteralSources: []string{"k1=v1"}})
if err != nil {
t.Fatalf("Merge initial literal source should not return error")
}
if len(ds.LiteralSources) != 1 { if len(kv) != 1 {
t.Fatalf("Initial literal source should have been added") t.Fatalf("Initial literal source should have been added")
} }
err = mergeFlagsIntoSecretArgs(ds, flagsAndArgs{LiteralSources: []string{"k2=v2"}}) mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{LiteralSources: []string{"k2=v2"}})
if err != nil {
t.Fatalf("Merge second literal source should not return error")
}
if len(ds.LiteralSources) != 2 { if len(kv) != 2 {
t.Fatalf("Second literal source should have been added") t.Fatalf("Second literal source should have been added")
} }
} }
func TestMergeFlagsIntoSecretArgs_FileSources(t *testing.T) { func TestMergeFlagsIntoSecretArgs_FileSources(t *testing.T) {
ds := &types.DataSources{} kv := []types.KVSource{}
err := mergeFlagsIntoSecretArgs(ds, flagsAndArgs{FileSources: []string{"file1"}}) mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{FileSources: []string{"file1"}})
if err != nil {
t.Fatalf("Merge initial file source should not return error")
}
if len(ds.FileSources) != 1 { if len(kv) != 1 {
t.Fatalf("Initial file source should have been added") t.Fatalf("Initial file source should have been added")
} }
err = mergeFlagsIntoSecretArgs(ds, flagsAndArgs{FileSources: []string{"file2"}}) mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{FileSources: []string{"file2"}})
if err != nil {
t.Fatalf("Merge second file source should not return error")
}
if len(ds.FileSources) != 2 { if len(kv) != 2 {
t.Fatalf("Second file source should have been added") t.Fatalf("Second file source should have been added")
} }
} }
@@ -113,19 +101,16 @@ func TestMergeFlagsIntoSecretArgs_FileSources(t *testing.T) {
func TestMergeFlagsIntoSecretArgs_EnvSource(t *testing.T) { func TestMergeFlagsIntoSecretArgs_EnvSource(t *testing.T) {
envFileName := "env1" envFileName := "env1"
envFileName2 := "env2" envFileName2 := "env2"
ds := &types.DataSources{} kv := []types.KVSource{}
err := mergeFlagsIntoSecretArgs(ds, flagsAndArgs{EnvFileSource: envFileName}) mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{EnvFileSource: envFileName})
if err != nil {
t.Fatalf("Merge initial env source should not return error") if len(kv) != 1 {
t.Fatalf("Initial env source should have been added")
} }
if ds.EnvSource != envFileName { mergeFlagsIntoSecretArgs(&kv, flagsAndArgs{EnvFileSource: envFileName2})
t.Fatalf("Initial env source filename should have been added") if len(kv) != 2 {
} t.Fatalf("Second env source should have been added")
err = mergeFlagsIntoSecretArgs(ds, flagsAndArgs{EnvFileSource: envFileName2})
if err == nil {
t.Fatalf("Updating env source should return an error")
} }
} }