forked from rabbitmq/cluster-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
233 lines (199 loc) · 8.12 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// RabbitMQ Cluster Operator
//
// Copyright 2020 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Mozilla Public license, Version 2.0 (the "License"). You may not use this product except in compliance with the Mozilla Public License.
//
// This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"time"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"github.com/rabbitmq/cluster-operator/v2/pkg/profiling"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client/config"
rabbitmqv1beta1 "github.com/rabbitmq/cluster-operator/v2/api/v1beta1"
"github.com/rabbitmq/cluster-operator/v2/controllers"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"
defaultscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
const controllerName = "rabbitmqcluster-controller"
var (
scheme = runtime.NewScheme()
log = ctrl.Log.WithName("setup")
)
func init() {
_ = rabbitmqv1beta1.AddToScheme(scheme)
_ = defaultscheme.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
var (
metricsAddr string
defaultRabbitmqImage = "rabbitmq:3.13.7-management"
controlRabbitmqImage = false
defaultUserUpdaterImage = "rabbitmqoperator/default-user-credential-updater:1.0.2"
defaultImagePullSecrets = ""
)
flag.StringVar(&metricsAddr, "metrics-bind-address", ":9782", "The address the metric endpoint binds to.")
opts := zap.Options{}
opts.BindFlags(flag.CommandLine)
flag.Parse()
logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
// https://github.com/kubernetes-sigs/controller-runtime/issues/1420#issuecomment-794525248
klog.SetLogger(logger.WithName("rabbitmq-cluster-operator"))
operatorNamespace := os.Getenv("OPERATOR_NAMESPACE")
if operatorNamespace == "" {
log.Info("unable to find operator namespace")
os.Exit(1)
}
// If the environment variable is not set Getenv returns an empty string which ctrl.Options.Namespace takes to mean all namespaces should be watched
operatorScopeNamespace := os.Getenv("OPERATOR_SCOPE_NAMESPACE")
if configuredDefaultRabbitmqImage, ok := os.LookupEnv("DEFAULT_RABBITMQ_IMAGE"); ok {
defaultRabbitmqImage = configuredDefaultRabbitmqImage
}
if configuredDefaultUserUpdaterImage, ok := os.LookupEnv("DEFAULT_USER_UPDATER_IMAGE"); ok {
defaultUserUpdaterImage = configuredDefaultUserUpdaterImage
}
// EXPERIMENTAL: If the environment variable CONTROL_RABBITMQ_IMAGE is set to `true`, the operator will
// automatically set the default image tags. (DEFAULT_RABBITMQ_IMAGE and DEFAULT_USER_UPDATER_IMAGE)
// No safety checks!
if configuredControlRabbitmqImage, ok := os.LookupEnv("CONTROL_RABBITMQ_IMAGE"); ok {
var err error
if controlRabbitmqImage, err = strconv.ParseBool(configuredControlRabbitmqImage); err != nil {
log.Error(err, "unable to start manager")
os.Exit(1)
}
}
if configuredDefaultImagePullSecrets, ok := os.LookupEnv("DEFAULT_IMAGE_PULL_SECRETS"); ok {
defaultImagePullSecrets = configuredDefaultImagePullSecrets
}
options := ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: metricsAddr,
},
LeaderElection: true,
LeaderElectionNamespace: operatorNamespace,
LeaderElectionID: "rabbitmq-cluster-operator-leader-election",
// Namespace is deprecated. Advice is to use Cache.Namespaces instead
}
if operatorScopeNamespace != "" {
if strings.Contains(operatorScopeNamespace, ",") {
namespaces := strings.Split(operatorScopeNamespace, ",")
// https://github.com/kubernetes-sigs/controller-runtime/blob/main/designs/cache_options.md#only-cache-namespaced-objects-in-the-foo-and-bar-namespace
// Sometimes I wish that controller-runtime graduated to 1.x
// This changed in 0.15, and again in 0.16 🤦
options.Cache.DefaultNamespaces = make(map[string]cache.Config)
for _, namespace := range namespaces {
options.Cache.DefaultNamespaces[namespace] = cache.Config{}
}
log.Info("limiting watch to specific namespaces for RabbitMQ resources", "namespaces", namespaces)
} else {
options.Cache = cache.Options{
DefaultNamespaces: map[string]cache.Config{operatorScopeNamespace: {}},
}
log.Info("limiting watch to one namespace", "namespace", operatorScopeNamespace)
}
}
rmqLabel, err := labels.NewRequirement("app.kubernetes.io/part-of", selection.Equals, []string{"rabbitmq"})
if err != nil {
log.Error(err, "unable to create a label filter")
os.Exit(1)
}
rmqSelector := labels.NewSelector().Add(*rmqLabel)
options.Cache.ByObject = map[client.Object]cache.ByObject{
&rabbitmqv1beta1.RabbitmqCluster{}: {},
&appsv1.StatefulSet{}: {Label: rmqSelector},
&corev1.Service{}: {Label: rmqSelector},
&corev1.ConfigMap{}: {Label: rmqSelector},
&corev1.Secret{}: {Label: rmqSelector},
&corev1.ServiceAccount{}: {Label: rmqSelector},
&corev1.Endpoints{}: {Label: rmqSelector},
&rbacv1.Role{}: {Label: rmqSelector},
&rbacv1.RoleBinding{}: {Label: rmqSelector},
}
if leaseDuration := getEnvInDuration("LEASE_DURATION"); leaseDuration != 0 {
log.Info("manager configured with lease duration", "seconds", int(leaseDuration.Seconds()))
options.LeaseDuration = &leaseDuration
}
if renewDeadline := getEnvInDuration("RENEW_DEADLINE"); renewDeadline != 0 {
log.Info("manager configured with renew deadline", "seconds", int(renewDeadline.Seconds()))
options.RenewDeadline = &renewDeadline
}
if retryPeriod := getEnvInDuration("RETRY_PERIOD"); retryPeriod != 0 {
log.Info("manager configured with retry period", "seconds", int(retryPeriod.Seconds()))
options.RetryPeriod = &retryPeriod
}
if enableDebugPprof, ok := os.LookupEnv("ENABLE_DEBUG_PPROF"); ok {
pprofEnabled, err := strconv.ParseBool(enableDebugPprof)
if err == nil && pprofEnabled {
o, err := profiling.AddDebugPprofEndpoints(&options)
if err != nil {
log.Error(err, "unable to add debug endpoints to manager")
os.Exit(1)
}
options = *o
}
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
if err != nil {
log.Error(err, "unable to start manager")
os.Exit(1)
}
clusterConfig := config.GetConfigOrDie()
err = (&controllers.RabbitmqClusterReconciler{
Client: mgr.GetClient(),
APIReader: mgr.GetAPIReader(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor(controllerName),
Namespace: operatorNamespace,
ClusterConfig: clusterConfig,
Clientset: kubernetes.NewForConfigOrDie(clusterConfig),
PodExecutor: controllers.NewPodExecutor(),
DefaultRabbitmqImage: defaultRabbitmqImage,
DefaultUserUpdaterImage: defaultUserUpdaterImage,
DefaultImagePullSecrets: defaultImagePullSecrets,
ControlRabbitmqImage: controlRabbitmqImage,
}).SetupWithManager(mgr)
if err != nil {
log.Error(err, "unable to create controller", controllerName)
os.Exit(1)
}
log.Info("started controller")
// +kubebuilder:scaffold:builder
log.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
log.Error(err, "problem running manager")
os.Exit(1)
}
}
func getEnvInDuration(envName string) time.Duration {
var durationInt int64
if durationStr := os.Getenv(envName); durationStr != "" {
var err error
if durationInt, err = strconv.ParseInt(durationStr, 10, 64); err != nil {
log.Error(err, fmt.Sprintf("unable to parse provided '%s'", envName))
os.Exit(1)
}
}
return time.Duration(durationInt) * time.Second
}