-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
185 lines (171 loc) · 6.51 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"os"
"strings"
flag "github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
etcdv1alpha1 "github.com/improbable-eng/etcd-cluster-operator/api/v1alpha1"
"github.com/improbable-eng/etcd-cluster-operator/controllers"
"github.com/improbable-eng/etcd-cluster-operator/internal/etcd"
"github.com/improbable-eng/etcd-cluster-operator/version"
"github.com/improbable-eng/etcd-cluster-operator/webhooks"
"github.com/robfig/cron/v3"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
defaultEtcdRepository = "quay.io/coreos/etcd"
// These are replaced as part of the build so that the images match the name
// prefix and version of the controller image. See Dockerfile.
defaultBackupAgentImage = "REPLACE_ME"
defaultRestoreAgentImage = "REPLACE_ME"
)
const (
defaultProxyPort = 80
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = etcdv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
var (
enableLeaderElection bool
leaderElectionID string
metricsAddr string
printVersion bool
proxyURL string
etcdRepository string
restoreAgentImage string
backupAgentImage string
)
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.StringVar(&leaderElectionID, "leader-election-id", "etcd-cluster-operator-controller-leader-election-helper",
"The name of the configmap that leader election will use for holding the leader lock.")
flag.StringVar(&etcdRepository, "etcd-repository", defaultEtcdRepository, "The Docker repository to use for the etcd image")
flag.StringVar(&backupAgentImage, "backup-agent-image", defaultBackupAgentImage, "The Docker image for the backup-agent")
flag.StringVar(&restoreAgentImage, "restore-agent-image", defaultRestoreAgentImage, "The Docker image to use to perform a restore")
flag.StringVar(&proxyURL, "proxy-url", "", "The URL of the upload/download proxy")
flag.BoolVar(&printVersion, "version", false,
"Print version to stdout and exit")
flag.Parse()
if printVersion {
fmt.Println(version.Version)
return
}
// TODO: Allow users to configure JSON logging.
// See https://github.com/improbable-eng/etcd-cluster-operator/issues/171
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
setupLog.Info(
"Starting manager",
"version", version.Version,
"etcd-repository", etcdRepository,
"backup-agent-image", backupAgentImage,
"restore-agent-image", restoreAgentImage,
"proxy-url", proxyURL,
)
if !strings.Contains(proxyURL, ":") {
// gRPC needs a port, and this address doesn't seem to have one.
proxyURL = fmt.Sprintf("%s:%d", proxyURL, defaultProxyPort)
setupLog.Info("Defaulting port on configured Proxy URL",
"default-proxy-port", defaultProxyPort,
"proxy-url", proxyURL)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: leaderElectionID,
Port: 9443,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&controllers.EtcdPeerReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("EtcdPeer"),
Etcd: &etcd.ClientEtcdAPIBuilder{},
EtcdRepository: etcdRepository,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EtcdPeer")
os.Exit(1)
}
if err = (&controllers.EtcdClusterReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("EtcdCluster"),
Recorder: mgr.GetEventRecorderFor("etcdcluster-reconciler"),
Etcd: &etcd.ClientEtcdAPIBuilder{},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EtcdCluster")
os.Exit(1)
}
if err = (&controllers.EtcdBackupReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("EtcdBackup"),
Scheme: mgr.GetScheme(),
BackupAgentImage: backupAgentImage,
ProxyURL: proxyURL,
Recorder: mgr.GetEventRecorderFor("etcdbackup-reconciler"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EtcdBackup")
os.Exit(1)
}
cronHandler := cron.New()
if err = (&controllers.EtcdBackupScheduleReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("EtcdBackupSchedule"),
CronHandler: cronHandler,
Schedules: controllers.NewScheduleMap(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EtcdBackupSchedule")
os.Exit(1)
}
if err = (&controllers.EtcdRestoreReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("EtcdRestore"),
Recorder: mgr.GetEventRecorderFor("etcdrestore-reconciler"),
RestorePodImage: restoreAgentImage,
ProxyURL: proxyURL,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "EtcdRestore")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
if os.Getenv("DISABLE_WEBHOOKS") != "" {
setupLog.Info("Skipping webhook set up.")
} else {
setupLog.Info("Setting up webhooks.")
if err = (&webhooks.EtcdCluster{
Log: ctrl.Log.WithName("webhooks").WithName("EtcdCluster"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "Unable to create webhook.", "webhook", "EtcdCluster")
os.Exit(1)
}
if err = (&webhooks.EtcdPeer{
Log: ctrl.Log.WithName("webhooks").WithName("EtcdPeer"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "Unable to create webhook.", "webhook", "EtcdPeer")
os.Exit(1)
}
}
setupLog.Info("Starting cron handler")
cronHandler.Start()
defer func() {
setupLog.Info("Stopping cron handler")
<-cronHandler.Stop().Done()
}()
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}