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

ROX-18427: read central DB ID override from optional ConfigMap #1198

Merged
merged 5 commits into from
Sep 12, 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
52 changes: 46 additions & 6 deletions fleetshard/pkg/central/reconciler/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ const (
dbUserTypeCentral = "central"
dbCentralUserName = "rhacs_central"

centralDbSecretName = "central-db-password" // pragma: allowlist secret
centralDeletePollInterval = 5 * time.Second
centralDbSecretName = "central-db-password" // pragma: allowlist secret
centralDbOverrideConfigMap = "central-db-override"
centralDeletePollInterval = 5 * time.Second

sensibleDeclarativeConfigSecretName = "cloud-service-sensible-declarative-configs" // pragma: allowlist secret
manualDeclarativeConfigSecretName = "cloud-service-manual-declarative-configs" // pragma: allowlist secret
Expand Down Expand Up @@ -991,7 +992,12 @@ func (r *CentralReconciler) ensureCentralDeleted(ctx context.Context, remoteCent
// skip Snapshot for remoteCentral created by probe
skipSnapshot := remoteCentral.Metadata.Internal

err = r.managedDBProvisioningClient.EnsureDBDeprovisioned(remoteCentral.Id, skipSnapshot)
databaseID, err := r.getDatabaseID(ctx, remoteCentral.Metadata.Namespace, remoteCentral.Id)
if err != nil {
return false, fmt.Errorf("getting DB ID: %w", err)
}

err = r.managedDBProvisioningClient.EnsureDBDeprovisioned(databaseID, skipSnapshot)
if err != nil {
if errors.Is(err, cloudprovider.ErrDBBackupInProgress) {
glog.Infof("Can not delete Central DB for: %s, backup in progress", remoteCentral.Metadata.Namespace)
Expand Down Expand Up @@ -1022,6 +1028,29 @@ func (r *CentralReconciler) ensureCentralDeleted(ctx context.Context, remoteCent
return globalDeleted, nil
}

johannes94 marked this conversation as resolved.
Show resolved Hide resolved
// getDatabaseID returns the cloud database ID for a central tenant.
// By default the database ID is equal to the centralID. It can be overridden by a ConfigMap.
func (r *CentralReconciler) getDatabaseID(ctx context.Context, remoteCentralNamespace, centralID string) (string, error) {
configMap := &corev1.ConfigMap{}
err := r.client.Get(ctx, ctrlClient.ObjectKey{Namespace: remoteCentralNamespace, Name: centralDbOverrideConfigMap}, configMap)
if err != nil {
if apiErrors.IsNotFound(err) {
return centralID, nil
}

return centralID, fmt.Errorf("getting central DB ID override ConfigMap: %w", err)
}

overrideValue, exists := configMap.Data["databaseID"]
if exists {
glog.Infof("The database ID for Central %s is overridden with: %s", centralID, overrideValue)
return overrideValue, nil
}

glog.Infof("The %s ConfigMap exists but contains no databaseID field, using default: %s", centralDbOverrideConfigMap, centralID)
return centralID, nil
}

// centralChanged compares the given central to the last central reconciled using a hash
func (r *CentralReconciler) centralChanged(central private.ManagedCentral) (bool, error) {
currentHash, err := util.MD5SumFromJSONStruct(&central)
Expand Down Expand Up @@ -1110,7 +1139,12 @@ func (r *CentralReconciler) getCentralDBConnectionString(ctx context.Context, re
}
}

dbConnection, err := r.managedDBProvisioningClient.GetDBConnection(remoteCentral.Id)
databaseID, err := r.getDatabaseID(ctx, remoteCentral.Metadata.Namespace, remoteCentral.Id)
if err != nil {
return "", fmt.Errorf("getting DB ID: %w", err)
}

dbConnection, err := r.managedDBProvisioningClient.GetDBConnection(databaseID)
if err != nil {
if !errors.Is(err, cloudprovider.ErrDBNotFound) {
return "", fmt.Errorf("getting RDS DB connection data: %w", err)
Expand All @@ -1123,6 +1157,7 @@ func (r *CentralReconciler) getCentralDBConnectionString(ctx context.Context, re
return "", fmt.Errorf("trying to restore DB: %w", err)
}
}

return dbConnection.GetConnectionForUserAndDB(dbCentralUserName, postgres.CentralDBName).WithSSLRootCert(postgres.DatabaseCACertificatePathCentral).AsConnectionString(), nil
}

Expand Down Expand Up @@ -1158,12 +1193,17 @@ func (r *CentralReconciler) ensureManagedCentralDBInitialized(ctx context.Contex
return fmt.Errorf("getting DB password from secret: %w", err)
}

err = r.managedDBProvisioningClient.EnsureDBProvisioned(ctx, remoteCentral.Id, remoteCentral.Id, dbMasterPassword, remoteCentral.Metadata.Internal)
databaseID, err := r.getDatabaseID(ctx, remoteCentralNamespace, remoteCentral.Id)
if err != nil {
return fmt.Errorf("getting DB ID: %w", err)
}

err = r.managedDBProvisioningClient.EnsureDBProvisioned(ctx, databaseID, remoteCentral.Id, dbMasterPassword, remoteCentral.Metadata.Internal)
if err != nil {
return fmt.Errorf("provisioning RDS DB: %w", err)
}

dbConnection, err := r.managedDBProvisioningClient.GetDBConnection(remoteCentral.Id)
dbConnection, err := r.managedDBProvisioningClient.GetDBConnection(databaseID)
if err != nil {
return fmt.Errorf("getting RDS DB connection data: %w", err)
}
Expand Down
108 changes: 95 additions & 13 deletions fleetshard/pkg/central/reconciler/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,8 @@ import (
"testing"
"time"

"github.com/stackrox/rox/pkg/declarativeconfig"
"github.com/stackrox/rox/pkg/utils"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/aws/aws-sdk-go/aws/credentials/stscreds"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
openshiftRouteV1 "github.com/openshift/api/route/v1"
"github.com/pkg/errors"
"github.com/stackrox/acs-fleet-manager/fleetshard/config"
Expand All @@ -38,16 +29,23 @@ import (
"github.com/stackrox/acs-fleet-manager/pkg/client/fleetmanager"
"github.com/stackrox/acs-fleet-manager/pkg/features"
"github.com/stackrox/rox/operator/apis/platform/v1alpha1"
"github.com/stackrox/rox/pkg/declarativeconfig"
"github.com/stackrox/rox/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
"helm.sh/helm/v3/pkg/chart/loader"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

const (
Expand Down Expand Up @@ -578,13 +576,17 @@ func TestDisablePauseAnnotation(t *testing.T) {

func TestReconcileDeleteWithManagedDB(t *testing.T) {
managedDBProvisioningClient := &cloudprovider.DBClientMock{}
managedDBProvisioningClient.EnsureDBProvisionedFunc = func(_ context.Context, _string, _ string, _ string, _ bool) error {
managedDBProvisioningClient.EnsureDBProvisionedFunc = func(_ context.Context, databaseID, acsInstanceID, _ string, _ bool) error {
require.Equal(t, databaseID, acsInstanceID)
require.Equal(t, databaseID, simpleManagedCentral.Id)
return nil
}
managedDBProvisioningClient.EnsureDBDeprovisionedFunc = func(_ string, _ bool) error {
managedDBProvisioningClient.EnsureDBDeprovisionedFunc = func(databaseID string, _ bool) error {
require.Equal(t, databaseID, simpleManagedCentral.Id)
return nil
}
managedDBProvisioningClient.GetDBConnectionFunc = func(_ string) (postgres.DBConnection, error) {
managedDBProvisioningClient.GetDBConnectionFunc = func(databaseID string) (postgres.DBConnection, error) {
require.Equal(t, databaseID, simpleManagedCentral.Id)
connection, err := postgres.NewDBConnection("localhost", 5432, "rhacs", "postgres")
if err != nil {
return postgres.DBConnection{}, err
Expand Down Expand Up @@ -644,6 +646,86 @@ func TestReconcileDeleteWithManagedDB(t *testing.T) {
assert.True(t, k8sErrors.IsNotFound(err))
}

func TestReconcileDeleteWithManagedDBOverride(t *testing.T) {
dbOverrideId := "override-1234"

managedDBProvisioningClient := &cloudprovider.DBClientMock{}
managedDBProvisioningClient.EnsureDBProvisionedFunc = func(_ context.Context, databaseID, acsInstanceID, _ string, _ bool) error {
require.Equal(t, databaseID, dbOverrideId)
require.Equal(t, acsInstanceID, simpleManagedCentral.Id)
return nil
}
managedDBProvisioningClient.EnsureDBDeprovisionedFunc = func(databaseID string, _ bool) error {
require.Equal(t, databaseID, dbOverrideId)
return nil
}
managedDBProvisioningClient.GetDBConnectionFunc = func(databaseID string) (postgres.DBConnection, error) {
require.Equal(t, databaseID, dbOverrideId)
connection, err := postgres.NewDBConnection("localhost", 5432, "rhacs", "postgres")
if err != nil {
return postgres.DBConnection{}, err
}
return connection, nil
}

reconcilerOptions := CentralReconcilerOptions{
UseRoutes: true,
ManagedDBEnabled: true,
}
_, _, r := getClientTrackerAndReconciler(
t,
defaultCentralConfig,
managedDBProvisioningClient,
reconcilerOptions,
)

namespace := &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: simpleManagedCentral.Metadata.Namespace,
},
}
err := r.client.Create(context.TODO(), namespace)
require.NoError(t, err)

dbOverrideConfigMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Namespace: simpleManagedCentral.Metadata.Namespace,
Name: centralDbOverrideConfigMap,
},
Data: map[string]string{"databaseID": dbOverrideId},
}
err = r.client.Create(context.TODO(), dbOverrideConfigMap)
require.NoError(t, err)

_, err = r.Reconcile(context.TODO(), simpleManagedCentral)
require.NoError(t, err)
assert.Len(t, managedDBProvisioningClient.EnsureDBProvisionedCalls(), 1)

deletedCentral := simpleManagedCentral
deletedCentral.Metadata.DeletionTimestamp = "2006-01-02T15:04:05+00:00"

// trigger deletion
managedDBProvisioningClient.EnsureDBProvisionedFunc = func(_ context.Context, _ string, _ string, _ string, _ bool) error {
return nil
}
statusTrigger, err := r.Reconcile(context.TODO(), deletedCentral)
require.Error(t, err, ErrDeletionInProgress)
require.Nil(t, statusTrigger)
assert.Len(t, managedDBProvisioningClient.EnsureDBProvisionedCalls(), 1)

// deletion completed needs second reconcile to check as deletion is async in a kubernetes cluster
statusDeletion, err := r.Reconcile(context.TODO(), deletedCentral)
require.NoError(t, err)
require.NotNil(t, statusDeletion)

readyCondition, ok := conditionForType(statusDeletion.Conditions, conditionTypeReady)
require.True(t, ok, "Ready condition not found in conditions", statusDeletion.Conditions)
assert.Equal(t, "False", readyCondition.Status)
assert.Equal(t, "Deleted", readyCondition.Reason)

assert.Len(t, managedDBProvisioningClient.EnsureDBDeprovisionedCalls(), 2)
}

func TestCentralChanged(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading