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

PTEUDO-2363 #407

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
4 changes: 4 additions & 0 deletions internal/controller/databaseclaim_controller_tagging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ var _ = Describe("Tagging", Ordered, func() {
},
}
mockReconciler.Config.Viper.Set("dbMultiAZEnabled", true)
mockReconciler.Config.Viper.Set("cloud", "aws")
mockReconciler.Setup()

// providing names of non-existing resources below
Expand Down Expand Up @@ -221,6 +222,7 @@ var _ = Describe("Tagging", Ordered, func() {
},
}
mockReconciler.Config.Viper.Set("dbMultiAZEnabled", false)
mockReconciler.Config.Viper.Set("cloud", "aws")
mockReconciler.Setup()

check, err := mockReconciler.Reconciler().ManageOperationalTagging(context.Background(), logger, name, name)
Expand Down Expand Up @@ -278,6 +280,7 @@ var _ = Describe("Tagging", Ordered, func() {
},
}
mockReconciler.Config.Viper.Set("dbMultiAZEnabled", true)
mockReconciler.Config.Viper.Set("cloud", "aws")
mockReconciler.Setup()

check, err := mockReconciler.Reconciler().ManageOperationalTagging(context.Background(), logger, name, name)
Expand Down Expand Up @@ -318,6 +321,7 @@ var _ = Describe("Tagging", Ordered, func() {
},
}
mockReconciler.Config.Viper.Set("dbMultiAZEnabled", true)
mockReconciler.Config.Viper.Set("cloud", "aws")
mockReconciler.Setup()

By("adding tags beforehand to .status.AtProvier.TagList. As in reality, if tags gets successfully added. It will reflect at the said path")
Expand Down
60 changes: 20 additions & 40 deletions pkg/databaseclaim/databaseclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package databaseclaim
import (
"context"
"fmt"
crossplaneaws "github.com/crossplane-contrib/provider-aws/apis/rds/v1alpha1"
"github.com/infobloxopen/db-controller/pkg/providers"
crossplanegcp "github.com/upbound/provider-gcp/apis/alloydb/v1beta2"
"strings"
"time"

crossplaneaws "github.com/crossplane-contrib/provider-aws/apis/rds/v1alpha1"
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/go-logr/logr"
_ "github.com/lib/pq"
gopassword "github.com/sethvargo/go-password/password"
"github.com/spf13/viper"
crossplanegcp "github.com/upbound/provider-gcp/apis/alloydb/v1beta2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -79,6 +80,7 @@ type DatabaseClaimReconciler struct {
Config *DatabaseClaimConfig
kctl *kctlutils.Client
statusManager *StatusManager
provider providers.Provider
}

// New returns a configured databaseclaim reconciler
Expand All @@ -88,6 +90,7 @@ func New(cli client.Client, cfg *DatabaseClaimConfig) *DatabaseClaimReconciler {
Config: cfg,
kctl: kctlutils.New(cli, cfg.Viper.GetString("SERVICE_NAMESPACE")),
statusManager: NewStatusManager(cli, cfg.Viper),
provider: providers.NewProvider(cfg.Viper, cli, cfg.Namespace),
}
}

