Skip to content

Commit a055828

Browse files
committed
separate single use helpers and actual shared code
1 parent a6c6877 commit a055828

File tree

11 files changed

+77
-109
lines changed

11 files changed

+77
-109
lines changed

hack/import-restrictions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@
441441
"github.com/openshift/origin/pkg/api/apihelpers",
442442
"github.com/openshift/origin/pkg/api/install",
443443
"github.com/openshift/origin/pkg/api/latest",
444+
"github.com/openshift/origin/pkg/api/legacygroupification",
444445
"github.com/openshift/origin/pkg/api/meta",
445446
"github.com/openshift/origin/pkg/apps/apis/apps",
446447
"github.com/openshift/origin/pkg/apps/apis/apps/install",
@@ -508,11 +509,11 @@
508509
"github.com/openshift/origin/pkg/serviceaccounts",
509510
"github.com/openshift/origin/pkg/serviceaccounts/util",
510511
"github.com/openshift/origin/pkg/service/admission",
511-
"github.com/openshift/origin/pkg/template",
512512
"github.com/openshift/origin/pkg/template/apis/template",
513513
"github.com/openshift/origin/pkg/template/apis/template/validation",
514514
"github.com/openshift/origin/pkg/template/client/internalversion",
515515
"github.com/openshift/origin/pkg/template/generator",
516+
"github.com/openshift/origin/pkg/template/templateprocessing",
516517
"github.com/openshift/origin/pkg/unidling/api",
517518
"github.com/openshift/origin/pkg/unidling/util",
518519
"github.com/openshift/origin/pkg/user/apis/user",

pkg/build/controller/jenkins/jenkins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
"github.com/golang/glog"
7+
"github.com/openshift/origin/pkg/template/templateprocessing"
78
"k8s.io/kubernetes/pkg/api/legacyscheme"
89

