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

🌱 Watch for Cluster resources in topology MachineSet & MachineDeployment controllers #10029

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 @@ -96,7 +96,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
// TODO: should this wait for Cluster.Status.InfrastructureReady similar to Infra Machine resources?
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
Copy link
Member

Choose a reason for hiding this comment

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

This is safe to remove because we have

		WithEventFilter(predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).

above right?

Copy link
Contributor Author

@adityabhatia adityabhatia Jan 22, 2024

Choose a reason for hiding this comment

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

right since WithEventFilter(...) adds global predicates

Copy link
Member

@sbueringer sbueringer Jan 22, 2024

Choose a reason for hiding this comment

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

Yeah the ones from WithEventFilter are added to the ones passed into For & Watches

),
),
).Complete(r)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/internal/controllers/topology/machineset"
Expand Down Expand Up @@ -55,14 +57,32 @@ type Reconciler struct {
}

func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.MachineDeployment{}).
clusterToMachineDeployments, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineDeploymentList{}, mgr.GetScheme())
if err != nil {
return err
}

err = ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.MachineDeployment{},
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ResourceIsTopologyOwned(ctrl.LoggerFrom(ctx)),
predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))),
),
).
Named("topology/machinedeployment").
WithOptions(options).
WithEventFilter(predicates.All(ctrl.LoggerFrom(ctx),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
predicates.ResourceIsTopologyOwned(ctrl.LoggerFrom(ctx)),
)).
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(clusterToMachineDeployments),
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
predicates.ClusterHasTopology(ctrl.LoggerFrom(ctx)),
),
),
).
Complete(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
32 changes: 26 additions & 6 deletions internal/controllers/topology/machineset/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import (
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
tlog "sigs.k8s.io/cluster-api/internal/log"
Expand Down Expand Up @@ -57,14 +59,32 @@ type Reconciler struct {
}

func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
err := ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.MachineSet{}).
clusterToMachineSets, err := util.ClusterToTypedObjectsMapper(mgr.GetClient(), &clusterv1.MachineSetList{}, mgr.GetScheme())
if err != nil {
return err
}

err = ctrl.NewControllerManagedBy(mgr).
For(&clusterv1.MachineSet{},
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ResourceIsTopologyOwned(ctrl.LoggerFrom(ctx)),
predicates.ResourceNotPaused(ctrl.LoggerFrom(ctx))),
),
).
Named("topology/machineset").
WithOptions(options).
WithEventFilter(predicates.All(ctrl.LoggerFrom(ctx),
predicates.ResourceNotPausedAndHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue),
predicates.ResourceIsTopologyOwned(ctrl.LoggerFrom(ctx)),
)).
WithEventFilter(predicates.ResourceHasFilterLabel(ctrl.LoggerFrom(ctx), r.WatchFilterValue)).
Watches(
&clusterv1.Cluster{},
handler.EnqueueRequestsFromMapFunc(clusterToMachineSets),
builder.WithPredicates(
predicates.All(ctrl.LoggerFrom(ctx),
predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx)),
predicates.ClusterHasTopology(ctrl.LoggerFrom(ctx)),
),
),
).
Complete(r)
if err != nil {
return errors.Wrap(err, "failed setting up with a controller manager")
Expand Down
21 changes: 0 additions & 21 deletions test/framework/finalizers_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -85,10 +84,6 @@ func ValidateFinalizersResilience(ctx context.Context, proxy ClusterProxy, names
// Unpause the cluster.
setClusterPause(ctx, proxy.GetClient(), clusterKey, false)

// Annotate the MachineDeployment to speed up reconciliation. This ensures MachineDeployment topology Finalizers are re-reconciled.
// TODO: Remove this as part of https://github.com/kubernetes-sigs/cluster-api/issues/9532
forceMachineDeploymentTopologyReconcile(ctx, proxy.GetClient(), clusterKey)

// Check that the Finalizers are as expected after further reconciliations.
assertFinalizersExist(ctx, proxy, namespace, objectsWithFinalizers, allFinalizerAssertions)
}
Expand Down Expand Up @@ -189,19 +184,3 @@ func concatenateFinalizerAssertions(finalizerAssertions ...map[string][]string)

return allFinalizerAssertions, kerrors.NewAggregate(allErrs)
}

// forceMachineDeploymentTopologyReconcile forces reconciliation of the MachineDeployment.
func forceMachineDeploymentTopologyReconcile(ctx context.Context, cli client.Client, clusterKey types.NamespacedName) {
mdList := &clusterv1.MachineDeploymentList{}
clientOptions := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
client.MatchingLabels{clusterv1.ClusterNameLabel: clusterKey.Name},
})
Expect(cli.List(ctx, mdList, clientOptions)).To(Succeed())

for i := range mdList.Items {
if _, ok := mdList.Items[i].GetLabels()[clusterv1.ClusterTopologyOwnedLabel]; ok {
annotationPatch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"metadata\":{\"annotations\":{\"cluster.x-k8s.io/modifiedAt\":\"%v\"}}}", time.Now().Format(time.RFC3339))))
Expect(cli.Patch(ctx, &mdList.Items[i], annotationPatch)).To(Succeed())
}
}
}
Loading