forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
137 lines (114 loc) · 4.73 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package buildconfig
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage/names"
kapi "k8s.io/kubernetes/pkg/api"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
"github.com/openshift/origin/pkg/build/apis/build/validation"
)
var (
// GroupStrategy is the logic that applies when creating and updating BuildConfig objects
// in the Group api.
// This differs from the LegacyStrategy in that on create it will set a default build
// pruning limit value for both successful and failed builds. This is new behavior that
// can only be introduced to users consuming the new group based api.
GroupStrategy = groupStrategy{strategy{kapi.Scheme, names.SimpleNameGenerator}}
// LegacyStrategy is the default logic that applies when creating BuildConfig objects.
// Specifically it will not set the default build pruning limit values because that was not
// part of the legacy api.
LegacyStrategy = legacyStrategy{strategy{kapi.Scheme, names.SimpleNameGenerator}}
)
// strategy implements most of the behavior for BuildConfig objects
// It does not provide a PrepareForCreate implementation, that is expected
// to be implemented by the child implementation.
type strategy struct {
runtime.ObjectTyper
names.NameGenerator
}
func (strategy) NamespaceScoped() bool {
return true
}
// AllowCreateOnUpdate is false for BuildConfig objects.
func (strategy) AllowCreateOnUpdate() bool {
return false
}
func (strategy) AllowUnconditionalUpdate() bool {
return false
}
// Canonicalize normalizes the object after validation.
func (strategy) Canonicalize(obj runtime.Object) {
}
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
// This is invoked by the Group and Legacy strategies.
func (s strategy) PrepareForCreate(ctx apirequest.Context, obj runtime.Object) {
bc := obj.(*buildapi.BuildConfig)
dropUnknownTriggers(bc)
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (strategy) PrepareForUpdate(ctx apirequest.Context, obj, old runtime.Object) {
newBC := obj.(*buildapi.BuildConfig)
oldBC := old.(*buildapi.BuildConfig)
dropUnknownTriggers(newBC)
// Do not allow the build version to go backwards or we'll
// get conflicts with existing builds.
if newBC.Status.LastVersion < oldBC.Status.LastVersion {
newBC.Status.LastVersion = oldBC.Status.LastVersion
}
}
// Validate validates a new policy.
func (strategy) Validate(ctx apirequest.Context, obj runtime.Object) field.ErrorList {
return validation.ValidateBuildConfig(obj.(*buildapi.BuildConfig))
}
// ValidateUpdate is the default update validation for an end user.
func (strategy) ValidateUpdate(ctx apirequest.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateBuildConfigUpdate(obj.(*buildapi.BuildConfig), old.(*buildapi.BuildConfig))
}
// groupStrategy implements behavior for BuildConfig objects in the Group api
type groupStrategy struct {
strategy
}
// PrepareForCreate delegates to the common strategy and sets default pruning limits
func (s groupStrategy) PrepareForCreate(ctx apirequest.Context, obj runtime.Object) {
s.strategy.PrepareForCreate(ctx, obj)
bc := obj.(*buildapi.BuildConfig)
if bc.Spec.SuccessfulBuildsHistoryLimit == nil {
v := buildapi.DefaultSuccessfulBuildsHistoryLimit
bc.Spec.SuccessfulBuildsHistoryLimit = &v
}
if bc.Spec.FailedBuildsHistoryLimit == nil {
v := buildapi.DefaultFailedBuildsHistoryLimit
bc.Spec.FailedBuildsHistoryLimit = &v
}
}
// legacyStrategy implements behavior for BuildConfig objects in the legacy api
type legacyStrategy struct {
strategy
}
// PrepareForCreate delegates to the common strategy.
func (s legacyStrategy) PrepareForCreate(ctx apirequest.Context, obj runtime.Object) {
s.strategy.PrepareForCreate(ctx, obj)
// legacy buildconfig api does not apply default pruning values, to maintain
// backwards compatibility.
}
// DefaultGarbageCollectionPolicy for legacy buildconfigs will orphan dependents.
func (s legacyStrategy) DefaultGarbageCollectionPolicy() rest.GarbageCollectionPolicy {
return rest.OrphanDependents
}
// CheckGracefulDelete allows a build config to be gracefully deleted.
func (strategy) CheckGracefulDelete(obj runtime.Object, options *metav1.DeleteOptions) bool {
return false
}
// dropUnknownTriggers drops any triggers that are of an unknown type
func dropUnknownTriggers(bc *buildapi.BuildConfig) {
triggers := []buildapi.BuildTriggerPolicy{}
for _, t := range bc.Spec.Triggers {
if buildapi.KnownTriggerTypes.Has(string(t.Type)) {
triggers = append(triggers, t)
}
}
bc.Spec.Triggers = triggers
}