-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
strategy.go
79 lines (66 loc) · 2.52 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
78
79
package project
import (
"context"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/kubernetes/pkg/api/legacyscheme"
kapi "k8s.io/kubernetes/pkg/apis/core"
projectapi "github.com/openshift/origin/pkg/project/apis/project"
"github.com/openshift/origin/pkg/project/apis/project/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{legacyscheme.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 context.Context, obj runtime.Object) {
project := obj.(*projectapi.Project)
hasProjectFinalizer := false
for i := range project.Spec.Finalizers {
if project.Spec.Finalizers[i] == projectapi.FinalizerOrigin {
hasProjectFinalizer = true
break
}
}
if !hasProjectFinalizer {
if len(project.Spec.Finalizers) == 0 {
project.Spec.Finalizers = []kapi.FinalizerName{projectapi.FinalizerOrigin}
} else {
project.Spec.Finalizers = append(project.Spec.Finalizers, projectapi.FinalizerOrigin)
}
}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (projectStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newProject := obj.(*projectapi.Project)
oldProject := old.(*projectapi.Project)
newProject.Spec.Finalizers = oldProject.Spec.Finalizers
newProject.Status = oldProject.Status
}
// Validate validates a new project.
func (projectStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
return validation.ValidateProject(obj.(*projectapi.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 context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateProjectUpdate(obj.(*projectapi.Project), old.(*projectapi.Project))
}