forked from openshift/origin
/
rest.go
108 lines (92 loc) · 3.46 KB
/
rest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package registry
import (
"fmt"
"math/rand"
"time"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/registry/generic"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
utilerr "github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/template"
"github.com/openshift/origin/pkg/template/api"
"github.com/openshift/origin/pkg/template/api/validation"
"github.com/openshift/origin/pkg/template/generator"
)
// templateStrategy implements behavior for Templates
type templateStrategy struct {
runtime.ObjectTyper
kapi.NameGenerator
}
// Strategy is the default logic that applies when creating and updating Template
// objects via the REST API.
var Strategy = templateStrategy{kapi.Scheme, kapi.SimpleNameGenerator}
// NamespaceScoped is true for templates.
func (templateStrategy) NamespaceScoped() bool {
return true
}
// ResetBeforeCreate clears fields that are not allowed to be set by end users on creation.
func (templateStrategy) ResetBeforeCreate(obj runtime.Object) {
}
// Validate validates a new template.
func (templateStrategy) Validate(obj runtime.Object) errors.ValidationErrorList {
template := obj.(*api.Template)
return validation.ValidateTemplate(template)
}
// AllowCreateOnUpdate is false for templates.
func (templateStrategy) AllowCreateOnUpdate() bool {
return false
}
// ValidateUpdate is the default update validation for an end user.
func (templateStrategy) ValidateUpdate(obj, old runtime.Object) errors.ValidationErrorList {
return validation.ValidateTemplateUpdate(obj.(*api.Template), old.(*api.Template))
}
// MatchTemplate returns a generic matcher for a given label and field selector.
func MatchTemplate(label, field labels.Selector) generic.Matcher {
return generic.MatcherFunc(func(obj runtime.Object) (bool, error) {
o, ok := obj.(*api.Template)
if !ok {
return false, fmt.Errorf("not a pod")
}
return label.Matches(labels.Set(o.Labels)), nil
})
}
// REST implements RESTStorage interface for processing Template objects.
type REST struct{}
// NewREST creates new RESTStorage interface for processing Template objects.
func NewREST() *REST {
return &REST{}
}
func (s *REST) New() runtime.Object {
return &api.Template{}
}
func (s *REST) Create(ctx kapi.Context, obj runtime.Object) (runtime.Object, error) {
tpl, ok := obj.(*api.Template)
if !ok {
return nil, errors.NewBadRequest("not a template")
}
if errs := validation.ValidateProcessedTemplate(tpl); len(errs) > 0 {
return nil, errors.NewInvalid("template", tpl.Name, errs)
}
generators := map[string]generator.Generator{
"expression": generator.NewExpressionValueGenerator(rand.New(rand.NewSource(time.Now().UnixNano()))),
}
processor := template.NewProcessor(generators)
cfg, err := processor.Process(tpl)
if 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())
}
if tpl.ObjectLabels != nil {
objectLabels := labels.Set(tpl.ObjectLabels)
if err := template.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())
}
}
return cfg, nil
}