Skip to content

Commit

Permalink
adding auto-scalling feature to rds postgres instance
Browse files Browse the repository at this point in the history
  • Loading branch information
abodhekar committed Nov 20, 2023
1 parent 6c7c2ef commit 9d0a687
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 10 deletions.
43 changes: 38 additions & 5 deletions controllers/databaseclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ const (
masterSecretSuffix = "-master"
masterPasswordKey = "password"
ErrMaxNameLen = Error("dbclaim name is too long. max length is 44 characters")
ErrMaxStorageReduced = Error("reducing .spec.MaxStorageGB value is not allowed")
ErrMaxStorageLesser = Error(".spec.MaxStorageGB should always be greater or equel to spec.minStorageGB")
// InfoLevel is used to set V level to 0 as suggested by official docs
// https://github.com/kubernetes-sigs/controller-runtime/blob/main/TMP-LOGGING.md
InfoLevel = 0
Expand Down Expand Up @@ -243,6 +245,12 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim)
return M_UseNewDB
}

// checkIfMaxStorageReduced checks if the MaxStorageGB has been reduced or disabled compared to earlier state.
// This can be done by comparing it with MaxStorageGB in the status.
func (r *DatabaseClaimReconciler) checkIfMaxStorageReduced(dbClaim *persistancev1.DatabaseClaim) bool {
return r.Input.HostParams.MaxStorageGB < dbClaim.Status.ActiveDB.MaxStorageGB || r.Input.HostParams.MaxStorageGB < dbClaim.Status.NewDB.MaxStorageGB
}

func (r *DatabaseClaimReconciler) setReqInfo(dbClaim *persistancev1.DatabaseClaim) error {
logr := r.Log.WithValues("databaseclaim", dbClaim.Namespace+"/"+dbClaim.Name, "func", "setReqInfo")

Expand Down Expand Up @@ -328,6 +336,22 @@ func (r *DatabaseClaimReconciler) setReqInfo(dbClaim *persistancev1.DatabaseClai
r.Input.EnableReplicationRole = *dbClaim.Spec.EnableReplicationRole
}

if hostParams.Engine == defaultPostgresStr {
if r.checkIfMaxStorageReduced(dbClaim) {
// if MaxStorageGB was present in earlier state, and now is being reduced or removed. This Should throw an error
return ErrMaxStorageReduced
} else if r.Input.HostParams.MaxStorageGB == 0 {
// If MaxStorageGB is currently not present or zero, and it was not present earlier as well, then we assign MinStorageGB value to it
// marking autoStorageScalling as disabled
r.Input.HostParams.MaxStorageGB = int64(r.Input.HostParams.MinStorageGB)
} else if r.Input.HostParams.MaxStorageGB != 0 {
if r.Input.HostParams.MaxStorageGB < int64(r.Input.HostParams.MinStorageGB) {
// If MaxStorageGB is being provided, but it is lasser than minStorage, This should not be allowed.
return ErrMaxStorageLesser
}
}
}

logr.V(DebugLevel).Info("setup values of ", "DatabaseClaimReconciler", r)
return nil
}
Expand Down Expand Up @@ -363,6 +387,7 @@ func (r *DatabaseClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reques
if err := r.setReqInfo(&dbClaim); err != nil {
return r.manageError(ctx, &dbClaim, err)
}

// name of our custom finalizer
dbFinalizerName := "databaseclaims.persistance.atlas.infoblox.com/finalizer"

Expand Down Expand Up @@ -668,6 +693,9 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context,
dbClaim.Status.NewDB = *dbClaim.Status.ActiveDB.DeepCopy()
if dbClaim.Status.NewDB.MinStorageGB != r.Input.HostParams.MinStorageGB {
dbClaim.Status.NewDB.MinStorageGB = r.Input.HostParams.MinStorageGB
if r.Input.HostParams.Engine == defaultPostgresStr {
dbClaim.Status.NewDB.MaxStorageGB = r.Input.HostParams.MaxStorageGB
}
}
} else {
updateClusterStatus(&dbClaim.Status.NewDB, &r.Input.HostParams)
Expand Down Expand Up @@ -1877,11 +1905,12 @@ func (r *DatabaseClaimReconciler) managePostgresDBInstance(ctx context.Context,
EngineVersion: &params.EngineVersion,
},
// Items from Claim and fragmentKey
Engine: &params.Engine,
MultiAZ: &multiAZ,
DBInstanceClass: &params.Shape,
AllocatedStorage: &ms64,
Tags: DBClaimTags(dbClaim.Spec.Tags).DBTags(),
Engine: &params.Engine,
MultiAZ: &multiAZ,
DBInstanceClass: &params.Shape,
AllocatedStorage: &ms64,
MaxAllocatedStorage: &params.MaxStorageGB,
Tags: DBClaimTags(dbClaim.Spec.Tags).DBTags(),
// Items from Config
MasterUsername: &params.MasterUsername,
PubliclyAccessible: &params.PubliclyAccessible,
Expand Down Expand Up @@ -2392,6 +2421,7 @@ func (r *DatabaseClaimReconciler) updateDBInstance(ctx context.Context, dbClaim
params := &r.Input.HostParams
ms64 := int64(params.MinStorageGB)
dbInstance.Spec.ForProvider.AllocatedStorage = &ms64
dbInstance.Spec.ForProvider.MaxAllocatedStorage = &params.MaxStorageGB
dbInstance.Spec.ForProvider.EnableCloudwatchLogsExports = r.Input.EnableCloudwatchLogsExport
dbInstance.Spec.ForProvider.MultiAZ = &multiAZ
}
Expand Down Expand Up @@ -2683,6 +2713,9 @@ func updateClusterStatus(status *persistancev1.Status, hostParams *hostparams.Ho
status.Type = persistancev1.DatabaseType(hostParams.Engine)
status.Shape = hostParams.Shape
status.MinStorageGB = hostParams.MinStorageGB
if hostParams.Engine == defaultPostgresStr {
status.MaxStorageGB = hostParams.MaxStorageGB
}
}

func getServiceNamespace() (string, error) {
Expand Down
Loading

0 comments on commit 9d0a687

Please sign in to comment.