Skip to content

Commit bb757c1

Browse files
committed
Merge branch 'master' into tinglwan-update-libs
2 parents 4e369ed + 90561b6 commit bb757c1

File tree

2 files changed

+87
-51
lines changed

2 files changed

+87
-51
lines changed

controllers/database/cdb_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const CDBFinalizer = "database.oracle.com/CDBfinalizer"
108108
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
109109
func (r *CDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
110110

111-
log := r.Log.WithValues("onpremdboperator", req.NamespacedName)
111+
log := r.Log.WithValues("multitenantoperator", req.NamespacedName)
112112
log.Info("Reconcile requested")
113113

114114
reconcilePeriod := r.Interval * time.Second
@@ -449,10 +449,10 @@ func (r *CDBReconciler) createPodSpec(cdb *dbapi.CDB) corev1.PodSpec {
449449
Name: "ORACLE_HOST",
450450
Value: cdb.Spec.DBServer,
451451
},
452-
{
453-
Name: "DBTNSURL",
454-
Value: cdb.Spec.DBTnsurl,
455-
},
452+
{
453+
Name: "DBTNSURL",
454+
Value: cdb.Spec.DBTnsurl,
455+
},
456456
{
457457
Name: "TLSCRT",
458458
Value: cdb.Spec.CDBTlsCrt.Secret.Key,

controllers/database/pdb_controller.go

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ var (
119119

120120
const PDBFinalizer = "database.oracle.com/PDBfinalizer"
121121

122+
var tdePassword string
123+
var tdeSecret string
124+
var floodcontrol bool = false
125+
122126
//+kubebuilder:rbac:groups=database.oracle.com,resources=pdbs,verbs=get;list;watch;create;update;patch;delete
123127
//+kubebuilder:rbac:groups=database.oracle.com,resources=pdbs/status,verbs=get;update;patch
124128
//+kubebuilder:rbac:groups=database.oracle.com,resources=pdbs/finalizers,verbs=get;create;update;patch;delete
@@ -133,7 +137,7 @@ const PDBFinalizer = "database.oracle.com/PDBfinalizer"
133137
// For more details, check Reconcile and its Result here:
134138
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
135139
func (r *PDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
136-
log := r.Log.WithValues("onpremdboperator", req.NamespacedName)
140+
log := r.Log.WithValues("multitenantoperator", req.NamespacedName)
137141
log.Info("Reconcile requested")
138142

139143
reconcilePeriod := r.Interval * time.Second
@@ -225,6 +229,8 @@ func (r *PDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
225229
err = r.getPDBState(ctx, req, pdb)
226230
case pdbPhaseMap:
227231
err = r.mapPDB(ctx, req, pdb)
232+
case pdbPhaseFail:
233+
err = r.mapPDB(ctx, req, pdb)
228234
default:
229235
log.Info("DEFAULT:", "Name", pdb.Name, "Phase", phase, "Status", strconv.FormatBool(pdb.Status.Status))
230236
return requeueN, nil
@@ -239,7 +245,7 @@ func (r *PDBReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
239245
}
240246

241247
log.Info("Reconcile completed")
242-
return requeueN, nil
248+
return requeueY, nil
243249
}
244250

245251
/*************************************************
@@ -444,11 +450,11 @@ func (r *PDBReconciler) callAPI(ctx context.Context, req ctrl.Request, pdb *dbap
444450
}
445451

446452
caCert := secret.Data[pdb.Spec.PDBTlsCat.Secret.Key]
447-
/*
448-
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSINFO", string(rsaKeyPEM))
449-
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSINFO", string(rsaCertPEM))
450-
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSINFO", string(caCert))
451-
*/
453+
/*
454+
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSINFO", string(rsaKeyPEM))
455+
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSINFO", string(rsaCertPEM))
456+
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSINFO", string(caCert))
457+
*/
452458

453459
certificate, err := tls.X509KeyPair([]byte(rsaCertPEM), []byte(rsaKeyPEM))
454460
if err != nil {
@@ -531,18 +537,28 @@ func (r *PDBReconciler) callAPI(ctx context.Context, req ctrl.Request, pdb *dbap
531537
if resp.StatusCode == 404 {
532538
pdb.Status.ConnString = ""
533539
pdb.Status.Msg = pdb.Spec.PDBName + " not found"
540+
534541
} else {
535-
pdb.Status.Msg = "ORDS Error - HTTP Status Code:" + strconv.Itoa(resp.StatusCode)
542+
if floodcontrol == false {
543+
pdb.Status.Msg = "ORDS Error - HTTP Status Code:" + strconv.Itoa(resp.StatusCode)
544+
}
545+
}
546+
547+
if floodcontrol == false {
548+
log.Info("ORDS Error - HTTP Status Code :"+strconv.Itoa(resp.StatusCode), "Err", string(bb))
536549
}
537-
log.Info("ORDS Error - HTTP Status Code :"+strconv.Itoa(resp.StatusCode), "Err", string(bb))
538550

539551
var apiErr ORDSError
540552
json.Unmarshal([]byte(bb), &apiErr)
541-
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSError", "Failed: %s", apiErr.Message)
553+
if floodcontrol == false {
554+
r.Recorder.Eventf(pdb, corev1.EventTypeWarning, "ORDSError", "Failed: %s", apiErr.Message)
555+
}
542556
//fmt.Printf("%+v", apiErr)
543557
//fmt.Println(string(bb))
558+
floodcontrol = true
544559
return "", errors.New("ORDS Error")
545560
}
561+
floodcontrol = false
546562

547563
defer resp.Body.Close()
548564

@@ -585,6 +601,8 @@ func (r *PDBReconciler) createPDB(ctx context.Context, req ctrl.Request, pdb *db
585601
log := r.Log.WithValues("createPDB", req.NamespacedName)
586602

587603
var err error
604+
var tdePassword string
605+
var tdeSecret string
588606

589607
cdb, err := r.getCDBResource(ctx, req, pdb)
590608
if err != nil {
@@ -613,14 +631,17 @@ func (r *PDBReconciler) createPDB(ctx context.Context, req ctrl.Request, pdb *db
613631
"getScript": strconv.FormatBool(*(pdb.Spec.GetScript))}
614632

615633
if *(pdb.Spec.TDEImport) {
616-
tdePassword, err := r.getSecret(ctx, req, pdb, pdb.Spec.TDEPassword.Secret.SecretName, pdb.Spec.TDEPassword.Secret.Key)
634+
tdePassword, err = r.getSecret(ctx, req, pdb, pdb.Spec.TDEPassword.Secret.SecretName, pdb.Spec.TDEPassword.Secret.Key)
617635
if err != nil {
618636
return err
619637
}
620-
tdeSecret, err := r.getSecret(ctx, req, pdb, pdb.Spec.TDESecret.Secret.SecretName, pdb.Spec.TDESecret.Secret.Key)
638+
tdeSecret, err = r.getSecret(ctx, req, pdb, pdb.Spec.TDESecret.Secret.SecretName, pdb.Spec.TDESecret.Secret.Key)
621639
if err != nil {
622640
return err
623641
}
642+
643+
tdeSecret = tdeSecret[:len(tdeSecret)-1]
644+
tdePassword = tdeSecret[:len(tdePassword)-1]
624645
values["tdePassword"] = tdePassword
625646
values["tdeKeystorePath"] = pdb.Spec.TDEKeystorePath
626647
values["tdeSecret"] = tdeSecret
@@ -636,17 +657,18 @@ func (r *PDBReconciler) createPDB(ctx context.Context, req ctrl.Request, pdb *db
636657
}
637658
_, err = r.callAPI(ctx, req, pdb, url, values, "POST")
638659
if err != nil {
660+
log.Error(err, "callAPI error", err.Error())
639661
return err
640662
}
641663

642664
r.Recorder.Eventf(pdb, corev1.EventTypeNormal, "Created", "PDB '%s' created successfully", pdb.Spec.PDBName)
643665

644-
if cdb.Spec.DBServer != "" {
645-
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
646-
} else {
647-
pdb.Status.ConnString = cdb.Spec.DBTnsurl
648-
}
649-
666+
if cdb.Spec.DBServer != "" {
667+
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
668+
} else {
669+
pdb.Status.ConnString = cdb.Spec.DBTnsurl
670+
}
671+
650672
log.Info("Created PDB Resource", "PDB Name", pdb.Spec.PDBName)
651673
r.getPDBState(ctx, req, pdb)
652674
return nil
@@ -700,12 +722,11 @@ func (r *PDBReconciler) clonePDB(ctx context.Context, req ctrl.Request, pdb *dba
700722

701723
r.Recorder.Eventf(pdb, corev1.EventTypeNormal, "Created", "PDB '%s' cloned successfully", pdb.Spec.PDBName)
702724

703-
if cdb.Spec.DBServer != "" {
704-
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
705-
} else {
706-
pdb.Status.ConnString = cdb.Spec.DBTnsurl
707-
}
708-
725+
if cdb.Spec.DBServer != "" {
726+
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
727+
} else {
728+
pdb.Status.ConnString = cdb.Spec.DBTnsurl
729+
}
709730

710731
log.Info("Cloned PDB successfully", "Source PDB Name", pdb.Spec.SrcPDBName, "Clone PDB Name", pdb.Spec.PDBName)
711732
r.getPDBState(ctx, req, pdb)
@@ -720,6 +741,8 @@ func (r *PDBReconciler) plugPDB(ctx context.Context, req ctrl.Request, pdb *dbap
720741
log := r.Log.WithValues("plugPDB", req.NamespacedName)
721742

722743
var err error
744+
var tdePassword string
745+
var tdeSecret string
723746

724747
cdb, err := r.getCDBResource(ctx, req, pdb)
725748
if err != nil {
@@ -742,14 +765,17 @@ func (r *PDBReconciler) plugPDB(ctx context.Context, req ctrl.Request, pdb *dbap
742765
"getScript": strconv.FormatBool(*(pdb.Spec.GetScript))}
743766

744767
if *(pdb.Spec.TDEImport) {
745-
tdePassword, err := r.getSecret(ctx, req, pdb, pdb.Spec.TDEPassword.Secret.SecretName, pdb.Spec.TDEPassword.Secret.Key)
768+
tdePassword, err = r.getSecret(ctx, req, pdb, pdb.Spec.TDEPassword.Secret.SecretName, pdb.Spec.TDEPassword.Secret.Key)
746769
if err != nil {
747770
return err
748771
}
749-
tdeSecret, err := r.getSecret(ctx, req, pdb, pdb.Spec.TDESecret.Secret.SecretName, pdb.Spec.TDESecret.Secret.Key)
772+
tdeSecret, err = r.getSecret(ctx, req, pdb, pdb.Spec.TDESecret.Secret.SecretName, pdb.Spec.TDESecret.Secret.Key)
750773
if err != nil {
751774
return err
752775
}
776+
777+
tdeSecret = tdeSecret[:len(tdeSecret)-1]
778+
tdePassword = tdeSecret[:len(tdePassword)-1]
753779
values["tdePassword"] = tdePassword
754780
values["tdeKeystorePath"] = pdb.Spec.TDEKeystorePath
755781
values["tdeSecret"] = tdeSecret
@@ -774,11 +800,11 @@ func (r *PDBReconciler) plugPDB(ctx context.Context, req ctrl.Request, pdb *dbap
774800

775801
r.Recorder.Eventf(pdb, corev1.EventTypeNormal, "Created", "PDB '%s' plugged successfully", pdb.Spec.PDBName)
776802

777-
if cdb.Spec.DBServer != "" {
778-
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
779-
} else {
780-
pdb.Status.ConnString = cdb.Spec.DBTnsurl
781-
}
803+
if cdb.Spec.DBServer != "" {
804+
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
805+
} else {
806+
pdb.Status.ConnString = cdb.Spec.DBTnsurl
807+
}
782808

783809
log.Info("Successfully plugged PDB", "PDB Name", pdb.Spec.PDBName)
784810
r.getPDBState(ctx, req, pdb)
@@ -793,6 +819,8 @@ func (r *PDBReconciler) unplugPDB(ctx context.Context, req ctrl.Request, pdb *db
793819
log := r.Log.WithValues("unplugPDB", req.NamespacedName)
794820

795821
var err error
822+
var tdePassword string
823+
var tdeSecret string
796824

797825
cdb, err := r.getCDBResource(ctx, req, pdb)
798826
if err != nil {
@@ -806,21 +834,25 @@ func (r *PDBReconciler) unplugPDB(ctx context.Context, req ctrl.Request, pdb *db
806834

807835
if *(pdb.Spec.TDEExport) {
808836
// Get the TDE Password
809-
tdePassword, err := r.getSecret(ctx, req, pdb, pdb.Spec.TDEPassword.Secret.SecretName, pdb.Spec.TDEPassword.Secret.Key)
837+
tdePassword, err = r.getSecret(ctx, req, pdb, pdb.Spec.TDEPassword.Secret.SecretName, pdb.Spec.TDEPassword.Secret.Key)
810838
if err != nil {
811839
return err
812840
}
813-
tdeSecret, err := r.getSecret(ctx, req, pdb, pdb.Spec.TDESecret.Secret.SecretName, pdb.Spec.TDESecret.Secret.Key)
841+
tdeSecret, err = r.getSecret(ctx, req, pdb, pdb.Spec.TDESecret.Secret.SecretName, pdb.Spec.TDESecret.Secret.Key)
814842
if err != nil {
815843
return err
816844
}
845+
846+
tdeSecret = tdeSecret[:len(tdeSecret)-1]
847+
tdePassword = tdeSecret[:len(tdePassword)-1]
817848
values["tdePassword"] = tdePassword
818849
values["tdeKeystorePath"] = pdb.Spec.TDEKeystorePath
819850
values["tdeSecret"] = tdeSecret
820851
values["tdeExport"] = strconv.FormatBool(*(pdb.Spec.TDEExport))
821852
}
822853

823854
url := "https://" + pdb.Spec.CDBResName + "-ords:" + strconv.Itoa(cdb.Spec.ORDSPort) + "/ords/_/db-api/latest/database/pdbs/" + pdb.Spec.PDBName + "/"
855+
log.Info("CallAPI(url)", "url", url)
824856

825857
pdb.Status.Phase = pdbPhaseUnplug
826858
pdb.Status.Msg = "Waiting for PDB to be unplugged"
@@ -865,6 +897,10 @@ func (r *PDBReconciler) modifyPDB(ctx context.Context, req ctrl.Request, pdb *db
865897

866898
err = r.getPDBState(ctx, req, pdb)
867899
if err != nil {
900+
if apierrors.IsNotFound(err) {
901+
log.Info("Warning PDB does not exist", "PDB Name", pdb.Spec.PDBName)
902+
return nil
903+
}
868904
return err
869905
}
870906

@@ -900,12 +936,11 @@ func (r *PDBReconciler) modifyPDB(ctx context.Context, req ctrl.Request, pdb *db
900936

901937
r.Recorder.Eventf(pdb, corev1.EventTypeNormal, "Modified", "PDB '%s' modified successfully", pdb.Spec.PDBName)
902938

903-
if cdb.Spec.DBServer != "" {
904-
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
905-
} else {
906-
pdb.Status.ConnString = cdb.Spec.DBTnsurl
907-
}
908-
939+
if cdb.Spec.DBServer != "" {
940+
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
941+
} else {
942+
pdb.Status.ConnString = cdb.Spec.DBTnsurl
943+
}
909944

910945
log.Info("Successfully modified PDB state", "PDB Name", pdb.Spec.PDBName)
911946
r.getPDBState(ctx, req, pdb)
@@ -938,6 +973,8 @@ func (r *PDBReconciler) getPDBState(ctx context.Context, req ctrl.Request, pdb *
938973

939974
if err != nil {
940975
pdb.Status.OpenMode = "UNKNOWN"
976+
pdb.Status.Msg = "CHECK PDB STATUS"
977+
pdb.Status.Status = false
941978
return err
942979
}
943980

@@ -993,12 +1030,11 @@ func (r *PDBReconciler) mapPDB(ctx context.Context, req ctrl.Request, pdb *dbapi
9931030
pdb.Status.OpenMode = objmap["open_mode"].(string)
9941031
pdb.Status.TotalSize = fmt.Sprintf("%.2f", totSizeInGB) + "G"
9951032

996-
if cdb.Spec.DBServer != "" {
997-
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
998-
} else {
999-
pdb.Status.ConnString = cdb.Spec.DBTnsurl
1000-
}
1001-
1033+
if cdb.Spec.DBServer != "" {
1034+
pdb.Status.ConnString = cdb.Spec.DBServer + ":" + strconv.Itoa(cdb.Spec.DBPort) + "/" + pdb.Spec.PDBName
1035+
} else {
1036+
pdb.Status.ConnString = cdb.Spec.DBTnsurl
1037+
}
10021038

10031039
log.Info("Successfully mapped PDB to Kubernetes resource", "PDB Name", pdb.Spec.PDBName)
10041040
return nil
@@ -1098,7 +1134,7 @@ func (r *PDBReconciler) deletePDBInstance(req ctrl.Request, ctx context.Context,
10981134
}
10991135

11001136
values := map[string]string{
1101-
"method": "DELETE",
1137+
"action": "KEEP",
11021138
"getScript": strconv.FormatBool(*(pdb.Spec.GetScript))}
11031139

11041140
if pdb.Spec.DropAction != "" {

0 commit comments

Comments
 (0)