diff --git a/pkg/template/generator/generator.go b/pkg/template/generator/generator.go index a451a1fafc93..d3f3e280aea2 100644 --- a/pkg/template/generator/generator.go +++ b/pkg/template/generator/generator.go @@ -22,6 +22,18 @@ func New(seed *rand.Rand) (Generator, error) { return Generator{seed: seed}, nil } +// MatchesGeneratorExpression loops over registered generators +// and searches for an expression matching the given string. +// Returns true if match is found, false otherwise. +func MatchesGeneratorExpression(value string) bool { + for regexp, _ := range generators { + if regexp.MatchString(value) { + return true + } + } + return false +} + // GenerateValue loops over registered generators and tries to find the // one matching the input expression. If match is found, it then generates // value using that matching generator diff --git a/pkg/template/template_storage.go b/pkg/template/template_storage.go index e29cbbb32c5a..2f6077ede0a2 100644 --- a/pkg/template/template_storage.go +++ b/pkg/template/template_storage.go @@ -44,12 +44,11 @@ func (s *TemplateConfigStorage) Update(minion interface{}) (<-chan interface{}, func (s *TemplateConfigStorage) Create(obj interface{}) (<-chan interface{}, error) { t, ok := obj.(*templateapi.TemplateConfig) if !ok { - return nil, fmt.Errorf("not a template: %#v", obj) + return nil, fmt.Errorf("Not a template config.") } - if t.ID == "" { - return nil, fmt.Errorf("ID should not be empty: %#v", t) + if errs := ValidateTemplateConfig(t); len(errs) > 0 { + return nil, fmt.Errorf("Invalid template config: %#v", errs) } - return apiserver.MakeAsync(func() (interface{}, error) { GenerateParameterValues(t, rand.New(rand.NewSource(time.Now().UnixNano()))) err := ProcessEnvParameters(t) diff --git a/pkg/template/templateapi/doc.go b/pkg/template/templateapi/doc.go new file mode 100644 index 000000000000..0d271ea5c962 --- /dev/null +++ b/pkg/template/templateapi/doc.go @@ -0,0 +1,2 @@ +// Package templateapi includes all types related to template package. +package templateapi diff --git a/pkg/template/validation.go b/pkg/template/validation.go new file mode 100644 index 000000000000..def26d8bf83a --- /dev/null +++ b/pkg/template/validation.go @@ -0,0 +1,46 @@ +package template + +import ( + "regexp" + + "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + errs "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" + + "github.com/openshift/origin/pkg/template/generator" + . "github.com/openshift/origin/pkg/template/templateapi" +) + +var parameterNameExp = regexp.MustCompile(`^[a-zA-Z0-9\_]+$`) + +// ValidateParameter tests if required fields in the Parameter are set. +func ValidateParameter(param *Parameter) errs.ErrorList { + allErrs := errs.ErrorList{} + if !parameterNameExp.MatchString(param.Name) { + allErrs = append(allErrs, errs.NewInvalid("Parameter.Name", param.Name)) + } + if !generator.MatchesGeneratorExpression(param.Generate) { + allErrs = append(allErrs, errs.NewNotSupported("Parameter.Generate", param.Name)) + } + return allErrs +} + +// ValidateTemplateConfig tests if required fields in the TemplateConfig are set. +func ValidateTemplateConfig(config *TemplateConfig) errs.ErrorList { + allErrs := errs.ErrorList{} + if config.ID == "" { + allErrs = append(allErrs, errs.NewInvalid("Config.ID", "")) + } + for i := range config.Services { + allErrs = append(allErrs, api.ValidateService(&config.Services[i])...) + } + for i := range config.Pods { + allErrs = append(allErrs, api.ValidatePod(&config.Pods[i])...) + } + for i := range config.ReplicationControllers { + allErrs = append(allErrs, api.ValidateReplicationController(&config.ReplicationControllers[i])...) + } + for i := range config.Parameters { + allErrs = append(allErrs, ValidateParameter(&config.Parameters[i])...) + } + return allErrs +}