Skip to content

Commit 66558c8

Browse files
CAPI MachineSet VAP
1 parent c3c9ca8 commit 66558c8

File tree

2 files changed

+366
-20
lines changed

2 files changed

+366
-20
lines changed

manifests/0000_30_cluster-api_09_admission-policies.yaml

Lines changed: 113 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ data:
254254
!(k in variables.newLabels)
255255
)
256256
)
257-
message: "Cannot add, modify or delete any machine.openshift.io/* or kubernetes.io/* label. This is because status.authoritativeAPI is set to Cluster API."
258-
257+
message: "Cannot add, modify or delete any machine.openshift.io/*, kubernetes.io/* or cluster.x-k8s.io/* label. This is because status.authoritativeAPI is set to Cluster API."
258+
259259
# Guard machine.openshift.io/* and cluster(s).x-k8s.io annotations
260260
- expression: >
261261
!(
@@ -268,16 +268,16 @@ data:
268268
!(k in variables.newAnn)
269269
)
270270
)
271-
message: "Cannot add, modify or delete any machine.openshift.io/* annotation. This is because status.authoritativeAPI is set to Cluster API."
272-
271+
message: "Cannot add, modify or delete any machine.openshift.io/* or cluster.x-k8s.io/* or clusters.x-k8s.io/* annotation. This is because status.authoritativeAPI is set to Cluster API."
272+
273273
# Param-controlled labels (labels on the CAPI machine) may change only to match the value on the CAPI Machine
274274
- expression: >
275275
variables.paramLabels.all(
276276
k,
277277
variables.newLabels[?k].orValue(null) == variables.oldLabels[?k].orValue(null) ||
278278
variables.newLabels[?k].orValue(null) == variables.paramLabels[k]
279279
)
280-
message: "Cannot modify a Cluster API controlled label except to match the Cluster API mirrored machine. This is because status.authoritativeAPI is set to Cluster API."
280+
message: "Cannot modify a Cluster API controlled label except to match the Cluster API mirrored MachineSet. This is because status.authoritativeAPI is set to Cluster API."
281281
---
282282
apiVersion: admissionregistration.k8s.io/v1
283283
kind: ValidatingAdmissionPolicyBinding
@@ -375,7 +375,7 @@ data:
375375
!(k in variables.newAnn)
376376
)
377377
)
378-
message: "Cannot add, modify or delete any machine.openshift.io/* or cluster.x-k8s.io or clusters.x-k8s.io annotation. This is because status.authoritativeAPI is set to Machine API."
378+
message: "Cannot add, modify or delete any machine.openshift.io/* or cluster.x-k8s.io/* or clusters.x-k8s.io/* annotation. This is because status.authoritativeAPI is set to Machine API."
379379
380380
# Param-controlled labels (labels on the MAPI machine) may change only to match the value on the MAPI Machine
381381
- expression: >
@@ -391,6 +391,113 @@ data:
391391
message: "Setting the 'machine-template-hash' label is forbidden.'"
392392
---
393393
apiVersion: admissionregistration.k8s.io/v1
394+
kind: ValidatingAdmissionPolicyBinding
395+
metadata:
396+
name: cluster-api-machine-set-vap
397+
spec:
398+
matchResources:
399+
namespaceSelector:
400+
matchLabels:
401+
kubernetes.io/metadata.name: openshift-cluster-api
402+
paramRef:
403+
namespace: openshift-machine-api
404+
# We 'Allow' here as we don't want to block CAPI Machine functionality
405+
# when no MAPI machine (param) exists. This might happen when a user
406+
# wants to not use MAPI, or is migrating.
407+
parameterNotFoundAction: Allow
408+
selector: {}
409+
policyName: cluster-api-machine-set-vap
410+
validationActions: [Deny]
411+
---
412+
apiVersion: admissionregistration.k8s.io/v1
413+
kind: ValidatingAdmissionPolicy
414+
metadata:
415+
name: cluster-api-machine-set-vap
416+
spec:
417+
failurePolicy: Fail
418+
419+
paramKind:
420+
apiVersion: machine.openshift.io/v1beta1
421+
kind: MachineSet
422+
423+
matchConstraints:
424+
resourceRules:
425+
- apiGroups: ["cluster.x-k8s.io"]
426+
apiVersions: ["v1beta2"]
427+
operations: ["UPDATE"]
428+
resources: ["machinesets"]
429+
430+
# Requests must satisfy every matchCondition to reach the validations
431+
matchConditions:
432+
- name: check-only-non-service-account-requests
433+
expression: >-
434+
!(request.userInfo.username in [
435+
"system:serviceaccount:openshift-machine-api:machine-api-controllers",
436+
"system:serviceaccount:openshift-cluster-api:cluster-capi-operator"
437+
])
438+
- name: check-param-match
439+
expression: 'object.metadata.name == params.metadata.name'
440+
- name: check-authoritativeAPI-machineapi
441+
expression: "params.?status.?authoritativeAPI.orValue(\"\") == \"MachineAPI\""
442+
variables:
443+
# label maps
444+
- name: newLabels
445+
expression: "object.metadata.?labels.orValue({})"
446+
- name: oldLabels
447+
expression: "oldObject.metadata.?labels.orValue({})"
448+
- name: paramLabels
449+
expression: "params.metadata.?labels.orValue({})"
450+
451+
# annotation maps
452+
- name: newAnn
453+
expression: "object.metadata.?annotations.orValue({})"
454+
- name: oldAnn
455+
expression: "oldObject.metadata.?annotations.orValue({})"
456+
457+
# All validations must evaluate to TRUE
458+
validations:
459+
# Only spec.authoritativeAPI may change
460+
- expression: "object.spec == oldObject.spec"
461+
message: "Changing .spec is not allowed. This is because status.authoritativeAPI is set to Machine API."
462+
463+
# Guard machine.openshift.io/* and kubernetes.io/* and cluster.x-k8s.io/* labels
464+
- expression: >
465+
!(
466+
variables.newLabels.exists(k,
467+
(k.startsWith('machine.openshift.io') || k.startsWith('kubernetes.io') || k.contains('cluster.x-k8s.io/')) &&
468+
(variables.oldLabels[?k].orValue(null) != variables.newLabels[k])
469+
) ||
470+
variables.oldLabels.exists(k,
471+
(k.startsWith('machine.openshift.io') || k.startsWith('kubernetes.io') || k.contains('cluster.x-k8s.io/')) &&
472+
!(k in variables.newLabels)
473+
)
474+
)
475+
message: "Cannot add, modify or delete any machine.openshift.io/*, kubernetes.io/* or cluster.x-k8s.io/* label. This is because status.authoritativeAPI is set to Machine API."
476+
477+
# Guard machine.openshift.io/* and cluster.x-k8s.io/* and clusters.x-k8s.io/* annotations
478+
- expression: >
479+
!(
480+
variables.newAnn.exists(k,
481+
(k.startsWith('machine.openshift.io') || k.contains('cluster.x-k8s.io') || k.contains('clusters.x-k8s.io')) &&
482+
(variables.oldAnn[?k].orValue(null) != variables.newAnn[k])
483+
) ||
484+
variables.oldAnn.exists(k,
485+
(k.startsWith('machine.openshift.io') || k.contains('cluster.x-k8s.io') || k.contains('clusters.x-k8s.io')) &&
486+
!(k in variables.newAnn)
487+
)
488+
)
489+
message: "Cannot add, modify or delete any machine.openshift.io/* or cluster.x-k8s.io/* or clusters.x-k8s.io/* annotation. This is because status.authoritativeAPI is set to Machine API."
490+
491+
# Param-controlled labels (labels on the MAPI machine) may change only to match the value on the MAPI Machine
492+
- expression: >
493+
variables.paramLabels.all(
494+
k,
495+
variables.newLabels[?k].orValue(null) == variables.oldLabels[?k].orValue(null) ||
496+
variables.newLabels[?k].orValue(null) == variables.paramLabels[k]
497+
)
498+
message: "Cannot modify a Machine API controlled label except to match the Machine API mirrored MachineSet. This is because status.authoritativeAPI is set to Machine API."
499+
---
500+
apiVersion: admissionregistration.k8s.io/v1
394501
kind: ValidatingAdmissionPolicy
395502
metadata:
396503
name: openshift-cluster-api-prevent-setting-of-capi-fields-unsupported-by-mapi

0 commit comments

Comments
 (0)