forked from grafana/grafana-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
206 lines (175 loc) · 6.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
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
package main
import (
"context"
"flag"
"fmt"
"github.com/integr8ly/grafana-operator/pkg/apis"
"github.com/integr8ly/grafana-operator/pkg/controller"
"github.com/integr8ly/grafana-operator/pkg/controller/common"
"github.com/integr8ly/grafana-operator/pkg/controller/grafanadashboard"
"github.com/integr8ly/grafana-operator/version"
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
"github.com/operator-framework/operator-sdk/pkg/leader"
"github.com/operator-framework/operator-sdk/pkg/ready"
sdkVersion "github.com/operator-framework/operator-sdk/version"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/runtime/signals"
"strings"
)
var log = logf.Log.WithName("cmd")
var flagImage string
var flagImageTag string
var flagPluginsInitContainerImage string
var flagPluginsInitContainerTag string
var flagPodLabelValue string
var flagNamespaces string
var scanAll bool
func printVersion() {
log.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
log.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
log.Info(fmt.Sprintf("operator-sdk Version: %v", sdkVersion.Version))
log.Info(fmt.Sprintf("operator Version: %v", version.Version))
}
func init() {
flagset := flag.CommandLine
flagset.StringVar(&flagImage, "grafana-image", "", "Overrides the default Grafana image")
flagset.StringVar(&flagImageTag, "grafana-image-tag", "", "Overrides the default Grafana image tag")
flagset.StringVar(&flagPluginsInitContainerImage, "grafana-plugins-init-container-image", "", "Overrides the default Grafana Plugins Init Container image")
flagset.StringVar(&flagPluginsInitContainerTag, "grafana-plugins-init-container-tag", "", "Overrides the default Grafana Plugins Init Container tag")
flagset.StringVar(&flagPodLabelValue, "pod-label-value", common.PodLabelDefaultValue, "Overrides the default value of the app label")
flagset.StringVar(&flagNamespaces, "namespaces", "", "Namespaces to scope the interaction of the Grafana operator. Mutually exclusive with --scan-all")
flagset.BoolVar(&scanAll, "scan-all", false, "Scans all namespaces for dashboards")
flagset.Parse(os.Args[1:])
}
// Starts a separate controller for the dashboard reconciliation in the background
func startDashboardController(ns string, cfg *rest.Config, signalHandler <-chan struct{}) {
// Create a new Cmd to provide shared dependencies and start components
dashboardMgr, err := manager.New(cfg, manager.Options{Namespace: ns})
if err != nil {
log.Error(err, "")
os.Exit(1)
}
// Setup Scheme for the dashboard resource
if err := apis.AddToScheme(dashboardMgr.GetScheme()); err != nil {
log.Error(err, "")
os.Exit(1)
}
// Use a separate manager for the dashboard controller
grafanadashboard.Add(dashboardMgr)
go func() {
if err := dashboardMgr.Start(signalHandler); err != nil {
log.Error(err, "dashboard manager exited non-zero")
os.Exit(1)
}
}()
}
// Get the trimmed and sanitized list of namespaces (if --namespaces was provided)
func getSanitizedNamespaceList() []string {
provided := strings.Split(flagNamespaces, ",")
var selected []string
for _, v := range provided {
v = strings.TrimSpace(v)
if v != "" {
selected = append(selected, v)
}
}
return selected
}
func main() {
// The logger instantiated here can be changed to any logger
// implementing the logr.Logger interface. This logger will
// be propagated through the whole operator, generating
// uniform and structured logs.
logf.SetLogger(logf.ZapLogger(false))
printVersion()
namespace, err := k8sutil.GetWatchNamespace()
if err != nil {
log.Error(err, "failed to get watch namespace")
os.Exit(1)
}
if scanAll && flagNamespaces != "" {
fmt.Fprint(os.Stderr, "--scan-all and --namespaces both set. Please provide only one")
os.Exit(1)
}
// Controller configuration
controllerConfig := common.GetControllerConfig()
controllerConfig.AddConfigItem(common.ConfigGrafanaImage, flagImage)
controllerConfig.AddConfigItem(common.ConfigGrafanaImageTag, flagImageTag)
controllerConfig.AddConfigItem(common.ConfigPluginsInitContainerImage, flagPluginsInitContainerImage)
controllerConfig.AddConfigItem(common.ConfigPluginsInitContainerTag, flagPluginsInitContainerTag)
controllerConfig.AddConfigItem(common.ConfigPodLabelValue, flagPodLabelValue)
controllerConfig.AddConfigItem(common.ConfigOperatorNamespace, namespace)
controllerConfig.AddConfigItem(common.ConfigDashboardLabelSelector, "")
// Get the namespaces to scan for dashboards
// It's either the same namespace as the controller's or it's all namespaces if the
// --scan-all flag has been passed
var dashboardNamespaces = []string{namespace}
if scanAll {
dashboardNamespaces = []string{""}
log.Info("Scanning for dashboards in all namespaces")
}
if flagNamespaces != "" {
dashboardNamespaces = getSanitizedNamespaceList()
if len(dashboardNamespaces) == 0 {
fmt.Fprint(os.Stderr, "--namespaces provided but no valid namespaces in list")
os.Exit(1)
}
log.Info(fmt.Sprintf("Scanning for dashboards in the following namespaces: [%s]", strings.Join(dashboardNamespaces, ",")))
}
// Get a config to talk to the apiserver
cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "")
os.Exit(1)
}
// Become the leader before proceeding
leader.Become(context.TODO(), "grafana-operator-lock")
r := ready.NewFileReady()
err = r.Set()
if err != nil {
log.Error(err, "")
os.Exit(1)
}
defer r.Unset()
// Create a new Cmd to provide shared dependencies and start components
mgr, err := manager.New(cfg, manager.Options{Namespace: namespace})
if err != nil {
log.Error(err, "")
os.Exit(1)
}
log.Info("Registering Components.")
// Setup Scheme for all resources
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Error(err, "")
os.Exit(1)
}
// Setup all Controllers
if err := controller.AddToManager(mgr); err != nil {
log.Error(err, "")
os.Exit(1)
}
log.Info("Starting the Cmd.")
// Starting the resource auto-detection for the grafana controller
autodetect, err := common.NewAutoDetect(mgr)
if err != nil {
log.Error(err, "failed to start the background process to auto-detect the operator capabilities")
} else {
autodetect.Start()
defer autodetect.Stop()
}
signalHandler := signals.SetupSignalHandler()
// Start one dashboard controller per watch namespace
for _, ns := range dashboardNamespaces {
startDashboardController(ns, cfg, signalHandler)
}
if err := mgr.Start(signalHandler); err != nil {
log.Error(err, "manager exited non-zero")
os.Exit(1)
}
}