-
Notifications
You must be signed in to change notification settings - Fork 30
/
clusteradmissionpolicy_types.go
246 lines (205 loc) · 8.72 KB
/
clusteradmissionpolicy_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
/*
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 v1
import (
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// ContextAwareResource identifies a Kubernetes resource
type ContextAwareResource struct {
// apiVersion of the resource (v1 for core group, groupName/groupVersions for other).
APIVersion string `json:"apiVersion"`
// Singular PascalCase name of the resource
Kind string `json:"kind"`
}
// ClusterAdmissionPolicySpec defines the desired state of ClusterAdmissionPolicy
type ClusterAdmissionPolicySpec struct {
PolicySpec `json:""` //nolint
// 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.
// <br/><br/>
// 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:
// <pre>
// "namespaceSelector": \{<br/>
// "matchExpressions": [<br/>
// \{<br/>
// "key": "runlevel",<br/>
// "operator": "NotIn",<br/>
// "values": [<br/>
// "0",<br/>
// "1"<br/>
// ]<br/>
// \}<br/>
// ]<br/>
// \}
// </pre>
// 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:
// <pre>
// "namespaceSelector": \{<br/>
// "matchExpressions": [<br/>
// \{<br/>
// "key": "environment",<br/>
// "operator": "In",<br/>
// "values": [<br/>
// "prod",<br/>
// "staging"<br/>
// ]<br/>
// \}<br/>
// ]<br/>
// \}
// </pre>
// See
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels
// for more examples of label selectors.
// <br/><br/>
// Default to the empty LabelSelector, which matches everything.
// +optional
NamespaceSelector *metav1.LabelSelector `json:"namespaceSelector,omitempty"`
// List of Kubernetes resources the policy is allowed to access at evaluation time.
// Access to these resources is done using the `ServiceAccount` of the PolicyServer
// the policy is assigned to.
// +optional
ContextAwareResources []ContextAwareResource `json:"contextAwareResources,omitempty"`
}
// ClusterAdmissionPolicy is the Schema for the clusteradmissionpolicies API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
// +kubebuilder:storageversion
// +kubebuilder:printcolumn:name="Policy Server",type=string,JSONPath=`.spec.policyServer`,description="Bound to Policy Server"
// +kubebuilder:printcolumn:name="Mutating",type=boolean,JSONPath=`.spec.mutating`,description="Whether the policy is mutating"
// +kubebuilder:printcolumn:name="BackgroundAudit",type=boolean,JSONPath=`.spec.backgroundAudit`,description="Whether the policy is used in audit checks"
// +kubebuilder:printcolumn:name="Mode",type=string,JSONPath=`.spec.mode`,description="Policy deployment mode"
// +kubebuilder:printcolumn:name="Observed mode",type=string,JSONPath=`.status.mode`,description="Policy deployment mode observed on the assigned Policy Server"
// +kubebuilder:printcolumn:name="Status",type=string,JSONPath=`.status.policyStatus`,description="Status of the policy"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"
// +kubebuilder:printcolumn:name="Severity",type=string,JSONPath=".metadata.annotations['io\\.kubewarden\\.policy\\.severity']",priority=1
// +kubebuilder:printcolumn:name="Category",type=string,JSONPath=".metadata.annotations['io\\.kubewarden\\.policy\\.category']",priority=1
type ClusterAdmissionPolicy struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ClusterAdmissionPolicySpec `json:"spec,omitempty"`
Status PolicyStatus `json:"status,omitempty"`
}
// ClusterAdmissionPolicyList contains a list of ClusterAdmissionPolicy
// +kubebuilder:object:root=true
type ClusterAdmissionPolicyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []ClusterAdmissionPolicy `json:"items"`
}
func init() {
SchemeBuilder.Register(&ClusterAdmissionPolicy{}, &ClusterAdmissionPolicyList{})
}
func (r *ClusterAdmissionPolicy) SetStatus(status PolicyStatusEnum) {
r.Status.PolicyStatus = status
}
func (r *ClusterAdmissionPolicy) GetPolicyMode() PolicyMode {
return r.Spec.Mode
}
func (r *ClusterAdmissionPolicy) SetPolicyModeStatus(policyMode PolicyModeStatus) {
r.Status.PolicyMode = policyMode
}
func (r *ClusterAdmissionPolicy) GetModule() string {
return r.Spec.Module
}
func (r *ClusterAdmissionPolicy) IsMutating() bool {
return r.Spec.Mutating
}
func (r *ClusterAdmissionPolicy) IsContextAware() bool {
return len(r.Spec.ContextAwareResources) > 0
}
func (r *ClusterAdmissionPolicy) GetSettings() runtime.RawExtension {
return r.Spec.Settings
}
func (r *ClusterAdmissionPolicy) GetStatus() *PolicyStatus {
return &r.Status
}
func (r *ClusterAdmissionPolicy) CopyInto(policy *Policy) {
*policy = r.DeepCopy()
}
func (r *ClusterAdmissionPolicy) GetSideEffects() *admissionregistrationv1.SideEffectClass {
return r.Spec.SideEffects
}
func (r *ClusterAdmissionPolicy) GetFailurePolicy() *admissionregistrationv1.FailurePolicyType {
return r.Spec.FailurePolicy
}
func (r *ClusterAdmissionPolicy) GetMatchPolicy() *admissionregistrationv1.MatchPolicyType {
return r.Spec.MatchPolicy
}
func (r *ClusterAdmissionPolicy) GetRules() []admissionregistrationv1.RuleWithOperations {
return r.Spec.Rules
}
func (r *ClusterAdmissionPolicy) GetUpdatedNamespaceSelector(deploymentNamespace string) *metav1.LabelSelector {
// exclude namespace where kubewarden was deployed
if r.Spec.NamespaceSelector != nil {
r.Spec.NamespaceSelector.MatchExpressions = append(r.Spec.NamespaceSelector.MatchExpressions, metav1.LabelSelectorRequirement{
Key: "kubernetes.io/metadata.name",
Operator: "NotIn",
Values: []string{deploymentNamespace},
})
} else {
r.Spec.NamespaceSelector = &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{{
Key: "kubernetes.io/metadata.name",
Operator: "NotIn",
Values: []string{deploymentNamespace},
}},
}
}
return r.Spec.NamespaceSelector
}
func (r *ClusterAdmissionPolicy) GetObjectSelector() *metav1.LabelSelector {
return r.Spec.ObjectSelector
}
func (r *ClusterAdmissionPolicy) GetTimeoutSeconds() *int32 {
return r.Spec.TimeoutSeconds
}
func (r *ClusterAdmissionPolicy) GetObjectMeta() *metav1.ObjectMeta {
return &r.ObjectMeta
}
func (r *ClusterAdmissionPolicy) GetPolicyServer() string {
return r.Spec.PolicyServer
}
func (r *ClusterAdmissionPolicy) GetUniqueName() string {
return "clusterwide-" + r.Name
}
func (r *ClusterAdmissionPolicy) GetContextAwareResources() []ContextAwareResource {
return r.Spec.ContextAwareResources
}
func (r *ClusterAdmissionPolicy) GetBackgroundAudit() bool {
return r.Spec.BackgroundAudit
}
func (r *ClusterAdmissionPolicy) GetSeverity() (string, bool) {
severity, present := r.Annotations[AnnotationSeverity]
return severity, present
}
func (r *ClusterAdmissionPolicy) GetCategory() (string, bool) {
category, present := r.Annotations[AnnotationCategory]
return category, present
}
func (r *ClusterAdmissionPolicy) GetTitle() (string, bool) {
title, present := r.Annotations[AnnotationTitle]
return title, present
}
func (r *ClusterAdmissionPolicy) GetDescription() (string, bool) {
desc, present := r.Annotations[AnnotationDescription]
return desc, present
}