-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
132 lines (111 loc) · 3.95 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
package main
import (
"flag"
"fmt"
"os"
"time"
"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"
dbv1 "github.com/PayU/redis-operator/api/v1"
"github.com/PayU/redis-operator/controllers"
"github.com/PayU/redis-operator/controllers/rediscli"
"github.com/PayU/redis-operator/controllers/view"
"github.com/PayU/redis-operator/server"
"github.com/go-logr/logr"
// +kubebuilder:scaffold:imports
)
var scheme = runtime.NewScheme()
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = dbv1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func getRedisCLI(log *logr.Logger) *rediscli.RedisCLI {
cli := rediscli.NewRedisCLI(log)
user := os.Getenv("REDIS_USERNAME")
if user != "" {
cli.Auth = &rediscli.RedisAuth{
User: user,
}
}
return cli
}
func main() {
go server.StartServer()
startManager()
}
func startManager() {
var metricsAddr, namespace, enableLeaderElection, devmode string
flag.StringVar(&metricsAddr, "metrics-addr", "0.0.0.0:9808", "The address the metric endpoint binds to.")
flag.StringVar(&namespace, "namespace", "default", "The namespace the operator will manage.")
flag.StringVar(&devmode, "devmode", "false", "Development mode toggle.")
flag.StringVar(&enableLeaderElection, "enable-leader-election", "true",
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.Parse()
setupLogger := zap.New(zap.UseDevMode(devmode == "true")).WithName("setup")
retryLockDuration := 4 * time.Second
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Namespace: namespace,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection == "true",
LeaderElectionID: "1747e98e.payu.com",
RetryPeriod: &retryLockDuration,
})
if err != nil {
setupLogger.Error(err, "failed to create new manager")
os.Exit(1)
}
rdcLogger := zap.New(zap.UseDevMode(devmode == "true")).
WithName("controllers").
WithName("RedisCluster")
configLogger := zap.New(zap.UseDevMode(devmode == "true")).
WithName("controllers").
WithName("RedisConfig")
operatorConfig, err := controllers.NewRedisOperatorConfig("/usr/local/etc/operator.conf", setupLogger)
if err != nil {
setupLogger.Error(err, fmt.Sprintf("Failed to get operator config file, falling back to default config"))
operatorConfig = controllers.DefaultRedisOperatorConfig(setupLogger)
setupLogger.Info(fmt.Sprintf("Loaded config: %+v", operatorConfig.Config))
}
k8sManager := controllers.K8sManager{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: configLogger,
}
if err = (&controllers.RedisClusterReconciler{
Client: mgr.GetClient(),
Log: rdcLogger,
Scheme: mgr.GetScheme(),
RedisCLI: getRedisCLI(&rdcLogger),
Config: &operatorConfig.Config,
State: controllers.NotExists,
RedisClusterStateView: &view.RedisClusterStateView{Name: controllers.RedisClusterStateMapName},
}).SetupWithManager(mgr); err != nil {
setupLogger.Error(err, "unable to create controller", "controller", "RedisCluster")
os.Exit(1)
}
if err = (&controllers.RedisConfigReconciler{
Client: mgr.GetClient(),
Log: configLogger,
K8sManager: &k8sManager,
Scheme: mgr.GetScheme(),
Config: &operatorConfig.Config,
RedisCLI: getRedisCLI(&configLogger),
}).SetupWithManager(mgr); err != nil {
setupLogger.Error(err, "unable to create controller", "controller", "RedisConfig")
os.Exit(1)
}
operatorConfig.Log = configLogger
// +kubebuilder:scaffold:builder
setupLogger.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLogger.Error(err, "failed to start the manager")
os.Exit(1)
}
}