forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
strategy.go
77 lines (65 loc) · 2.42 KB
/
strategy.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
package project
import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/storage/names"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/project/api"
"github.com/openshift/origin/pkg/project/api/validation"
)
// projectStrategy implements behavior for projects
type projectStrategy struct {
runtime.ObjectTyper
names.NameGenerator
}
// Strategy is the default logic that applies when creating and updating Project
// objects via the REST API.
var Strategy = projectStrategy{kapi.Scheme, names.SimpleNameGenerator}
// NamespaceScoped is false for projects.
func (projectStrategy) NamespaceScoped() bool {
return false
}
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (projectStrategy) PrepareForCreate(ctx apirequest.Context, obj runtime.Object) {
project := obj.(*api.Project)
hasProjectFinalizer := false
for i := range project.Spec.Finalizers {
if project.Spec.Finalizers[i] == api.FinalizerOrigin {
hasProjectFinalizer = true
break
}
}
if !hasProjectFinalizer {
if len(project.Spec.Finalizers) == 0 {
project.Spec.Finalizers = []kapi.FinalizerName{api.FinalizerOrigin}
} else {
project.Spec.Finalizers = append(project.Spec.Finalizers, api.FinalizerOrigin)
}
}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (projectStrategy) PrepareForUpdate(ctx apirequest.Context, obj, old runtime.Object) {
newProject := obj.(*api.Project)
oldProject := old.(*api.Project)
newProject.Spec.Finalizers = oldProject.Spec.Finalizers
newProject.Status = oldProject.Status
}
// Validate validates a new project.
func (projectStrategy) Validate(ctx apirequest.Context, obj runtime.Object) field.ErrorList {
return validation.ValidateProject(obj.(*api.Project))
}
// AllowCreateOnUpdate is false for project.
func (projectStrategy) AllowCreateOnUpdate() bool {
return false
}
func (projectStrategy) AllowUnconditionalUpdate() bool {
return false
}
// Canonicalize normalizes the object after validation.
func (projectStrategy) Canonicalize(obj runtime.Object) {
}
// ValidateUpdate is the default update validation for an end user.
func (projectStrategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateProjectUpdate(obj.(*api.Project), old.(*api.Project))
}