-
Notifications
You must be signed in to change notification settings - Fork 63
/
main.go
380 lines (320 loc) · 13 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Copyright 2021.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"os"
"sort"
"strings"
"time"
"github.com/artemiscloud/activemq-artemis-operator/version"
"github.com/go-logr/logr"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"fmt"
goruntime "runtime"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
routev1 "github.com/openshift/api/route/v1"
"github.com/artemiscloud/activemq-artemis-operator/pkg/log"
"github.com/artemiscloud/activemq-artemis-operator/pkg/sdkk8sutil"
"github.com/artemiscloud/activemq-artemis-operator/pkg/utils/common"
brokerv1alpha1 "github.com/artemiscloud/activemq-artemis-operator/api/v1alpha1"
brokerv1beta1 "github.com/artemiscloud/activemq-artemis-operator/api/v1beta1"
brokerv2alpha1 "github.com/artemiscloud/activemq-artemis-operator/api/v2alpha1"
brokerv2alpha2 "github.com/artemiscloud/activemq-artemis-operator/api/v2alpha2"
brokerv2alpha3 "github.com/artemiscloud/activemq-artemis-operator/api/v2alpha3"
brokerv2alpha4 "github.com/artemiscloud/activemq-artemis-operator/api/v2alpha4"
brokerv2alpha5 "github.com/artemiscloud/activemq-artemis-operator/api/v2alpha5"
"github.com/artemiscloud/activemq-artemis-operator/controllers"
//+kubebuilder:scaffold:imports
)
var (
metricsHost = "0.0.0.0"
metricsPort int32 = 8383
)
var (
//hard coded because the sdk version pkg is moved in internal package
sdkVersion = "1.28.0"
scheme = runtime.NewScheme()
)
var setupLog logr.Logger
func printVersion() {
setupLog.Info(fmt.Sprintf("Go Version: %s", goruntime.Version()))
setupLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", goruntime.GOOS, goruntime.GOARCH))
setupLog.Info(fmt.Sprintf("Version of operator-sdk: %v", sdkVersion))
setupLog.Info(fmt.Sprintf("Version of the operator: %s %s", version.Version, version.BuildTimestamp))
setupLog.Info(fmt.Sprintf("Supported ActiveMQArtemis Kubernetes Image Versions: %s", getSupportedBrokerVersions()))
}
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(routev1.AddToScheme(scheme))
utilruntime.Must(brokerv2alpha1.AddToScheme(scheme))
utilruntime.Must(brokerv2alpha2.AddToScheme(scheme))
utilruntime.Must(brokerv2alpha3.AddToScheme(scheme))
utilruntime.Must(brokerv2alpha4.AddToScheme(scheme))
utilruntime.Must(brokerv2alpha5.AddToScheme(scheme))
utilruntime.Must(brokerv1alpha1.AddToScheme(scheme))
utilruntime.Must(brokerv1beta1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
type TraceLogWarnings struct {
Log logr.Logger
}
func (l *TraceLogWarnings) HandleWarningHeader(code int, agent string, message string) {
l.Log.V(2).Info(message, "code", code, "agent", agent)
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var leaseDurationSeconds int64
var renewDeadlineSeconds int64
var retryPeriodSeconds int64
var probeAddr string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.Int64Var(&leaseDurationSeconds, "lease-duration", 15, "LeaseDuration is the duration that non-leader candidates will wait to force acquire leadership. This is measured against time of last observed ack. Default is 15 seconds.")
flag.Int64Var(&renewDeadlineSeconds, "renew-deadline", 10, "RenewDeadline is the duration that the acting controlplane will retry refreshing leadership before giving up. Default is 10 seconds.")
flag.Int64Var(&retryPeriodSeconds, "retry-period", 2, "RetryPeriod is the duration the LeaderElector clients should wait between tries of actions. Default is 2 seconds.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
os.Args = append(os.Args, strings.Split(os.Getenv("ARGS"), " ")...)
flag.Parse()
logger := zap.New(zap.UseFlagOptions(&opts))
// Exclude the logger with the name comparator because it logs the compared objects
filteredLogSink := log.NewFilteredLogSink(logger.GetSink(), []string{"comparator"})
ctrl.SetLogger(logger.WithSink(filteredLogSink))
setupLog = ctrl.Log.WithName("setup")
printVersion()
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
setupLog.Error(err, "Error getting config for APIServer")
os.Exit(1)
}
oprNamespace, err := sdkk8sutil.GetOperatorNamespace()
if err != nil {
setupLog.Error(err, "failed to get operator namespace")
os.Exit(1)
}
setupLog.Info("Got operator namespace", "operator ns", oprNamespace)
watchNamespace, _ := sdkk8sutil.GetWatchNamespace()
// Expose the operator's namespace and watchNamespace
if err := os.Setenv("OPERATOR_NAMESPACE", oprNamespace); err != nil {
setupLog.Error(err, "failed to set operator's namespace to env")
}
if err := os.Setenv("OPERATOR_WATCH_NAMESPACE", watchNamespace); err != nil {
setupLog.Error(err, "failed to set operator's watch namespace to env")
}
leaseDuration := time.Duration(leaseDurationSeconds) * time.Second
renewDeadline := time.Duration(renewDeadlineSeconds) * time.Second
retryPeriod := time.Duration(retryPeriodSeconds) * time.Second
mgrOptions := ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
},
WebhookServer: &webhook.DefaultServer{
Options: webhook.Options{
Port: 9443,
},
},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "d864aab0.amq.io",
LeaseDuration: &leaseDuration,
RenewDeadline: &renewDeadline,
RetryPeriod: &retryPeriod,
Logger: setupLog,
}
mgrOptions.Client.WarningHandler.SuppressWarnings = true
rest.SetDefaultWarningHandler(&TraceLogWarnings{Log: ctrl.Log})
isLocal, watchList := common.ResolveWatchNamespaceForManager(oprNamespace, watchNamespace)
if isLocal {
setupLog.Info("setting up operator to watch local namespace")
mgrOptions.Cache.DefaultNamespaces = map[string]cache.Config{
oprNamespace: {}}
} else {
if watchList != nil {
if len(watchList) == 1 {
setupLog.Info("setting up operator to watch single namespace")
} else {
setupLog.Info("setting up operator to watch multiple namespaces", "namespace(s)", watchList)
}
nsMap := map[string]cache.Config{}
for _, ns := range watchList {
nsMap[ns] = cache.Config{}
}
mgrOptions.Cache.DefaultNamespaces = nsMap
} else {
setupLog.Info("setting up operator to watch all namespaces")
}
}
setupLog.Info("Manager options",
"Namespaces", mgrOptions.Cache.DefaultNamespaces,
"MetricsBindAddress", mgrOptions.Metrics.BindAddress,
"Port", mgrOptions.WebhookServer.(*webhook.DefaultServer).Options.Port,
"HealthProbeBindAddress", mgrOptions.HealthProbeBindAddress,
"LeaderElection", mgrOptions.LeaderElection,
"LeaderElectionID", mgrOptions.LeaderElectionID,
"LeaseDuration", mgrOptions.LeaseDuration,
"RenewDeadline", mgrOptions.RenewDeadline,
"RetryPeriod", mgrOptions.RetryPeriod)
mgr, err := ctrl.NewManager(cfg, mgrOptions)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Set the service account name for the drainer pod
// It will be broken without this as it won't have
// permission to list the endpoints in drain.sh
name := os.Getenv("POD_NAME")
clnt, err := client.New(cfg, client.Options{})
if err != nil {
setupLog.Error(err, "can't create client from config")
os.Exit(1)
} else {
setupAccountName(clnt, context.TODO(), oprNamespace, name)
}
isOpenshift, err := common.DetectOpenshiftWith(cfg)
if err != nil {
setupLog.Error(err, "can't determine api server type")
os.Exit(1)
}
brokerReconciler := controllers.NewActiveMQArtemisReconciler(
mgr,
ctrl.Log.WithName("ActiveMQArtemisReconciler"),
isOpenshift)
if err = brokerReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ActiveMQArtemis")
os.Exit(1)
}
addressReconciler := controllers.NewActiveMQArtemisAddressReconciler(
mgr.GetClient(),
mgr.GetScheme(),
ctrl.Log.WithName("ActiveMQArtemisAddressReconciler"))
if err = addressReconciler.SetupWithManager(mgr, context.TODO()); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ActiveMQArtemisAddress")
os.Exit(1)
}
scaledownReconciler := controllers.NewActiveMQArtemisScaledownReconciler(
mgr.GetClient(),
mgr.GetScheme(),
mgr.GetConfig(),
ctrl.Log.WithName("ActiveMQArtemisScaledownReconciler"))
if err = scaledownReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ActiveMQArtemisScaledown")
os.Exit(1)
}
securityReconciler := controllers.NewActiveMQArtemisSecurityReconciler(
mgr.GetClient(),
mgr.GetScheme(),
brokerReconciler,
ctrl.Log.WithName("ActiveMQArtemisSecurityReconciler"))
if err = securityReconciler.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ActiveMQArtemisSecurity")
os.Exit(1)
}
enableWebhooks := os.Getenv("ENABLE_WEBHOOKS")
if enableWebhooks != "false" {
setupLog.Info("Setting up webhook functions", "ENABLE_WEBHOOKS", enableWebhooks)
if err = (&brokerv1beta1.ActiveMQArtemis{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ActiveMQArtemis")
os.Exit(1)
}
if err = (&brokerv1beta1.ActiveMQArtemisSecurity{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ActiveMQArtemisSecurity")
os.Exit(1)
}
if err = (&brokerv1beta1.ActiveMQArtemisAddress{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "ActiveMQArtemisAddress")
os.Exit(1)
}
} else {
setupLog.Info("NOT Setting up webhook functions", "ENABLE_WEBHOOKS", enableWebhooks)
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("Starting the manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func getSupportedBrokerVersions() string {
allSupportVersions := make([]string, 0, 10)
relatedImageEnvVarPrefix := "RELATED_IMAGE_ActiveMQ_Artemis_Broker_Kubernetes_"
// The full env var name should be relatedImageEnvVarPrefix + compactVersion
for _, envLine := range os.Environ() {
envPair := strings.Split(envLine, "=")
if strings.HasPrefix(envPair[0], relatedImageEnvVarPrefix) {
//try get compact version
compactVersion := envPair[0][len(relatedImageEnvVarPrefix):]
if fullVersion, ok := version.FullVersionFromCompactVersion[compactVersion]; ok {
allSupportVersions = append(allSupportVersions, fullVersion)
}
}
}
sort.Strings(allSupportVersions)
supportedProductVersions := ""
for _, k := range allSupportVersions {
supportedProductVersions += k
supportedProductVersions += " "
}
return strings.TrimSpace(supportedProductVersions)
}
func setupAccountName(clnt client.Client, ctx context.Context, ns, podname string) {
pod := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Pod",
},
}
key := client.ObjectKey{Namespace: ns, Name: podname}
err := clnt.Get(ctx, key, pod)
if err != nil {
setupLog.Error(err, "failed to get pod", "namespace", ns, "pod name", podname)
} else {
setupLog.Info("service account name: " + pod.Spec.ServiceAccountName)
err = os.Setenv("SERVICE_ACCOUNT", pod.Spec.ServiceAccountName)
if err != nil {
setupLog.Error(err, "failed to set env variable")
}
}
}