forked from oracle/oracle-database-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbcssystem_controller.go
314 lines (278 loc) · 12.1 KB
/
dbcssystem_controller.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
/*
** 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 controllers
import (
"context"
"reflect"
databasev1alpha1 "github.com/oracle/oracle-database-operator/apis/database/v1alpha1"
dbcsv1 "github.com/oracle/oracle-database-operator/commons/dbcssystem"
"github.com/oracle/oracle-database-operator/commons/finalizer"
"github.com/oracle/oracle-database-operator/commons/oci"
"github.com/go-logr/logr"
"github.com/oracle/oci-go-sdk/v65/core"
"github.com/oracle/oci-go-sdk/v65/database"
"github.com/oracle/oci-go-sdk/v65/workrequests"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
// DbcsSystemReconciler reconciles a DbcsSystem object
type DbcsSystemReconciler struct {
KubeClient client.Client
Scheme *runtime.Scheme
Logv1 logr.Logger
Logger logr.Logger
dbClient database.DatabaseClient
nwClient core.VirtualNetworkClient
wrClient workrequests.WorkRequestClient
Recorder record.EventRecorder
}
//+kubebuilder:rbac:groups=database.oracle.com,resources=dbcssystems,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=database.oracle.com,resources=dbcssystems/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=database.oracle.com,resources=dbcssystems/finalizers,verbs=get;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=configmaps;secrets;namespaces,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// TODO(user): Modify the Reconcile function to compare the state specified by
// the DbcsSystem object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *DbcsSystemReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
r.Logger = log.FromContext(ctx)
// your logic here
//r.Logger = r.Logv1.WithValues("Instance.Namespace", req.NamespacedName)
var err error
// Get the dbcs instance from the cluster
dbcsInst := &databasev1alpha1.DbcsSystem{}
if err := r.KubeClient.Get(context.TODO(), req.NamespacedName, dbcsInst); err != nil {
if !errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}
// Create oci-go-sdk client
authData := oci.APIKeyAuth{
ConfigMapName: &dbcsInst.Spec.OCIConfigMap,
SecretName: &dbcsInst.Spec.OCISecret,
Namespace: dbcsInst.GetNamespace(),
}
provider, err := oci.GetOCIProvider(r.KubeClient, authData)
if err != nil {
return ctrl.Result{}, err
}
r.dbClient, err = database.NewDatabaseClientWithConfigurationProvider(provider)
if err != nil {
return ctrl.Result{}, err
}
r.nwClient, err = core.NewVirtualNetworkClientWithConfigurationProvider(provider)
if err != nil {
return ctrl.Result{}, err
}
r.wrClient, err = workrequests.NewWorkRequestClientWithConfigurationProvider(provider)
r.Logger.Info("OCI provider configured succesfully")
/*
Using Finalizer for object deletion
*/
if dbcsInst.ObjectMeta.DeletionTimestamp.IsZero() {
// The object is not being deleted
if dbcsInst.Spec.HardLink && !finalizer.HasFinalizer(dbcsInst) {
finalizer.Register(r.KubeClient, dbcsInst)
r.Logger.Info("Finalizer registered successfully.")
} else if !dbcsInst.Spec.HardLink && finalizer.HasFinalizer(dbcsInst) {
finalizer.Unregister(r.KubeClient, dbcsInst)
r.Logger.Info("Finalizer unregistered successfully.")
}
} else {
// The object is being deleted
r.Logger.Info("Terminate DbcsSystem Database: " + dbcsInst.Spec.DbSystem.DisplayName)
if err := dbcsv1.DeleteDbcsSystemSystem(r.dbClient, *dbcsInst.Spec.Id); err != nil {
r.Logger.Error(err, "Fail to terminate DbcsSystem Instance")
// Change the status to Failed
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Terminate, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
// The reconciler should not requeue since the error returned from OCI during update will not be solved by requeue
return ctrl.Result{}, nil
}
finalizer.Unregister(r.KubeClient, dbcsInst)
r.Logger.Info("Finalizer unregistered successfully.")
// Stop reconciliation as the item is being deleted
return ctrl.Result{}, nil
}
/*
Determine whether it's a provision or bind operation
*/
lastSucSpec, err := dbcsInst.GetLastSuccessfulSpec()
if err != nil {
return ctrl.Result{}, err
}
if dbcsInst.Spec.Id == nil && lastSucSpec == nil {
// If no DbcsSystem ID specified, create a DB System
// ======================== Validate Specs ==============
err = dbcsv1.ValidateSpex(r.Logger, r.KubeClient, r.dbClient, dbcsInst, r.nwClient, r.Recorder)
if err != nil {
return ctrl.Result{}, err
}
r.Logger.Info("DbcsSystem DBSystem provisioning")
dbcsID, err := dbcsv1.CreateAndGetDbcsId(r.Logger, r.KubeClient, r.dbClient, dbcsInst, r.nwClient, r.wrClient)
if err != nil {
r.Logger.Error(err, "Fail to provision and get DbcsSystem System ID")
// Change the status to Failed
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Failed, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
// The reconciler should not requeue since the error returned from OCI during update will not be solved by requeue
return ctrl.Result{}, nil
}
assignDBCSID(dbcsInst, dbcsID)
if err := dbcsv1.UpdateDbcsSystemId(r.KubeClient, dbcsInst); err != nil {
// Change the status to Failed
assignDBCSID(dbcsInst, dbcsID)
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Failed, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
return ctrl.Result{}, err
}
r.Logger.Info("DbcsSystem system provisioned succesfully")
assignDBCSID(dbcsInst, dbcsID)
if err := dbcsInst.UpdateLastSuccessfulSpec(r.KubeClient); err != nil {
return ctrl.Result{}, err
}
assignDBCSID(dbcsInst, dbcsID)
} else {
if lastSucSpec == nil {
if err := dbcsv1.GetDbSystemId(r.Logger, r.dbClient, dbcsInst); err != nil {
// Change the status to Failed
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Failed, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
return ctrl.Result{}, err
}
if err := dbcsv1.SetDBCSDatabaseLifecycleState(r.Logger, r.KubeClient, r.dbClient, dbcsInst, r.nwClient, r.wrClient); err != nil {
// Change the status to required state
return ctrl.Result{}, err
}
dbcsInstId := *dbcsInst.Spec.Id
if err := dbcsv1.UpdateDbcsSystemId(r.KubeClient, dbcsInst); err != nil {
// Change the status to Failed
assignDBCSID(dbcsInst, dbcsInstId)
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Failed, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
return ctrl.Result{}, err
}
r.Logger.Info("Sync information from remote DbcsSystem System successfully")
dbcsInstId = *dbcsInst.Spec.Id
if err := dbcsInst.UpdateLastSuccessfulSpec(r.KubeClient); err != nil {
return ctrl.Result{}, err
}
assignDBCSID(dbcsInst, dbcsInstId)
} else {
if dbcsInst.Spec.Id == nil {
dbcsInst.Spec.Id = lastSucSpec.Id
}
if err := dbcsv1.UpdateDbcsSystemIdInst(r.Logger, r.dbClient, dbcsInst, r.KubeClient, r.nwClient, r.wrClient); err != nil {
r.Logger.Error(err, "Fail to update DbcsSystem Id")
// Change the status to Failed
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Failed, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
// The reconciler should not requeue since the error returned from OCI during update will not be solved by requeue
return ctrl.Result{}, nil
}
if err := dbcsv1.SetDBCSDatabaseLifecycleState(r.Logger, r.KubeClient, r.dbClient, dbcsInst, r.nwClient, r.wrClient); err != nil {
// Change the status to required state
return ctrl.Result{}, err
}
}
}
// Update the Wallet Secret when the secret name is given
//r.updateWalletSecret(dbcs)
// Update the last succesful spec
dbcsInstId := *dbcsInst.Spec.Id
if err := dbcsInst.UpdateLastSuccessfulSpec(r.KubeClient); err != nil {
return ctrl.Result{}, err
}
//assignDBCSID(dbcsInst,dbcsI)
// Change the phase to "Available"
assignDBCSID(dbcsInst, dbcsInstId)
if statusErr := dbcsv1.SetLifecycleState(r.KubeClient, r.dbClient, dbcsInst, databasev1alpha1.Available, r.nwClient, r.wrClient); statusErr != nil {
return ctrl.Result{}, statusErr
}
return ctrl.Result{}, nil
}
func assignDBCSID(dbcsInst *databasev1alpha1.DbcsSystem, dbcsID string) {
dbcsInst.Spec.Id = &dbcsID
}
func (r *DbcsSystemReconciler) eventFilterPredicate() predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
// Get the dbName as old dbName when an update event happens
oldObject := e.ObjectOld.DeepCopyObject().(*databasev1alpha1.DbcsSystem)
newObject := e.ObjectNew.DeepCopyObject().(*databasev1alpha1.DbcsSystem)
specObject := !reflect.DeepEqual(oldObject.Spec, newObject.Spec)
deletionTimeStamp := !reflect.DeepEqual(oldObject.GetDeletionTimestamp(), newObject.GetDeletionTimestamp())
if specObject || deletionTimeStamp {
return true
}
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
}
}
// SetupWithManager sets up the controller with the Manager.
func (r *DbcsSystemReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&databasev1alpha1.DbcsSystem{}).
WithEventFilter(r.eventFilterPredicate()).
WithOptions(controller.Options{MaxConcurrentReconciles: 50}).
Complete(r)
}