forked from knative/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontexts.go
279 lines (232 loc) · 9.54 KB
/
contexts.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
/*
Copyright 2019 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package apis
import (
"context"
"net/http"
admissionv1 "k8s.io/api/admission/v1"
authenticationv1 "k8s.io/api/authentication/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// This is attached to contexts passed to webhook interfaces when
// the receiver being validated is being created.
type inCreateKey struct{}
// WithinCreate is used to note that the webhook is calling within
// the context of a Create operation.
func WithinCreate(ctx context.Context) context.Context {
return context.WithValue(ctx, inCreateKey{}, struct{}{})
}
// IsInCreate checks whether the context is a Create.
func IsInCreate(ctx context.Context) bool {
return ctx.Value(inCreateKey{}) != nil
}
// This is attached to contexts passed to webhook interfaces when
// the receiver being validated is being deleted.
type inDeleteKey struct{}
// WithinDelete is used to note that the webhook is calling within
// the context of a Delete operation.
func WithinDelete(ctx context.Context) context.Context {
return context.WithValue(ctx, inDeleteKey{}, struct{}{})
}
// IsInDelete checks whether the context is a Delete.
func IsInDelete(ctx context.Context) bool {
return ctx.Value(inDeleteKey{}) != nil
}
// This is attached to contexts passed to webhook interfaces when
// the receiver being validated is being updated.
type inUpdateKey struct{}
type updatePayload struct {
base interface{}
subresource string
}
// WithinUpdate is used to note that the webhook is calling within
// the context of a Update operation.
func WithinUpdate(ctx context.Context, base interface{}) context.Context {
return context.WithValue(ctx, inUpdateKey{}, &updatePayload{
base: base,
})
}
// WithinSubResourceUpdate is used to note that the webhook is calling within
// the context of a Update operation on a subresource.
func WithinSubResourceUpdate(ctx context.Context, base interface{}, sr string) context.Context {
return context.WithValue(ctx, inUpdateKey{}, &updatePayload{
base: base,
subresource: sr,
})
}
// IsInUpdate checks whether the context is an Update.
func IsInUpdate(ctx context.Context) bool {
return ctx.Value(inUpdateKey{}) != nil
}
// GetUpdatedSubresource returns the subresource being updated or "" if there
// is no subresource that's being updated. Examples are "status" for Status
// updates, or "scale" for scaling Deployment.
func GetUpdatedSubresource(ctx context.Context) string {
value := ctx.Value(inUpdateKey{})
if value == nil {
return ""
}
up := value.(*updatePayload)
return up.subresource
}
// IsInStatusUpdate checks whether the context is an Update.
func IsInStatusUpdate(ctx context.Context) bool {
return GetUpdatedSubresource(ctx) == "status"
}
// GetBaseline returns the baseline of the update, or nil when we
// are not within an update context.
func GetBaseline(ctx context.Context) interface{} {
value := ctx.Value(inUpdateKey{})
if value == nil {
return nil
}
return value.(*updatePayload).base
}
// This is attached to contexts passed to webhook interfaces when
// the receiver being validated is being created.
type userInfoKey struct{}
// WithUserInfo is used to note that the webhook is calling within
// the context of a Create operation.
func WithUserInfo(ctx context.Context, ui *authenticationv1.UserInfo) context.Context {
return context.WithValue(ctx, userInfoKey{}, ui)
}
// GetUserInfo accesses the UserInfo attached to the webhook context.
func GetUserInfo(ctx context.Context) *authenticationv1.UserInfo {
if ui, ok := ctx.Value(userInfoKey{}).(*authenticationv1.UserInfo); ok {
return ui
}
return nil
}
// This is attached to contexts as they are passed down through a resource
// being validated or defaulted to signal the ObjectMeta of the enclosing
// resource.
type parentMetaKey struct{}
// WithinParent attaches the ObjectMeta of the resource enclosing the
// nested resources we are validating. This is intended for use with
// interfaces like apis.Defaultable and apis.Validatable.
func WithinParent(ctx context.Context, om metav1.ObjectMeta) context.Context {
return context.WithValue(ctx, parentMetaKey{}, om)
}
// IsWithinParent returns true if we're within parent context.
func IsWithinParent(ctx context.Context) bool {
_, ok := ctx.Value(parentMetaKey{}).(metav1.ObjectMeta)
return ok
}
// ParentMeta accesses the ObjectMeta of the enclosing parent resource
// from the context. See WithinParent for how to attach the parent's
// ObjectMeta to the context.
func ParentMeta(ctx context.Context) metav1.ObjectMeta {
if om, ok := ctx.Value(parentMetaKey{}).(metav1.ObjectMeta); ok {
return om
}
return metav1.ObjectMeta{}
}
// This is attached to contexts as they are passed down through a resource
// being validated or defaulted to signal that we are within a Spec.
type inSpec struct{}
// WithinSpec notes on the context that further validation or defaulting
// is within the context of a Spec. This is intended for use with
// interfaces like apis.Defaultable and apis.Validatable.
func WithinSpec(ctx context.Context) context.Context {
return context.WithValue(ctx, inSpec{}, struct{}{})
}
// IsInSpec returns whether the context of validation or defaulting is
// the Spec of the parent resource.
func IsInSpec(ctx context.Context) bool {
return ctx.Value(inSpec{}) != nil
}
// This is attached to contexts as they are passed down through a resource
// being validated or defaulted to signal that we are within a Status.
type inStatus struct{}
// WithinStatus notes on the context that further validation or defaulting
// is within the context of a Status. This is intended for use with
// interfaces like apis.Defaultable and apis.Validatable.
func WithinStatus(ctx context.Context) context.Context {
return context.WithValue(ctx, inStatus{}, struct{}{})
}
// IsInStatus returns whether the context of validation or defaulting is
// the Status of the parent resource.
func IsInStatus(ctx context.Context) bool {
return ctx.Value(inStatus{}) != nil
}
// This is attached to contexts as they are passed down through a resource
// being validated to direct them to disallow deprecated fields.
type disallowDeprecated struct{}
// DisallowDeprecated notes on the context that further validation
// should disallow the used of deprecated fields. This may be used
// to ensure that new paths through resources to a common type don't
// allow the mistakes of old versions to be introduced.
func DisallowDeprecated(ctx context.Context) context.Context {
return context.WithValue(ctx, disallowDeprecated{}, struct{}{})
}
// IsDeprecatedAllowed checks the context to see whether deprecated fields
// are allowed.
func IsDeprecatedAllowed(ctx context.Context) bool {
return ctx.Value(disallowDeprecated{}) == nil
}
// This is attached to contexts as they are passed down through a resource
// being validated to direct them to allow namespaces (or missing namespace)
// outside the parent (as indicated by WithinParent.
type allowDifferentNamespace struct{}
// AllowDifferentNamespace notes on the context that further validation
// should allow different namespaces from the encapsulating object. Mainly
// used by KReference, since it by default requires namespaces to match.
func AllowDifferentNamespace(ctx context.Context) context.Context {
return context.WithValue(ctx, allowDifferentNamespace{}, struct{}{})
}
// IsDifferentNamespaceAllowed checks the context to see whether different
// namespace is allowed from the encapsulating object.
func IsDifferentNamespaceAllowed(ctx context.Context) bool {
return ctx.Value(allowDifferentNamespace{}) != nil
}
// This is attached to contexts passed to webhook interfaces when the user
// has requested DryRun mode.
type isDryRun struct{}
// WithDryRun is used to indicate that this call is in DryRun mode.
func WithDryRun(ctx context.Context) context.Context {
return context.WithValue(ctx, isDryRun{}, struct{}{})
}
// IsDryRun indicates that this request is in DryRun mode.
func IsDryRun(ctx context.Context) bool {
return ctx.Value(isDryRun{}) != nil
}
// This is attached to contexts passed to webhook interfaces with
// additional context from the HTTP request.
type httpReq struct{}
type admissionRequest struct{}
// WithHTTPRequest associated the HTTP request object the webhook
// received with the context.
func WithHTTPRequest(ctx context.Context, r *http.Request) context.Context {
return context.WithValue(ctx, httpReq{}, r)
}
// GetHTTPRequest fetches the raw HTTP request received by the webhook.
func GetHTTPRequest(ctx context.Context) *http.Request {
v := ctx.Value(httpReq{})
if v == nil {
return nil
}
return v.(*http.Request)
}
// WithAdmissionRequest associated the admissionv1.AdmissionRequest object the webhook
// received with the context.
func WithAdmissionRequest(ctx context.Context, r *admissionv1.AdmissionRequest) context.Context {
return context.WithValue(ctx, admissionRequest{}, r)
}
// GetAdmissionRequest fetches the admissionv1.AdmissionRequest received by the webhook.
func GetAdmissionRequest(ctx context.Context) *admissionv1.AdmissionRequest {
v := ctx.Value(admissionRequest{})
if v == nil {
return nil
}
return v.(*admissionv1.AdmissionRequest)
}