/
jspolicy_types.go
279 lines (240 loc) · 10.8 KB
/
jspolicy_types.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
package v1beta1
import (
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// +genclient
// +genclient:nonNamespaced
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// JsPolicy holds the webhook configuration
// +k8s:openapi-gen=true
// +kubebuilder:subresource:status
type JsPolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec JsPolicySpec `json:"spec,omitempty"`
Status JsPolicyStatus `json:"status,omitempty"`
}
type JsPolicySpec struct {
// Operations is the operations the admission hook cares about - CREATE, UPDATE, DELETE, CONNECT or *
// for all of those operations and any future admission operations that are added.
// If '*' is present, the length of the slice must be one.
// Required.
Operations []admissionregistrationv1.OperationType `json:"operations,omitempty" protobuf:"bytes,1,rep,name=operations,casttype=OperationType"`
// Resources is a list of resources this rule applies to.
//
// For example:
// 'pods' means pods.
// 'pods/log' means the log subresource of pods.
// '*' means all resources, but not subresources.
// 'pods/*' means all subresources of pods.
// '*/scale' means all scale subresources.
// '*/*' means all resources and their subresources.
//
// If wildcard is present, the validation rule will ensure resources do not
// overlap with each other.
//
// Depending on the enclosing object, subresources might not be allowed.
// Required.
Resources []string `json:"resources,omitempty" protobuf:"bytes,3,rep,name=resources"`
// APIGroups is the API groups the resources belong to. '*' is all groups.
// If '*' is present, the length of the slice must be one.
// +optional
APIGroups []string `json:"apiGroups,omitempty" protobuf:"bytes,1,rep,name=apiGroups"`
// APIVersions is the API versions the resources belong to. '*' is all versions.
// If '*' is present, the length of the slice must be one.
// +optional
APIVersions []string `json:"apiVersions,omitempty" protobuf:"bytes,2,rep,name=apiVersions"`
// scope specifies the scope of this rule.
// Valid values are "Cluster", "Namespaced", and "*"
// "Cluster" means that only cluster-scoped resources will match this rule.
// Namespace API objects are cluster-scoped.
// "Namespaced" means that only namespaced resources will match this rule.
// "*" means that there are no scope restrictions.
// Subresources match the scope of their parent resource.
// Default is "*".
//
// +optional
Scope *admissionregistrationv1.ScopeType `json:"scope,omitempty" protobuf:"bytes,4,rep,name=scope"`
// FailurePolicy defines how unrecognized errors from the admission endpoint are handled -
// allowed values are Ignore or Fail. Defaults to Fail.
// +optional
FailurePolicy *admissionregistrationv1.FailurePolicyType `json:"failurePolicy,omitempty" protobuf:"bytes,4,opt,name=failurePolicy,casttype=FailurePolicyType"`
// matchPolicy defines how the "rules" list is used to match incoming requests.
// Allowed values are "Exact" or "Equivalent".
//
// - Exact: match a request only if it exactly matches a specified rule.
// For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1,
// but "rules" only included `apiGroups:["apps"], apiVersions:["v1"], resources: ["deployments"]`,
// a request to apps/v1beta1 or extensions/v1beta1 would not be sent to the webhook.
//
// - Equivalent: match a request if modifies a resource listed in rules, even via another API group or version.
// For example, if deployments can be modified via apps/v1, apps/v1beta1, and extensions/v1beta1,
// and "rules" only included `apiGroups:["apps"], apiVersions:["v1"], resources: ["deployments"]`,
// a request to apps/v1beta1 or extensions/v1beta1 would be converted to apps/v1 and sent to the webhook.
//
// Defaults to "Equivalent"
// +optional
MatchPolicy *admissionregistrationv1.MatchPolicyType `json:"matchPolicy,omitempty" protobuf:"bytes,9,opt,name=matchPolicy,casttype=MatchPolicyType"`
// NamespaceSelector decides whether to run the webhook on an object based
// on whether the namespace for that object matches the selector. If the
// object itself is a namespace, the matching is performed on
// object.metadata.labels. If the object is another cluster scoped resource,
// it never skips the webhook.
//
// For example, to run the webhook on any objects whose namespace is not
// associated with "runlevel" of "0" or "1"; you will set the selector as
// follows:
// "namespaceSelector": {
// "matchExpressions": [
// {
// "key": "runlevel",
// "operator": "NotIn",
// "values": [
// "0",
// "1"
// ]
// }
// ]
// }
//
// If instead you want to only run the webhook on any objects whose
// namespace is associated with the "environment" of "prod" or "staging";
// you will set the selector as follows:
// "namespaceSelector": {
// "matchExpressions": [
// {
// "key": "environment",
// "operator": "In",
// "values": [
// "prod",
// "staging"
// ]
// }
// ]
// }
//
// See
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels
// for more examples of label selectors.
//
// Default to the empty LabelSelector, which matches everything.
// +optional
NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty" protobuf:"bytes,5,opt,name=namespaceSelector"`
// ObjectSelector decides whether to run the webhook based on if the
// object has matching labels. objectSelector is evaluated against both
// the oldObject and newObject that would be sent to the webhook, and
// is considered to match if either object matches the selector. A null
// object (oldObject in the case of create, or newObject in the case of
// delete) or an object that cannot have labels (like a
// DeploymentRollback or a PodProxyOptions object) is not considered to
// match.
// Use the object selector only if the webhook is opt-in, because end
// users may skip the admission webhook by setting the labels.
// Default to the empty LabelSelector, which matches everything.
// +optional
ObjectSelector *metav1.LabelSelector `json:"objectSelector,omitempty" protobuf:"bytes,10,opt,name=objectSelector"`
// TimeoutSeconds specifies the timeout for this webhook. After the timeout passes,
// the webhook call will be ignored or the API call will fail based on the
// failure policy.
// The timeout value must be between 1 and 30 seconds.
// Default to 10 seconds.
// +optional
TimeoutSeconds *int32 `json:"timeoutSeconds,omitempty" protobuf:"varint,7,opt,name=timeoutSeconds"`
// Violation policy describes how violations should be handled. You can either specify deny (which is the default),
// warn or dry.
// +optional
ViolationPolicy *ViolationPolicyType `json:"violationPolicy,omitempty"`
// AuditPolicy defines if violations should be logged to the webhook status or not. By default, violations
// will be logged to the CRD status.
// +optional
AuditPolicy *AuditPolicyType `json:"auditPolicy,omitempty"`
// AuditLogSize defines how many violations should be logged in the status. Defaults to 10
// +optional
AuditLogSize *int32 `json:"auditLogSize,omitempty"`
// Type defines what kind of policy the object represents. Valid values are Validating, Mutating and
// Controller. Defaults to Validating.
// +optional
Type PolicyType `json:"type,omitempty"`
// Dependencies is a map of npm modules this webhook should be bundled with
// +optional
Dependencies map[string]string `json:"dependencies,omitempty"`
// JavaScript is the payload of the webhook that will be executed. If this is not defined,
// jsPolicy expects the user to create a JsPolicyBundle for this policy.
// +optional
JavaScript string `json:"javascript,omitempty"`
}
type JsPolicyStatus struct {
// Phase describes how the syncing status of the webhook is
// +optional
Phase WebhookPhase `json:"phase,omitempty"`
// Reason holds the error in machine-readable language if the webhook is in a failed state
// +optional
Reason string `json:"reason,omitempty"`
// Message describes the error in human-readable language if the webhook is in a failed state
// +optional
Message string `json:"message,omitempty"`
// Conditions holds several conditions the virtual cluster might be in
// +optional
Conditions Conditions `json:"conditions,omitempty"`
// ObservedGeneration is the latest generation observed by the controller.
// +optional
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
// BundleHash is used to determine if we have to re-bundle the javascript
// +optional
BundleHash string `json:"bundleHash,omitempty"`
}
// PolicyType is the type of a JsPolicy
type PolicyType string
const (
// PolicyTypeValidating indicates that the JsPolicy should be a Validating webhook
PolicyTypeValidating PolicyType = "Validating"
// PolicyTypeMutating indicates that the JsPolicy should be a Mutating webhook
PolicyTypeMutating PolicyType = "Mutating"
// PolicyTypeController indicates that the JsPolicy should be a Kubernetes controller
PolicyTypeController PolicyType = "Controller"
)
// ViolationPolicyType specify how to handle violations
type ViolationPolicyType string
const (
// ViolationPolicyPolicyDeny indicates that the webhook should deny the request
// if it violates the specified javascript rule.
ViolationPolicyPolicyDeny ViolationPolicyType = "Deny"
// ViolationPolicyPolicyWarn indicates that the webhook should warn the user that
// the request violates the specified javascript rule.
ViolationPolicyPolicyWarn ViolationPolicyType = "Warn"
// ViolationPolicyPolicyDry indicates that the webhook should record the violation
// but not deny or warn the user about it.
ViolationPolicyPolicyDry ViolationPolicyType = "Dry"
// ViolationPolicyPolicyController indicates that the violation was written by
// a controller policy that did not do any action.
ViolationPolicyPolicyController ViolationPolicyType = "Controller"
)
type AuditPolicyType string
const (
AuditPolicyLog AuditPolicyType = "Log"
AuditPolicySkip AuditPolicyType = "Skip"
)
type WebhookPhase string
const (
WebhookPhaseSynced WebhookPhase = "Synced"
WebhookPhaseFailed WebhookPhase = "Failed"
)
// GetConditions returns the set of conditions for this object.
func (in *JsPolicy) GetConditions() Conditions {
return in.Status.Conditions
}
// SetConditions sets the conditions on this object.
func (in *JsPolicy) SetConditions(conditions Conditions) {
in.Status.Conditions = conditions
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// JsPolicyList contains a list of JsPolicy
type JsPolicyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []JsPolicy `json:"items"`
}
func init() {
SchemeBuilder.Register(&JsPolicy{}, &JsPolicyList{})
}