forked from knative/pkg
/
contexts.go
182 lines (151 loc) · 6.13 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
/*
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"
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 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
}
// IsInStatusUpdate checks whether the context is an Update.
func IsInStatusUpdate(ctx context.Context) bool {
value := ctx.Value(inUpdateKey{})
if value == nil {
return false
}
up := value.(*updatePayload)
return up.subresource == "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)
}
// 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
}