Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit 515a7f3

Browse files
committed
watch instead of loop check nested cluster
1 parent d14245e commit 515a7f3

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

controllers/nestedcluster_controller.go

+55-3
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,26 @@ package controllers
2020

2121
import (
2222
"context"
23+
"reflect"
2324
"time"
2425

2526
"github.com/go-logr/logr"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
"k8s.io/apimachinery/pkg/runtime"
2829
"k8s.io/apimachinery/pkg/types"
30+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
2931
"sigs.k8s.io/cluster-api/util"
32+
"sigs.k8s.io/cluster-api/util/annotations"
33+
"sigs.k8s.io/cluster-api/util/predicates"
3034
ctrl "sigs.k8s.io/controller-runtime"
35+
"sigs.k8s.io/controller-runtime/pkg/builder"
3136
"sigs.k8s.io/controller-runtime/pkg/client"
37+
"sigs.k8s.io/controller-runtime/pkg/controller"
38+
"sigs.k8s.io/controller-runtime/pkg/event"
39+
"sigs.k8s.io/controller-runtime/pkg/handler"
40+
"sigs.k8s.io/controller-runtime/pkg/predicate"
41+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
42+
"sigs.k8s.io/controller-runtime/pkg/source"
3243

3344
infrav1 "sigs.k8s.io/cluster-api-provider-nested/api/v1alpha4"
3445
controlplanev1 "sigs.k8s.io/cluster-api-provider-nested/controlplane/nested/api/v1alpha4"
@@ -48,10 +59,51 @@ type NestedClusterReconciler struct {
4859
}
4960

5061
// SetupWithManager sets up the controller with the Manager.
51-
func (r *NestedClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
62+
func (r *NestedClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
63+
clusterToInfraFn := util.ClusterToInfrastructureMapFunc(infrav1.GroupVersion.WithKind("NestedCluster"))
64+
log := ctrl.LoggerFrom(ctx)
65+
5266
return ctrl.NewControllerManagedBy(mgr).
53-
For(&infrav1.NestedCluster{}).
54-
Owns(&controlplanev1.NestedControlPlane{}).
67+
WithOptions(options).
68+
For(&infrav1.NestedCluster{},
69+
builder.WithPredicates(
70+
predicate.Funcs{
71+
// Avoid reconciling if the event triggering the reconciliation is related to incremental status updates
72+
UpdateFunc: func(e event.UpdateEvent) bool {
73+
oldCluster := e.ObjectOld.(*infrav1.NestedCluster).DeepCopy()
74+
newCluster := e.ObjectNew.(*infrav1.NestedCluster).DeepCopy()
75+
oldCluster.Status = infrav1.NestedClusterStatus{}
76+
newCluster.Status = infrav1.NestedClusterStatus{}
77+
oldCluster.ObjectMeta.ResourceVersion = ""
78+
newCluster.ObjectMeta.ResourceVersion = ""
79+
return !reflect.DeepEqual(oldCluster, newCluster)
80+
},
81+
},
82+
),
83+
).
84+
Watches(
85+
&source.Kind{Type: &clusterv1.Cluster{}},
86+
handler.EnqueueRequestsFromMapFunc(func(o client.Object) []reconcile.Request {
87+
requests := clusterToInfraFn(o)
88+
if len(requests) < 1 {
89+
return nil
90+
}
91+
92+
c := &infrav1.NestedCluster{}
93+
if err := r.Client.Get(ctx, requests[0].NamespacedName, c); err != nil {
94+
log.V(4).Error(err, "Failed to get Nested cluster")
95+
return nil
96+
}
97+
98+
if annotations.IsExternallyManaged(c) {
99+
log.V(4).Info("Nested cluster is externally managed, skipping mapping.")
100+
return nil
101+
}
102+
return requests
103+
}),
104+
builder.WithPredicates(predicates.ClusterUnpaused(ctrl.LoggerFrom(ctx))),
105+
).
106+
WithEventFilter(predicates.ResourceIsNotExternallyManaged(ctrl.LoggerFrom(ctx))).
55107
Complete(r)
56108
}
57109

main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"sigs.k8s.io/cluster-api/feature"
3434
"sigs.k8s.io/cluster-api/version"
3535
ctrl "sigs.k8s.io/controller-runtime"
36+
"sigs.k8s.io/controller-runtime/pkg/controller"
3637
"sigs.k8s.io/controller-runtime/pkg/healthz"
3738

3839
infrastructurev1 "sigs.k8s.io/cluster-api-provider-nested/api/v1alpha4"
@@ -149,7 +150,7 @@ func main() {
149150
Client: mgr.GetClient(),
150151
Log: ctrl.Log.WithName("controllers").WithName("infrastructure").WithName("NestedCluster"),
151152
Scheme: mgr.GetScheme(),
152-
}).SetupWithManager(mgr); err != nil {
153+
}).SetupWithManager(ctx, mgr, concurrency(10)); err != nil {
153154
setupLog.Error(err, "unable to create controller", "controller", "NestedCluster")
154155
os.Exit(1)
155156
}
@@ -167,3 +168,8 @@ func main() {
167168
os.Exit(1)
168169
}
169170
}
171+
172+
// TODO: update all hard code usage
173+
func concurrency(c int) controller.Options {
174+
return controller.Options{MaxConcurrentReconciles: c}
175+
}

0 commit comments

Comments
 (0)