-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathobjects.go
115 lines (91 loc) · 3.32 KB
/
objects.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
package v1
import (
"encoding/json"
"fmt"
admissionv1 "k8s.io/api/admission/v1"
"k8s.io/api/rbac/v1"
)
// RoleOldAndNewFromRequest gets the old and new Role objects, respectively, from the webhook request.
// If the request is a Delete operation, then the new object is the zero value for Role.
// Similarly, if the request is a Create operation, then the old object is the zero value for Role.
func RoleOldAndNewFromRequest(request *admissionv1.AdmissionRequest) (*v1.Role, *v1.Role, error) {
if request == nil {
return nil, nil, fmt.Errorf("nil request")
}
object := &v1.Role{}
oldObject := &v1.Role{}
if request.Operation != admissionv1.Delete {
err := json.Unmarshal(request.Object.Raw, object)
if err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal request object: %w", err)
}
}
if request.Operation == admissionv1.Create {
return oldObject, object, nil
}
err := json.Unmarshal(request.OldObject.Raw, oldObject)
if err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal request oldObject: %w", err)
}
return oldObject, object, nil
}
// RoleFromRequest returns a Role object from the webhook request.
// If the operation is a Delete operation, then the old object is returned.
// Otherwise, the new object is returned.
func RoleFromRequest(request *admissionv1.AdmissionRequest) (*v1.Role, error) {
if request == nil {
return nil, fmt.Errorf("nil request")
}
object := &v1.Role{}
raw := request.Object.Raw
if request.Operation == admissionv1.Delete {
raw = request.OldObject.Raw
}
err := json.Unmarshal(raw, object)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal request object: %w", err)
}
return object, nil
}
// RoleBindingOldAndNewFromRequest gets the old and new RoleBinding objects, respectively, from the webhook request.
// If the request is a Delete operation, then the new object is the zero value for RoleBinding.
// Similarly, if the request is a Create operation, then the old object is the zero value for RoleBinding.
func RoleBindingOldAndNewFromRequest(request *admissionv1.AdmissionRequest) (*v1.RoleBinding, *v1.RoleBinding, error) {
if request == nil {
return nil, nil, fmt.Errorf("nil request")
}
object := &v1.RoleBinding{}
oldObject := &v1.RoleBinding{}
if request.Operation != admissionv1.Delete {
err := json.Unmarshal(request.Object.Raw, object)
if err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal request object: %w", err)
}
}
if request.Operation == admissionv1.Create {
return oldObject, object, nil
}
err := json.Unmarshal(request.OldObject.Raw, oldObject)
if err != nil {
return nil, nil, fmt.Errorf("failed to unmarshal request oldObject: %w", err)
}
return oldObject, object, nil
}
// RoleBindingFromRequest returns a RoleBinding object from the webhook request.
// If the operation is a Delete operation, then the old object is returned.
// Otherwise, the new object is returned.
func RoleBindingFromRequest(request *admissionv1.AdmissionRequest) (*v1.RoleBinding, error) {
if request == nil {
return nil, fmt.Errorf("nil request")
}
object := &v1.RoleBinding{}
raw := request.Object.Raw
if request.Operation == admissionv1.Delete {
raw = request.OldObject.Raw
}
err := json.Unmarshal(raw, object)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal request object: %w", err)
}
return object, nil
}