Skip to content

Commit c330599

Browse files
authored
Merge pull request #11969 from chrischdi/pr-cluster-validate-update-refs
🌱 cluster: validate that infrastructureRef and controlPlaneRef cannot be unset
2 parents a86a739 + e5b4e60 commit c330599

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

internal/webhooks/cluster.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,17 @@ func (webhook *Cluster) validate(ctx context.Context, oldCluster, newCluster *cl
209209
)
210210
}
211211
}
212+
212213
specPath := field.NewPath("spec")
214+
if newCluster.Spec.InfrastructureRef == nil && oldCluster != nil && oldCluster.Spec.InfrastructureRef != nil {
215+
allErrs = append(
216+
allErrs,
217+
field.Forbidden(
218+
specPath.Child("infrastructureRef"),
219+
"cannot be removed",
220+
),
221+
)
222+
}
213223
if newCluster.Spec.InfrastructureRef != nil && newCluster.Spec.InfrastructureRef.Namespace != newCluster.Namespace {
214224
allErrs = append(
215225
allErrs,
@@ -221,6 +231,15 @@ func (webhook *Cluster) validate(ctx context.Context, oldCluster, newCluster *cl
221231
)
222232
}
223233

234+
if newCluster.Spec.ControlPlaneRef == nil && oldCluster != nil && oldCluster.Spec.ControlPlaneRef != nil {
235+
allErrs = append(
236+
allErrs,
237+
field.Forbidden(
238+
specPath.Child("controlPlaneRef"),
239+
"cannot be removed",
240+
),
241+
)
242+
}
224243
if newCluster.Spec.ControlPlaneRef != nil && newCluster.Spec.ControlPlaneRef.Namespace != newCluster.Namespace {
225244
allErrs = append(
226245
allErrs,

internal/webhooks/cluster_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,26 @@ func TestClusterValidation(t *testing.T) {
16131613
in: builder.Cluster("fooNamespace", "thisNameContainsInvalid!@NonAlphanumerics").Build(),
16141614
expectErr: true,
16151615
},
1616+
{
1617+
name: "error when controlPlaneRef gets unset",
1618+
in: builder.Cluster("fooNamespace", "cluster1").
1619+
Build(),
1620+
old: builder.Cluster("fooNamespace", "cluster1").
1621+
WithControlPlane(
1622+
builder.ControlPlane("fooNamespace", "cp1").Build()).
1623+
Build(),
1624+
expectErr: true,
1625+
},
1626+
{
1627+
name: "error when infrastructureRef gets unset",
1628+
in: builder.Cluster("fooNamespace", "cluster1").
1629+
Build(),
1630+
old: builder.Cluster("fooNamespace", "cluster1").
1631+
WithInfrastructureCluster(
1632+
builder.InfrastructureClusterTemplate("fooNamespace", "infra1").Build()).
1633+
Build(),
1634+
expectErr: true,
1635+
},
16161636
}
16171637
for _, tt := range tests {
16181638
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)