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

remove storage update from rds upgrade workflow #169

Merged
merged 8 commits into from
Aug 18, 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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager cmd/manager/mai

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM alpine:3.17
RUN apk --update add postgresql14-client=14.9-r0 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.17/main
FROM alpine:3.18
RUN apk --update add postgresql15-client=15.4-r0 --repository=http://dl-cdn.alpinelinux.org/alpine/v3.18/main
WORKDIR /
COPY --from=builder /workspace/manager .
COPY --from=builder /workspace/config/ /config/
Expand Down
67 changes: 34 additions & 33 deletions controllers/databaseclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@ import (
"github.com/infobloxopen/db-controller/pkg/rdsauth"
)

type Error string

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

const (
defaultPassLen = 32
defaultNumDig = 10
defaultNumSimb = 10
// rotation time in minutes
minRotationTime = 60
defaultPassLen = 32
defaultNumDig = 10
defaultNumSimb = 10
minRotationTime = 60 // rotation time in minutes
maxRotationTime = 1440
maxWaitTime = 10
maxNameLen = 44 // max length of dbclaim name
defaultRotationTime = minRotationTime
serviceNamespaceEnvVar = "SERVICE_NAMESPACE"
defaultPostgresStr = "postgres"
Expand All @@ -69,6 +73,7 @@ const (
tempSourceDsn = "sourceDsn"
masterSecretSuffix = "-master"
masterPasswordKey = "password"
ErrMaxNameLen = Error("dbclaim name is too long. max length is 44 characters")
)

type ModeEnum int
Expand Down Expand Up @@ -240,6 +245,11 @@ func (r *DatabaseClaimReconciler) setReqInfo(dbClaim *persistancev1.DatabaseClai
DbType: string(dbClaim.Spec.Type), HostParams: *hostParams,
}
if manageCloudDB {
//check if dbclaim.name is > maxNameLen and if so, error out
if len(dbClaim.Name) > maxNameLen {
return ErrMaxNameLen
}

r.Input.DbHostIdentifier = r.getDynamicHostName(dbClaim)
}
if r.Config.GetBool("supportSuperUserElevation") {
Expand Down Expand Up @@ -508,6 +518,9 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context,

if r.Input.MasterConnInfo.Host == dbClaim.Status.ActiveDB.ConnectionInfo.Host {
dbClaim.Status.NewDB = *dbClaim.Status.ActiveDB.DeepCopy()
if dbClaim.Status.NewDB.MinStorageGB != r.Input.HostParams.MinStorageGB {
dbClaim.Status.NewDB.MinStorageGB = r.Input.HostParams.MinStorageGB
}
} else {
updateClusterStatus(&dbClaim.Status.NewDB, &r.Input.HostParams)
}
Expand Down Expand Up @@ -1034,13 +1047,12 @@ func (r *DatabaseClaimReconciler) readResourceSecret(ctx context.Context, secret
}

func (r *DatabaseClaimReconciler) getDynamicHostName(dbClaim *persistancev1.DatabaseClaim) string {
prefix := "dbc-"
var prefix string
suffix := "-" + r.Input.HostParams.Hash()

if r.DbIdentifierPrefix != "" {
prefix = prefix + r.DbIdentifierPrefix + "-"
prefix = r.DbIdentifierPrefix + "-"
}

if r.Input.FragmentKey == "" {
return prefix + dbClaim.Name + suffix
}
Expand Down Expand Up @@ -1405,8 +1417,7 @@ func (r *DatabaseClaimReconciler) managePostgresDBInstance(ctx context.Context,
params := &r.Input.HostParams
ms64 := int64(params.MinStorageGB)
multiAZ := r.getMultiAZEnabled()
perfIns := true
encryptStrg := true
trueVal := true
storageType := r.Config.GetString("storageType")

dbClaim.Spec.Tags = r.configureBackupPolicy(dbClaim.Spec.BackupPolicy, dbClaim.Spec.Tags)
Expand All @@ -1426,6 +1437,7 @@ func (r *DatabaseClaimReconciler) managePostgresDBInstance(ctx context.Context,
ForProvider: crossplanerds.DBInstanceParameters{
Region: region,
CustomDBInstanceParameters: crossplanerds.CustomDBInstanceParameters{
ApplyImmediately: &trueVal,
SkipFinalSnapshot: params.SkipFinalSnapshotBeforeDeletion,
VPCSecurityGroupIDRefs: []xpv1.Reference{
{Name: r.getVpcSecurityGroupIDRefs()},
Expand All @@ -1450,8 +1462,8 @@ func (r *DatabaseClaimReconciler) managePostgresDBInstance(ctx context.Context,
MasterUsername: &params.MasterUsername,
PubliclyAccessible: &params.PubliclyAccessible,
EnableIAMDatabaseAuthentication: &params.EnableIAMDatabaseAuthentication,
EnablePerformanceInsights: &perfIns,
StorageEncrypted: &encryptStrg,
EnablePerformanceInsights: &trueVal,
StorageEncrypted: &trueVal,
StorageType: &storageType,
Port: &params.Port,
},
Expand Down Expand Up @@ -1520,7 +1532,7 @@ func (r *DatabaseClaimReconciler) manageAuroraDBInstance(ctx context.Context, db
dbInstance := &crossplanerds.DBInstance{}

params := &r.Input.HostParams
perfIns := true
trueVal := true

dbClaim.Spec.Tags = r.configureBackupPolicy(dbClaim.Spec.BackupPolicy, dbClaim.Spec.Tags)

Expand All @@ -1540,6 +1552,7 @@ func (r *DatabaseClaimReconciler) manageAuroraDBInstance(ctx context.Context, db
ForProvider: crossplanerds.DBInstanceParameters{
Region: region,
CustomDBInstanceParameters: crossplanerds.CustomDBInstanceParameters{
ApplyImmediately: &trueVal,
SkipFinalSnapshot: params.SkipFinalSnapshotBeforeDeletion,
EngineVersion: &params.EngineVersion,
},
Expand All @@ -1551,7 +1564,7 @@ func (r *DatabaseClaimReconciler) manageAuroraDBInstance(ctx context.Context, db
// Items from Config
PubliclyAccessible: &params.PubliclyAccessible,
DBClusterIdentifier: &dbClusterIdentifier,
EnablePerformanceInsights: &perfIns,
EnablePerformanceInsights: &trueVal,
},
ResourceSpec: xpv1.ResourceSpec{
ProviderConfigReference: &providerConfigReference,
Expand Down Expand Up @@ -1948,31 +1961,19 @@ func (r *DatabaseClaimReconciler) updateDBInstance(ctx context.Context, dbClaim
// Update DBInstance
dbClaim.Spec.Tags = r.configureBackupPolicy(dbClaim.Spec.BackupPolicy, dbClaim.Spec.Tags)
dbInstance.Spec.ForProvider.Tags = DBClaimTags(dbClaim.Spec.Tags).DBTags()

// TODO:currently ignoring changes to shape and minStorage if a CR already exists.
/*
params := r.getDynamicHostParams(ctx, fragmentKey, dbClaim)

// Update Shape and MinGibStorage for now
if rdsInstance.Spec.ForProvider.DBInstanceClass != params.Shape {
rdsInstance.Spec.ForProvider.DBInstanceClass = params.Shape
update = true
}

if *rdsInstance.Spec.ForProvider.AllocatedStorage != params.MinStorageGB {
rdsInstance.Spec.ForProvider.AllocatedStorage = &params.MinStorageGB
update = true
}
*/

if dbClaim.Spec.Type == defaultPostgresStr {
params := &r.Input.HostParams
ms64 := int64(params.MinStorageGB)
dbInstance.Spec.ForProvider.AllocatedStorage = &ms64
}
// Compute a json patch based on the changed DBInstance
dbInstancePatchData, err := patchDBInstance.Data(dbInstance)
if err != nil {
return false, err
}
// an empty json patch will be {}, we can assert that no update is required if len == 2
// we could also just apply the empty patch if additional call to apiserver isn't an issue
if len(dbInstancePatchData) == 2 {
if len(dbInstancePatchData) <= 2 {
return false, nil
}
r.Log.Info("updating crossplane DBInstance resource", "DBInstance", dbInstance.Name)
Expand Down Expand Up @@ -2001,7 +2002,7 @@ func (r *DatabaseClaimReconciler) updateDBCluster(ctx context.Context, dbClaim *
}
// an empty json patch will be {}, we can assert that no update is required if len == 2
// we could also just apply the empty patch if additional call to apiserver isn't an issue
if len(dbClusterPatchData) == 2 {
if len(dbClusterPatchData) <= 2 {
return false, nil
}
r.Log.Info("updating crossplane DBCluster resource", "DBCluster", dbCluster.Name)
Expand Down
93 changes: 91 additions & 2 deletions controllers/databaseclaim_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func TestDatabaseClaimReconciler_getDynamicHostName(t *testing.T) {
},
},
},
"dbc-boxing-x-identity-dbclaim-name-1620c8de",
"boxing-x-identity-dbclaim-name-d391a72a",
},
{
"OK",
Expand All @@ -1170,7 +1170,7 @@ func TestDatabaseClaimReconciler_getDynamicHostName(t *testing.T) {
},
},
},
"dbc-boxing-x-athena-362c4cd6",
"boxing-x-athena-362c4cd6",
},
}
for _, tt := range tests {
Expand All @@ -1191,6 +1191,95 @@ func TestDatabaseClaimReconciler_getDynamicHostName(t *testing.T) {
})
}
}
func TestDatabaseClaimReconciler_setReqInfo(t *testing.T) {
opts := zap.Options{
Development: true,
}
flse := false
type fields struct {
Client client.Client
Log logr.Logger
Scheme *runtime.Scheme
Config *viper.Viper
MasterAuth *rdsauth.MasterAuth
DbIdentifierPrefix string
Mode ModeEnum
Input *input
}
type args struct {
dbClaim *persistancev1.DatabaseClaim
}
tests := []struct {
name string
fields fields
args args
want error
}{
{
"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",
MinStorageGB: 20}},
},
args{
dbClaim: &persistancev1.DatabaseClaim{
ObjectMeta: v1.ObjectMeta{Name: "44characterlengthname23456789012345678901234"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse},
},
},
nil,
},
{
"Dbname too long",
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: "nametooooooooooooooooooloooooooooooooooooooong"},
Spec: persistancev1.DatabaseClaimSpec{
AppID: "identity",
DatabaseName: "identity",
EnableReplicationRole: &flse},
},
},
ErrMaxNameLen,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &DatabaseClaimReconciler{
Client: tt.fields.Client,
Log: tt.fields.Log,
Scheme: tt.fields.Scheme,
Config: tt.fields.Config,
MasterAuth: tt.fields.MasterAuth,
DbIdentifierPrefix: tt.fields.DbIdentifierPrefix,
Mode: tt.fields.Mode,
Input: tt.fields.Input,
}
if got := r.setReqInfo(tt.args.dbClaim); got != tt.want {
t.Errorf("DatabaseClaimReconciler.setReqInfo() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatabaseClaimReconciler_getMode(t *testing.T) {
tru := true
Expand Down
8 changes: 4 additions & 4 deletions helm/db-controller/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ webhook:
resources:
limits:
cpu: 100m
memory: 128Mi
memory: 512Mi
requests:
cpu: 100m
memory: 128Mi
memory: 256Mi

autoscaling:
enabled: false
Expand Down Expand Up @@ -121,9 +121,9 @@ controllerConfig:
dbSubnetGroupNameRef:
dynamicHostWaitTimeMin: 1
defaultShape: db.t4g.medium
defaultMinStorageGB: 20
defaultMinStorageGB: 50
defaultEngine: postgres
defaultEngineVersion: 13.8
defaultEngineVersion: 15.3
defaultMasterPort: 5432
defaultSslMode: require
defaultMasterUsername: root
Expand Down
8 changes: 1 addition & 7 deletions pkg/hostparams/hostparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ type HostParams struct {
}

func (p *HostParams) String() string {
if p.Engine == defaultAuroraPostgresStr {
//storage does not apply to aurora db.
return fmt.Sprintf("%s-%s-%s", p.Engine, p.Shape, p.EngineVersion)
} else {
return fmt.Sprintf("%s-%s-%s-%s", p.Engine, p.Shape, p.EngineVersion, strconv.Itoa(p.MinStorageGB))
}
return fmt.Sprintf("%s-%s-%s", p.Engine, p.Shape, p.EngineVersion)
}

func (p *HostParams) Hash() string {
Expand Down Expand Up @@ -83,7 +78,6 @@ func (p *HostParams) HasVersionChanged(activeVersion string) bool {
func (p *HostParams) IsUpgradeRequested(np *HostParams) bool {
return p.HasEngineChanged(np.Engine) ||
p.HasShapeChanged(np.Shape) ||
p.HasStorageChanged(np.MinStorageGB) ||
p.HasVersionChanged(np.EngineVersion)

}
Expand Down
10 changes: 5 additions & 5 deletions pkg/hostparams/hostparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ func TestHostParams_Hash(t *testing.T) {
fields fields
want string
}{
{name: "test_Execute_ok", want: "1620c8de",
{name: "test_Execute_ok", want: "d391a72a",
fields: fields{Engine: "postgres",
Shape: "db.t4g.medium",
MinStorageGB: 20,
EngineVersion: "12.11",
},
},
{name: "test_Execute_engine_change", want: "9993e156",
{name: "test_Execute_engine_change", want: "0cd64830",
fields: fields{Engine: "postgres",
Shape: "db.t4g.medium",
MinStorageGB: 20,
EngineVersion: "13.11",
},
},
{name: "test_Execute_shape_change", want: "1e67c367",
{name: "test_Execute_shape_change", want: "ef75846d",
fields: fields{Engine: "postgres",
Shape: "db.t2.medium",
MinStorageGB: 20,
EngineVersion: "12.11",
},
},
{name: "test_Execute_storage_change", want: "8fbac8dc",
{name: "test_Execute_storage_change", want: "d391a72a",
fields: fields{Engine: "postgres",
Shape: "db.t4g.medium",
MinStorageGB: 21,
Expand Down Expand Up @@ -201,7 +201,7 @@ func TestHostParams_IsUpgradeRequested(t *testing.T) {
},
},
},
{name: "storage-different-postgres", want: true,
{name: "storage-different-postgres", want: false,
fields: fields{Engine: "postgres",
Shape: "db.t4g.medium",
MinStorageGB: 20,
Expand Down
Loading