Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.7] 🐛 Defaulting webhook should check class is set in ClusterClass-based clusters #10673

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/webhooks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ func (webhook *Cluster) Default(ctx context.Context, obj runtime.Object) error {
if !strings.HasPrefix(cluster.Spec.Topology.Version, "v") {
cluster.Spec.Topology.Version = "v" + cluster.Spec.Topology.Version
}

if cluster.Spec.Topology.Class == "" {
allErrs = append(
allErrs,
field.Required(
field.NewPath("spec", "topology", "class"),
"class cannot be empty",
),
)
return apierrors.NewInvalid(clusterv1.GroupVersion.WithKind("Cluster").GroupKind(), cluster.Name, allErrs)
}

clusterClass, err := webhook.pollClusterClassForCluster(ctx, cluster)
if err != nil {
// If the ClusterClass can't be found or is not up to date ignore the error.
Expand Down
17 changes: 17 additions & 0 deletions internal/webhooks/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,23 @@ func TestClusterDefaultTopologyVersion(t *testing.T) {
g.Expect(c.Spec.Topology.Version).To(HavePrefix("v"))
}

func TestClusterFailOnMissingClassField(t *testing.T) {
// NOTE: ClusterTopology feature flag is disabled by default, thus preventing to set Cluster.Topologies.
// Enabling the feature flag temporarily for this test.
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()

g := NewWithT(t)

c := builder.Cluster("fooboo", "cluster1").
WithTopology(builder.ClusterTopology().
WithClass(""). // THis is invalid.
WithVersion("1.19.1").
Build()).
Build()

g.Expect((&Cluster{}).Default(ctx, c)).ToNot(Succeed())
}

func TestClusterValidation(t *testing.T) {
// NOTE: ClusterTopology feature flag is disabled by default, thus preventing to set Cluster.Topologies.

Expand Down