-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
status.go
318 lines (288 loc) · 9.24 KB
/
status.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package status
import (
"reflect"
"sync"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
)
// updater transparently switches for status updates between /status and the main entrypoint for resource,
// allowing CRDs to enable or disable the status subresource support anytime.
type updater struct {
lock sync.Mutex
subresource bool
cli kubecli.KubevirtClient
}
func (u *updater) update(obj runtime.Object) (err error) {
if u.getSubresource() {
return u.updateWithSubresource(obj)
} else {
return u.updateWithoutSubresource(obj)
}
}
func (u *updater) patch(obj runtime.Object, pt types.PatchType, data []byte) (err error) {
if u.getSubresource() {
return u.patchWithSubresource(obj, pt, data)
} else {
return u.patchWithoutSubresource(obj, pt, data)
}
}
// updateWithoutSubresource will try to update the status via PUT sent to the main REST endpoint.
// If status of the returned object did not change, it knows that it should have used the /status subresource
// and will switch the updater itself over to permanently use the /status subresource.
func (u *updater) updateWithoutSubresource(obj runtime.Object) (err error) {
oldStatus, newStatus, err := u.updateUnstructured(obj)
if err != nil {
return err
}
if !reflect.DeepEqual(oldStatus, newStatus) {
u.setSubresource(true)
return u.updateStatusUnstructured(obj)
}
return nil
}
// updateWithSubresource will try to update the status via PUT sent to the /status subresource.
// If a 404 error is returned, it will try the main rest entrypoint instead. In case that this
// call succeeds, it will switch the updater to permanently use the main entrypoint.
func (u *updater) updateWithSubresource(obj runtime.Object) (updateStatusErr error) {
updateStatusErr = u.updateStatusUnstructured(obj)
if updateStatusErr != nil && !errors.IsNotFound(updateStatusErr) {
return updateStatusErr
}
if errors.IsNotFound(updateStatusErr) {
oldStatus, newStatus, err := u.updateUnstructured(obj)
if errors.IsNotFound(err) {
// object does not exist
return err
}
if err == nil && reflect.DeepEqual(oldStatus, newStatus) {
u.setSubresource(false)
} else if err == nil {
return updateStatusErr
}
return err
} else {
return updateStatusErr
}
}
// patchWithoutSubresource will try to update the status via PATCH sent to the main REST endpoint.
// If the resource version of the returned object did not change, it knows that it should have used the /status subresource
// and will switch the updater itself over to permanently use the /status subresource.
func (u *updater) patchWithoutSubresource(obj runtime.Object, patchType types.PatchType, data []byte) (err error) {
oldResourceVersion, newResourceVersion, err := u.patchUnstructured(obj, patchType, data)
if err != nil {
return err
}
if oldResourceVersion == newResourceVersion {
u.setSubresource(true)
return u.patchStatusUnstructured(obj, patchType, data)
}
return nil
}
// patchWithSubresource will try to update the status via PATCH sent to the /status subresource.
// If a 404 error is returned, it will try the main rest entrypoint instead. In case that this
// call succeeds, it will switch the updater to permanently use the main entrypoint.
func (u *updater) patchWithSubresource(obj runtime.Object, patchType types.PatchType, data []byte) (patchStatusErr error) {
patchStatusErr = u.patchStatusUnstructured(obj, patchType, data)
if patchStatusErr != nil && !errors.IsNotFound(patchStatusErr) {
return patchStatusErr
}
if errors.IsNotFound(patchStatusErr) {
oldResourceVersion, newResourceVersions, err := u.patchUnstructured(obj, patchType, data)
if errors.IsNotFound(err) {
// object does not exist
return err
}
if err == nil && oldResourceVersion != newResourceVersions {
u.setSubresource(false)
} else if err == nil {
return patchStatusErr
}
return err
} else {
return patchStatusErr
}
}
func (u *updater) patchUnstructured(obj runtime.Object, patchType types.PatchType, data []byte) (oldResourceVersion, newResourceVerions string, err error) {
a, err := meta.Accessor(obj)
if err != nil {
return "", "", err
}
switch obj.(type) {
case *v1.VirtualMachine:
oldObj := obj.(*v1.VirtualMachine)
newObj, err := u.cli.VirtualMachine(a.GetNamespace()).Patch(a.GetName(), patchType, data)
if err != nil {
return "", "", err
}
return oldObj.ResourceVersion, newObj.ResourceVersion, nil
case *v1.KubeVirt:
oldObj := obj.(*v1.KubeVirt)
newObj, err := u.cli.KubeVirt(a.GetNamespace()).Patch(a.GetName(), patchType, data)
if err != nil {
return "", "", err
}
return oldObj.ResourceVersion, newObj.ResourceVersion, nil
default:
panic("Unknown object")
}
}
func (u *updater) patchStatusUnstructured(obj runtime.Object, patchType types.PatchType, data []byte) (err error) {
a, err := meta.Accessor(obj)
if err != nil {
return err
}
switch obj.(type) {
case *v1.VirtualMachine:
_, err = u.cli.VirtualMachine(a.GetNamespace()).PatchStatus(a.GetName(), patchType, data)
return err
case *v1.KubeVirt:
_, err = u.cli.KubeVirt(a.GetNamespace()).PatchStatus(a.GetName(), patchType, data)
return err
default:
panic("Unknown object")
}
}
func (u *updater) updateUnstructured(obj runtime.Object) (oldStatus interface{}, newStatus interface{}, err error) {
a, err := meta.Accessor(obj)
if err != nil {
return nil, nil, err
}
switch obj.(type) {
case *v1.VirtualMachine:
oldObj := obj.(*v1.VirtualMachine)
newObj, err := u.cli.VirtualMachine(a.GetNamespace()).Update(oldObj)
if err != nil {
return nil, nil, err
}
return oldObj.Status, newObj.Status, nil
case *v1.VirtualMachineInstanceReplicaSet:
oldObj := obj.(*v1.VirtualMachineInstanceReplicaSet)
newObj, err := u.cli.ReplicaSet(a.GetNamespace()).Update(oldObj)
if err != nil {
return nil, nil, err
}
return oldObj.Status, newObj.Status, nil
case *v1.VirtualMachineInstanceMigration:
oldObj := obj.(*v1.VirtualMachineInstanceMigration)
newObj, err := u.cli.VirtualMachineInstanceMigration(a.GetNamespace()).Update(oldObj)
if err != nil {
return nil, nil, err
}
return oldObj.Status, newObj.Status, nil
case *v1.KubeVirt:
oldObj := obj.(*v1.KubeVirt)
newObj, err := u.cli.KubeVirt(a.GetNamespace()).Update(oldObj)
if err != nil {
return nil, nil, err
}
return oldObj.Status, newObj.Status, nil
default:
panic("Unknown object")
}
}
func (u *updater) updateStatusUnstructured(obj runtime.Object) (err error) {
a, err := meta.Accessor(obj)
if err != nil {
return err
}
switch obj.(type) {
case *v1.VirtualMachine:
oldObj := obj.(*v1.VirtualMachine)
_, err = u.cli.VirtualMachine(a.GetNamespace()).UpdateStatus(oldObj)
return err
case *v1.VirtualMachineInstanceReplicaSet:
oldObj := obj.(*v1.VirtualMachineInstanceReplicaSet)
_, err = u.cli.ReplicaSet(a.GetNamespace()).UpdateStatus(oldObj)
return err
case *v1.VirtualMachineInstanceMigration:
oldObj := obj.(*v1.VirtualMachineInstanceMigration)
_, err = u.cli.VirtualMachineInstanceMigration(a.GetNamespace()).UpdateStatus(oldObj)
return err
case *v1.KubeVirt:
oldObj := obj.(*v1.KubeVirt)
_, err = u.cli.KubeVirt(a.GetNamespace()).UpdateStatus(oldObj)
return err
default:
panic("Unknown object")
}
}
func (u *updater) setSubresource(exists bool) {
u.lock.Lock()
defer u.lock.Unlock()
u.subresource = exists
}
func (u *updater) getSubresource() bool {
u.lock.Lock()
defer u.lock.Unlock()
return u.subresource
}
type VMStatusUpdater struct {
updater updater
}
func (v *VMStatusUpdater) UpdateStatus(vm *v1.VirtualMachine) error {
return v.updater.update(vm)
}
func (v *VMStatusUpdater) PatchStatus(vm *v1.VirtualMachine, pt types.PatchType, data []byte) error {
return v.updater.patch(vm, pt, data)
}
func NewVMStatusUpdater(cli kubecli.KubevirtClient) *VMStatusUpdater {
return &VMStatusUpdater{
updater: updater{
lock: sync.Mutex{},
subresource: true,
cli: cli,
},
}
}
type VMIRSStatusUpdater struct {
updater updater
}
func (v *VMIRSStatusUpdater) UpdateStatus(vmirs *v1.VirtualMachineInstanceReplicaSet) error {
return v.updater.update(vmirs)
}
func NewVMIRSStatusUpdater(cli kubecli.KubevirtClient) *VMIRSStatusUpdater {
return &VMIRSStatusUpdater{
updater: updater{
lock: sync.Mutex{},
subresource: true,
cli: cli,
},
}
}
type KVStatusUpdater struct {
updater updater
}
func (v *KVStatusUpdater) UpdateStatus(kv *v1.KubeVirt) error {
return v.updater.update(kv)
}
func (v *KVStatusUpdater) PatchStatus(kv *v1.KubeVirt, pt types.PatchType, data []byte) error {
return v.updater.patch(kv, pt, data)
}
func NewKubeVirtStatusUpdater(cli kubecli.KubevirtClient) *KVStatusUpdater {
return &KVStatusUpdater{
updater: updater{
lock: sync.Mutex{},
subresource: true,
cli: cli,
},
}
}
type MigrationStatusUpdater struct {
updater updater
}
func (v *MigrationStatusUpdater) UpdateStatus(migration *v1.VirtualMachineInstanceMigration) error {
return v.updater.update(migration)
}
func NewMigrationStatusUpdater(cli kubecli.KubevirtClient) *MigrationStatusUpdater {
return &MigrationStatusUpdater{
updater: updater{
lock: sync.Mutex{},
subresource: true,
cli: cli,
},
}
}