Skip to content

Commit deb1808

Browse files
committed
Add ValidatingAdmissionPolicy for EgressIP
This commit is to add couple of ValidatingAdmissionPolicy to take care of following conditions: - k8s.ovn.org/egressip-mark annotation should not be added while creating an EgressIP. - A regular user should not be able to add k8s.ovn.org/egressip-mark annotation. Only a system user is allowed to do so. Signed-off-by: Arnab Ghosh <arnabghosh89@gmail.com>
1 parent 8d09651 commit deb1808

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
apiVersion: admissionregistration.k8s.io/v1
2+
kind: ValidatingAdmissionPolicy
3+
metadata:
4+
name: egressip-update-validation
5+
spec:
6+
failurePolicy: Fail
7+
matchConditions:
8+
- expression: '("k8s.ovn.org/egressip-mark" in object.metadata.annotations) && (!has(oldObject.metadata.annotations) ||
9+
!("k8s.ovn.org/egressip-mark" in oldObject.metadata.annotations))'
10+
name: egressip-mark-annotation-update
11+
matchConstraints:
12+
resourceRules:
13+
- apiGroups: ["k8s.ovn.org"]
14+
apiVersions: ["v1"]
15+
operations: ["UPDATE"]
16+
resources: ["egressips"]
17+
validations:
18+
- expression: '(request.userInfo.username == "system:serviceaccount:openshift-ovn-kubernetes:ovn-kubernetes-control-plane")'
19+
message: 'A regular user must not add "k8s.ovn.org/egressip-mark" annotation to an EgressIP custom resource.'
20+
reason: Invalid
21+
22+
---
23+
apiVersion: admissionregistration.k8s.io/v1
24+
kind: ValidatingAdmissionPolicy
25+
metadata:
26+
name: egressip-create-validation
27+
spec:
28+
failurePolicy: Fail
29+
matchConstraints:
30+
resourceRules:
31+
- apiGroups: ["k8s.ovn.org"]
32+
apiVersions: ["v1"]
33+
operations: ["CREATE"]
34+
resources: ["egressips"]
35+
validations:
36+
- expression: '!has(object.metadata.annotations) || !("k8s.ovn.org/egressip-mark" in object.metadata.annotations)'
37+
message: 'EgressIP resources cannot be created with the "k8s.ovn.org/egressip-mark" annotation. This annotation is managed by the system.'
38+
reason: Invalid
39+
40+
---
41+
apiVersion: admissionregistration.k8s.io/v1
42+
kind: ValidatingAdmissionPolicyBinding
43+
metadata:
44+
name: egressip-update-validation-binding
45+
spec:
46+
policyName: egressip-update-validation
47+
validationActions: [Deny]
48+
49+
---
50+
apiVersion: admissionregistration.k8s.io/v1
51+
kind: ValidatingAdmissionPolicyBinding
52+
metadata:
53+
name: egressip-create-validation-binding
54+
spec:
55+
policyName: egressip-create-validation
56+
validationActions: [Deny]

pkg/network/ovn_kubernetes_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,7 +4153,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41534153
client: cnofake.NewFakeClient(),
41544154
featureGates: noFeatureGates,
41554155
},
4156-
expectNumObjs: 38,
4156+
expectNumObjs: 42,
41574157
},
41584158
{
41594159
name: "render routeadvertisements",
@@ -4168,7 +4168,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41684168
client: cnofake.NewFakeClient(),
41694169
featureGates: noFeatureGates,
41704170
},
4171-
expectNumObjs: 39,
4171+
expectNumObjs: 43,
41724172
},
41734173
{
41744174
name: "render with UDN",
@@ -4179,7 +4179,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41794179
client: cnofake.NewFakeClient(),
41804180
featureGates: udnFeatureGate,
41814181
},
4182-
expectNumObjs: 44,
4182+
expectNumObjs: 48,
41834183
},
41844184
{
41854185
name: "render with PreconfiguredUDNAddresses, UDN, persistent-IP, and RA",
@@ -4193,7 +4193,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41934193
client: cnofake.NewFakeClient(),
41944194
featureGates: preDefUDNFeatureGates,
41954195
},
4196-
expectNumObjs: 47,
4196+
expectNumObjs: 51,
41974197
},
41984198
}
41994199
for _, tt := range tests {

0 commit comments

Comments
 (0)