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

🌱 Use ClusterClass name index in ClusterClass webhook #5810

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ import (
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/component-base/featuregate/testing"
"sigs.k8s.io/controller-runtime/pkg/client"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/feature"
tlog "sigs.k8s.io/cluster-api/internal/log"
"sigs.k8s.io/cluster-api/internal/test/builder"
)

func TestClusterClassReconciler_reconcile(t *testing.T) {
defer utilfeature.SetFeatureGateDuringTest(t, feature.Gates, feature.ClusterTopology, true)()
g := NewWithT(t)
timeout := 30 * time.Second

Expand Down
5 changes: 5 additions & 0 deletions internal/controllers/clusterclass/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/component-base/featuregate"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/test/envtest"
)

Expand All @@ -51,6 +53,9 @@ func init() {
_ = apiextensionsv1.AddToScheme(fakeScheme)
}
func TestMain(m *testing.M) {
if err := feature.Gates.(featuregate.MutableFeatureGate).Set(fmt.Sprintf("%s=%v", feature.ClusterTopology, true)); err != nil {
panic(fmt.Sprintf("unable to set ClusterTopology feature gate: %v", err))
}
setupIndexes := func(ctx context.Context, mgr ctrl.Manager) {
if err := index.AddDefaultIndexes(ctx, mgr); err != nil {
panic(fmt.Sprintf("unable to setup index: %v", err))
Expand Down
5 changes: 5 additions & 0 deletions internal/controllers/topology/cluster/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ func TestMain(m *testing.M) {
if err := index.AddDefaultIndexes(ctx, mgr); err != nil {
panic(fmt.Sprintf("unable to setup index: %v", err))
}
// Set up the ClusterClassName index explicitly here. This index is ordinarily created in
// index.AddDefaultIndexes. That doesn't happen here because the ClusterClass feature flag is not set.
if err := index.ByClusterClassName(ctx, mgr); err != nil {
panic(fmt.Sprintf("unable to setup index: %v", err))
}
}
setupReconcilers := func(ctx context.Context, mgr ctrl.Manager) {
unstructuredCachingClient, err := client.NewDelegatingClient(
Expand Down
13 changes: 3 additions & 10 deletions internal/webhooks/clusterclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/api/v1beta1/index"
"sigs.k8s.io/cluster-api/feature"
"sigs.k8s.io/cluster-api/internal/topology/check"
"sigs.k8s.io/cluster-api/internal/topology/variables"
Expand Down Expand Up @@ -440,22 +441,14 @@ func (webhook *ClusterClass) classNamesFromWorkerClass(w clusterv1.WorkersClass)

func (webhook *ClusterClass) getClustersUsingClusterClass(ctx context.Context, clusterClass *clusterv1.ClusterClass) ([]clusterv1.Cluster, error) {
clusters := &clusterv1.ClusterList{}
clustersUsingClusterClass := []clusterv1.Cluster{}
err := webhook.Client.List(ctx, clusters,
client.MatchingLabels{
clusterv1.ClusterTopologyOwnedLabel: "",
},
client.MatchingFields{index.ClusterClassNameField: clusterClass.Name},
client.InNamespace(clusterClass.Namespace),
)
if err != nil {
return nil, err
}
for _, c := range clusters.Items {
if c.Spec.Topology.Class == clusterClass.Name {
clustersUsingClusterClass = append(clustersUsingClusterClass, c)
}
}
return clustersUsingClusterClass, nil
return clusters.Items, nil
}

func getClusterClassVariablesMapWithReverseIndex(clusterClassVariables []clusterv1.ClusterClassVariable) (map[string]*clusterv1.ClusterClassVariable, map[string]int) {
Expand Down
230 changes: 19 additions & 211 deletions internal/webhooks/clusterclass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
expectErr bool
}{
{
name: "error if a MachineDeploymentClass in use gets removed",
name: "pass if a MachineDeploymentClass not in use gets removed",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
Expand Down Expand Up @@ -1190,53 +1190,19 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
builder.ControlPlaneTemplate(metav1.NamespaceDefault, "cp1").
Build()).
WithWorkerMachineDeploymentClasses(
*builder.MachineDeploymentClass("aa").
*builder.MachineDeploymentClass("bb").
WithInfrastructureTemplate(
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithBootstrapTemplate(
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
Build()).
Build(),
expectErr: true,
expectErr: false,
},
{
name: "error if many MachineDeploymentClasses, used in multiple Clusters using the modified ClusterClass, are removed",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests removed here as they no longer work after the move to using the index. Index requires envtest and these tests rely solely on fakeclient.

name: "error if a MachineDeploymentClass in use gets removed",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class1").
WithMachineDeployment(
builder.MachineDeploymentTopology("workers1").
WithClass("bb").
Build(),
).
WithMachineDeployment(
builder.MachineDeploymentTopology("workers2").
WithClass("aa").
Build(),
).
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster2").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class1").
WithMachineDeployment(
builder.MachineDeploymentTopology("workers1").
WithClass("aa").
Build(),
).
WithMachineDeployment(
builder.MachineDeploymentTopology("workers2").
WithClass("aa").
Build(),
).
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster3").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
Expand Down Expand Up @@ -1276,7 +1242,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
builder.ControlPlaneTemplate(metav1.NamespaceDefault, "cp1").
Build()).
WithWorkerMachineDeploymentClasses(
*builder.MachineDeploymentClass("bb").
*builder.MachineDeploymentClass("aa").
WithInfrastructureTemplate(
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithBootstrapTemplate(
Expand All @@ -1286,7 +1252,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
expectErr: true,
},
{
name: "pass if a similar MachineDeploymentClass is deleted when it is only used in Clusters not belonging to the ClusterClass",
name: "error if many MachineDeploymentClasses, used in multiple Clusters using the modified ClusterClass, are removed",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
Expand All @@ -1298,64 +1264,31 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
WithClass("bb").
Build(),
).
WithMachineDeployment(
builder.MachineDeploymentTopology("workers2").
WithClass("aa").
Build(),
).
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster2").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class2").
WithClass("class1").
WithMachineDeployment(
builder.MachineDeploymentTopology("workers1").

// A MachineDeploymentClass with the same name is in ClusterClass "class1" but
// this cluster is based on ClusterClass "class2" and does not impact deletion.
WithClass("aa").
Build(),
).
WithMachineDeployment(
builder.MachineDeploymentTopology("workers2").
WithClass("aa").
Build(),
).
Build()).
Build(),
},
oldClusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(
builder.InfrastructureClusterTemplate(metav1.NamespaceDefault, "inf").Build()).
WithControlPlaneTemplate(
builder.ControlPlaneTemplate(metav1.NamespaceDefault, "cp1").
Build()).
WithWorkerMachineDeploymentClasses(
*builder.MachineDeploymentClass("aa").
WithInfrastructureTemplate(
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithBootstrapTemplate(
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
Build(),
*builder.MachineDeploymentClass("bb").
WithInfrastructureTemplate(
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithBootstrapTemplate(
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
Build()).
Build(),
newClusterClass: builder.ClusterClass(metav1.NamespaceDefault, "class1").
WithInfrastructureClusterTemplate(
builder.InfrastructureClusterTemplate(metav1.NamespaceDefault, "inf").Build()).
WithControlPlaneTemplate(
builder.ControlPlaneTemplate(metav1.NamespaceDefault, "cp1").
Build()).
WithWorkerMachineDeploymentClasses(
*builder.MachineDeploymentClass("bb").
WithInfrastructureTemplate(
builder.InfrastructureMachineTemplate(metav1.NamespaceDefault, "infra1").Build()).
WithBootstrapTemplate(
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
Build()).
Build(),
expectErr: false,
},
{
name: "pass if a MachineDeploymentClass not in use gets removed",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
builder.Cluster(metav1.NamespaceDefault, "cluster3").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
Expand Down Expand Up @@ -1402,7 +1335,7 @@ func TestClusterClassValidationWithClusterAwareChecks(t *testing.T) {
builder.BootstrapTemplate(metav1.NamespaceDefault, "bootstrap1").Build()).
Build()).
Build(),
expectErr: false,
expectErr: true,
},
}

Expand Down Expand Up @@ -2450,128 +2383,3 @@ func TestClusterClassValidationWithVariableChecks(t *testing.T) {
})
}
}

func TestClusterClass_ValidateDelete(t *testing.T) {
class := builder.ClusterClass(metav1.NamespaceDefault, "class1").Build()

tests := []struct {
name string
clusters []client.Object
expectErr bool
}{
{
name: "allow deletion if a cluster exists but does not reference the ClusterClass for deletion",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class2").
Build()).
Build(),
},
expectErr: false,
},
{
name: "error if cluster exists with a reference to the ClusterClass for deletion",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class1").
Build()).
Build(),
},
expectErr: true,
},
{
name: "error if multiple clusters exist and at least one references to the ClusterClass for deletion",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class1").
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster2").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class2").
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster3").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class3").
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster4").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class4").
Build()).
Build(),
},
expectErr: true,
},
{
name: "allow deletion if multiple clusters exist and none of them references to the ClusterClass for deletion",
clusters: []client.Object{
builder.Cluster(metav1.NamespaceDefault, "cluster1").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class5").
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster2").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class2").
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster3").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class3").
Build()).
Build(),
builder.Cluster(metav1.NamespaceDefault, "cluster4").
WithLabels(map[string]string{clusterv1.ClusterTopologyOwnedLabel: ""}).
WithTopology(
builder.ClusterTopology().
WithClass("class4").
Build()).
Build(),
},
expectErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
// Sets up the fakeClient for the test case.
fakeClient := fake.NewClientBuilder().
WithObjects(tt.clusters...).
WithScheme(fakeScheme).
Build()

// Create the webhook and add the fakeClient as its client.
webhook := &ClusterClass{Client: fakeClient}
err := webhook.ValidateDelete(ctx, class)
if tt.expectErr {
g.Expect(err).To(HaveOccurred())
return
}
g.Expect(err).ToNot(HaveOccurred())
})
}
}
Loading