Skip to content

Commit 89e689d

Browse files
committed
MachineOSConfig name should match MachineConfigPool
Add a restriction where the name of the MachineOSConfig object must be the same as the MachineConfigPool it is created for. This will ensure that we only have one MOSC per MCP. Signed-off-by: Urvashi <[email protected]>
1 parent 356d265 commit 89e689d

File tree

5 files changed

+199
-8
lines changed

5 files changed

+199
-8
lines changed

machineconfiguration/v1/tests/machineosconfigs.machineconfiguration.openshift.io/AAA_ungated.yaml

Lines changed: 180 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tests:
88
apiVersion: machineconfiguration.openshift.io/v1
99
kind: MachineOSConfig
1010
metadata:
11-
name: foobar
11+
name: worker
1212
spec:
1313
machineConfigPool:
1414
name: worker
@@ -23,7 +23,7 @@ tests:
2323
apiVersion: machineconfiguration.openshift.io/v1
2424
kind: MachineOSConfig
2525
metadata:
26-
name: foobar
26+
name: worker
2727
spec:
2828
machineConfigPool:
2929
name: worker
@@ -39,7 +39,7 @@ tests:
3939
apiVersion: machineconfiguration.openshift.io/v1
4040
kind: MachineOSConfig
4141
metadata:
42-
name: foobar
42+
name: worker
4343
spec:
4444
machineConfigPool:
4545
name: worker
@@ -54,7 +54,7 @@ tests:
5454
apiVersion: machineconfiguration.openshift.io/v1
5555
kind: MachineOSConfig
5656
metadata:
57-
name: foobar
57+
name: worker
5858
spec:
5959
machineConfigPool:
6060
name: worker
@@ -70,7 +70,7 @@ tests:
7070
apiVersion: machineconfiguration.openshift.io/v1
7171
kind: MachineOSConfig
7272
metadata:
73-
name: foobar
73+
name: worker
7474
spec:
7575
machineConfigPool:
7676
name: worker
@@ -87,7 +87,7 @@ tests:
8787
apiVersion: machineconfiguration.openshift.io/v1
8888
kind: MachineOSConfig
8989
metadata:
90-
name: foobar
90+
name: worker
9191
spec:
9292
machineConfigPool:
9393
name: worker
@@ -100,12 +100,184 @@ tests:
100100
apiVersion: machineconfiguration.openshift.io/v1
101101
kind: MachineOSConfig
102102
metadata:
103-
name: foobar
103+
name: worker
104104
spec:
105105
machineConfigPool:
106106
name: worker
107-
imageBuilder:
107+
imageBuilder:
108+
imageBuilderType: Job
109+
renderedImagePushSecret:
110+
name: foo
111+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
112+
- name: Should succeed when MachineOSConfig name matches MachineConfigPool name
113+
initial: |
114+
apiVersion: machineconfiguration.openshift.io/v1
115+
kind: MachineOSConfig
116+
metadata:
117+
name: worker
118+
spec:
119+
machineConfigPool:
120+
name: worker
121+
imageBuilder:
122+
imageBuilderType: Job
123+
renderedImagePushSecret:
124+
name: foo
125+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
126+
expected: |
127+
apiVersion: machineconfiguration.openshift.io/v1
128+
kind: MachineOSConfig
129+
metadata:
130+
name: worker
131+
spec:
132+
machineConfigPool:
133+
name: worker
134+
imageBuilder:
135+
imageBuilderType: Job
136+
renderedImagePushSecret:
137+
name: foo
138+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
139+
- name: Should fail when MachineOSConfig name does not match MachineConfigPool name
140+
initial: |
141+
apiVersion: machineconfiguration.openshift.io/v1
142+
kind: MachineOSConfig
143+
metadata:
144+
name: different-name
145+
spec:
146+
machineConfigPool:
147+
name: worker
148+
imageBuilder:
149+
imageBuilderType: Job
150+
renderedImagePushSecret:
151+
name: foo
152+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
153+
expectedError: "MachineOSConfig name must match the referenced MachineConfigPool name; can only have one MachineOSConfig per MachineConfigPool"
154+
onUpdate:
155+
- name: Should allow changing other fields when a persisted value is no longer valid (mismatched names)
156+
initialCRDPatches:
157+
- op: remove
158+
path: /spec/versions/0/schema/openAPIV3Schema/x-kubernetes-validations # Remove the name matching validation
159+
initial: |
160+
apiVersion: machineconfiguration.openshift.io/v1
161+
kind: MachineOSConfig
162+
metadata:
163+
name: different-name
164+
spec:
165+
machineConfigPool:
166+
name: worker
167+
imageBuilder:
168+
imageBuilderType: Job
169+
renderedImagePushSecret:
170+
name: foo
171+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
172+
updated: |
173+
apiVersion: machineconfiguration.openshift.io/v1
174+
kind: MachineOSConfig
175+
metadata:
176+
name: different-name
177+
spec:
178+
machineConfigPool:
179+
name: worker
180+
imageBuilder:
181+
imageBuilderType: Job
182+
renderedImagePushSecret:
183+
name: foo
184+
renderedImagePushSpec: quay.io/mco/renderedImg:v2.0
185+
containerFile:
186+
- containerfileArch: AMD64
187+
content: |
188+
FROM configs AS final
189+
RUN rpm-ostree install tree && \
190+
ostree container commit
191+
expected: |
192+
apiVersion: machineconfiguration.openshift.io/v1
193+
kind: MachineOSConfig
194+
metadata:
195+
name: different-name
196+
spec:
197+
machineConfigPool:
198+
name: worker
199+
imageBuilder:
200+
imageBuilderType: Job
201+
renderedImagePushSecret:
202+
name: foo
203+
renderedImagePushSpec: quay.io/mco/renderedImg:v2.0
204+
containerFile:
205+
- containerfileArch: AMD64
206+
content: |
207+
FROM configs AS final
208+
RUN rpm-ostree install tree && \
209+
ostree container commit
210+
- name: Should allow updating a persisted value that is no longer valid to a valid value (fix mismatched names)
211+
initialCRDPatches:
212+
- op: remove
213+
path: /spec/versions/0/schema/openAPIV3Schema/x-kubernetes-validations # Remove the name matching validation
214+
initial: |
215+
apiVersion: machineconfiguration.openshift.io/v1
216+
kind: MachineOSConfig
217+
metadata:
218+
name: worker
219+
spec:
220+
machineConfigPool:
221+
name: different-pool
222+
imageBuilder:
223+
imageBuilderType: Job
224+
renderedImagePushSecret:
225+
name: foo
226+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
227+
updated: |
228+
apiVersion: machineconfiguration.openshift.io/v1
229+
kind: MachineOSConfig
230+
metadata:
231+
name: worker
232+
spec:
233+
machineConfigPool:
234+
name: worker
235+
imageBuilder:
236+
imageBuilderType: Job
237+
renderedImagePushSecret:
238+
name: foo
239+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
240+
expected: |
241+
apiVersion: machineconfiguration.openshift.io/v1
242+
kind: MachineOSConfig
243+
metadata:
244+
name: worker
245+
spec:
246+
machineConfigPool:
247+
name: worker
248+
imageBuilder:
249+
imageBuilderType: Job
250+
renderedImagePushSecret:
251+
name: foo
252+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
253+
- name: Should not allow updating a persisted value that is no longer valid to a still invalid value (different mismatched MCP names)
254+
initialCRDPatches:
255+
- op: remove
256+
path: /spec/versions/0/schema/openAPIV3Schema/x-kubernetes-validations # Remove the name matching validation
257+
initial: |
258+
apiVersion: machineconfiguration.openshift.io/v1
259+
kind: MachineOSConfig
260+
metadata:
261+
name: different-name
262+
spec:
263+
machineConfigPool:
264+
name: worker
265+
imageBuilder:
266+
imageBuilderType: Job
267+
renderedImagePushSecret:
268+
name: foo
269+
renderedImagePushSpec: quay.io/mco/renderedImg:latest
270+
updated: |
271+
apiVersion: machineconfiguration.openshift.io/v1
272+
kind: MachineOSConfig
273+
metadata:
274+
name: different-name
275+
spec:
276+
machineConfigPool:
277+
name: worker-different
278+
imageBuilder:
108279
imageBuilderType: Job
109280
renderedImagePushSecret:
110281
name: foo
111282
renderedImagePushSpec: quay.io/mco/renderedImg:latest
283+
expectedError: "MachineOSConfig name must match the referenced MachineConfigPool name; can only have one MachineOSConfig per MachineConfigPool"

