-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathstrategy.go
More file actions
291 lines (244 loc) · 8.67 KB
/
strategy.go
File metadata and controls
291 lines (244 loc) · 8.67 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package apiserver
import (
"context"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage/names"
"sigs.k8s.io/structured-merge-diff/v6/fieldpath"
"github.com/grafana/grafana-app-sdk/logging"
"github.com/grafana/grafana-app-sdk/resource"
)
type strategy struct {
ObjectTyper runtime.ObjectTyper
kind resource.Kind
namer names.NameGenerator
}
func newStrategy(scheme *runtime.Scheme, kind resource.Kind) *strategy {
return &strategy{
ObjectTyper: scheme,
kind: kind,
namer: names.SimpleNameGenerator,
}
}
func (s *strategy) NamespaceScoped() bool {
return s.kind.Scope() == resource.NamespacedScope
}
func (s *strategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
fields := map[fieldpath.APIVersion]*fieldpath.Set{
fieldpath.APIVersion(s.kind.GroupVersionKind().GroupVersion().String()): fieldpath.NewSet(
fieldpath.MakePathOrDie("status"),
),
}
return fields
}
func (s *strategy) GenerateName(base string) string {
return s.namer.GenerateName(base)
}
func (*strategy) PrepareForCreate(_ context.Context, _ runtime.Object) {
}
func (*strategy) Validate(_ context.Context, _ runtime.Object) field.ErrorList {
return field.ErrorList{}
}
func (*strategy) Canonicalize(_ runtime.Object) {
}
func (*strategy) AllowCreateOnUpdate() bool {
return false
}
func (*strategy) WarningsOnCreate(_ context.Context, _ runtime.Object) []string {
return nil
}
func (*strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
if obj == nil || old == nil {
return
}
newObj, ok1 := obj.(resource.Object)
oldObj, ok2 := old.(resource.Object)
if !ok1 || !ok2 {
logging.FromContext(ctx).Error("PrepareForUpdate called with non-resource.Object object")
return
}
status, ok := oldObj.GetSubresource(string(resource.SubresourceStatus))
if ok {
err := newObj.SetSubresource(string(resource.SubresourceStatus), status)
if err != nil {
logging.FromContext(ctx).Error("PrepareForUpdate set status error", "error", err)
}
}
}
func (*strategy) ValidateUpdate(_ context.Context, _, _ runtime.Object) field.ErrorList {
return field.ErrorList{}
}
func (*strategy) AllowUnconditionalUpdate() bool {
return false
}
func (*strategy) WarningsOnUpdate(_ context.Context, _, _ runtime.Object) []string {
return nil
}
func (*strategy) PrepareForDelete(_ context.Context, _ runtime.Object) {
}
func (*strategy) WarningsOnDelete(_ context.Context, _ runtime.Object) []string {
return nil
}
func (s *strategy) ObjectKinds(obj runtime.Object) ([]schema.GroupVersionKind, bool, error) {
return s.ObjectTyper.ObjectKinds(obj)
}
func (s *strategy) Recognizes(gvk schema.GroupVersionKind) bool {
return gvk == s.kind.GroupVersionKind()
}
func (*strategy) CheckGracefulDelete(_ context.Context, _ runtime.Object, _ *metav1.DeleteOptions) bool {
return false
}
var _ rest.Scoper = &strategy{}
var _ rest.RESTCreateStrategy = &strategy{}
var _ rest.RESTUpdateStrategy = &strategy{}
var _ rest.RESTDeleteStrategy = &strategy{}
var _ rest.RESTGracefulDeleteStrategy = &strategy{}
// genericStatusStrategy allows for writing objects with status fields, however may not create them.
// It ignores spec and metadata fields, and does not allow for updates outside of the status field.
type genericStatusStrategy struct {
runtime.ObjectTyper
names.NameGenerator
gv schema.GroupVersion
namespaced bool
}
// NewStatusStrategy creates a new genericStatusStrategy.
func newStatusStrategy(typer runtime.ObjectTyper, gv schema.GroupVersion, namespaced bool) *genericStatusStrategy {
return &genericStatusStrategy{typer, names.SimpleNameGenerator, gv, namespaced}
}
func (g *genericStatusStrategy) NamespaceScoped() bool {
return g.namespaced
}
func (g *genericStatusStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
fields := map[fieldpath.APIVersion]*fieldpath.Set{
fieldpath.APIVersion(g.gv.String()): fieldpath.NewSet(
fieldpath.MakePathOrDie("spec"),
fieldpath.MakePathOrDie("metadata"),
),
}
return fields
}
func (*genericStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
if obj == nil || old == nil {
return
}
oldObj, ok1 := old.(resource.Object)
newObj, ok2 := obj.(resource.Object)
if !ok1 || !ok2 {
logging.FromContext(ctx).Error("Status PrepareForUpdate called with non-resource.Object object")
return
}
newObj.SetAnnotations(oldObj.GetAnnotations())
newObj.SetLabels(oldObj.GetLabels())
newObj.SetFinalizers(oldObj.GetFinalizers())
newObj.SetOwnerReferences(oldObj.GetOwnerReferences())
// TODO: we shouldn't have to do this, right? It's not being done in OSS grafana, but we lose the spec otherwise here...
err := newObj.SetSpec(oldObj.GetSpec())
if err != nil {
logging.FromContext(ctx).Error("Status PrepareForUpdate set spec error", "error", err)
}
}
func (*genericStatusStrategy) AllowCreateOnUpdate() bool {
return false
}
func (*genericStatusStrategy) AllowUnconditionalUpdate() bool {
return false
}
// Canonicalize normalizes the object after validation.
func (*genericStatusStrategy) Canonicalize(_ runtime.Object) {
}
// ValidateUpdate validates an update of genericStatusStrategy.
func (*genericStatusStrategy) ValidateUpdate(_ context.Context, _, _ runtime.Object) field.ErrorList {
return field.ErrorList{}
}
// WarningsOnUpdate returns warnings for the given update.
func (*genericStatusStrategy) WarningsOnUpdate(_ context.Context, _, _ runtime.Object) []string {
return nil
}
// genericStatusStrategy allows for writing objects with status fields, however may not create them.
// It ignores spec and metadata fields, and does not allow for updates outside of the subresource field.
type genericSubresourceStrategy struct {
runtime.ObjectTyper
names.NameGenerator
gv schema.GroupVersion
subresource string
resetFields []string
namespaced bool
}
// NewSubresourceStrategy creates a new genericStatusStrategy.
func newSubresourceStrategy(typer runtime.ObjectTyper, gv schema.GroupVersion, subresource string, resetFields []string, namespaced bool) *genericSubresourceStrategy {
return &genericSubresourceStrategy{
ObjectTyper: typer,
NameGenerator: names.SimpleNameGenerator,
gv: gv,
subresource: subresource,
resetFields: resetFields,
namespaced: namespaced,
}
}
func (g *genericSubresourceStrategy) NamespaceScoped() bool {
return g.namespaced
}
func (g *genericSubresourceStrategy) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
paths := make([]fieldpath.Path, 0, len(g.resetFields))
for _, path := range g.resetFields {
paths = append(paths, fieldpath.MakePathOrDie(path))
}
fields := map[fieldpath.APIVersion]*fieldpath.Set{
fieldpath.APIVersion(g.gv.String()): fieldpath.NewSet(paths...),
}
return fields
}
func (g *genericSubresourceStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
if obj == nil || old == nil {
return
}
oldObj, ok1 := old.(resource.Object)
newObj, ok2 := obj.(resource.Object)
if !ok1 || !ok2 {
logging.FromContext(ctx).Error("Status PrepareForUpdate called with non-resource.Object object")
return
}
newObj.SetAnnotations(oldObj.GetAnnotations())
newObj.SetLabels(oldObj.GetLabels())
newObj.SetFinalizers(oldObj.GetFinalizers())
newObj.SetOwnerReferences(oldObj.GetOwnerReferences())
// TODO: we shouldn't have to do this, right? It's not being done in OSS grafana, but we lose the spec otherwise here...
err := newObj.SetSpec(oldObj.GetSpec())
if err != nil {
logging.FromContext(ctx).Error("Status PrepareForUpdate set spec error", "error", err)
}
// set other subresources
for _, sr := range g.resetFields {
if sr == "metadata" || sr == "spec" {
continue
}
oldSubresource, ok := oldObj.GetSubresource(sr)
if ok {
err := newObj.SetSubresource(sr, oldSubresource)
if err != nil {
logging.FromContext(ctx).Error(fmt.Sprintf("Subresource %s PrepareForUpdate set %s error", g.subresource, sr), "error", err)
}
}
}
}
func (*genericSubresourceStrategy) AllowCreateOnUpdate() bool {
return false
}
func (*genericSubresourceStrategy) AllowUnconditionalUpdate() bool {
return false
}
// Canonicalize normalizes the object after validation.
func (*genericSubresourceStrategy) Canonicalize(_ runtime.Object) {
}
// ValidateUpdate validates an update of genericStatusStrategy.
func (*genericSubresourceStrategy) ValidateUpdate(_ context.Context, _, _ runtime.Object) field.ErrorList {
return field.ErrorList{}
}
// WarningsOnUpdate returns warnings for the given update.
func (*genericSubresourceStrategy) WarningsOnUpdate(_ context.Context, _, _ runtime.Object) []string {
return nil
}