@@ -21,17 +21,18 @@ import (
21
21
"os"
22
22
"time"
23
23
24
- "github.com/tinkerbell/cluster-api-provider-tinkerbell/controllers"
25
24
"k8s.io/apimachinery/pkg/runtime"
26
25
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
27
26
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
28
27
"k8s.io/client-go/tools/record"
28
+ "k8s.io/klog"
29
+ "k8s.io/klog/klogr"
30
+ clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
29
31
ctrl "sigs.k8s.io/controller-runtime"
30
32
"sigs.k8s.io/controller-runtime/pkg/healthz"
31
- "sigs.k8s.io/controller-runtime/pkg/log/zap"
32
33
33
34
infrastructurev1alpha3 "github.com/tinkerbell/cluster-api-provider-tinkerbell/api/v1alpha3"
34
- clusterv1 "sigs.k8s.io/ cluster-api/api/v1alpha3 "
35
+ "github.com/tinkerbell/ cluster-api-provider-tinkerbell/controllers "
35
36
// +kubebuilder:scaffold:imports
36
37
)
37
38
@@ -49,115 +50,105 @@ func init() {
49
50
// +kubebuilder:scaffold:scheme
50
51
}
51
52
52
- //nolint:funlen,gomnd
53
- func main () {
54
- var (
55
- enableLeaderElection bool
56
- leaderElectionNamespace string
57
- healthAddr string
58
- metricsAddr string
59
- webhookPort int
60
- syncPeriod time.Duration
61
- watchNamespace string
62
- )
53
+ // optionsFromFlags parse CLI flags and converts them to controller runtime options.
54
+ func optionsFromFlags () ctrl.Options {
55
+ klog .InitFlags (nil )
56
+
57
+ // Machine and cluster operations can create enough events to trigger the event recorder spam filter
58
+ // Setting the burst size higher ensures all events will be recorded and submitted to the API
59
+ broadcaster := record .NewBroadcasterWithCorrelatorOptions (record.CorrelatorOptions {
60
+ BurstSize : 100 , //nolint:gomnd
61
+ })
62
+
63
+ var syncPeriod time.Duration
64
+
65
+ options := ctrl.Options {
66
+ Scheme : scheme ,
67
+ LeaderElectionID : "controller-leader-election-capt" ,
68
+ EventBroadcaster : broadcaster ,
69
+ SyncPeriod : & syncPeriod ,
70
+ }
63
71
64
- flag .BoolVar (& enableLeaderElection , "enable-leader-election" , false ,
72
+ flag .BoolVar (& options . LeaderElection , "enable-leader-election" , false ,
65
73
"Enable leader election for controller manager. " +
66
74
"Enabling this will ensure there is only one active controller manager." )
67
75
68
- flag .StringVar (
69
- & leaderElectionNamespace ,
70
- "leader-election-namespace" ,
71
- "" ,
76
+ flag .StringVar (& options .LeaderElectionNamespace , "leader-election-namespace" , "" ,
72
77
"Namespace that the controller performs leader election in. " +
73
78
"If unspecified, the controller will discover which namespace it is running in." ,
74
79
)
75
80
76
- flag .StringVar (& healthAddr ,
77
- "health-addr" ,
78
- ":9440" ,
79
- "The address the health endpoint binds to." ,
80
- )
81
+ flag .StringVar (& options .HealthProbeBindAddress , "health-addr" , ":9440" , "The address the health endpoint binds to." )
81
82
82
- flag .StringVar (& metricsAddr , "metrics-addr" , ":8080" , "The address the metric endpoint binds to." )
83
+ flag .StringVar (& options . MetricsBindAddress , "metrics-addr" , ":8080" , "The address the metric endpoint binds to." )
83
84
84
- flag .DurationVar (& syncPeriod ,
85
- "sync-period" ,
86
- 10 * time .Minute ,
85
+ flag .DurationVar (& syncPeriod , "sync-period" , 10 * time .Minute , //nolint:gomnd
87
86
"The minimum interval at which watched resources are reconciled (e.g. 15m)" ,
88
87
)
89
88
90
- flag .StringVar (
91
- & watchNamespace ,
92
- "namespace" ,
93
- "" ,
89
+ flag .StringVar (& options .Namespace , "namespace" , "" ,
94
90
"Namespace that the controller watches to reconcile cluster-api objects. " +
95
91
"If unspecified, the controller watches for cluster-api objects across all namespaces." ,
96
92
)
97
93
98
- flag .IntVar (& webhookPort ,
99
- "webhook-port" ,
100
- 0 ,
94
+ flag .IntVar (& options .Port , "webhook-port" , 0 ,
101
95
"Webhook Server port, disabled by default. When enabled, the manager will only " +
102
96
"work as webhook server, no reconcilers are installed." ,
103
97
)
104
98
105
99
flag .Parse ()
106
100
107
- ctrl .SetLogger (zap .New (zap .UseDevMode (true )))
101
+ return options
102
+ }
108
103
109
- if watchNamespace != "" {
110
- setupLog .Info ("Watching cluster-api objects only in namespace for reconciliation" , "namespace" , watchNamespace )
104
+ func validateOptions (options ctrl.Options ) error {
105
+ if options .Namespace != "" {
106
+ setupLog .Info ("Watching cluster-api objects only in namespace for reconciliation" , "namespace" , options .Namespace )
111
107
}
112
108
113
- // Machine and cluster operations can create enough events to trigger the event recorder spam filter
114
- // Setting the burst size higher ensures all events will be recorded and submitted to the API
115
- broadcaster := record .NewBroadcasterWithCorrelatorOptions (record.CorrelatorOptions {
116
- BurstSize : 100 ,
117
- })
109
+ if options .Port != 0 {
110
+ // TODO: add the webhook configuration
111
+ return errors .New ("webhook not implemented" )
112
+ }
118
113
119
- mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
120
- Scheme : scheme ,
121
- MetricsBindAddress : metricsAddr ,
122
- Port : webhookPort ,
123
- EventBroadcaster : broadcaster ,
124
- LeaderElection : enableLeaderElection ,
125
- LeaderElectionID : "controller-leader-election-capt" ,
126
- LeaderElectionNamespace : leaderElectionNamespace ,
127
- Namespace : watchNamespace ,
128
- SyncPeriod : & syncPeriod ,
129
- HealthProbeBindAddress : healthAddr ,
130
- })
114
+ return nil
115
+ }
116
+
117
+ func main () {
118
+ ctrl .SetLogger (klogr .New ())
119
+
120
+ options := optionsFromFlags ()
121
+
122
+ if err := validateOptions (options ); err != nil {
123
+ setupLog .Error (err , "validating controllers configuration" )
124
+ os .Exit (1 )
125
+ }
126
+
127
+ mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), options )
131
128
if err != nil {
132
129
setupLog .Error (err , "unable to start manager" )
133
130
os .Exit (1 )
134
131
}
135
132
136
133
// TODO: Get a Tinkerbell client.
137
134
138
- if webhookPort == 0 {
139
- if err = (& controllers.TinkerbellClusterReconciler {
140
- Client : mgr .GetClient (),
141
- Log : ctrl .Log .WithName ("controllers" ).WithName ("TinerellCluster" ),
142
- Recorder : mgr .GetEventRecorderFor ("tinerellcluster-controller" ),
143
- Scheme : mgr .GetScheme (),
144
- }).SetupWithManager (mgr ); err != nil {
145
- setupLog .Error (err , "unable to create controller" , "controller" , "TinkerbellCluster" )
146
- os .Exit (1 )
147
- }
148
-
149
- if err = (& controllers.TinkerbellMachineReconciler {
150
- Client : mgr .GetClient (),
151
- Log : ctrl .Log .WithName ("controllers" ).WithName ("TinkerbellMachine" ),
152
- Scheme : mgr .GetScheme (),
153
- Recorder : mgr .GetEventRecorderFor ("tinkerbellmachine-controller" ),
154
- }).SetupWithManager (mgr ); err != nil {
155
- setupLog .Error (err , "unable to create controller" , "controller" , "TinkerbellMachine" )
156
- os .Exit (1 )
157
- }
158
- } else {
159
- // TODO: add the webhook configuration
160
- setupLog .Error (errors .New ("webhook not implemented" ), "webhook" , "not available" )
135
+ if err = (& controllers.TinkerbellClusterReconciler {
136
+ Client : mgr .GetClient (),
137
+ Log : ctrl .Log .WithName ("controllers" ).WithName ("TinerellCluster" ),
138
+ Recorder : mgr .GetEventRecorderFor ("tinerellcluster-controller" ),
139
+ Scheme : mgr .GetScheme (),
140
+ }).SetupWithManager (mgr ); err != nil {
141
+ setupLog .Error (err , "unable to create controller" , "controller" , "TinkerbellCluster" )
142
+ os .Exit (1 )
143
+ }
144
+
145
+ if err = (& controllers.TinkerbellMachineReconciler {
146
+ Client : mgr .GetClient (),
147
+ Log : ctrl .Log .WithName ("controllers" ).WithName ("TinkerbellMachine" ),
148
+ Scheme : mgr .GetScheme (),
149
+ Recorder : mgr .GetEventRecorderFor ("tinkerbellmachine-controller" ),
150
+ }).SetupWithManager (mgr ); err != nil {
151
+ setupLog .Error (err , "unable to create controller" , "controller" , "TinkerbellMachine" )
161
152
os .Exit (1 )
162
153
}
163
154
// +kubebuilder:scaffold:builder
0 commit comments