Skip to content

Conversation

edimarlnx
Copy link

@edimarlnx edimarlnx commented Sep 26, 2025

Summary

This PR adds support for custom cluster domains in Kubernetes deployments by introducing a configurable ClusterDomain field in the MongoDB resource spec and utilizing the CLUSTER_DOMAIN environment variable. This change improves flexibility for deployments in Kubernetes clusters that use custom DNS suffixes instead of the default cluster.local.

Key Changes:

CRD & API Changes

  • Added optional ClusterDomain field to MongoDBCommunitySpec with hostname format validation
  • Updated CRD definitions in both config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml and helm_chart/crds/mongodbcommunity.mongodb.com_mongodbcommunity.yaml
  • Implemented GetClusterDomain() method in mongodbcommunity_types.go:1149 with fallback hierarchy: spec field → CLUSTER_DOMAIN environment variable → default cluster.local
  • Added ClusterDomainEnv constant in constants.go:14 with value "CLUSTER_DOMAIN"

Connection URI Refactoring

Removed clusterDomain parameter from all public URI methods - now retrieve domain directly from spec:

  • MongoURI() in mongodbcommunity_types.go:927
  • MongoSRVURI() in mongodbcommunity_types.go:934
  • MongoAuthUserURI() in mongodbcommunity_types.go:941
  • MongoAuthUserSRVURI() in mongodbcommunity_types.go:955
  • Hosts() in mongodbcommunity_types.go:967

Controller Updates

  • Updated buildAutomationConfig() in replica_set_controller.go:533 to use mdb.Spec.GetClusterDomain()
  • Modified getMongodConfigSearchModification() in replica_set_controller.go:837 to accept and pass clusterDomain parameter
  • Removed environment variable reads from Reconcile() method in replica_set_controller.go:237,250
  • Updated updateConnectionStringSecrets() in mongodb_users.go:44 to remove clusterDomain parameter
  • Modified applySearchOverrides() in mongodbreplicaset_controller.go:659 to pass cluster domain to GetMongodConfigParameters()

MongoDB Search Integration

  • Updated GetMongodConfigParameters() in mongodbsearch_reconcile_helper.go:391 to accept clusterDomain parameter
  • Updated mongotHostAndPort() in mongodbsearch_reconcile_helper.go:406 to accept clusterDomain parameter
  • Fixed host seed generation in CommunitySearchSource.HostSeeds() (community_search_source.go:27)
  • Fixed host seed generation in EnterpriseResourceSearchSource.HostSeeds() (enterprise_search_source.go:25)

Testing

  • Updated all unit tests in mongodbcommunity_types_test.go to use spec field instead of parameter
  • Updated E2E tests across multiple test suites to remove cluster domain parameter from URI method calls

Proof of Work

  • ✅ All hardcoded cluster.local references replaced with configurable cluster domain
  • ✅ Backward compatibility maintained with default fallback to cluster.local
  • ✅ Changelog file added documenting the fix
  • ✅ Code follows existing patterns and Kubernetes best practices
  • ✅ Unit tests updated and passing

Files modified:

  • changelog/20250925_feature_use_cluster_domain_environment_variable.md (new)
  • config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml (added clusterDomain field)
  • helm_chart/crds/mongodbcommunity.mongodb.com_mongodbcommunity.yaml (added clusterDomain field)
  • controllers/operator/mongodbreplicaset_controller.go:659
  • controllers/searchcontroller/community_search_source.go:29
  • controllers/searchcontroller/enterprise_search_source.go:27
  • controllers/searchcontroller/mongodbsearch_reconcile_helper.go:391-407
  • mongodb-community-operator/api/v1/mongodbcommunity_types.go:75,927-1152
  • mongodb-community-operator/api/v1/mongodbcommunity_types_test.go (multiple test updates)
  • mongodb-community-operator/controllers/mongodb_users.go:44-82
  • mongodb-community-operator/controllers/replica_set_controller.go:237,250,533-837
  • mongodb-community-operator/pkg/util/constants/constants.go:14