Expand Down Expand Up @@ -189,7 +192,8 @@ func (r *DatabaseClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
if basefun.GetCloud(r.Config.Viper) == "aws" {
// our finalizer is present, so lets handle any external dependency
if err := r.deleteExternalResourcesAWS(ctx, &reqInfo, &dbClaim); err != nil {
spec := NewDatabaseSpecFromRequestInfo(&reqInfo, &dbClaim, r.getMode(ctx, &reqInfo, &dbClaim), r.Config.Viper)
if _, err := r.provider.DeleteDatabase(ctx, spec); err != nil {
// if fail to delete the external dependency here, return with error
// so that it can be retried
return ctrl.Result{}, err
Expand Down Expand Up @@ -247,52 +251,26 @@ func (r *DatabaseClaimReconciler) createMetricsDeployment(ctx context.Context, d
}

func (r *DatabaseClaimReconciler) postMigrationInProgress(ctx context.Context, dbClaim *v1.DatabaseClaim) (ctrl.Result, error) {

logr := log.FromContext(ctx).WithValues("databaseclaim", dbClaim.Namespace+"/"+dbClaim.Name)

logr.Info("post migration is in progress")

// get name of DBInstance from connectionInfo
dbInstanceName := strings.Split(dbClaim.Status.OldDB.ConnectionInfo.Host, ".")[0]

var dbParamGroupName string
// get name of DBParamGroup from connectionInfo
if dbClaim.Status.OldDB.Type == v1.AuroraPostgres {
dbParamGroupName = dbInstanceName + "-a-" + (strings.Split(dbClaim.Status.OldDB.DBVersion, "."))[0]
} else {
dbParamGroupName = dbInstanceName + "-" + (strings.Split(dbClaim.Status.OldDB.DBVersion, "."))[0]
deleted, err := r.provider.DeleteDatabase(ctx, providers.DatabaseSpec{ResourceName: dbInstanceName})
if err != nil {
return ctrl.Result{}, err
}

TagsVerified, err := r.manageOperationalTagging(ctx, logr, dbInstanceName, dbParamGroupName)

// Even though we get error in updating tags, we log the error
// and go ahead with deleting resources
if err != nil || TagsVerified {

if time.Since(dbClaim.Status.OldDB.PostMigrationActionStartedAt.Time).Minutes() > 10 {
_, err := r.provider.DeleteDatabase(ctx, providers.DatabaseSpec{ResourceName: dbInstanceName, TagInactive: false})
if err != nil {
logr.Error(err, "Failed updating or verifying operational tags")
}

if err = r.deleteCloudDatabaseAWS(dbInstanceName, ctx); err != nil {
logr.Error(err, "Could not delete crossplane DBInstance/DBCLluster")
}
if err = r.deleteParameterGroupAWS(ctx, dbParamGroupName); err != nil {
logr.Error(err, "Could not delete crossplane DBParamGroup/DBClusterParamGroup")
return ctrl.Result{}, err
}

dbClaim.Status.OldDB = v1.StatusForOldDB{}
} else if time.Since(dbClaim.Status.OldDB.PostMigrationActionStartedAt.Time).Minutes() > 10 {
// Lets keep the state of old as it is for defined time to wait and verify tags before actually deleting resources
logr.Info("defined wait time is over to verify operational tags on AWS resources. Moving ahead to delete associated crossplane resources anyway")

if err = r.deleteCloudDatabaseAWS(dbInstanceName, ctx); err != nil {
logr.Error(err, "Could not delete crossplane DBInstance/DBCLluster")
}
if err = r.deleteParameterGroupAWS(ctx, dbParamGroupName); err != nil {
logr.Error(err, "Could not delete crossplane DBParamGroup/DBClusterParamGroup")
}
}

dbClaim.Status.OldDB = v1.StatusForOldDB{}
if !deleted {
return ctrl.Result{RequeueAfter: time.Minute}, nil
}

if err := r.statusManager.ClearError(ctx, dbClaim); err != nil {
Expand All @@ -303,7 +281,8 @@ func (r *DatabaseClaimReconciler) postMigrationInProgress(ctx context.Context, d
if !dbClaim.ObjectMeta.DeletionTimestamp.IsZero() {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{RequeueAfter: time.Minute}, nil

return ctrl.Result{}, err
}

// Create, migrate or upgrade database
Expand Down Expand Up @@ -514,7 +493,8 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context, reqInfo *r
isReady := false
var err error
if cloud == "aws" {
isReady, err = r.manageCloudHostAWS(ctx, reqInfo, dbClaim, operationalMode)
spec := NewDatabaseSpecFromRequestInfo(reqInfo, dbClaim, operationalMode, r.Config.Viper)
isReady, err = r.provider.CreateDatabase(ctx, spec)
if err != nil {
logr.Error(err, "manage_cloud_host_AWS")
return ctrl.Result{}, err
Expand Down
37 changes: 37 additions & 0 deletions pkg/databaseclaim/requestinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package databaseclaim
import (
"context"
"fmt"
"github.com/infobloxopen/db-controller/pkg/providers"

v1 "github.com/infobloxopen/db-controller/api/v1"
basefun "github.com/infobloxopen/db-controller/pkg/basefunctions"
Expand Down Expand Up @@ -94,3 +95,39 @@ func NewRequestInfo(ctx context.Context, cfg *viper.Viper, dbClaim *v1.DatabaseC

return ri, nil
}

func NewDatabaseSpecFromRequestInfo(ri *requestInfo, claim *v1.DatabaseClaim, mode ModeEnum, cfg *viper.Viper) providers.DatabaseSpec {
var snapshotID *string
if mode == M_UseNewDB && claim.Spec.RestoreFrom != "" {
snapshotID = &claim.Spec.RestoreFrom
}

var prefix string
suffix := "-" + ri.HostParams.Hash()

if basefun.GetDBIdentifierPrefix(cfg) != "" {
prefix = basefun.GetDBIdentifierPrefix(cfg) + "-"
}

return providers.DatabaseSpec{
ResourceName: prefix + claim.Name + suffix,
HostParams: ri.HostParams,
DbType: ri.DbType,
SharedDBHost: ri.SharedDBHost,
MasterConnInfo: ri.MasterConnInfo,
TempSecret: ri.TempSecret,
EnableReplicationRole: ri.EnableReplicationRole,
EnableSuperUser: ri.EnableSuperUser,
EnablePerfInsight: ri.EnablePerfInsight,
EnableCloudwatchLogsExport: ri.EnableCloudwatchLogsExport,
BackupRetentionDays: ri.BackupRetentionDays,
CACertificateIdentifier: &ri.CACertificateIdentifier,
Tags: providers.ConvertToProviderTags(claim.Spec.Tags, func(tag v1.Tag) (string, string) {
return tag.Key, tag.Value
}),
Labels: claim.Labels,
PreferredMaintenanceWindow: claim.Spec.PreferredMaintenanceWindow,
BackupPolicy: claim.Spec.BackupPolicy, // this is added as a TAG
SnapshotID: snapshotID,
}
}
26 changes: 26 additions & 0 deletions pkg/providers/cloudnative_pg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package providers

import (
"context"
"github.com/spf13/viper"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type CloudNativePGProvider struct {
}

func NewCloudNativePGProvider(k8sClient client.Client, config *viper.Viper) *CloudNativePGProvider {
return &CloudNativePGProvider{}
}

func (p *CloudNativePGProvider) CreateDatabase(ctx context.Context, spec DatabaseSpec) error {
return nil
}

func (p *CloudNativePGProvider) DeleteDatabase(ctx context.Context, spec DatabaseSpec) error {
return nil
}

func (p *CloudNativePGProvider) GetDatabase(ctx context.Context, name string) (*DatabaseSpec, error) {
return nil, nil
}
Loading