Skip to content

Commit d054ab9

Browse files
authored
Merge pull request #10671 from sbueringer/pr-fix-webhook-validation
🐛 Defaulting webhook should check class is set in ClusterClass-based clusters
2 parents 8e8650c + c2d63c5 commit d054ab9

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

internal/webhooks/cluster.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ func (webhook *Cluster) Default(ctx context.Context, obj runtime.Object) error {
101101
if !strings.HasPrefix(cluster.Spec.Topology.Version, "v") {
102102
cluster.Spec.Topology.Version = "v" + cluster.Spec.Topology.Version
103103
}
104+
105+
if cluster.Spec.Topology.Class == "" {
106+
allErrs = append(
107+
allErrs,
108+
field.Required(
109+
field.NewPath("spec", "topology", "class"),
110+
"class cannot be empty",
111+
),
112+
)
113+
return apierrors.NewInvalid(clusterv1.GroupVersion.WithKind("Cluster").GroupKind(), cluster.Name, allErrs)
114+
}
115+
104116
clusterClass, err := webhook.pollClusterClassForCluster(ctx, cluster)
105117
if err != nil {
106118
// If the ClusterClass can't be found or is not up to date ignore the error.

internal/webhooks/cluster_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,23 @@ func TestClusterDefaultTopologyVersion(t *testing.T) {
897897
g.Expect(c.Spec.Topology.Version).To(HavePrefix("v"))
898898
}
899899

900+
func TestClusterFailOnMissingClassField(t *testing.T) {
901+
// NOTE: ClusterTopology feature flag is disabled by default, thus preventing to set Cluster.Topologies.
902+
// Enabling the feature flag temporarily for this test.
903+
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
904+
905+
g := NewWithT(t)
906+
907+
c := builder.Cluster("fooboo", "cluster1").
908+
WithTopology(builder.ClusterTopology().
909+
WithClass(""). // THis is invalid.
910+
WithVersion("1.19.1").
911+
Build()).
912+
Build()
913+
914+
g.Expect((&Cluster{}).Default(ctx, c)).ToNot(Succeed())
915+
}
916+
900917
func TestClusterValidation(t *testing.T) {
901918
// NOTE: ClusterTopology feature flag is disabled by default, thus preventing to set Cluster.Topologies.
902919

0 commit comments

Comments
 (0)