Skip to content

Commit 0df45f5

Browse files
authored
Allow overriding clusterDomain for connection strings (#747)
1 parent 6f390f2 commit 0df45f5

File tree

16 files changed

+51
-36
lines changed

16 files changed

+51
-36
lines changed

api/v1/mongodbcommunity_types.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ const (
4646
defaultMode AuthMode = "SCRAM-SHA-256"
4747
)
4848

49+
const (
50+
defaultClusterDomain = "cluster.local"
51+
)
52+
4953
// MongoDBCommunitySpec defines the desired state of MongoDB
5054
type MongoDBCommunitySpec struct {
5155
// Members is the number of members in the replica set
@@ -498,32 +502,36 @@ func (m MongoDBCommunity) AutomationConfigMembersThisReconciliation() int {
498502
}
499503

500504
// MongoURI returns a mongo uri which can be used to connect to this deployment
501-
func (m MongoDBCommunity) MongoURI() string {
502-
return fmt.Sprintf("mongodb://%s", strings.Join(m.Hosts(), ","))
505+
func (m MongoDBCommunity) MongoURI(clusterDomain string) string {
506+
return fmt.Sprintf("mongodb://%s", strings.Join(m.Hosts(clusterDomain), ","))
503507
}
504508

505509
// MongoSRVURI returns a mongo srv uri which can be used to connect to this deployment
506-
func (m MongoDBCommunity) MongoSRVURI() string {
507-
clusterDomain := "svc.cluster.local" // TODO: make this configurable
508-
return fmt.Sprintf("mongodb+srv://%s.%s.%s", m.ServiceName(), m.Namespace, clusterDomain)
510+
func (m MongoDBCommunity) MongoSRVURI(clusterDomain string) string {
511+
if clusterDomain == "" {
512+
clusterDomain = defaultClusterDomain
513+
}
514+
return fmt.Sprintf("mongodb+srv://%s.%s.svc.%s", m.ServiceName(), m.Namespace, clusterDomain)
509515
}
510516

511517
// MongoAuthUserURI returns a mongo uri which can be used to connect to this deployment
512518
// and includes the authentication data for the user
513-
func (m MongoDBCommunity) MongoAuthUserURI(user scram.User, password string) string {
519+
func (m MongoDBCommunity) MongoAuthUserURI(user scram.User, password string, clusterDomain string) string {
514520
return fmt.Sprintf("mongodb://%s:%s@%s/%s?ssl=%t",
515521
url.QueryEscape(user.Username),
516522
url.QueryEscape(password),
517-
strings.Join(m.Hosts(), ","),
523+
strings.Join(m.Hosts(clusterDomain), ","),
518524
user.Database,
519525
m.Spec.Security.TLS.Enabled)
520526
}
521527

522528
// MongoAuthUserSRVURI returns a mongo srv uri which can be used to connect to this deployment
523529
// and includes the authentication data for the user
524-
func (m MongoDBCommunity) MongoAuthUserSRVURI(user scram.User, password string) string {
525-
clusterDomain := "svc.cluster.local" // TODO: make this configurable
526-
return fmt.Sprintf("mongodb+srv://%s:%s@%s.%s.%s/%s?ssl=%t",
530+
func (m MongoDBCommunity) MongoAuthUserSRVURI(user scram.User, password string, clusterDomain string) string {
531+
if clusterDomain == "" {
532+
clusterDomain = defaultClusterDomain
533+
}
534+
return fmt.Sprintf("mongodb+srv://%s:%s@%s.%s.svc.%s/%s?ssl=%t",
527535
url.QueryEscape(user.Username),
528536
url.QueryEscape(password),
529537
m.ServiceName(),
@@ -533,11 +541,15 @@ func (m MongoDBCommunity) MongoAuthUserSRVURI(user scram.User, password string)
533541
m.Spec.Security.TLS.Enabled)
534542
}
535543

536-
func (m MongoDBCommunity) Hosts() []string {
544+
func (m MongoDBCommunity) Hosts(clusterDomain string) []string {
537545
hosts := make([]string, m.Spec.Members)
538-
clusterDomain := "svc.cluster.local" // TODO: make this configurable
546+
547+
if clusterDomain == "" {
548+
clusterDomain = defaultClusterDomain
549+
}
550+
539551
for i := 0; i < m.Spec.Members; i++ {
540-
hosts[i] = fmt.Sprintf("%s-%d.%s.%s.%s:%d", m.Name, i, m.ServiceName(), m.Namespace, clusterDomain, 27017)
552+
hosts[i] = fmt.Sprintf("%s-%d.%s.%s.svc.%s:%d", m.Name, i, m.ServiceName(), m.Namespace, clusterDomain, 27017)
541553
}
542554
return hosts
543555
}

api/v1/mongodbcommunity_types_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ import (
99

1010
func TestMongoDB_MongoURI(t *testing.T) {
1111
mdb := newReplicaSet(2, "my-rs", "my-namespace")
12-
assert.Equal(t, mdb.MongoURI(), "mongodb://my-rs-0.my-rs-svc.my-namespace.svc.cluster.local:27017,my-rs-1.my-rs-svc.my-namespace.svc.cluster.local:27017")
12+
assert.Equal(t, mdb.MongoURI(""), "mongodb://my-rs-0.my-rs-svc.my-namespace.svc.cluster.local:27017,my-rs-1.my-rs-svc.my-namespace.svc.cluster.local:27017")
13+
assert.Equal(t, mdb.MongoURI("my.cluster"), "mongodb://my-rs-0.my-rs-svc.my-namespace.svc.my.cluster:27017,my-rs-1.my-rs-svc.my-namespace.svc.my.cluster:27017")
1314
mdb = newReplicaSet(1, "my-single-rs", "my-single-namespace")
14-
assert.Equal(t, mdb.MongoURI(), "mongodb://my-single-rs-0.my-single-rs-svc.my-single-namespace.svc.cluster.local:27017")
15+
assert.Equal(t, mdb.MongoURI(""), "mongodb://my-single-rs-0.my-single-rs-svc.my-single-namespace.svc.cluster.local:27017")
16+
assert.Equal(t, mdb.MongoURI("my.cluster"), "mongodb://my-single-rs-0.my-single-rs-svc.my-single-namespace.svc.my.cluster:27017")
1517
mdb = newReplicaSet(5, "my-big-rs", "my-big-namespace")
16-
assert.Equal(t, mdb.MongoURI(), "mongodb://my-big-rs-0.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-1.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-2.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-3.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-4.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017")
18+
assert.Equal(t, mdb.MongoURI(""), "mongodb://my-big-rs-0.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-1.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-2.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-3.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017,my-big-rs-4.my-big-rs-svc.my-big-namespace.svc.cluster.local:27017")
19+
assert.Equal(t, mdb.MongoURI("my.cluster"), "mongodb://my-big-rs-0.my-big-rs-svc.my-big-namespace.svc.my.cluster:27017,my-big-rs-1.my-big-rs-svc.my-big-namespace.svc.my.cluster:27017,my-big-rs-2.my-big-rs-svc.my-big-namespace.svc.my.cluster:27017,my-big-rs-3.my-big-rs-svc.my-big-namespace.svc.my.cluster:27017,my-big-rs-4.my-big-rs-svc.my-big-namespace.svc.my.cluster:27017")
1720
}
1821

1922
func TestGetScramCredentialsSecretName(t *testing.T) {

controllers/mongodb_users.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (r ReplicaSetReconciler) ensureUserResources(mdb mdbv1.MongoDBCommunity) er
3737

3838
// updateConnectionStringSecrets updates secrets where user specific connection strings are stored.
3939
// The client applications can mount these secrets and connect to the mongodb cluster
40-
func (r ReplicaSetReconciler) updateConnectionStringSecrets(mdb mdbv1.MongoDBCommunity) error {
40+
func (r ReplicaSetReconciler) updateConnectionStringSecrets(mdb mdbv1.MongoDBCommunity, clusterDomain string) error {
4141
for _, user := range mdb.GetScramUsers() {
4242
secretNamespacedName := types.NamespacedName{Name: user.PasswordSecretName, Namespace: mdb.Namespace}
4343
pwd, err := secret.ReadKey(r.client, user.PasswordSecretKey, secretNamespacedName)
@@ -48,8 +48,8 @@ func (r ReplicaSetReconciler) updateConnectionStringSecrets(mdb mdbv1.MongoDBCom
4848
connectionStringSecret := secret.Builder().
4949
SetName(user.GetConnectionStringSecretName(mdb)).
5050
SetNamespace(mdb.Namespace).
51-
SetField("connectionString.standard", mdb.MongoAuthUserURI(user, pwd)).
52-
SetField("connectionString.standardSrv", mdb.MongoAuthUserSRVURI(user, pwd)).
51+
SetField("connectionString.standard", mdb.MongoAuthUserURI(user, pwd, clusterDomain)).
52+
SetField("connectionString.standardSrv", mdb.MongoAuthUserSRVURI(user, pwd, clusterDomain)).
5353
SetField("username", user.Username).
5454
SetField("password", pwd).
5555
SetOwnerReferences(mdb.GetOwnerReferences()).

controllers/replica_set_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (r ReplicaSetReconciler) Reconcile(ctx context.Context, request reconcile.R
220220

221221
res, err := status.Update(r.client.Status(), &mdb,
222222
statusOptions().
223-
withMongoURI(mdb.MongoURI()).
223+
withMongoURI(mdb.MongoURI(os.Getenv(clusterDNSName))).
224224
withMongoDBMembers(mdb.AutomationConfigMembersThisReconciliation()).
225225
withStatefulSetReplicas(mdb.StatefulSetReplicasThisReconciliation()).
226226
withMessage(None, "").
@@ -232,7 +232,7 @@ func (r ReplicaSetReconciler) Reconcile(ctx context.Context, request reconcile.R
232232
return res, err
233233
}
234234

235-
if err := r.updateConnectionStringSecrets(mdb); err != nil {
235+
if err := r.updateConnectionStringSecrets(mdb, os.Getenv(clusterDNSName)); err != nil {
236236
r.log.Errorf("Could not update connection string secrets: %s", err)
237237
}
238238

test/e2e/mongodbtests/mongodbtests.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func BasicFunctionality(mdb *mdbv1.MongoDBCommunity) func(*testing.T) {
318318
t.Run("Connection string secrets are configured", ConnectionStringSecretsAreConfigured(mdb, mdbOwnerReference))
319319
t.Run("Test Status Was Updated", Status(mdb,
320320
mdbv1.MongoDBCommunityStatus{
321-
MongoURI: mdb.MongoURI(),
321+
MongoURI: mdb.MongoURI(""),
322322
Phase: mdbv1.Running,
323323
Version: mdb.GetMongoDBVersion(),
324324
CurrentMongoDBMembers: mdb.Spec.Members,

test/e2e/replica_set/replica_set_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestReplicaSet(t *testing.T) {
4141
t.Run("Basic tests", mongodbtests.BasicFunctionality(&mdb))
4242
t.Run("Keyfile authentication is configured", tester.HasKeyfileAuth(3))
4343
t.Run("Test Basic Connectivity", tester.ConnectivitySucceeds())
44-
t.Run("Test SRV Connectivity", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI()), WithoutTls(), WithReplicaSet((mdb.Name))))
44+
t.Run("Test SRV Connectivity", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI("")), WithoutTls(), WithReplicaSet((mdb.Name))))
4545
t.Run("Test Basic Connectivity with generated connection string secret",
4646
tester.ConnectivitySucceeds(WithURI(mongodbtests.GetConnectionStringForUser(mdb, scramUser))))
4747
t.Run("Test SRV Connectivity with generated connection string secret",

test/e2e/replica_set_custom_persistent_volume/replica_set_custom_persistent_volume.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestReplicaSetCustomPersistentVolumes(t *testing.T) {
133133
t.Run("Basic tests", mongodbtests.BasicFunctionality(&mdb))
134134
t.Run("Keyfile authentication is configured", tester.HasKeyfileAuth(3))
135135
t.Run("Test Basic Connectivity", tester.ConnectivitySucceeds())
136-
t.Run("Test SRV Connectivity", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI()), WithoutTls(), WithReplicaSet((mdb.Name))))
136+
t.Run("Test SRV Connectivity", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI("")), WithoutTls(), WithReplicaSet((mdb.Name))))
137137
t.Run("Test Basic Connectivity with generated connection string secret",
138138
tester.ConnectivitySucceeds(WithURI(mongodbtests.GetConnectionStringForUser(mdb, scramUser))))
139139
t.Run("Test SRV Connectivity with generated connection string secret",

test/e2e/replica_set_multiple/replica_set_multiple_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func TestReplicaSetMultiple(t *testing.T) {
7474
t.Run("AutomationConfig's version has been increased", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb0, 3))
7575
t.Run("Test Status Was Updated", mongodbtests.Status(&mdb0,
7676
mdbv1.MongoDBCommunityStatus{
77-
MongoURI: mdb0.MongoURI(),
77+
MongoURI: mdb0.MongoURI(""),
7878
Phase: mdbv1.Running,
7979
CurrentMongoDBMembers: 5,
8080
CurrentStatefulSetReplicas: 5,
@@ -87,7 +87,7 @@ func TestReplicaSetMultiple(t *testing.T) {
8787
//t.Run("AutomationConfig's version has been increased", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb0, 3))
8888
//t.Run("Test Status Was Updated", mongodbtests.Status(&mdb0,
8989
// mdbv1.MongoDBStatus{
90-
// MongoURI: mdb0.MongoURI(),
90+
// MongoURI: mdb0.MongoURI(""),
9191
// Phase: mdbv1.Running,
9292
// CurrentMongoDBMembers: 5,
9393
// CurrentStatefulSetReplicas: 5,

test/e2e/replica_set_recovery/replica_set_recovery_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestReplicaSetRecovery(t *testing.T) {
5656
t.Run("MongoDB Reaches Running Phase", mongodbtests.MongoDBReachesRunningPhase(&mdb))
5757
t.Run("Test Status Was Updated", mongodbtests.Status(&mdb,
5858
mdbv1.MongoDBCommunityStatus{
59-
MongoURI: mdb.MongoURI(),
59+
MongoURI: mdb.MongoURI(""),
6060
Phase: mdbv1.Running,
6161
Version: mdb.GetMongoDBVersion(),
6262
CurrentMongoDBMembers: 3,

test/e2e/replica_set_scale/replica_set_scaling_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestReplicaSetScaleUp(t *testing.T) {
5151
t.Run("AutomationConfig's version has been increased", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb, 3))
5252
t.Run("Test Status Was Updated", mongodbtests.Status(&mdb,
5353
mdbv1.MongoDBCommunityStatus{
54-
MongoURI: mdb.MongoURI(),
54+
MongoURI: mdb.MongoURI(""),
5555
Phase: mdbv1.Running,
5656
Version: mdb.GetMongoDBVersion(),
5757
CurrentMongoDBMembers: 5,
@@ -65,7 +65,7 @@ func TestReplicaSetScaleUp(t *testing.T) {
6565
//t.Run("AutomationConfig's version has been increased", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb, 5))
6666
//t.Run("Test Status Was Updated", mongodbtests.Status(&mdb,
6767
// mdbv1.MongoDBStatus{
68-
// MongoURI: mdb.MongoURI(),
68+
// MongoURI: mdb.MongoURI(""),
6969
// Phase: mdbv1.Running,
7070
// Version: mdb.GetMongoDBVersion(),
7171
// CurrentMongoDBMembers: 3,

test/e2e/replica_set_scale_down/replica_set_scale_down_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestReplicaSetScaleDown(t *testing.T) {
5454
t.Run("AutomationConfig's version has been increased", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb, 3))
5555
t.Run("Test Status Was Updated", mongodbtests.Status(&mdb,
5656
mdbv1.MongoDBCommunityStatus{
57-
MongoURI: mdb.MongoURI(),
57+
MongoURI: mdb.MongoURI(""),
5858
Phase: mdbv1.Running,
5959
Version: mdb.GetMongoDBVersion(),
6060
CurrentMongoDBMembers: 1,

test/e2e/replica_set_tls/replica_set_tls_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestReplicaSetTLS(t *testing.T) {
4545
mongodbtests.SkipTestIfLocal(t, "Ensure MongoDB TLS Configuration", func(t *testing.T) {
4646
t.Run("Has TLS Mode", tester.HasTlsMode("requireSSL", 60, WithTls(mdb)))
4747
t.Run("Basic Connectivity Succeeds", tester.ConnectivitySucceeds(WithTls(mdb)))
48-
t.Run("SRV Connectivity Succeeds", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI()), WithTls(mdb)))
48+
t.Run("SRV Connectivity Succeeds", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI("")), WithTls(mdb)))
4949
t.Run("Basic Connectivity With Generated Connection String Secret Succeeds",
5050
tester.ConnectivitySucceeds(WithURI(mongodbtests.GetConnectionStringForUser(mdb, scramUser)), WithTls(mdb)))
5151
t.Run("SRV Connectivity With Generated Connection String Secret Succeeds",

test/e2e/replica_set_tls_pem_file/replica_set_pem_file_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func TestReplicaSetTLS(t *testing.T) {
4545
mongodbtests.SkipTestIfLocal(t, "Ensure MongoDB TLS Configuration", func(t *testing.T) {
4646
t.Run("Has TLS Mode", tester.HasTlsMode("requireSSL", 60, WithTls(mdb)))
4747
t.Run("Basic Connectivity Succeeds", tester.ConnectivitySucceeds(WithTls(mdb)))
48-
t.Run("SRV Connectivity Succeeds", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI()), WithTls(mdb)))
48+
t.Run("SRV Connectivity Succeeds", tester.ConnectivitySucceeds(WithURI(mdb.MongoSRVURI("")), WithTls(mdb)))
4949
t.Run("Basic Connectivity With Generated Connection String Secret Succeeds",
5050
tester.ConnectivitySucceeds(WithURI(mongodbtests.GetConnectionStringForUser(mdb, scramUser)), WithTls(mdb)))
5151
t.Run("SRV Connectivity With Generated Connection String Secret Succeeds",

test/e2e/replica_set_tls_recreate_mdbc/replica_set_tls_recreate_mdbc_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestReplicaSetTLSRecreateMdbc(t *testing.T) {
5656
mongodbtests.SkipTestIfLocal(t, "Ensure MongoDB TLS Configuration", func(t *testing.T) {
5757
t.Run("Has TLS Mode", tester1.HasTlsMode("requireSSL", 60, WithTls(mdb2)))
5858
t.Run("Basic Connectivity Succeeds", tester1.ConnectivitySucceeds(WithTls(mdb2)))
59-
t.Run("SRV Connectivity Succeeds", tester1.ConnectivitySucceeds(WithURI(mdb2.MongoSRVURI()), WithTls(mdb2)))
59+
t.Run("SRV Connectivity Succeeds", tester1.ConnectivitySucceeds(WithURI(mdb2.MongoSRVURI("")), WithTls(mdb2)))
6060
t.Run("Basic Connectivity With Generated Connection String Secret Succeeds",
6161
tester1.ConnectivitySucceeds(WithURI(mongodbtests.GetConnectionStringForUser(mdb2, scramUser)), WithTls(mdb2)))
6262
t.Run("SRV Connectivity With Generated Connection String Secret Succeeds",

test/e2e/statefulset_delete/statefulset_delete_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestStatefulSetDelete(t *testing.T) {
3939
t.Run("MongoDB Reaches Running Phase", mongodbtests.MongoDBReachesRunningPhase(&mdb))
4040
t.Run("Test Status Was Updated", mongodbtests.Status(&mdb,
4141
mdbv1.MongoDBCommunityStatus{
42-
MongoURI: mdb.MongoURI(),
42+
MongoURI: mdb.MongoURI(""),
4343
Phase: mdbv1.Running,
4444
Version: mdb.GetMongoDBVersion(),
4545
CurrentMongoDBMembers: mdb.DesiredReplicas(),

test/e2e/util/mongotester/mongotester.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ type OptionApplier interface {
5151
func FromResource(t *testing.T, mdb mdbv1.MongoDBCommunity, opts ...OptionApplier) (*Tester, error) {
5252
var clientOpts []*options.ClientOptions
5353

54-
clientOpts = WithHosts(mdb.Hosts()).ApplyOption(clientOpts...)
54+
clientOpts = WithHosts(mdb.Hosts("")).ApplyOption(clientOpts...)
5555

56-
t.Logf("Configuring hosts: %s for MongoDB: %s", mdb.Hosts(), mdb.NamespacedName())
56+
t.Logf("Configuring hosts: %s for MongoDB: %s", mdb.Hosts(""), mdb.NamespacedName())
5757

5858
users := mdb.Spec.Users
5959
if len(users) == 1 {

0 commit comments

Comments
 (0)