Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autoscale feature added #216

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 33 additions & 8 deletions controllers/databaseclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *DatabaseClaimReconciler) getMode(dbClaim *persistancev1.DatabaseClaim)

if dbClaim.Status.OldDB.DbState == persistancev1.PostMigrationInProgress {
if dbClaim.Status.OldDB.ConnectionInfo == nil || dbClaim.Status.ActiveDB.DbState != persistancev1.Ready ||
r.Input.SharedDBHost || *dbClaim.Spec.UseExistingSource || dbClaim.Spec.SourceDataFrom != nil {
r.Input.SharedDBHost {
return M_NotSupported
}
}
Expand Down Expand Up @@ -363,6 +363,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 @@ -444,7 +445,15 @@ func (r *DatabaseClaimReconciler) updateStatus(ctx context.Context, dbClaim *per
} else if !canTag {
logr.Info("Skipping post migration actions due to DB being used by other entities")
dbClaim.Status.OldDB = persistancev1.StatusForOldDB{}
return r.manageSuccess(ctx, dbClaim)
dbClaim.Status.Error = ""
if err = r.updateClientStatus(ctx, dbClaim); err != nil {
return r.manageError(ctx, dbClaim, err)
}
if !dbClaim.ObjectMeta.DeletionTimestamp.IsZero() {
return ctrl.Result{Requeue: true}, nil
} else {
return ctrl.Result{RequeueAfter: time.Minute}, nil
}
}

// get name of DBInstance from connectionInfo
Expand Down Expand Up @@ -490,7 +499,15 @@ func (r *DatabaseClaimReconciler) updateStatus(ctx context.Context, dbClaim *per
dbClaim.Status.OldDB = persistancev1.StatusForOldDB{}
}

return r.manageSuccess(ctx, dbClaim)
dbClaim.Status.Error = ""
if err = r.updateClientStatus(ctx, dbClaim); err != nil {
return r.manageError(ctx, dbClaim, err)
}
if !dbClaim.ObjectMeta.DeletionTimestamp.IsZero() {
return ctrl.Result{Requeue: true}, nil
} else {
return ctrl.Result{RequeueAfter: time.Minute}, nil
}

}
if r.Mode == M_UseExistingDB {
Expand Down Expand Up @@ -669,6 +686,9 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context,
if dbClaim.Status.NewDB.MinStorageGB != r.Input.HostParams.MinStorageGB {
dbClaim.Status.NewDB.MinStorageGB = r.Input.HostParams.MinStorageGB
}
if r.Input.HostParams.Engine == defaultPostgresStr && int(dbClaim.Status.NewDB.MaxStorageGB) != int(r.Input.HostParams.MaxStorageGB) {
dbClaim.Status.NewDB.MaxStorageGB = r.Input.HostParams.MaxStorageGB
}
} else {
updateClusterStatus(&dbClaim.Status.NewDB, &r.Input.HostParams)
}
Expand Down Expand Up @@ -1877,11 +1897,12 @@ func (r *DatabaseClaimReconciler) managePostgresDBInstance(ctx context.Context,
EngineVersion: &params.EngineVersion,
},
// Items from Claim and fragmentKey
Engine: &params.Engine,
MultiAZ: &multiAZ,
DBInstanceClass: &params.InstanceClass,
AllocatedStorage: &ms64,
Tags: DBClaimTags(dbClaim.Spec.Tags).DBTags(),
Engine: &params.Engine,
MultiAZ: &multiAZ,
DBInstanceClass: &params.InstanceClass,
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 +2413,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 @@ -2684,6 +2706,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
194 changes: 189 additions & 5 deletions controllers/databaseclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ func TestDatabaseClaimReconciler_getDynamicHostName(t *testing.T) {
})
}
}

