Skip to content

Commit

Permalink
Merge pull request #1605 from kargakis/appropriate-refactor
Browse files Browse the repository at this point in the history
Merged by openshift-bot
  • Loading branch information
OpenShift Bot committed Apr 6, 2015
2 parents 397373f + 841d9de commit 5b7b650
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
3 changes: 2 additions & 1 deletion pkg/template/registry/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/openshift/origin/pkg/template/api"
"github.com/openshift/origin/pkg/template/api/validation"
"github.com/openshift/origin/pkg/template/generator"
"github.com/openshift/origin/pkg/util"
)

// templateStrategy implements behavior for Templates
Expand Down Expand Up @@ -101,7 +102,7 @@ func (s *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, err

if tpl.ObjectLabels != nil {
objectLabels := labels.Set(tpl.ObjectLabels)
if err := template.AddConfigLabels(cfg, objectLabels); len(err) > 0 {
if err := util.AddConfigLabels(cfg, objectLabels); len(err) > 0 {
// TODO: We don't report the processing errors to users as there is no
// good way how to do it for just some items.
glog.V(1).Infof(utilerr.NewAggregate(err).Error())
Expand Down
13 changes: 4 additions & 9 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"
configapi "github.com/openshift/origin/pkg/config/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"

"github.com/openshift/origin/pkg/template/api"
. "github.com/openshift/origin/pkg/template/generator"
"github.com/openshift/origin/pkg/util"
)

var parameterExp = regexp.MustCompile(`\$\{([a-zA-Z0-9\_]+)\}`)

// reportError reports the single item validation error and properly set the
// prefix and index to match the Config item JSON index
func reportError(allErrs *fielderrors.ValidationErrorList, index int, err fielderrors.ValidationError) {
i := fielderrors.ValidationErrorList{}
*allErrs = append(*allErrs, append(i, &err).PrefixIndex(index).Prefix("item")...)
}

// Processor process the Template into the List with substituted parameters
type Processor struct {
Generators map[string]Generator
Expand All @@ -48,12 +43,12 @@ func (p *Processor) Process(template *api.Template) (*configapi.Config, fielderr
for i, item := range template.Objects {
newItem, err := p.SubstituteParameters(template.Parameters, item)
if err != nil {
reportError(&templateErrors, i, *fielderrors.NewFieldNotSupported("parameters", err))
util.ReportError(&templateErrors, i, *fielderrors.NewFieldNotSupported("parameters", err))
}
// Remove namespace from the item
itemMeta, err := meta.Accessor(newItem)
if err != nil {
reportError(&templateErrors, i, *fielderrors.NewFieldInvalid("namespace", err, "failed to remove the item namespace"))
util.ReportError(&templateErrors, i, *fielderrors.NewFieldInvalid("namespace", err, "failed to remove the item namespace"))
}
itemMeta.SetNamespace("")
template.Objects[i] = newItem
Expand Down
12 changes: 10 additions & 2 deletions pkg/template/labels.go → pkg/util/labels.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package template
package util

import (
"fmt"
Expand All @@ -9,6 +9,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/fielderrors"

configapi "github.com/openshift/origin/pkg/config/api"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
Expand All @@ -20,6 +21,13 @@ const (
ErrorOnDifferentDstKeyValue
)

// ReportError reports the single item validation error and properly set the
// prefix and index to match the Config item JSON index
func ReportError(allErrs *fielderrors.ValidationErrorList, index int, err fielderrors.ValidationError) {
i := fielderrors.ValidationErrorList{}
*allErrs = append(*allErrs, append(i, &err).PrefixIndex(index).Prefix("item")...)
}

// addReplicationControllerNestedLabels adds new label(s) to a nested labels of a single ReplicationController object
func addReplicationControllerNestedLabels(obj *kapi.ReplicationController, labels labels.Set) error {
if obj.Spec.Template.Labels == nil {
Expand Down Expand Up @@ -103,7 +111,7 @@ func AddConfigLabels(c *configapi.Config, labels labels.Set) fielderrors.Validat
itemErrors := fielderrors.ValidationErrorList{}
for i, in := range c.Items {
if err := AddObjectLabels(in, labels); err != nil {
reportError(&itemErrors, i, *fielderrors.NewFieldInvalid("labels", err, fmt.Sprintf("error applying labels %v to %v", labels, in)))
ReportError(&itemErrors, i, *fielderrors.NewFieldInvalid("labels", err, fmt.Sprintf("error applying labels %v to %v", labels, in)))
}
}
return itemErrors.Prefix("Config")
Expand Down
3 changes: 2 additions & 1 deletion pkg/template/labels_test.go → pkg/util/labels_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package template
package util

import (
"reflect"
Expand All @@ -7,6 +7,7 @@ import (
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
kmeta "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"

deployapi "github.com/openshift/origin/pkg/deploy/api"
)

Expand Down

0 comments on commit 5b7b650

Please sign in to comment.