forked from oracle/coherence-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
245 lines (212 loc) · 7.14 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
/*
* Copyright (c) 2019, 2020 Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
package main
import (
"flag"
"fmt"
"github.com/oracle/coherence-operator/controllers/webhook"
"github.com/oracle/coherence-operator/pkg/clients"
"github.com/oracle/coherence-operator/pkg/operator"
"github.com/oracle/coherence-operator/pkg/rest"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
"runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"strings"
apiruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/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"
coh "github.com/oracle/coherence-operator/api/v1"
"github.com/oracle/coherence-operator/controllers"
// +kubebuilder:scaffold:imports
)
const (
flagMetricsAddress = "metrics-addr"
flagHealthAddress = "health-addr"
flagLeaderElection = "enable-leader-election"
)
var (
scheme = apiruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
// Cmd is the cobra command to start the manager.
Cmd = &cobra.Command{
Use: "manager",
Short: "Start the operator manager",
Long: "manager starts the manager for this operator, which will in turn create the necessary controller.",
Run: func(cmd *cobra.Command, args []string) {
execute()
},
}
)
func init() {
Cmd.Flags().String(flagMetricsAddress, ":8080", "The address the metric endpoint binds to.")
Cmd.Flags().String(flagHealthAddress, ":8088", "The address the health endpoint binds to.")
Cmd.Flags().Bool(flagLeaderElection, false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
operator.SetupFlags(Cmd)
// Add flags registered by imported packages (e.g. glog and controller-runtime)
flagSet := pflag.NewFlagSet("operator", pflag.ContinueOnError)
flagSet.AddGoFlagSet(flag.CommandLine)
if err := viper.BindPFlags(flagSet); err != nil {
setupLog.Error(err, "binding flags")
os.Exit(1)
}
// Validate the command line flags and environment variables
if err := operator.ValidateFlags(); err != nil {
fmt.Println(err.Error())
_ = Cmd.Help()
os.Exit(1)
}
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(coh.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
if err := Cmd.Execute(); err != nil {
logf.Log.WithName("main").Error(err, "Unexpected error while executing command")
}
}
func execute() {
ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
printVersion()
cfg := ctrl.GetConfigOrDie()
cl, err := clients.NewForConfig(cfg)
if err != nil {
setupLog.Error(err, "unable to create client set")
os.Exit(1)
}
initialiseOperator(cl)
options := ctrl.Options{
Scheme: scheme,
HealthProbeBindAddress: viper.GetString(flagHealthAddress),
MetricsBindAddress: viper.GetString(flagMetricsAddress),
Port: 9443,
LeaderElection: viper.GetBool(flagLeaderElection),
LeaderElectionID: "ca804aa8.oracle.com",
}
// Determine the Operator scope...
watchNamespaces := getWatchNamespace()
switch len(watchNamespaces) {
case 0:
// Watching all namespaces
setupLog.Info("Operator will watch all namespaces")
case 1:
// Watch a single namespace
setupLog.Info("Operator will watch single namespace: " + watchNamespaces[0])
options.Namespace = watchNamespaces[0]
default:
// Watch a multiple namespaces
setupLog.Info(fmt.Sprintf("Operator will watch multiple namespaces: %v", watchNamespaces))
options.NewCache = cache.MultiNamespacedCacheBuilder(watchNamespaces)
}
mgr, err := ctrl.NewManager(cfg, options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Set-up the Coherence reconciler
if err = (&controllers.CoherenceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Coherence"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Coherence")
os.Exit(1)
}
// Create the REST server
if err := rest.NewServer(cl).SetupWithManager(mgr); err != nil {
setupLog.Error(err, " unable to start REST server")
os.Exit(1)
}
// Set-up webhooks if required
var cr *webhook.CertReconciler
if operator.ShouldEnableWebhooks() {
// Set-up the webhook certificate reconciler
cr = &webhook.CertReconciler{
Clientset: cl,
}
if err := cr.SetupWithManager(mgr); err != nil {
setupLog.Error(err, " unable to create webhook certificate controller","controller", "Certs")
os.Exit(1)
}
// Set-up the webhooks
if err = (&coh.Coherence{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, " unable to create webhook", "webhook", "Coherence")
os.Exit(1)
}
}
//// ToDo: Make the ready check actually check stuff...
//if err = mgr.AddReadyzCheck("operator", func(req *http.Request) error {
// return nil
//}); err != nil {
// setupLog.Error(err, " unable to add operator readyz check")
// os.Exit(1)
//}
// +kubebuilder:scaffold:builder
// We intercept the signal handler here so that we can do clean-up before the Manager stops
signal := ctrl.SetupSignalHandler()
stop := make(chan struct{})
go func() {
<- signal
if cr != nil {
cr.Cleanup()
}
close(stop)
}()
setupLog.Info("starting manager")
if err := mgr.Start(stop); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func initialiseOperator(cl clients.ClientSet) {
opLog := ctrl.Log.WithName("operator")
// Ensure that the CRDs exist
err := coh.EnsureCRDs(cl)
if err != nil {
opLog.Error(err, "")
os.Exit(1)
}
}
// getWatchNamespace returns the Namespace(s) the operator should be watching for changes
func getWatchNamespace() []string {
// WatchNamespaceEnvVar is the constant for env variable WATCH_NAMESPACE
// which specifies the Namespace to watch.
// An empty value means the operator is running with cluster scope.
var watchNamespaceEnvVar = "WATCH_NAMESPACE"
var watches []string
ns, found := os.LookupEnv(watchNamespaceEnvVar)
if !found || ns == "" || strings.TrimSpace(ns) == "" {
return watches
}
for _, s := range strings.Split(ns, ",") {
watches = append(watches, strings.TrimSpace(s))
}
return watches
}
func printVersion() {
opLog := ctrl.Log.WithName("operator")
opLog.Info(fmt.Sprintf("Operator Version: %s", Version))
opLog.Info(fmt.Sprintf("Operator Build Date: %s", Date))
opLog.Info(fmt.Sprintf("Operator Git Commit: %s", Commit))
opLog.Info(fmt.Sprintf("Operator Coherence Image: %s", viper.GetString(operator.FlagCoherenceImage)))
opLog.Info(fmt.Sprintf("Operator Utils Image: %s", viper.GetString(operator.FlagUtilsImage)))
opLog.Info(fmt.Sprintf("Go Version: %s", runtime.Version()))
opLog.Info(fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH))
}
var (
// build information injected by the Go linker at build time.
Version string
Commit string
Date string
)