Skip to content

Commit 75426a2

Browse files
Enable selectable watching core provider changes in generic reconcilers
enable it for all non core provider types (Infrastructure, Bootstrap, ControlPlane, Addon, IPAM, RuntimeExtension). Signed-off-by: Danil-Grigorev <[email protected]>
1 parent 4f5f616 commit 75426a2

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

cmd/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager, watchConfigSecretCh
244244
Client: mgr.GetClient(),
245245
Config: mgr.GetConfig(),
246246
WatchConfigSecretChanges: watchConfigSecretChanges,
247+
WatchCoreProviderChanges: true,
247248
}).SetupWithManager(ctx, mgr, concurrency(concurrencyNumber)); err != nil {
248249
setupLog.Error(err, "unable to create controller", "controller", "InfrastructureProvider")
249250
os.Exit(1)
@@ -255,6 +256,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager, watchConfigSecretCh
255256
Client: mgr.GetClient(),
256257
Config: mgr.GetConfig(),
257258
WatchConfigSecretChanges: watchConfigSecretChanges,
259+
WatchCoreProviderChanges: true,
258260
}).SetupWithManager(ctx, mgr, concurrency(concurrencyNumber)); err != nil {
259261
setupLog.Error(err, "unable to create controller", "controller", "BootstrapProvider")
260262
os.Exit(1)
@@ -266,6 +268,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager, watchConfigSecretCh
266268
Client: mgr.GetClient(),
267269
Config: mgr.GetConfig(),
268270
WatchConfigSecretChanges: watchConfigSecretChanges,
271+
WatchCoreProviderChanges: true,
269272
}).SetupWithManager(ctx, mgr, concurrency(concurrencyNumber)); err != nil {
270273
setupLog.Error(err, "unable to create controller", "controller", "ControlPlaneProvider")
271274
os.Exit(1)
@@ -277,6 +280,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager, watchConfigSecretCh
277280
Client: mgr.GetClient(),
278281
Config: mgr.GetConfig(),
279282
WatchConfigSecretChanges: watchConfigSecretChanges,
283+
WatchCoreProviderChanges: true,
280284
}).SetupWithManager(ctx, mgr, concurrency(concurrencyNumber)); err != nil {
281285
setupLog.Error(err, "unable to create controller", "controller", "AddonProvider")
282286
os.Exit(1)
@@ -288,6 +292,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager, watchConfigSecretCh
288292
Client: mgr.GetClient(),
289293
Config: mgr.GetConfig(),
290294
WatchConfigSecretChanges: watchConfigSecretChanges,
295+
WatchCoreProviderChanges: true,
291296
}).SetupWithManager(ctx, mgr, concurrency(concurrencyNumber)); err != nil {
292297
setupLog.Error(err, "unable to create controller", "controller", "IPAMProvider")
293298
os.Exit(1)
@@ -299,6 +304,7 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager, watchConfigSecretCh
299304
Client: mgr.GetClient(),
300305
Config: mgr.GetConfig(),
301306
WatchConfigSecretChanges: watchConfigSecretChanges,
307+
WatchCoreProviderChanges: true,
302308
}).SetupWithManager(ctx, mgr, concurrency(concurrencyNumber)); err != nil {
303309
setupLog.Error(err, "unable to create controller", "controller", "RuntimeExtensionProvider")
304310
os.Exit(1)

internal/controller/genericprovider_controller.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"errors"
2424
"fmt"
2525
"hash"
26-
"reflect"
2726

2827
corev1 "k8s.io/api/core/v1"
2928
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -49,6 +48,7 @@ type GenericProviderReconciler struct {
4948
Client client.Client
5049
Config *rest.Config
5150
WatchConfigSecretChanges bool
51+
WatchCoreProviderChanges bool
5252

5353
DeletePhases []PhaseFn
5454
ReconcilePhases []PhaseFn
@@ -58,17 +58,17 @@ const (
5858
appliedSpecHashAnnotation = "operator.cluster.x-k8s.io/applied-spec-hash"
5959
)
6060

61-
func (r *GenericProviderReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
61+
func (r *GenericProviderReconciler) BuildWithManager(ctx context.Context, mgr ctrl.Manager) (*ctrl.Builder, error) {
6262
builder := ctrl.NewControllerManagedBy(mgr).
6363
For(r.Provider)
6464

6565
if r.WatchConfigSecretChanges {
6666
if err := mgr.GetFieldIndexer().IndexField(ctx, r.Provider, configSecretNameField, configSecretNameIndexFunc); err != nil {
67-
return err
67+
return nil, err
6868
}
6969

7070
if err := mgr.GetFieldIndexer().IndexField(ctx, r.Provider, configSecretNamespaceField, configSecretNamespaceIndexFunc); err != nil {
71-
return err
71+
return nil, err
7272
}
7373

7474
builder.Watches(
@@ -78,7 +78,7 @@ func (r *GenericProviderReconciler) SetupWithManager(ctx context.Context, mgr ct
7878
}
7979

8080
// We don't want to receive secondary events from the CoreProvider for itself.
81-
if reflect.TypeOf(r.Provider) != reflect.TypeOf(genericprovider.GenericProvider(&operatorv1.CoreProvider{})) {
81+
if r.WatchCoreProviderChanges {
8282
builder.Watches(
8383
&operatorv1.CoreProvider{},
8484
handler.EnqueueRequestsFromMapFunc(newCoreProviderToProviderFuncMapForProviderList(r.Client, r.ProviderList)),
@@ -102,8 +102,16 @@ func (r *GenericProviderReconciler) SetupWithManager(ctx context.Context, mgr ct
102102
reconciler.Delete,
103103
}
104104

105-
return builder.WithOptions(options).
106-
Complete(r)
105+
return builder, nil
106+
}
107+
108+
func (r *GenericProviderReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
109+
builder, err := r.BuildWithManager(ctx, mgr)
110+
if err != nil {
111+
return err
112+
}
113+
114+
return builder.WithOptions(options).Complete(r)
107115
}
108116

109117
func (r *GenericProviderReconciler) Reconcile(ctx context.Context, req reconcile.Request) (_ reconcile.Result, reterr error) {

internal/controller/suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func TestMain(m *testing.M) {
5858
ProviderList: &operatorv1.InfrastructureProviderList{},
5959
Client: env,
6060
WatchConfigSecretChanges: true,
61+
WatchCoreProviderChanges: true,
6162
}).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
6263
panic(fmt.Sprintf("Failed to start InfrastructureProviderReconciler: %v", err))
6364
}
@@ -67,6 +68,7 @@ func TestMain(m *testing.M) {
6768
ProviderList: &operatorv1.BootstrapProviderList{},
6869
Client: env,
6970
WatchConfigSecretChanges: true,
71+
WatchCoreProviderChanges: true,
7072
}).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
7173
panic(fmt.Sprintf("Failed to start BootstrapProviderReconciler: %v", err))
7274
}
@@ -76,6 +78,7 @@ func TestMain(m *testing.M) {
7678
ProviderList: &operatorv1.ControlPlaneProviderList{},
7779
Client: env,
7880
WatchConfigSecretChanges: true,
81+
WatchCoreProviderChanges: true,
7982
}).SetupWithManager(ctx, env.Manager, controller.Options{MaxConcurrentReconciles: 1}); err != nil {
8083
panic(fmt.Sprintf("Failed to start ControlPlaneProviderReconciler: %v", err))
8184
}

0 commit comments

Comments
 (0)