/
clusteradmissionpolicy_types.go
176 lines (147 loc) · 6.11 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
/*
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 v1alpha2
import (
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)
// 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"`
}
// ClusterAdmissionPolicy is the Schema for the clusteradmissionpolicies API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:scope=Cluster
// +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="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"
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) 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) GetNamespaceSelector() *metav1.LabelSelector {
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
}