910
kerrs "k8s.io/apimachinery/pkg/api/errors"
@@ -13,7 +14,6 @@ import (
1314
kapi "k8s.io/kubernetes/pkg/apis/core"
1415

1516
serverapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
16-
"github.com/openshift/origin/pkg/template"
1717
templateapi "github.com/openshift/origin/pkg/template/apis/template"
1818
templateinternal "github.com/openshift/origin/pkg/template/client/internalversion"
1919
templateclient "github.com/openshift/origin/pkg/template/generated/internalclientset"
@@ -104,7 +104,7 @@ func substituteTemplateParameters(params map[string]string, t *templateapi.Templ
104104
errors = append(errors, fmt.Errorf("template parameter name cannot be empty (%q)", value))
105105
continue
106106
}
107-
if v := template.GetParameterByName(t, name); v != nil {
107+
if v := templateprocessing.GetParameterByName(t, name); v != nil {
108108
v.Value = value
109109
v.Generate = ""
110110
} else {

pkg/oc/cli/cmd/process.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/openshift/origin/pkg/template/templateprocessing"
1112
"github.com/spf13/cobra"
1213
"k8s.io/apimachinery/pkg/api/errors"
1314
"k8s.io/apimachinery/pkg/api/meta"
@@ -28,7 +29,6 @@ import (
2829
"github.com/openshift/origin/pkg/oc/cli/describe"
2930
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
3031
"github.com/openshift/origin/pkg/oc/generate/app"
31-
"github.com/openshift/origin/pkg/template"
3232
templateapi "github.com/openshift/origin/pkg/template/apis/template"
3333
templatevalidation "github.com/openshift/origin/pkg/template/apis/template/validation"
3434
templateinternalclient "github.com/openshift/origin/pkg/template/client/internalversion"
@@ -363,7 +363,7 @@ func RunProcess(f *clientcmd.Factory, in io.Reader, out, errout io.Writer, cmd *
363363
func injectUserVars(values app.Environment, t *templateapi.Template, ignoreUnknownParameters bool) []error {
364364
var errors []error
365365
for param, val := range values {
366-
v := template.GetParameterByName(t, param)
366+
v := templateprocessing.GetParameterByName(t, param)
367367
if v != nil {
368368
v.Value = val
369369
v.Generate = ""
@@ -380,7 +380,7 @@ func processTemplateLocally(tpl *templateapi.Template) error {
380380
if errs := templatevalidation.ValidateProcessedTemplate(tpl); len(errs) > 0 {
381381
return errors.NewInvalid(templateapi.Kind("Template"), tpl.Name, errs)
382382
}
383-
processor := template.NewProcessor(map[string]generator.Generator{
383+
processor := templateprocessing.NewProcessor(map[string]generator.Generator{
384384
"expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(time.Now().UnixNano()))),
385385
})
386386
if errs := processor.Process(tpl); len(errs) > 0 {

pkg/oc/generate/app/templatelookup.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
kapi "k8s.io/kubernetes/pkg/apis/core"
1414
"k8s.io/kubernetes/pkg/kubectl/resource"
1515

16-
"github.com/openshift/origin/pkg/template"
1716
templateapi "github.com/openshift/origin/pkg/template/apis/template"
1817
templateclient "github.com/openshift/origin/pkg/template/generated/internalclientset/typed/template/internalversion"
1918
)
@@ -34,7 +33,7 @@ func (r TemplateSearcher) Search(precise bool, terms ...string) (ComponentMatche
3433
matches := ComponentMatches{}
3534
var errs []error
3635
for _, term := range terms {
37-
ref, err := template.ParseTemplateReference(term)
36+
ref, err := parseTemplateReference(term)
3837
if err != nil {
3938
glog.V(2).Infof("template references must be of the form [<namespace>/]<name>, term %q did not qualify", term)
4039
continue
@@ -45,7 +44,7 @@ func (r TemplateSearcher) Search(precise bool, terms ...string) (ComponentMatche
4544
}
4645

4746
namespaces := r.Namespaces
48-
if ref.HasNamespace() {
47+
if ref.hasNamespace() {
4948
namespaces = []string{ref.Namespace}
5049
}
5150

@@ -182,3 +181,41 @@ func (r *TemplateFileSearcher) Search(precise bool, terms ...string) (ComponentM
182181

183182
return matches, errs
184183
}
184+
185+
// templateReference points to a stored template
186+
type templateReference struct {
187+
Namespace string
188+
Name string
189+
}
190+
191+
// parseTemplateReference parses the reference to a template into a
192+
// TemplateReference.
193+
func parseTemplateReference(s string) (templateReference, error) {
194+
var ref templateReference
195+
parts := strings.Split(s, "/")
196+
switch len(parts) {
197+
case 2:
198+
// namespace/name
199+
ref.Namespace = parts[0]
200+
ref.Name = parts[1]
201+
break
202+
case 1:
203+
// name
204+
ref.Name = parts[0]
205+
break
206+
default:
207+
return ref, fmt.Errorf("the template reference must be either the template name or namespace and template name separated by slashes")
208+
}
209+
return ref, nil
210+
}
211+
212+
func (r templateReference) hasNamespace() bool {
213+
return len(r.Namespace) > 0
214+
}
215+
216+
func (r templateReference) String() string {
217+
if r.hasNamespace() {
218+
return fmt.Sprintf("%s/%s", r.Namespace, r.Name)
219+
}
220+
return r.Name
221+
}

pkg/oc/generate/cmd/template.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"io"
66
"strings"
77

8+
"github.com/openshift/origin/pkg/template/templateprocessing"
89
"k8s.io/apimachinery/pkg/runtime"
910
"k8s.io/apimachinery/pkg/util/errors"
1011
"k8s.io/kubernetes/pkg/api/legacyscheme"
1112

1213
"github.com/openshift/origin/pkg/api/latest"
1314
"github.com/openshift/origin/pkg/oc/generate/app"
14-
"github.com/openshift/origin/pkg/template"
1515
templateapi "github.com/openshift/origin/pkg/template/apis/template"
1616
templateinternalclient "github.com/openshift/origin/pkg/template/client/internalversion"
1717
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -21,7 +21,7 @@ import (
2121
func TransformTemplate(tpl *templateapi.Template, templateProcessor templateinternalclient.TemplateProcessorInterface, namespace string, parameters map[string]string, ignoreUnknownParameters bool) (*templateapi.Template, error) {
2222
// only set values that match what's expected by the template.
2323
for k, value := range parameters {
24-
v := template.GetParameterByName(tpl, k)
24+
v := templateprocessing.GetParameterByName(tpl, k)
2525
if v != nil {
2626
v.Value = value
2727
v.Generate = ""

pkg/template/doc.go

Lines changed: 0 additions & 3 deletions
This file was deleted.

pkg/template/helper.go

Lines changed: 0 additions & 87 deletions
This file was deleted.

pkg/template/registry/template/rest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import (
55
"time"
66

77
"github.com/golang/glog"
8+
"github.com/openshift/origin/pkg/template/templateprocessing"
89
"k8s.io/apimachinery/pkg/api/errors"
910
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1011
"k8s.io/apimachinery/pkg/runtime"
1112
apirequest "k8s.io/apiserver/pkg/endpoints/request"
1213
"k8s.io/apiserver/pkg/registry/rest"
1314

14-
"github.com/openshift/origin/pkg/template"
1515
templateapi "github.com/openshift/origin/pkg/template/apis/template"
1616
templatevalidation "github.com/openshift/origin/pkg/template/apis/template/validation"
1717
"github.com/openshift/origin/pkg/template/generator"
@@ -50,7 +50,7 @@ func (s *REST) Create(ctx apirequest.Context, obj runtime.Object, _ rest.Validat
5050
generators := map[string]generator.Generator{
5151
"expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(time.Now().UnixNano()))),
5252
}
53-
processor := template.NewProcessor(generators)
53+
processor := templateprocessing.NewProcessor(generators)
5454
if errs := processor.Process(tpl); len(errs) > 0 {
5555
glog.V(1).Infof(errs.ToAggregate().Error())
5656
return nil, errors.NewInvalid(templateapi.Kind("Template"), tpl.Name, errs)

pkg/template/registry/templateinstance/strategy.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
rbacregistry "k8s.io/kubernetes/pkg/registry/rbac"
1818

1919
"github.com/openshift/origin/pkg/authorization/util"
20-
template "github.com/openshift/origin/pkg/template"
2120
templateapi "github.com/openshift/origin/pkg/template/apis/template"
2221
"github.com/openshift/origin/pkg/template/apis/template/validation"
2322
)
@@ -60,7 +59,7 @@ func (templateInstanceStrategy) PrepareForCreate(ctx apirequest.Context, obj run
6059
if templateInstance.Spec.Requester == nil {
6160

6261
if user, ok := apirequest.UserFrom(ctx); ok {
63-
templateReq := template.ConvertUserToTemplateInstanceRequester(user)
62+
templateReq := convertUserToTemplateInstanceRequester(user)
6463
templateInstance.Spec.Requester = &templateReq
6564
}
6665
}
@@ -192,3 +191,24 @@ func (statusStrategy) Canonicalize(obj runtime.Object) {
192191
func (statusStrategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
193192
return validation.ValidateTemplateInstanceUpdate(obj.(*templateapi.TemplateInstance), old.(*templateapi.TemplateInstance))
194193
}
194+
195+
// convertUserToTemplateInstanceRequester copies analogous fields from user.Info to TemplateInstanceRequester
196+
func convertUserToTemplateInstanceRequester(u user.Info) templateapi.TemplateInstanceRequester {
197+
templatereq := templateapi.TemplateInstanceRequester{}
198+
199+
if u != nil {
200+
extra := map[string]templateapi.ExtraValue{}
201+
if u.GetExtra() != nil {
202+
for k, v := range u.GetExtra() {
203+
extra[k] = templateapi.ExtraValue(v)
204+
}
205+
}
206+
207+
templatereq.Username = u.GetName()
208+
templatereq.UID = u.GetUID()
209+
templatereq.Groups = u.GetGroups()
210+
templatereq.Extra = extra
211+
}
212+
213+
return templatereq
214+
}

pkg/template/template.go renamed to pkg/template/templateprocessing/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package template
1+
package templateprocessing
22

33
import (
44
"fmt"

pkg/template/template_test.go renamed to pkg/template/templateprocessing/template_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package template
1+
package templateprocessing
22

33
import (
44
"encoding/json"
@@ -420,12 +420,12 @@ func TestEvaluateLabels(t *testing.T) {
420420

421421
func TestProcessTemplateParameters(t *testing.T) {
422422
var template, expectedTemplate templateapi.Template
423-
jsonData, _ := ioutil.ReadFile("../../test/templates/testdata/guestbook.json")
423+
jsonData, _ := ioutil.ReadFile("../../../test/templates/testdata/guestbook.json")
424424
if err := runtime.DecodeInto(legacyscheme.Codecs.UniversalDecoder(), jsonData, &template); err != nil {
425425
t.Fatalf("unexpected error: %v", err)
426426
}
427427

428-
expectedData, _ := ioutil.ReadFile("../../test/templates/testdata/guestbook_list.json")
428+
expectedData, _ := ioutil.ReadFile("../../../test/templates/testdata/guestbook_list.json")
429429
if err := runtime.DecodeInto(legacyscheme.Codecs.UniversalDecoder(), expectedData, &expectedTemplate); err != nil {
430430
t.Fatalf("unexpected error: %v", err)
431431
}

0 commit comments

Comments
 (0)