-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
79 lines (65 loc) · 2.23 KB
/
update.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 organization
import (
"context"
"errors"
"fmt"
orgv1 "github.com/appuio/control-api/apis/organization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
)
var _ rest.Updater = &organizationStorage{}
var _ rest.CreaterUpdater = &organizationStorage{}
func (s *organizationStorage) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo,
createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc,
forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
oldObj, err := s.Get(ctx, name, nil)
if err != nil {
return nil, false, err
}
oldOrg, ok := oldObj.(*orgv1.Organization)
if !ok {
return nil, false, fmt.Errorf("old object is not an organization")
}
objInfo = rest.WrapUpdatedObjectInfo(objInfo, filterStatusUpdates)
newObj, err := objInfo.UpdatedObject(ctx, oldObj)
if err != nil {
return nil, false, err
}
newOrg, ok := newObj.(*orgv1.Organization)
if !ok {
return nil, false, fmt.Errorf("new object is not an organization")
}
if updateValidation != nil {
err = updateValidation(ctx, newOrg, oldObj)
if err != nil {
return nil, false, err
}
}
if err := s.billingEntityValidator(ctx, newOrg, oldOrg); err != nil {
return nil, false, fmt.Errorf("failed to validate billing entity reference: %w", err)
}
return newOrg, false, convertNamespaceError(s.namepaces.UpdateNamespace(ctx, newOrg.ToNamespace(), options))
}
func filterStatusUpdates(ctx context.Context, newObj, oldObj runtime.Object) (transformedNewObj runtime.Object, err error) {
requestInfo, found := request.RequestInfoFrom(ctx)
if !found {
return nil, errors.New("no RequestInfo found in the context")
}
oldOrg, ok := oldObj.(*orgv1.Organization)
if !ok {
return nil, fmt.Errorf("old object is not an organization")
}
newOrg, ok := newObj.(*orgv1.Organization)
if !ok {
return nil, fmt.Errorf("new object is not an organization")
}
if requestInfo.Subresource == "status" {
withUpdatedStatus := oldOrg.DeepCopy()
withUpdatedStatus.Status = newOrg.Status
return withUpdatedStatus, nil
}
newOrg.Status = oldOrg.Status
return newOrg, nil
}