forked from oracle/oracle-database-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
267 lines (247 loc) · 10.4 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
/*
** Copyright (c) 2022 Oracle and/or its affiliates.
**
** The Universal Permissive License (UPL), Version 1.0
**
** Subject to the condition set forth below, permission is hereby granted to any
** person obtaining a copy of this software, associated documentation and/or data
** (collectively the "Software"), free of charge and under any and all copyright
** rights in the Software, and any and all patent rights owned or freely
** licensable by each licensor hereunder covering either (i) the unmodified
** Software as contributed to or provided by such licensor, or (ii) the Larger
** Works (as defined below), to deal in both
**
** (a) the Software, and
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
** one is included with the Software (each a "Larger Work" to which the Software
** is contributed by such licensors),
**
** without restriction, including without limitation the rights to copy, create
** derivative works of, display, perform, and distribute the Software and make,
** use, sell, offer for sale, import, export, have made, and have sold the
** Software and the Larger Work(s), and to sublicense the foregoing rights on
** either these or other terms.
**
** This license is subject to the following condition:
** The above copyright notice and either this complete permission notice or at
** a minimum a reference to the UPL must be included in all copies or
** substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
** SOFTWARE.
*/
package main
import (
"context"
"flag"
"os"
"strconv"
"time"
"go.uber.org/zap/zapcore"
"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/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
databasev1alpha1 "github.com/oracle/oracle-database-operator/apis/database/v1alpha1"
databasecontroller "github.com/oracle/oracle-database-operator/controllers/database"
// +kubebuilder:scaffold:imports
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(databasev1alpha1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
var metricsAddr string
var enableLeaderElection bool
flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.Parse()
// Initialize new logger Opts
options := &zap.Options{
Development: true,
TimeEncoder: zapcore.RFC3339TimeEncoder,
}
ctrl.SetLogger(zap.New(func(o *zap.Options) { *o = *options }))
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "a9d608ea.oracle.com",
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Get Cache
cache := mgr.GetCache()
// ADB family controllers
if err = (&databasecontroller.AutonomousDatabaseReconciler{
KubeClient: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("database").WithName("AutonomousDatabase"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("AutonomousDatabase"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AutonomousDatabase")
os.Exit(1)
}
if err = (&databasecontroller.AutonomousDatabaseBackupReconciler{
KubeClient: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AutonomousDatabaseBackup"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("AutonomousDatabaseBackup"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AutonomousDatabaseBackup")
os.Exit(1)
}
if err = (&databasecontroller.AutonomousDatabaseRestoreReconciler{
KubeClient: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AutonomousDatabaseRestore"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("AutonomousDatabaseRestore"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AutonomousDatabaseRestore")
os.Exit(1)
}
if err = (&databasecontroller.AutonomousContainerDatabaseReconciler{
KubeClient: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("AutonomousContainerDatabase"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("AutonomousContainerDatabase"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AutonomousContainerDatabase")
os.Exit(1)
}
if err = (&databasecontroller.SingleInstanceDatabaseReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("database").WithName("SingleInstanceDatabase"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
Recorder: mgr.GetEventRecorderFor("SingleInstanceDatabase"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "SingleInstanceDatabase")
os.Exit(1)
}
if err = (&databasecontroller.ShardingDatabaseReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("database").WithName("ShardingDatabase"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("ShardingDatabase"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ShardingDatabase")
os.Exit(1)
}
if err = (&databasecontroller.DbcsSystemReconciler{
KubeClient: mgr.GetClient(),
Logger: ctrl.Log.WithName("controllers").WithName("database").WithName("DbcsSystem"),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("DbcsSystem"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "DbcsSystem")
os.Exit(1)
}
if err = (&databasecontroller.OracleRestDataServiceReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("OracleRestDataService"),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
Recorder: mgr.GetEventRecorderFor("OracleRestDataService"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "OracleRestDataService")
os.Exit(1)
}
// Set RECONCILE_INTERVAL environment variable if you want to change the default value from 15 secs
interval := os.Getenv("RECONCILE_INTERVAL")
i, err := strconv.ParseInt(interval, 10, 64)
if err != nil {
i = 15
setupLog.Info("Setting default reconcile period for database-controller", "Secs", i)
}
// Set ENABLE_WEBHOOKS=false when we run locally to skip webhook part when testing just the controller. Not to be used in production.
if os.Getenv("ENABLE_WEBHOOKS") != "false" {
if err = (&databasev1alpha1.SingleInstanceDatabase{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "SingleInstanceDatabase")
os.Exit(1)
}
if err = (&databasev1alpha1.OracleRestDataService{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "OracleRestDataService")
os.Exit(1)
}
if err = (&databasev1alpha1.PDB{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "PDB")
os.Exit(1)
}
if err = (&databasev1alpha1.CDB{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "CDB")
os.Exit(1)
}
if err = (&databasev1alpha1.AutonomousDatabase{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AutonomousDatabase")
os.Exit(1)
}
if err = (&databasev1alpha1.AutonomousDatabaseBackup{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AutonomousDatabaseBackup")
os.Exit(1)
}
if err = (&databasev1alpha1.AutonomousDatabaseRestore{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AutonomousDatabaseRestore")
os.Exit(1)
}
if err = (&databasev1alpha1.AutonomousContainerDatabase{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "AutonomousContainerDatabase")
os.Exit(1)
}
}
// PDB Reconciler
if err = (&databasecontroller.PDBReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("PDB"),
Interval: time.Duration(i),
Recorder: mgr.GetEventRecorderFor("PDB"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PDB")
os.Exit(1)
}
// CDB Reconciler
if err = (&databasecontroller.CDBReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Config: mgr.GetConfig(),
Log: ctrl.Log.WithName("controllers").WithName("CDB"),
Interval: time.Duration(i),
Recorder: mgr.GetEventRecorderFor("CDB"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CDB")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
// Add index for PDB CR to enable mgr to cache PDBs
indexFunc := func(obj client.Object) []string {
return []string{obj.(*databasev1alpha1.PDB).Spec.PDBName}
}
if err = cache.IndexField(context.TODO(), &databasev1alpha1.PDB{}, "spec.pdbName", indexFunc); err != nil {
setupLog.Error(err, "unable to create index function for ", "controller", "PDB")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}