-
Notifications
You must be signed in to change notification settings - Fork 1
/
v1_admission_review.go
138 lines (111 loc) · 3.22 KB
/
v1_admission_review.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
// nolint:dupl
package facade
import (
"encoding/json"
"k8s.io/apimachinery/pkg/types"
"github.com/pkg/errors"
v1 "k8s.io/api/admission/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
var v1PatchTypeJSONPatch = v1.PatchTypeJSONPatch
func V1AdmissionReviewFromBytes(bytes []byte) (AdmissionReview, error) {
target := &v1.AdmissionReview{}
if err := json.Unmarshal(bytes, target); err != nil {
return nil, errors.Wrap(err, "got an admission review v1, but could not serialize it")
}
return V1(target), nil
}
type v1AdmissionReview struct {
target *v1.AdmissionReview
request *v1AdmissionReviewRequest
}
// Review decorator functions
var _ AdmissionReview = &v1AdmissionReview{}
func (v *v1AdmissionReview) Marshal() ([]byte, error) {
return json.Marshal(v.target)
}
func (v *v1AdmissionReview) Request() AdmissionRequest {
if v.request == nil && v.target.Request != nil {
v.request = &v1AdmissionReviewRequest{target: v.target.Request}
}
return v.request
}
func (v *v1AdmissionReview) Response() AdmissionResponse {
return v
}
func (v *v1AdmissionReview) Version() string {
return v1.SchemeGroupVersion.Version
}
func (v *v1AdmissionReview) ClearRequest() {
v.target.Request = nil
}
// Request decorator functions
var _ AdmissionRequest = &v1AdmissionReviewRequest{}
type v1AdmissionReviewRequest struct {
target *v1.AdmissionRequest
}
func (v *v1AdmissionReviewRequest) ResourceID() types.NamespacedName {
return types.NamespacedName{Namespace: v.target.Namespace, Name: v.target.Name}
}
func (v *v1AdmissionReviewRequest) Namespace() string {
return v.target.Namespace
}
func (v *v1AdmissionReviewRequest) Kind() metav1.GroupVersionKind {
return v.target.Kind
}
func (v *v1AdmissionReviewRequest) Object() *runtime.RawExtension {
return &v.target.Object
}
func (v *v1AdmissionReviewRequest) OldObject() *runtime.RawExtension {
return &v.target.OldObject
}
func (v *v1AdmissionReviewRequest) ResourceKind() metav1.GroupVersionResource {
return v.target.Resource
}
// Response decorator functions
var _ AdmissionResponse = &v1AdmissionReview{}
func (v *v1AdmissionReview) withResponse(handler func(response *v1.AdmissionResponse)) {
if v.target.Response == nil {
if v.target.Request == nil {
return
}
v.target.Response = &v1.AdmissionResponse{UID: v.target.Request.UID}
}
handler(v.target.Response)
}
func (v *v1AdmissionReview) Allow() {
v.withResponse(func(response *v1.AdmissionResponse) {
response.Allowed = true
})
}
func (v *v1AdmissionReview) Deny(status *metav1.Status) {
v.withResponse(func(response *v1.AdmissionResponse) {
response.Allowed = false
response.Result = status
})
}
func (v *v1AdmissionReview) PatchJSON(bytes []byte) {
v.withResponse(func(response *v1.AdmissionResponse) {
response.Allowed = true
if len(bytes) > 0 {
response.PatchType = &v1PatchTypeJSONPatch
response.Patch = bytes
}
})
}
func (v *v1AdmissionReview) IsSet() bool {
return v.target.Response != nil
}
func (v *v1AdmissionReview) ResponseType() ResponseType {
if v.target.Response == nil {
return AdmissionError
}
if len(v.target.Response.Patch) > 0 {
return AdmissionMutated
}
if v.target.Response.Allowed {
return AdmissionAllowed
}
return AdmissionDenied
}