Skip to content

Commit

Permalink
Add TemplateConfig validation
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek committed Sep 2, 2014
1 parent 8853d77 commit a52edc8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
12 changes: 12 additions & 0 deletions pkg/template/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions pkg/template/template_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/template/templateapi/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package templateapi includes all types related to template package.
package templateapi
46 changes: 46 additions & 0 deletions pkg/template/validation.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a52edc8

Please sign in to comment.