forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrategy.go
172 lines (137 loc) · 6.03 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package deployconfig
import (
"context"
"reflect"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage/names"
"k8s.io/kubernetes/pkg/api/legacyscheme"
appsapi "github.com/openshift/origin/pkg/apps/apis/apps"
"github.com/openshift/origin/pkg/apps/apis/apps/validation"
)
// strategy implements behavior for DeploymentConfig objects
type strategy struct {
runtime.ObjectTyper
names.NameGenerator
}
// CommonStrategy is the default logic that applies when creating and updating DeploymentConfig objects.
var CommonStrategy = strategy{legacyscheme.Scheme, names.SimpleNameGenerator}
// LegacyStrategy is the logic that applies when creating and updating DeploymentConfig objects in the legacy API.
// An example would be setting different defaults depending on API group
var LegacyStrategy = legacyStrategy{CommonStrategy}
// GroupStrategy is the logic that applies when creating and updating DeploymentConfig objects in the group API.
// An example would be setting different defaults depending on API group
var GroupStrategy = groupStrategy{CommonStrategy}
// NamespaceScoped is true for DeploymentConfig objects.
func (strategy) NamespaceScoped() bool {
return true
}
// AllowCreateOnUpdate is false for DeploymentConfig objects.
func (strategy) AllowCreateOnUpdate() bool {
return false
}
func (strategy) AllowUnconditionalUpdate() bool {
return false
}
func (s strategy) Export(ctx context.Context, obj runtime.Object, exact bool) error {
s.PrepareForCreate(ctx, obj)
return nil
}
// PrepareForCreate clears fields that are not allowed to be set by end users on creation.
func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
dc := obj.(*appsapi.DeploymentConfig)
dc.Generation = 1
dc.Status = appsapi.DeploymentConfigStatus{}
for i := range dc.Spec.Triggers {
if params := dc.Spec.Triggers[i].ImageChangeParams; params != nil {
params.LastTriggeredImage = ""
}
}
}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update.
func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newDc := obj.(*appsapi.DeploymentConfig)
oldDc := old.(*appsapi.DeploymentConfig)
newVersion := newDc.Status.LatestVersion
oldVersion := oldDc.Status.LatestVersion
// Persist status
newDc.Status = oldDc.Status
// oc deploy --latest from old clients
// TODO: Remove once we drop support for older clients
if newVersion == oldVersion+1 {
newDc.Status.LatestVersion = newVersion
}
// TODO: Disallow lastTriggeredImage updates from this update path.
// Any changes to the spec or labels, increment the generation number, any changes
// to the status should reflect the generation number of the corresponding object
// (should be handled by the controller).
if !reflect.DeepEqual(oldDc.Spec, newDc.Spec) || newDc.Status.LatestVersion != oldDc.Status.LatestVersion {
newDc.Generation = oldDc.Generation + 1
}
}
// Canonicalize normalizes the object after validation.
func (strategy) Canonicalize(obj runtime.Object) {
}
// Validate validates a new policy.
func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
return validation.ValidateDeploymentConfig(obj.(*appsapi.DeploymentConfig))
}
// ValidateUpdate is the default update validation for an end user.
func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateDeploymentConfigUpdate(obj.(*appsapi.DeploymentConfig), old.(*appsapi.DeploymentConfig))
}
// CheckGracefulDelete allows a deployment config to be gracefully deleted.
func (strategy) CheckGracefulDelete(obj runtime.Object, options *metav1.DeleteOptions) bool {
return false
}
// legacyStrategy implements behavior for DeploymentConfig objects in the legacy API
type legacyStrategy struct {
strategy
}
// PrepareForCreate delegates to the common strategy.
func (s legacyStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
s.strategy.PrepareForCreate(ctx, obj)
}
var _ rest.GarbageCollectionDeleteStrategy = legacyStrategy{}
// DefaultGarbageCollectionPolicy for legacy DeploymentConfigs will orphan dependents.
func (s legacyStrategy) DefaultGarbageCollectionPolicy(ctx context.Context) rest.GarbageCollectionPolicy {
return rest.OrphanDependents
}
// groupStrategy implements behavior for DeploymentConfig objects in the Group API
type groupStrategy struct {
strategy
}
// PrepareForCreate delegates to the common strategy and sets defaults applicable only to Group API
func (s groupStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
s.strategy.PrepareForCreate(ctx, obj)
dc := obj.(*appsapi.DeploymentConfig)
appsV1DeploymentConfigLayeredDefaults(dc)
}
// statusStrategy implements behavior for DeploymentConfig status updates.
type statusStrategy struct {
strategy
}
var StatusStrategy = statusStrategy{CommonStrategy}
// PrepareForUpdate clears fields that are not allowed to be set by end users on update of status.
func (statusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
newDc := obj.(*appsapi.DeploymentConfig)
oldDc := old.(*appsapi.DeploymentConfig)
newDc.Spec = oldDc.Spec
newDc.Labels = oldDc.Labels
}
// ValidateUpdate is the default update validation for an end user updating status.
func (statusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateDeploymentConfigStatusUpdate(obj.(*appsapi.DeploymentConfig), old.(*appsapi.DeploymentConfig))
}
// Applies defaults only for API group "apps.openshift.io" and not for the legacy API.
// This function is called from storage layer where differentiation
// between legacy and group API can be made and is not related to other functions here
// which are called fom auto-generated code.
func appsV1DeploymentConfigLayeredDefaults(dc *appsapi.DeploymentConfig) {
if dc.Spec.RevisionHistoryLimit == nil {
v := appsapi.DefaultRevisionHistoryLimit
dc.Spec.RevisionHistoryLimit = &v
}
}