Skip to content

Commit

Permalink
Merge pull request #10673 from k8s-infra-cherrypick-robot/cherry-pick…
Browse files Browse the repository at this point in the history
…-10671-to-release-1.7

[release-1.7] 🐛 Defaulting webhook should check class is set in ClusterClass-based clusters
  • Loading branch information
k8s-ci-robot authored May 24, 2024
2 parents 4a30454 + 6ffad3b commit 59cd48f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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

0 comments on commit 59cd48f

Please sign in to comment.