func TestDatabaseClaimReconciler_setReqInfo(t *testing.T) {
opts := zap.Options{
Development: true,
Expand All @@ -1212,10 +1213,11 @@ func TestDatabaseClaimReconciler_setReqInfo(t *testing.T) {
dbClaim *persistancev1.DatabaseClaim
}
tests := []struct {
name string
fields fields
args args
want error
name string
fields fields
args args
want error
wantMaxStorageGB int
}{
{
"OK",
Expand All @@ -1239,6 +1241,7 @@ func TestDatabaseClaimReconciler_setReqInfo(t *testing.T) {
},
},
nil,
20,
},
{
"Dbname too long",
Expand All @@ -1262,6 +1265,183 @@ func TestDatabaseClaimReconciler_setReqInfo(t *testing.T) {
},
},
ErrMaxNameLen,
20,
},
{
"MaxStorageGB reduced",
fields{
Config: NewConfig(multiConfig),
Log: zap.New(zap.UseFlagOptions(&opts)),
DbIdentifierPrefix: "boxing-x",
Input: &input{FragmentKey: "",
HostParams: hostparams.HostParams{Engine: "postgres",
EngineVersion: "12.11",
Shape: "db.t4g.medium",
MinStorageGB: 20}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse,
MaxStorageGB: 60},
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{
MaxStorageGB: 100,
},
},
},
},
hostparams.ErrMaxStorageReduced,
20,
},
{
"MaxStorageGB removed",
fields{
Config: NewConfig(multiConfig),
Log: zap.New(zap.UseFlagOptions(&opts)),
DbIdentifierPrefix: "boxing-x",
Input: &input{FragmentKey: "",
HostParams: hostparams.HostParams{Engine: "postgres",
EngineVersion: "12.11",
Shape: "db.t4g.medium",
MinStorageGB: 20}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse,
},
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{
MaxStorageGB: 100,
},
},
},
},
hostparams.ErrMaxStorageReduced,
100,
},
{
"ok, MaxStorageGB stays same",
fields{
Config: NewConfig(multiConfig),
Log: zap.New(zap.UseFlagOptions(&opts)),
DbIdentifierPrefix: "boxing-x",
Input: &input{FragmentKey: "",
HostParams: hostparams.HostParams{Engine: "postgres",
EngineVersion: "12.11",
Shape: "db.t4g.medium",
MinStorageGB: 20}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse,
MaxStorageGB: 60},
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{
MaxStorageGB: 60,
},
},
},
},
nil,
60,
},
{
"ok, MaxStorageGB increased ",
fields{
Config: NewConfig(multiConfig),
Log: zap.New(zap.UseFlagOptions(&opts)),
DbIdentifierPrefix: "boxing-x",
Input: &input{FragmentKey: "",
HostParams: hostparams.HostParams{Engine: "postgres",
EngineVersion: "12.11",
Shape: "db.t4g.medium",
MinStorageGB: 20}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse,
MaxStorageGB: 100},
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{
MaxStorageGB: 60,
},
},
},
},
nil,
100,
},
{
"MaxStorageGB lesser",
fields{
Config: NewConfig(multiConfig),
Log: zap.New(zap.UseFlagOptions(&opts)),
DbIdentifierPrefix: "boxing-x",
Input: &input{FragmentKey: "",
HostParams: hostparams.HostParams{Engine: "postgres",
EngineVersion: "12.11",
Shape: "db.t4g.medium",
}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse,
MaxStorageGB: 10},
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{},
},
},
},
hostparams.ErrMaxStorageLesser,
10,
},
{
"ok",
fields{
Config: NewConfig(multiConfig),
Log: zap.New(zap.UseFlagOptions(&opts)),
DbIdentifierPrefix: "boxing-x",
Input: &input{FragmentKey: "",
HostParams: hostparams.HostParams{Engine: "postgres",
EngineVersion: "12.11",
Shape: "db.t4g.medium",
}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse,
},
Status: persistancev1.DatabaseClaimStatus{
ActiveDB: persistancev1.Status{},
},
},
},
nil,
20,
},
}
for _, tt := range tests {
Expand All @@ -1276,9 +1456,13 @@ func TestDatabaseClaimReconciler_setReqInfo(t *testing.T) {
Mode: tt.fields.Mode,
Input: tt.fields.Input,
}
if got := r.setReqInfo(tt.args.dbClaim); got != tt.want {
got := r.setReqInfo(tt.args.dbClaim)
if got != tt.want {
t.Errorf("DatabaseClaimReconciler.setReqInfo() = %v, want %v", got, tt.want)
}
if got == nil && tt.wantMaxStorageGB != int(r.Input.HostParams.MaxStorageGB) {
t.Errorf("hostParams.MaxStorageGB = %v, want %v ", r.Input.HostParams.MaxStorageGB, tt.wantMaxStorageGB)
}
})
}
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/hostparams/hostparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ import (
"github.com/spf13/viper"
)

type Error string

const (
defaultAuroraPostgresStr = "aurora-postgresql"
defaultPostgresStr = "postgres"
shapeDelimiter = "!"
INSTANCE_CLASS_INDEX = 0
STORAGE_TYPE_INDEX = 1
ErrMaxStorageReduced = Error("reducing .spec.MaxStorageGB value is not allowed")
ErrMaxStorageLesser = Error(".spec.MaxStorageGB should always be greater or equel to spec.minStorageGB")
)

type HostParams struct {
Engine string
Shape string
MinStorageGB int
MaxStorageGB int64
EngineVersion string
MasterUsername string
InstanceClass string
Expand All @@ -40,6 +45,8 @@ type HostParams struct {
isDefaultVersion bool
}

func (e Error) Error() string { return string(e) }

func (p *HostParams) String() string {
return fmt.Sprintf("%s-%s-%s", p.Engine, p.InstanceClass, p.EngineVersion)
}
Expand Down Expand Up @@ -112,6 +119,7 @@ func New(config *viper.Viper, fragmentKey string, dbClaim *persistancev1.Databas
hostParams.Shape = dbClaim.Spec.Shape
hostParams.MinStorageGB = dbClaim.Spec.MinStorageGB
port = dbClaim.Spec.Port
hostParams.MaxStorageGB = dbClaim.Spec.MaxStorageGB
} else {
hostParams.MasterUsername = config.GetString(fmt.Sprintf("%s::masterUsername", fragmentKey))
hostParams.Engine = config.GetString(fmt.Sprintf("%s::Engine", fragmentKey))
Expand Down Expand Up @@ -172,6 +180,21 @@ func New(config *viper.Viper, fragmentKey string, dbClaim *persistancev1.Databas
return &HostParams{}, err
}

if hostParams.Engine == defaultPostgresStr {

if hostParams.MaxStorageGB == 0 {
hostParams.MaxStorageGB = int64(hostParams.MinStorageGB)
}

if hostParams.MaxStorageGB < int64(hostParams.MinStorageGB) {
return &HostParams{}, ErrMaxStorageLesser
}

if hostParams.MaxStorageGB < dbClaim.Status.ActiveDB.MaxStorageGB && dbClaim.Status.ActiveDB.MaxStorageGB != 0 {
return &HostParams{}, ErrMaxStorageReduced
}
}

return &hostParams, nil
}

Expand Down
Loading
Loading