Add validator interface

This commit is contained in:
Jingfang Liu
2018-10-02 15:09:19 -07:00
parent ad093555a6
commit fa89a0ab4d
7 changed files with 132 additions and 49 deletions

View File

@@ -24,7 +24,6 @@ import (
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/types"
"sigs.k8s.io/kustomize/pkg/validators"
)
// kindOfAdd is the kind of metadata being added: label or annotation
@@ -48,12 +47,12 @@ func (k kindOfAdd) String() string {
type addMetadataOptions struct {
metadata map[string]string
mapValidator validators.MapValidatorFunc
mapValidator func(map[string]string) error
kind kindOfAdd
}
// newCmdAddAnnotation adds one or more commonAnnotations to the kustomization file.
func newCmdAddAnnotation(fSys fs.FileSystem, v validators.MapValidatorFunc) *cobra.Command {
func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *cobra.Command {
var o addMetadataOptions
o.kind = annotation
o.mapValidator = v
@@ -70,7 +69,7 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v validators.MapValidatorFunc) *cob
}
// newCmdAddLabel adds one or more commonLabels to the kustomization file.
func newCmdAddLabel(fSys fs.FileSystem, v validators.MapValidatorFunc) *cobra.Command {
func newCmdAddLabel(fSys fs.FileSystem, v func(map[string]string) error) *cobra.Command {
var o addMetadataOptions
o.kind = label
o.mapValidator = v

View File

@@ -24,7 +24,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/validators"
"sigs.k8s.io/kustomize/pkg/ifc"
)
// NewDefaultCommand returns the default (aka root) command for kustomize command.
@@ -45,7 +45,7 @@ See https://sigs.k8s.io/kustomize
c.AddCommand(
// TODO: Make consistent API for newCmd* functions.
newCmdBuild(stdOut, fsys, k8sdeps.NewKustDecoder()),
newCmdEdit(fsys),
newCmdEdit(fsys, k8sdeps.NewKustValidator()),
newCmdConfig(fsys),
newCmdVersion(stdOut),
)
@@ -58,7 +58,7 @@ See https://sigs.k8s.io/kustomize
}
// newCmdEdit returns an instance of 'edit' subcommand.
func newCmdEdit(fsys fs.FileSystem) *cobra.Command {
func newCmdEdit(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "edit",
Short: "Edits a kustomization file",
@@ -73,14 +73,14 @@ func newCmdEdit(fsys fs.FileSystem) *cobra.Command {
Args: cobra.MinimumNArgs(1),
}
c.AddCommand(
newCmdAdd(fsys),
newCmdSet(fsys),
newCmdAdd(fsys, v),
newCmdSet(fsys, v),
)
return c
}
// newAddCommand returns an instance of 'add' subcommand.
func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
func newCmdAdd(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "add",
Short: "Adds configmap/resource/patch/base to the kustomization file.",
@@ -112,14 +112,14 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
newCmdAddPatch(fsys),
newCmdAddConfigMap(fsys),
newCmdAddBase(fsys),
newCmdAddLabel(fsys, validators.MakeLabelValidator()),
newCmdAddAnnotation(fsys, validators.MakeAnnotationValidator()),
newCmdAddLabel(fsys, v.MakeLabelValidator()),
newCmdAddAnnotation(fsys, v.MakeAnnotationValidator()),
)
return c
}
// newSetCommand returns an instance of 'set' subcommand.
func newCmdSet(fsys fs.FileSystem) *cobra.Command {
func newCmdSet(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
c := &cobra.Command{
Use: "set",
Short: "Sets the value of different fields in kustomization file.",
@@ -133,7 +133,7 @@ func newCmdSet(fsys fs.FileSystem) *cobra.Command {
c.AddCommand(
newCmdSetNamePrefix(fsys),
newCmdSetNamespace(fsys),
newCmdSetNamespace(fsys, v),
newCmdSetImageTag(fsys),
)
return c

View File

@@ -22,17 +22,18 @@ import (
"strings"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/validation"
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
type setNamespaceOptions struct {
namespace string
validator ifc.Validator
}
// newCmdSetNamespace sets the value of the namespace field in the kustomization.
func newCmdSetNamespace(fsys fs.FileSystem) *cobra.Command {
func newCmdSetNamespace(fsys fs.FileSystem, v ifc.Validator) *cobra.Command {
var o setNamespaceOptions
cmd := &cobra.Command{
@@ -45,6 +46,7 @@ will add the field "namespace: staging" to the kustomization file if it doesn't
and overwrite the value with "staging" if the field does exist.
`,
RunE: func(cmd *cobra.Command, args []string) error {
o.validator = v
err := o.Validate(args)
if err != nil {
return err
@@ -61,7 +63,7 @@ func (o *setNamespaceOptions) Validate(args []string) error {
return errors.New("must specify exactly one namespace value")
}
ns := args[0]
if errs := validation.IsDNS1123Label(ns); len(errs) != 0 {
if errs := o.validator.ValidateNamespace(ns); len(errs) != 0 {
return fmt.Errorf("%q is not a valid namespace name: %s", ns, strings.Join(errs, ";"))
}
o.namespace = ns

View File

@@ -23,6 +23,7 @@ import (
"sigs.k8s.io/kustomize/pkg/constants"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/validators"
)
const (
@@ -33,7 +34,7 @@ func TestSetNamespaceHappyPath(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdSetNamespace(fakeFS)
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
args := []string{goodNamespaceValue}
err := cmd.RunE(cmd, args)
if err != nil {
@@ -53,7 +54,7 @@ func TestSetNamespaceOverride(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdSetNamespace(fakeFS)
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
args := []string{goodNamespaceValue}
err := cmd.RunE(cmd, args)
if err != nil {
@@ -77,7 +78,7 @@ func TestSetNamespaceOverride(t *testing.T) {
func TestSetNamespaceNoArgs(t *testing.T) {
fakeFS := fs.MakeFakeFS()
cmd := newCmdSetNamespace(fakeFS)
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
err := cmd.Execute()
if err == nil {
t.Errorf("expected error: %v", err)
@@ -91,7 +92,7 @@ func TestSetNamespaceInvalid(t *testing.T) {
fakeFS := fs.MakeFakeFS()
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
cmd := newCmdSetNamespace(fakeFS)
cmd := newCmdSetNamespace(fakeFS, validators.MakeFakeValidator())
args := []string{"/badnamespace/"}
err := cmd.RunE(cmd, args)
if err == nil {