Checklist

  • Have you linked a jira ticket and/or is the ticket in the title?
  • Have you checked whether your jira ticket required DOCSP changes?
  • Have you added changelog file?

Copy link

There is an existing patch(es) for this commit SHA:

Please note that the status that is posted is not in the context of this PR but rather the (latest) existing patch and that may affect some tests that may depend on the particular PR. If your tests do not rely on any PR-specific values (like base or head branch name) then your tests will report the same status. If you would like a patch to run in the context of this PR and abort the other(s), comment 'evergreen retry'.

… field

- Introduced `ClusterDomain` field in `MongoDBCommunitySpec`.
- Updated functions to use `ClusterDomain` from spec instead of environment variables.
- Adjusted unit tests for new `ClusterDomain` behavior.
…d functions

- Updated `GetMongodConfigParameters` to accept `ClusterDomain` as a parameter.
- Modified calls to `mongotHostAndPort` to include the `ClusterDomain` argument.
Copy link
Collaborator

@MaciejKaras MaciejKaras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the changes are really good!

We just need to clarify spec.GetClusterDomain issue from the comment.

@edimarlnx edimarlnx requested a review from MaciejKaras October 2, 2025 02:58
@edimarlnx
Copy link
Author

@MaciejKaras, one question: my changes require updating the custom resource CRD. I didn't include the changes for Helm charts in this PR because I believe they will be released automatically in a new repository version.
Please let me know if I also need to make this change.

@MaciejKaras
Copy link
Collaborator

I didn't include the changes for Helm charts in this PR because I believe they will be released automatically in a new repository version.

@edimarlnx Change request should contain also changes to helm charts. Please generate CRD changes by running make manifests and adding them to the PR.

Copy link
Collaborator

@MaciejKaras MaciejKaras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @edimarlnx and thanks for the fix!

Copy link
Collaborator

@vinilage vinilage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really thanks for your collaboration @edimarlnx, we really appreciate it.
LGTM from my side.

@MaciejKaras
Copy link
Collaborator

@edimarlnx
I see some of the unit tests are failing. You can run them yourself calling make golang-tests. I think they are related to this:

mongodb-community-operator/test/e2e/util/mongotester/mongotester.go:86:35: too many arguments in call to mdb.Hosts

mongodb-community-operator/test/e2e/replica_set_x509/replica_set_x509_test.go:104:20: undefined: GetAgentCert (typecheck)

Also please run make precommit afterwards, because there are some linting issues as well.

- Updated calls to `mdb.MongoURI()` and `mdb.MongoSRVURI()` to exclude unnecessary string argument.
…ementation

- Expanded description of changes for `ClusterDomain` field in `CRD` and related methods
- Added references to updated methods, tests, and configurations
- Refined description of `ClusterDomain` support in the changelog
- Streamlined explanation of the fallback hierarchy and related CRD changes
@MaciejKaras
Copy link
Collaborator

@edimarlnx still some unit tests are failing + linting as well.

@MaciejKaras
Copy link
Collaborator

@edimarlnx last linting issue ;)

[2025/10/09 18:52:18.691] lint_code: mongodb-community-operator/api/v1/mongodbcommunity_types.go:1153:9: use of envvar.GetEnvOrDefault forbidden because "Using this envvar package here is prohibited. Please work with environment variables in the main package." (forbidigo)
[2025/10/09 18:52:18.691] lint_code: return envvar.GetEnvOrDefault(constants.ClusterDomainEnv, defaultClusterDomain)
[2025/10/09 18:52:18.691] lint_code: ^

@edimarlnx
Copy link
Author

@edimarlnx last linting issue ;)