machineconfiguration/v1/types_machineosconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
// MachineOSConfig describes the configuration for a build process managed by the MCO
1818
// Compatibility level 1: Stable within a major release for a minimum of 12 months or 3 minor releases (whichever is longer).
1919
// +openshift:compatibility-gen:level=1
20+
// +kubebuilder:validation:XValidation:rule="self.metadata.name == self.spec.machineConfigPool.name || oldSelf.hasValue() && oldSelf.spec.machineConfigPool.name.value() == self.spec.machineConfigPool.name",optionalOldSelf=true,message="MachineOSConfig name must match the referenced MachineConfigPool name; can only have one MachineOSConfig per MachineConfigPool"
2021
type MachineOSConfig struct {
2122
metav1.TypeMeta `json:",inline"`
2223

machineconfiguration/v1/zz_generated.crd-manifests/0000_80_machine-config_01_machineosconfigs.crd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ spec:
345345
required:
346346
- spec
347347
type: object
348+
x-kubernetes-validations:
349+
- message: MachineOSConfig name must match the referenced MachineConfigPool
350+
name; can only have one MachineOSConfig per MachineConfigPool
351+
optionalOldSelf: true
352+
rule: self.metadata.name == self.spec.machineConfigPool.name || oldSelf.hasValue()
353+
&& oldSelf.spec.machineConfigPool.name.value() == self.spec.machineConfigPool.name
348354
served: true
349355
storage: true
350356
subresources:

machineconfiguration/v1/zz_generated.featuregated-crd-manifests/machineosconfigs.machineconfiguration.openshift.io/AAA_ungated.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ spec:
346346
required:
347347
- spec
348348
type: object
349+
x-kubernetes-validations:
350+
- message: MachineOSConfig name must match the referenced MachineConfigPool
351+
name; can only have one MachineOSConfig per MachineConfigPool
352+
optionalOldSelf: true
353+
rule: self.metadata.name == self.spec.machineConfigPool.name || oldSelf.hasValue()
354+
&& oldSelf.spec.machineConfigPool.name.value() == self.spec.machineConfigPool.name
349355
served: true
350356
storage: true
351357
subresources:

payload-manifests/crds/0000_80_machine-config_01_machineosconfigs.crd.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ spec:
345345
required:
346346
- spec
347347
type: object
348+
x-kubernetes-validations:
349+
- message: MachineOSConfig name must match the referenced MachineConfigPool
350+
name; can only have one MachineOSConfig per MachineConfigPool
351+
optionalOldSelf: true
352+
rule: self.metadata.name == self.spec.machineConfigPool.name || oldSelf.hasValue()
353+
&& oldSelf.spec.machineConfigPool.name.value() == self.spec.machineConfigPool.name
348354
served: true
349355
storage: true
350356
subresources:

0 commit comments

Comments
 (0)