Skip to content

Commit 0641664

Browse files
authored
Merge pull request #24 from kinvolk/invidian/main-cleanups
main.go fixes and improvements
2 parents 2e8a69f + b0efcf0 commit 0641664

File tree

2 files changed

+70
-78
lines changed

2 files changed

+70
-78
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
k8s.io/api v0.17.16
88
k8s.io/apimachinery v0.17.16
99
k8s.io/client-go v0.17.16
10+
k8s.io/klog v1.0.0
1011
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
1112
sigs.k8s.io/cluster-api v0.3.12
1213
sigs.k8s.io/controller-runtime v0.5.14

Diff for: main.go

+69-78
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,18 @@ import (
2121
"os"
2222
"time"
2323

24-
"github.com/tinkerbell/cluster-api-provider-tinkerbell/controllers"
2524
"k8s.io/apimachinery/pkg/runtime"
2625
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2726
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2827
"k8s.io/client-go/tools/record"
28+
"k8s.io/klog"
29+
"k8s.io/klog/klogr"
30+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
2931
ctrl "sigs.k8s.io/controller-runtime"
3032
"sigs.k8s.io/controller-runtime/pkg/healthz"
31-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3233

3334
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"
3536
// +kubebuilder:scaffold:imports
3637
)
3738

@@ -49,115 +50,105 @@ func init() {
4950
// +kubebuilder:scaffold:scheme
5051
}
5152

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+
}
6371

64-
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
72+
flag.BoolVar(&options.LeaderElection, "enable-leader-election", false,
6573
"Enable leader election for controller manager. "+
6674
"Enabling this will ensure there is only one active controller manager.")
6775

68-
flag.StringVar(
69-
&leaderElectionNamespace,
70-
"leader-election-namespace",
71-
"",
76+
flag.StringVar(&options.LeaderElectionNamespace, "leader-election-namespace", "",
7277
"Namespace that the controller performs leader election in. "+
7378
"If unspecified, the controller will discover which namespace it is running in.",
7479
)
7580

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.")
8182

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.")
8384

84-
flag.DurationVar(&syncPeriod,
85-
"sync-period",
86-
10*time.Minute,
85+
flag.DurationVar(&syncPeriod, "sync-period", 10*time.Minute, //nolint:gomnd
8786
"The minimum interval at which watched resources are reconciled (e.g. 15m)",
8887
)
8988

90-
flag.StringVar(
91-
&watchNamespace,
92-
"namespace",
93-
"",
89+
flag.StringVar(&options.Namespace, "namespace", "",
9490
"Namespace that the controller watches to reconcile cluster-api objects. "+
9591
"If unspecified, the controller watches for cluster-api objects across all namespaces.",
9692
)
9793

98-
flag.IntVar(&webhookPort,
99-
"webhook-port",
100-
0,
94+
flag.IntVar(&options.Port, "webhook-port", 0,
10195
"Webhook Server port, disabled by default. When enabled, the manager will only "+
10296
"work as webhook server, no reconcilers are installed.",
10397
)
10498

10599
flag.Parse()
106100

107-
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
101+
return options
102+
}
108103

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)
111107
}
112108

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+
}
118113

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)
131128
if err != nil {
132129
setupLog.Error(err, "unable to start manager")
133130
os.Exit(1)
134131
}
135132

136133
// TODO: Get a Tinkerbell client.
137134

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")
161152
os.Exit(1)
162153
}
163154
// +kubebuilder:scaffold:builder

0 commit comments

Comments
 (0)