[2025/10/09 18:52:18.691] lint_code: mongodb-community-operator/api/v1/mongodbcommunity_types.go:1153:9: use of envvar.GetEnvOrDefault forbidden because "Using this envvar package here is prohibited. Please work with environment variables in the main package." (forbidigo)
[2025/10/09 18:52:18.691] lint_code: return envvar.GetEnvOrDefault(constants.ClusterDomainEnv, defaultClusterDomain)
[2025/10/09 18:52:18.691] lint_code: ^

Would you have any documentation to run all these tests locally?

@MaciejKaras
Copy link
Collaborator

@edimarlnx Unfortunately not :( additionally outside contributors cannot access CI logs, which is not helping either.

We should definitely provide some guidance on how to run at least:

  • unit tests
  • linters and other checks
  • regenerate helm charts and manifests
  • install local dependencies

This could be added in CONTRIBUTING.md. I will bring this up to the team.

@edimarlnx
Copy link
Author

edimarlnx commented Oct 10, 2025

@edimarlnx I see some of the unit tests are failing. You can run them yourself calling make golang-tests. I think they are related to this:

mongodb-community-operator/test/e2e/util/mongotester/mongotester.go:86:35: too many arguments in call to mdb.Hosts

mongodb-community-operator/test/e2e/replica_set_x509/replica_set_x509_test.go:104:20: undefined: GetAgentCert (typecheck)

Also please run make precommit afterwards, because there are some linting issues as well.

I tried, but it shows an error and shows an internal link for instructions on how to resolve it.

image image

If it's okay with you, I'll continue with trial and error. I will fix the ones you mentioned and push them to the tests again. Okay?

@MaciejKaras
Copy link
Collaborator

Hey @edimarlnx, after running linter locally here are the changes needed to fix the PR:

Subject: [PATCH] Fix linter issues
---
Index: mongodb-community-operator/api/v1/mongodbcommunity_types.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/mongodb-community-operator/api/v1/mongodbcommunity_types.go b/mongodb-community-operator/api/v1/mongodbcommunity_types.go
--- a/mongodb-community-operator/api/v1/mongodbcommunity_types.go	(revision 47adda14bdb1cac747f5925ffe70a0f45b0cba0b)
+++ b/mongodb-community-operator/api/v1/mongodbcommunity_types.go	(date 1760102034848)
@@ -6,7 +6,6 @@
 	"regexp"
 	"strings"
 
-	"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/envvar"
 	"github.com/stretchr/objx"
 	"k8s.io/apimachinery/pkg/runtime"
 	"k8s.io/apimachinery/pkg/runtime/schema"
@@ -22,6 +21,7 @@
 	"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/automationconfig"
 	"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/kube/annotations"
 	"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/constants"
+	"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/envvar"
 	"github.com/mongodb/mongodb-kubernetes/mongodb-community-operator/pkg/util/scale"
 )
 
@@ -1146,11 +1146,12 @@
 func (m MongoDBCommunity) GetAgentMaxLogFileDurationHours() int {
 	return m.Spec.AgentConfiguration.MaxLogFileDurationHours
 }
+
 func (m MongoDBCommunitySpec) GetClusterDomain() string {
 	if m.ClusterDomain != "" {
 		return m.ClusterDomain
 	}
-	return envvar.GetEnvOrDefault(constants.ClusterDomainEnv, defaultClusterDomain)
+	return envvar.GetEnvOrDefault(constants.ClusterDomainEnv, defaultClusterDomain) // nolint:forbidigo
 }
 
 type automationConfigReplicasScaler struct {
Index: public/crds.yaml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/public/crds.yaml b/public/crds.yaml
--- a/public/crds.yaml	(revision 47adda14bdb1cac747f5925ffe70a0f45b0cba0b)
+++ b/public/crds.yaml	(date 1760102037475)
@@ -6844,6 +6844,9 @@
                         x-kubernetes-preserve-unknown-fields: true
                     type: object
                 type: object
+              clusterDomain:
+                format: hostname
+                type: string
               featureCompatibilityVersion:
                 description: |-
                   FeatureCompatibilityVersion configures the feature compatibility version that will

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants