Skip to content

PTEUDO-2363: Implement AWS provider #407

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

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a175599
feat: add new providers package
bfabricio Feb 18, 2025
f935421
feat: add aws crossplane provider
bfabricio Feb 27, 2025
b943fd1
refactor: improve error handling
bfabricio Feb 27, 2025
fc52c08
Merge branch 'main' into PTEUDO-2363
bfabricio Feb 27, 2025
4fce343
fix: wrong import sorting
bfabricio Feb 27, 2025
168ca17
refactor: improve error handling
bfabricio Feb 27, 2025
2ee8efe
fix: add not foud handler for is resource ready
bfabricio Feb 28, 2025
343eb8f
test: unit test create rds postgres instance
bfabricio Mar 3, 2025
1036292
fix: add cloud provider to unit test config
bfabricio Mar 3, 2025
00865ee
Merge branch 'main' into PTEUDO-2363
bfabricio Mar 6, 2025
1e66982
fix: incorrect master secret key
bfabricio Mar 7, 2025
14cf7cf
refactor: increase reconsiler separation of concern
bfabricio Mar 10, 2025
340a859
fix: wrong paramenter group for postgres rds
bfabricio Mar 15, 2025
12d8890
refactor: embed k8s client into aws provider
bfabricio Mar 15, 2025
f1e6baf
feat: add ensureResource to reduce code complexity
bfabricio Mar 15, 2025
07e88eb
Merge branch 'main' into PTEUDO-2363
bfabricio Mar 17, 2025
57dc387
refactor: add feature toggle
bfabricio Mar 18, 2025
a9b97c5
fix: call to aurora instance creation
bfabricio Mar 18, 2025
4cb32cb
fix: aurora instance unit test
bfabricio Mar 18, 2025
8b6995b
fix: missing restore from on aurora cluster
bfabricio Mar 18, 2025
a18808e
test: add ensure resource test
bfabricio Mar 18, 2025
7173a3e
fix: add update tag to postgres instance
bfabricio Mar 19, 2025
67c0d5c
logs: add proper logging to ensure resource
bfabricio Mar 19, 2025
a99ed24
refactor: improve overall namming convention
bfabricio Mar 21, 2025
e715837
Merge branch 'main' into PTEUDO-2363
bfabricio Apr 15, 2025
94fda9e
refactor: disable new provider for deployments
bfabricio Apr 15, 2025
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
1 change: 1 addition & 0 deletions helm/db-controller/minikube.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
controllerConfig:
enableProvider: true
#AWS us-east-1 GCP us-east1
region: us-east-1
vpcSecurityGroupIDRefs: box-3
Expand Down
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, true)
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, true)
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
4 changes: 4 additions & 0 deletions pkg/basefunctions/basefunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,7 @@ func GetDynamicHostWaitTime(viperConfig *viper.Viper) time.Duration {
func GetDBIdentifierPrefix(viperConfig *viper.Viper) string {
return viperConfig.GetString("env")
}

func IsProviderEnable(viperConfig *viper.Viper) bool {
return viperConfig.GetBool("enableProvider")
}
52 changes: 48 additions & 4 deletions pkg/databaseclaim/databaseclaim.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"
"strings"
"time"

Expand Down Expand Up @@ -79,6 +80,7 @@ type DatabaseClaimReconciler struct {
Config *DatabaseClaimConfig
kctl *kctlutils.Client
statusManager *StatusManager
cloudProvider 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),
cloudProvider: providers.NewProvider(cfg.Viper, cli, cfg.Namespace),
}
}

Expand Down Expand Up @@ -187,7 +190,12 @@ func (r *DatabaseClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reques
//ignore delete request, continue to process rds migration
return r.executeDbClaimRequest(ctx, &reqInfo, &dbClaim)
}
if basefun.GetCloud(r.Config.Viper) == "aws" {
if basefun.IsProviderEnable(r.Config.Viper) {
spec := NewDatabaseSpecFromRequestInfo(&reqInfo, &dbClaim, r.getMode(ctx, &reqInfo, &dbClaim), r.Config.Viper)
if _, err := r.cloudProvider.DeleteDatabaseResources(ctx, spec); err != nil {
return ctrl.Result{}, err
}
} else 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 {
// if fail to delete the external dependency here, return with error
Expand Down Expand Up @@ -250,8 +258,40 @@ func (r *DatabaseClaimReconciler) createMetricsDeployment(ctx context.Context, d

func (r *DatabaseClaimReconciler) postMigrationInProgress(ctx context.Context, dbClaim *v1.DatabaseClaim) (ctrl.Result, error) {
logger := log.FromContext(ctx).WithValues("databaseclaim", dbClaim.Namespace+"/"+dbClaim.Name)
logger.Info("Post migration is in progress")
logger.Info("post migration is in progress")

if basefun.IsProviderEnable(r.Config.Viper) {
dbInstanceName := strings.Split(dbClaim.Status.OldDB.ConnectionInfo.Host, ".")[0]
deleted, err := r.cloudProvider.DeleteDatabaseResources(ctx, providers.DatabaseSpec{ResourceName: dbInstanceName})
if err != nil {
return ctrl.Result{}, err
}

if time.Since(dbClaim.Status.OldDB.PostMigrationActionStartedAt.Time).Minutes() > 10 {
_, err := r.cloudProvider.DeleteDatabaseResources(ctx, providers.DatabaseSpec{ResourceName: dbInstanceName, TagInactive: false})
if err != nil {
return ctrl.Result{}, err
}
dbClaim.Status.OldDB = v1.StatusForOldDB{}
}

if !deleted {
return ctrl.Result{RequeueAfter: time.Minute}, nil
}

if err := r.statusManager.ClearError(ctx, dbClaim); err != nil {
logger.Error(err, "Error updating DatabaseClaim status")
return ctrl.Result{}, err
}

if !dbClaim.ObjectMeta.DeletionTimestamp.IsZero() {
return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}

// TODO: after cloudProvider implementation is validated, below code can be deprecated
// get name of DBInstance from connectionInfo
dbInstanceName := strings.Split(dbClaim.Status.OldDB.ConnectionInfo.Host, ".")[0]
isAurora := dbClaim.Status.OldDB.Type == v1.AuroraPostgres
dbVersionPrefix := strings.Split(dbClaim.Status.OldDB.DBVersion, ".")[0]
Expand Down Expand Up @@ -515,7 +555,11 @@ func (r *DatabaseClaimReconciler) reconcileNewDB(ctx context.Context, reqInfo *r

isReady := false
var err error
if cloud == "aws" {
// TODO: Once the providers implementation is ready, we could completely remove this if cloud condition
if basefun.IsProviderEnable(r.Config.Viper) {
spec := NewDatabaseSpecFromRequestInfo(reqInfo, dbClaim, operationalMode, r.Config.Viper)
isReady, err = r.cloudProvider.CreateDatabase(ctx, spec)
} else if cloud == "aws" {
isReady, err = r.manageCloudHostAWS(ctx, reqInfo, dbClaim, operationalMode)
if err != nil {
logr.Error(err, "manage_cloud_host_AWS")
Expand Down Expand Up @@ -627,7 +671,7 @@ func (r *DatabaseClaimReconciler) providerCRAlreadyExists(ctx context.Context, r
case "gcp":
instance, cluster = &crossplanegcp.Instance{}, &crossplanegcp.Cluster{}
default:
return false, fmt.Errorf("unsupported cloud provider: %s", cloudProvider)
return false, fmt.Errorf("unsupported cloud cloudProvider: %s", cloudProvider)
}

exists := crExists(ctx, r.Client, dbHostIdentifier, cluster) && crExists(ctx, r.Client, dbHostIdentifier, instance)
Expand Down
4 changes: 2 additions & 2 deletions pkg/databaseclaim/databaseclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ func Test_providerCRAlreadyExists(t *testing.T) {
_, err := r.providerCRAlreadyExists(ctx, reqInfo, dbClaim)
if err == nil {
t.Errorf("expected an error but got nil")
} else if err.Error() != "unsupported cloud provider: anything" {
t.Errorf("expected error 'unsupported cloud provider: anything', got '%s'", err.Error())
} else if err.Error() != "unsupported cloud cloudProvider: anything" {
t.Errorf("expected error 'unsupported cloud cloudProvider: anything', got '%s'", err.Error())
}
})
}
45 changes: 45 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,47 @@ 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,
DatabaseName: ri.MasterConnInfo.DatabaseName,
DbType: ri.HostParams.Type,
Port: ri.HostParams.Port,
MinStorageGB: ri.HostParams.MinStorageGB,
MaxStorageGB: ri.HostParams.MaxStorageGB,
DBVersion: ri.HostParams.DBVersion,
MasterUsername: ri.HostParams.MasterUsername,
InstanceClass: ri.HostParams.InstanceClass,
StorageType: ri.HostParams.StorageType,
SkipFinalSnapshotBeforeDeletion: ri.HostParams.SkipFinalSnapshotBeforeDeletion,
PubliclyAccessible: ri.HostParams.PubliclyAccessible,
EnableIAMDatabaseAuthentication: ri.HostParams.EnableIAMDatabaseAuthentication,
DeletionPolicy: ri.HostParams.DeletionPolicy,
IsDefaultVersion: ri.HostParams.IsDefaultVersion,

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,
}
}
6 changes: 3 additions & 3 deletions pkg/providers/cloudnative_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
type CloudNativePGProvider struct {
}

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

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

func (p *CloudNativePGProvider) DeleteDatabase(ctx context.Context, spec DatabaseSpec) error {
Expand Down
Loading
Loading