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

Commit 2d53c06

Browse files
authored
Merge pull request #200 from jichenjc/bug/137
✨ Watch instead of loop check nested cluster
2 parents d14245e + 35a946e commit 2d53c06

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

controllers/nestedcluster_controller.go

+56-4
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@ package controllers
2020

2121
import (
2222
"context"
23-
"time"
23+
"reflect"
2424

2525
"github.com/go-logr/logr"
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/apimachinery/pkg/types"
29+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
2930
"sigs.k8s.io/cluster-api/util"
31+
"sigs.k8s.io/cluster-api/util/annotations"
32+
"sigs.k8s.io/cluster-api/util/predicates"
3033
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/builder"
3135
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/controller"
37+
"sigs.k8s.io/controller-runtime/pkg/event"
38+
"sigs.k8s.io/controller-runtime/pkg/handler"
39+
"sigs.k8s.io/controller-runtime/pkg/predicate"
40+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
41+
"sigs.k8s.io/controller-runtime/pkg/source"
3242

3343
infrav1 "sigs.k8s.io/cluster-api-provider-nested/api/v1alpha4"
3444
controlplanev1 "sigs.k8s.io/cluster-api-provider-nested/controlplane/nested/api/v1alpha4"
@@ -48,10 +58,52 @@ type NestedClusterReconciler struct {
4858
}
4959

5060
// SetupWithManager sets up the controller with the Manager.
51-
func (r *NestedClusterReconciler) SetupWithManager(mgr ctrl.Manager) error {
61+
func (r *NestedClusterReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
62+
clusterToInfraFn := util.ClusterToInfrastructureMapFunc(infrav1.GroupVersion.WithKind("NestedCluster"))
63+
log := ctrl.LoggerFrom(ctx)
64+
5265
return ctrl.NewControllerManagedBy(mgr).
53-
For(&infrav1.NestedCluster{}).
66+
WithOptions(options).
67+
For(&infrav1.NestedCluster{},
68+
builder.WithPredicates(
69+
predicate.Funcs{
70+
// Avoid reconciling if the event triggering the reconciliation is related to incremental status updates
71+
UpdateFunc: func(e event.UpdateEvent) bool {
72+
oldCluster := e.ObjectOld.(*infrav1.NestedCluster).DeepCopy()
73+
newCluster := e.ObjectNew.(*infrav1.NestedCluster).DeepCopy()
74+
oldCluster.Status = infrav1.NestedClusterStatus{}
75+
newCluster.Status = infrav1.NestedClusterStatus{}
76+
oldCluster.ObjectMeta.ResourceVersion = ""
77+
newCluster.ObjectMeta.ResourceVersion = ""
78+
return !reflect.DeepEqual(oldCluster, newCluster)
79+
},
80+
},
81+
),
82+
).
5483
Owns(&controlplanev1.NestedControlPlane{}).
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

@@ -101,5 +153,5 @@ func (r *NestedClusterReconciler) Reconcile(ctx context.Context, req ctrl.Reques
101153
return ctrl.Result{}, nil
102154
}
103155

104-
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
156+
return ctrl.Result{}, nil
105157
}

main.go

+10-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"
@@ -55,6 +56,7 @@ var (
5556
syncPeriod time.Duration
5657
webhookPort int
5758
healthAddr string
59+
nestedclusterConcurrency int
5860
)
5961

6062
func init() {
@@ -93,6 +95,9 @@ func InitFlags(fs *pflag.FlagSet) {
9395
fs.IntVar(&webhookPort, "webhook-port", 0,
9496
"Webhook Server port, disabled by default. When enabled, the manager will only work as webhook server, no reconcilers are installed.")
9597

98+
fs.IntVar(&nestedclusterConcurrency, "nestedcluster-concurrency", 10,
99+
"Number of NestedClusters to process simultaneously")
100+
96101
fs.StringVar(&healthAddr, "health-addr", ":9440",
97102
"The address the health endpoint binds to.")
98103

@@ -149,7 +154,7 @@ func main() {
149154
Client: mgr.GetClient(),
150155
Log: ctrl.Log.WithName("controllers").WithName("infrastructure").WithName("NestedCluster"),
151156
Scheme: mgr.GetScheme(),
152-
}).SetupWithManager(mgr); err != nil {
157+
}).SetupWithManager(ctx, mgr, concurrency(nestedclusterConcurrency)); err != nil {
153158
setupLog.Error(err, "unable to create controller", "controller", "NestedCluster")
154159
os.Exit(1)
155160
}
@@ -167,3 +172,7 @@ func main() {
167172
os.Exit(1)
168173
}
169174
}
175+
176+
func concurrency(c int) controller.Options {
177+
return controller.Options{MaxConcurrentReconciles: c}
178+
}

0 commit comments

Comments
 (0)