Skip to content

Commit c228ecb

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 6688e9a commit c228ecb

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
@@ -4139,7 +4139,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41394139
client: cnofake.NewFakeClient(),
41404140
featureGates: noFeatureGates,
41414141
},
4142-
expectNumObjs: 38,
4142+
expectNumObjs: 42,
41434143
},
41444144
{
41454145
name: "render routeadvertisements",
@@ -4154,7 +4154,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41544154
client: cnofake.NewFakeClient(),
41554155
featureGates: noFeatureGates,
41564156
},
4157-
expectNumObjs: 39,
4157+
expectNumObjs: 43,
41584158
},
41594159
{
41604160
name: "render with UDN",
@@ -4165,7 +4165,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41654165
client: cnofake.NewFakeClient(),
41664166
featureGates: udnFeatureGate,
41674167
},
4168-
expectNumObjs: 44,
4168+
expectNumObjs: 48,
41694169
},
41704170
{
41714171
name: "render with PreconfiguredUDNAddresses, UDN, persistent-IP, and RA",
@@ -4179,7 +4179,7 @@ func Test_renderOVNKubernetes(t *testing.T) {
41794179
client: cnofake.NewFakeClient(),
41804180
featureGates: preDefUDNFeatureGates,
41814181
},
4182-
expectNumObjs: 45,
4182+
expectNumObjs: 49,
41834183
},
41844184
}
41854185
for _, tt := range tests {

0 commit comments

Comments
 (0)