@@ -46,6 +46,10 @@ const (
46
46
defaultMode AuthMode = "SCRAM-SHA-256"
47
47
)
48
48
49
+ const (
50
+ defaultClusterDomain = "cluster.local"
51
+ )
52
+
49
53
// MongoDBCommunitySpec defines the desired state of MongoDB
50
54
type MongoDBCommunitySpec struct {
51
55
// Members is the number of members in the replica set
@@ -498,32 +502,36 @@ func (m MongoDBCommunity) AutomationConfigMembersThisReconciliation() int {
498
502
}
499
503
500
504
// 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 ), "," ))
503
507
}
504
508
505
509
// 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 )
509
515
}
510
516
511
517
// MongoAuthUserURI returns a mongo uri which can be used to connect to this deployment
512
518
// 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 {
514
520
return fmt .Sprintf ("mongodb://%s:%s@%s/%s?ssl=%t" ,
515
521
url .QueryEscape (user .Username ),
516
522
url .QueryEscape (password ),
517
- strings .Join (m .Hosts (), "," ),
523
+ strings .Join (m .Hosts (clusterDomain ), "," ),
518
524
user .Database ,
519
525
m .Spec .Security .TLS .Enabled )
520
526
}
521
527
522
528
// MongoAuthUserSRVURI returns a mongo srv uri which can be used to connect to this deployment
523
529
// 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" ,
527
535
url .QueryEscape (user .Username ),
528
536
url .QueryEscape (password ),
529
537
m .ServiceName (),
@@ -533,11 +541,15 @@ func (m MongoDBCommunity) MongoAuthUserSRVURI(user scram.User, password string)
533
541
m .Spec .Security .TLS .Enabled )
534
542
}
535
543
536
- func (m MongoDBCommunity ) Hosts () []string {
544
+ func (m MongoDBCommunity ) Hosts (clusterDomain string ) []string {
537
545
hosts := make ([]string , m .Spec .Members )
538
- clusterDomain := "svc.cluster.local" // TODO: make this configurable
546
+
547
+ if clusterDomain == "" {
548
+ clusterDomain = defaultClusterDomain
549
+ }
550
+
539
551
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 )
541
553
}
542
554
return hosts
543
555
}
0 commit comments