/
main.go
114 lines (94 loc) · 3.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"flag"
"fmt"
"os"
"time"
configv1 "github.com/openshift/api/config/v1"
"github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"
vsphereapis "github.com/openshift/machine-api-operator/pkg/apis/vsphereprovider"
capimachine "github.com/openshift/machine-api-operator/pkg/controller/machine"
machine "github.com/openshift/machine-api-operator/pkg/controller/vsphere"
"github.com/openshift/machine-api-operator/pkg/version"
"k8s.io/klog"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
)
// The default durations for the leader electrion operations.
var (
leaseDuration = 120 * time.Second
renewDealine = 110 * time.Second
retryPeriod = 90 * time.Second
)
func main() {
var printVersion bool
flag.BoolVar(&printVersion, "version", false, "print version and exit")
klog.InitFlags(nil)
watchNamespace := flag.String(
"namespace",
"",
"Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.",
)
leaderElectResourceNamespace := flag.String(
"leader-elect-resource-namespace",
"",
"The namespace of resource object that is used for locking during leader election. If unspecified and running in cluster, defaults to the service account namespace for the controller. Required for leader-election outside of a cluster.",
)
leaderElect := flag.Bool(
"leader-elect",
false,
"Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.",
)
leaderElectLeaseDuration := flag.Duration(
"leader-elect-lease-duration",
leaseDuration,
"The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire leadership of a led but unrenewed leader slot. This is effectively the maximum duration that a leader can be stopped before it is replaced by another candidate. This is only applicable if leader election is enabled.",
)
flag.Set("logtostderr", "true")
flag.Parse()
if printVersion {
fmt.Println(version.String)
os.Exit(0)
}
cfg := config.GetConfigOrDie()
opts := manager.Options{
// Disable metrics serving
MetricsBindAddress: "0",
LeaderElection: *leaderElect,
LeaderElectionNamespace: *leaderElectResourceNamespace,
LeaderElectionID: "cluster-api-provider-vsphere-leader",
LeaseDuration: leaderElectLeaseDuration,
// Slow the default retry and renew election rate to reduce etcd writes at idle: BZ 1858400
RetryPeriod: &retryPeriod,
RenewDeadline: &renewDealine,
}
if *watchNamespace != "" {
opts.Namespace = *watchNamespace
klog.Infof("Watching machine-api objects only in namespace %q for reconciliation.", opts.Namespace)
}
// Setup a Manager
mgr, err := manager.New(cfg, opts)
if err != nil {
klog.Fatalf("Failed to set up overall controller manager: %v", err)
}
// Initialize machine actuator.
machineActuator := machine.NewActuator(machine.ActuatorParams{
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
EventRecorder: mgr.GetEventRecorderFor("vspherecontroller"),
})
if err := configv1.AddToScheme(mgr.GetScheme()); err != nil {
klog.Fatal(err)
}
if err := vsphereapis.AddToScheme(mgr.GetScheme()); err != nil {
klog.Fatal(err)
}
if err := v1beta1.AddToScheme(mgr.GetScheme()); err != nil {
klog.Fatal(err)
}
capimachine.AddWithActuator(mgr, machineActuator)
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
klog.Fatalf("Failed to run manager: %v", err)
}
}