@@ -12,9 +12,8 @@ import (
1212 "path/filepath"
1313 "time"
1414
15- "github.com/spf13/cobra"
16- "github.com/spf13/pflag"
17-
15+ "github.com/ironcore-dev/controller-utils/cmdutils/switches"
16+ "github.com/ironcore-dev/controller-utils/configutils"
1817 commonv1alpha1 "github.com/ironcore-dev/ironcore/api/common/v1alpha1"
1918 ipamv1alpha1 "github.com/ironcore-dev/ironcore/api/ipam/v1alpha1"
2019 networkingv1alpha1 "github.com/ironcore-dev/ironcore/api/networking/v1alpha1"
@@ -28,8 +27,8 @@ import (
2827 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/vcm"
2928 "github.com/ironcore-dev/ironcore/poollet/volumepoollet/vem"
3029 "github.com/ironcore-dev/ironcore/utils/client/config"
31-
32- "github.com/ironcore-dev/controller-utils/configutils "
30+ "github.com/spf13/cobra"
31+ "github.com/spf13/pflag "
3332 "k8s.io/apimachinery/pkg/runtime"
3433 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3534 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -43,6 +42,11 @@ import (
4342
4443var scheme = runtime .NewScheme ()
4544
45+ const (
46+ volumeController = "volume"
47+ volumeSnapshotController = "volumesnapshot"
48+ )
49+
4650func init () {
4751 utilruntime .Must (clientgoscheme .AddToScheme (scheme ))
4852 utilruntime .Must (storagev1alpha1 .AddToScheme (scheme ))
@@ -85,6 +89,8 @@ type Options struct {
8589 RelistPeriod time.Duration
8690 RelistThreshold time.Duration
8791
92+ Switches * switches.Switches
93+
8894 WatchFilterValue string
8995
9096 MaxConcurrentReconciles int
@@ -132,6 +138,18 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
132138 fs .StringVar (& o .WatchFilterValue , "watch-filter" , "" , "Value to filter for while watching." )
133139
134140 fs .IntVar (& o .MaxConcurrentReconciles , "max-concurrent-reconciles" , 1 , "Maximum number of concurrent reconciles." )
141+
142+ o .Switches = switches .New (
143+ volumeController ,
144+ volumeSnapshotController ,
145+ )
146+ fs .Var (o .Switches , "controllers" ,
147+ fmt .Sprintf ("Controllers to enable. All controllers: %v. Disabled-by-default controllers: %v" ,
148+ o .Switches .All (),
149+ o .Switches .DisabledByDefault (),
150+ ),
151+ )
152+
135153}
136154
137155func (o * Options ) MarkFlagsRequired (cmd * cobra.Command ) {
@@ -302,88 +320,100 @@ func Run(ctx context.Context, opts Options) error {
302320 if err != nil {
303321 return fmt .Errorf ("error getting volume runtime version: %w" , err )
304322 }
305- volumeClassMapper := vcm .NewGeneric (volumeRuntime , vcm.GenericOptions {})
306- if err := mgr .Add (volumeClassMapper ); err != nil {
307- return fmt .Errorf ("error adding volume class mapper: %w" , err )
308- }
309- volumeEventMapper := vem .NewVolumeEventMapper (mgr .GetClient (), volumeRuntime , mgr .GetEventRecorderFor ("volume-cluster-events" ), vem.VolumeEventMapperOptions {})
310- if err := mgr .Add (volumeEventMapper ); err != nil {
311- return fmt .Errorf ("error adding volume event mapper: %w" , err )
312- }
313323
314- volumeEvents := irievent .NewGenerator (func (ctx context.Context ) ([]* iri.Volume , error ) {
315- res , err := volumeRuntime .ListVolumes (ctx , & iri.ListVolumesRequest {})
316- if err != nil {
317- return nil , err
324+ indexer := mgr .GetFieldIndexer ()
325+
326+ var volumeClassMapper vcm.VolumeClassMapper
327+ var volumeEventMapper * vem.VolumeEventMapper
328+ var volumeEvents irievent.Generator [* iri.Volume ]
329+ if opts .Switches .Enabled (volumeController ) {
330+ volumeClassMapper := vcm .NewGeneric (volumeRuntime , vcm.GenericOptions {})
331+ if err := mgr .Add (volumeClassMapper ); err != nil {
332+ return fmt .Errorf ("error adding volume class mapper: %w" , err )
333+ }
334+ volumeEventMapper = vem .NewVolumeEventMapper (mgr .GetClient (), volumeRuntime , mgr .GetEventRecorderFor ("volume-cluster-events" ), vem.VolumeEventMapperOptions {})
335+ if err := mgr .Add (volumeEventMapper ); err != nil {
336+ return fmt .Errorf ("error adding volume event mapper: %w" , err )
318337 }
319- return res .Volumes , nil
320- }, irievent.GeneratorOptions {
321- ChannelCapacity : opts .ChannelCapacity ,
322- RelistPeriod : opts .RelistPeriod ,
323- RelistThreshold : opts .RelistThreshold ,
324- })
325- if err := mgr .Add (volumeEvents ); err != nil {
326- return fmt .Errorf ("error adding volume event generator: %w" , err )
327- }
328- if err := mgr .AddHealthzCheck ("volume-events" , volumeEvents .Check ); err != nil {
329- return fmt .Errorf ("error adding volume event generator healthz check: %w" , err )
330- }
331338
332- volumeSnapshotEvents := irievent .NewGenerator (func (ctx context.Context ) ([]* iri.VolumeSnapshot , error ) {
333- res , err := volumeRuntime .ListVolumeSnapshots (ctx , & iri.ListVolumeSnapshotsRequest {})
334- if err != nil {
335- return nil , err
339+ volumeEvents = irievent .NewGenerator (func (ctx context.Context ) ([]* iri.Volume , error ) {
340+ res , err := volumeRuntime .ListVolumes (ctx , & iri.ListVolumesRequest {})
341+ if err != nil {
342+ return nil , err
343+ }
344+ return res .Volumes , nil
345+ }, irievent.GeneratorOptions {
346+ ChannelCapacity : opts .ChannelCapacity ,
347+ RelistPeriod : opts .RelistPeriod ,
348+ RelistThreshold : opts .RelistThreshold ,
349+ })
350+ if err := mgr .Add (volumeEvents ); err != nil {
351+ return fmt .Errorf ("error adding volume event generator: %w" , err )
352+ }
353+ if err := mgr .AddHealthzCheck ("volume-events" , volumeEvents .Check ); err != nil {
354+ return fmt .Errorf ("error adding volume event generator healthz check: %w" , err )
336355 }
337- return res .VolumeSnapshots , nil
338- }, irievent.GeneratorOptions {
339- ChannelCapacity : opts .ChannelCapacity ,
340- RelistPeriod : opts .RelistPeriod ,
341- RelistThreshold : opts .RelistThreshold ,
342- })
343- if err := mgr .Add (volumeSnapshotEvents ); err != nil {
344- return fmt .Errorf ("error adding volume snapshot event generator: %w" , err )
345- }
346- if err := mgr .AddHealthzCheck ("volume-snapshot-events" , volumeSnapshotEvents .Check ); err != nil {
347- return fmt .Errorf ("error adding volume snapshot event generator healthz check: %w" , err )
348- }
349356
350- indexer := mgr .GetFieldIndexer ()
351- if err := storageclient .SetupVolumeSpecVolumePoolRefNameFieldIndexer (ctx , indexer ); err != nil {
352- return fmt .Errorf ("error setting up %s indexer with manager: %w" , storageclient .VolumeSpecVolumePoolRefNameField , err )
353- }
354- if err := storageclient .SetupVolumeSpecVolumeSnapshotRefNameFieldIndexer (ctx , indexer ); err != nil {
355- return fmt .Errorf ("error setting up %s indexer with manager: %w" , storageclient .VolumeSpecVolumeSnapshotRefNameField , err )
357+ if err := storageclient .SetupVolumeSpecVolumePoolRefNameFieldIndexer (ctx , indexer ); err != nil {
358+ return fmt .Errorf ("error setting up %s indexer with manager: %w" , storageclient .VolumeSpecVolumePoolRefNameField , err )
359+ }
356360 }
357361
358- onInitialized := func (ctx context.Context ) error {
359- volumeClassMapperSyncCtx , cancel := context .WithTimeout (ctx , opts .VolumeClassMapperSyncTimeout )
360- defer cancel ()
361-
362- if err := volumeClassMapper .WaitForSync (volumeClassMapperSyncCtx ); err != nil {
363- return fmt .Errorf ("error waiting for volume class mapper to sync: %w" , err )
362+ var volumeSnapshotEvents irievent.Generator [* iri.VolumeSnapshot ]
363+ if opts .Switches .Enabled (volumeController ) {
364+ volumeSnapshotEvents = irievent .NewGenerator (func (ctx context.Context ) ([]* iri.VolumeSnapshot , error ) {
365+ res , err := volumeRuntime .ListVolumeSnapshots (ctx , & iri.ListVolumeSnapshotsRequest {})
366+ if err != nil {
367+ return nil , err
368+ }
369+ return res .VolumeSnapshots , nil
370+ }, irievent.GeneratorOptions {
371+ ChannelCapacity : opts .ChannelCapacity ,
372+ RelistPeriod : opts .RelistPeriod ,
373+ RelistThreshold : opts .RelistThreshold ,
374+ })
375+ if err := mgr .Add (volumeSnapshotEvents ); err != nil {
376+ return fmt .Errorf ("error adding volume snapshot event generator: %w" , err )
364377 }
365-
366- if err := (& controllers.VolumeReconciler {
367- EventRecorder : mgr .GetEventRecorderFor ("volumes" ),
368- Client : mgr .GetClient (),
369- Scheme : scheme ,
370- VolumeRuntime : volumeRuntime ,
371- VolumeRuntimeName : version .RuntimeName ,
372- VolumeClassMapper : volumeClassMapper ,
373- VolumePoolName : opts .VolumePoolName ,
374- DownwardAPILabels : opts .VolumeDownwardAPILabels ,
375- DownwardAPIAnnotations : opts .VolumeDownwardAPIAnnotations ,
376- WatchFilterValue : opts .WatchFilterValue ,
377- MaxConcurrentReconciles : opts .MaxConcurrentReconciles ,
378- }).SetupWithManager (mgr ); err != nil {
379- return fmt .Errorf ("error setting up volume reconciler with manager: %w" , err )
378+ if err := mgr .AddHealthzCheck ("volume-snapshot-events" , volumeSnapshotEvents .Check ); err != nil {
379+ return fmt .Errorf ("error adding volume snapshot event generator healthz check: %w" , err )
380+ }
381+ if err := storageclient .SetupVolumeSpecVolumeSnapshotRefNameFieldIndexer (ctx , indexer ); err != nil {
382+ return fmt .Errorf ("error setting up %s indexer with manager: %w" , storageclient .VolumeSpecVolumeSnapshotRefNameField , err )
380383 }
384+ }
381385
382- if err := (& controllers.VolumeAnnotatorReconciler {
383- Client : mgr .GetClient (),
384- VolumeEvents : volumeEvents ,
385- }).SetupWithManager (mgr ); err != nil {
386- return fmt .Errorf ("error setting up volume annotator reconciler with manager: %w" , err )
386+ onInitialized := func (ctx context.Context ) error {
387+ if opts .Switches .Enabled (volumeController ) {
388+ volumeClassMapperSyncCtx , cancel := context .WithTimeout (ctx , opts .VolumeClassMapperSyncTimeout )
389+ defer cancel ()
390+
391+ if err := volumeClassMapper .WaitForSync (volumeClassMapperSyncCtx ); err != nil {
392+ return fmt .Errorf ("error waiting for volume class mapper to sync: %w" , err )
393+ }
394+
395+ if err := (& controllers.VolumeReconciler {
396+ EventRecorder : mgr .GetEventRecorderFor ("volumes" ),
397+ Client : mgr .GetClient (),
398+ Scheme : scheme ,
399+ VolumeRuntime : volumeRuntime ,
400+ VolumeRuntimeName : version .RuntimeName ,
401+ VolumeClassMapper : volumeClassMapper ,
402+ VolumePoolName : opts .VolumePoolName ,
403+ DownwardAPILabels : opts .VolumeDownwardAPILabels ,
404+ DownwardAPIAnnotations : opts .VolumeDownwardAPIAnnotations ,
405+ WatchFilterValue : opts .WatchFilterValue ,
406+ MaxConcurrentReconciles : opts .MaxConcurrentReconciles ,
407+ }).SetupWithManager (mgr ); err != nil {
408+ return fmt .Errorf ("error setting up volume reconciler with manager: %w" , err )
409+ }
410+
411+ if err := (& controllers.VolumeAnnotatorReconciler {
412+ Client : mgr .GetClient (),
413+ VolumeEvents : volumeEvents ,
414+ }).SetupWithManager (mgr ); err != nil {
415+ return fmt .Errorf ("error setting up volume annotator reconciler with manager: %w" , err )
416+ }
387417 }
388418
389419 if err := (& controllers.VolumeSnapshotReconciler {
0 commit comments