Skip to content

K8SPSMDB-1640 configure globally read and write concerns#2406

Merged
hors merged 4 commits into
mainfrom
K8SPSMDB-1640
Jun 22, 2026
Merged

K8SPSMDB-1640 configure globally read and write concerns#2406
hors merged 4 commits into
mainfrom
K8SPSMDB-1640

Conversation

@gkech

@gkech gkech commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

https://perconadev.atlassian.net/browse/K8SPSMDB-1640

CHANGE DESCRIPTION

Problem:

Configure read/write concerns separately through a global cr option.

related issue: #2311

helm pr: percona/percona-helm-charts#873

Cause:
Short explanation of the root cause of the issue if applicable.

Solution:
Short explanation of the solution we are providing with this PR.

CHECKLIST

Jira

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?
  • Are OpenShift compare files changed for E2E tests (compare/*-oc.yml)?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported MongoDB version?
  • Does the change support oldest and newest supported Kubernetes version?

Copilot AI review requested due to automatic review settings June 15, 2026 11:09
@pull-request-size pull-request-size Bot added the size/L 100-499 lines label Jun 15, 2026
@github-actions github-actions Bot added the tests label Jun 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds a new CR spec option to configure MongoDB cluster-wide default read/write concerns and wires it into the controller’s setDefaultRWConcern calls, with corresponding CRD and deepcopy regeneration.

Changes:

  • Introduces spec.defaultRWConcern (readConcern/writeConcern) in the API types and regenerates deepcopy code.
  • Uses the new spec values when calling SetDefaultRWConcern during reconciliation.
  • Updates CRD manifests/bundles and adds a unit test for defaulting behavior.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
pkg/controller/perconaservermongodb/mgo.go Adds helper to derive default RW concerns from CR spec and uses it in SetDefaultRWConcern calls.
pkg/controller/perconaservermongodb/mgo_test.go Adds unit test coverage for defaultRWConcern behavior.
pkg/apis/psmdb/v1/psmdb_types.go Adds DefaultRWConcern API type and spec.defaultRWConcern field.
pkg/apis/psmdb/v1/zz_generated.deepcopy.go Regenerates deepcopy methods for the new API field/type.
config/crd/bases/psmdb.percona.com_perconaservermongodbs.yaml Adds CRD schema for spec.defaultRWConcern.
deploy/crd.yaml Propagates CRD schema update into deploy artifact.
deploy/bundle.yaml Propagates CRD schema update into bundle artifact.
deploy/cw-bundle.yaml Propagates CRD schema update into cloud bundle artifact.
e2e-tests/version-service/conf/crd.yaml Updates version-service CRD schema snapshot for tests.
Files not reviewed (1)
  • pkg/apis/psmdb/v1/zz_generated.deepcopy.go: Generated file
Comments suppressed due to low confidence (1)

pkg/controller/perconaservermongodb/mgo.go:263

  • DefaultRWConcern is only applied in this non-sharded branch when replset.Arbiter.Enabled is true; for a standard non-sharded replica set without an arbiter, the new spec.defaultRWConcern setting will never be applied (confirmed: these are the only SetDefaultRWConcern call sites in the repo). If the intent is “configure globally”, consider applying SetDefaultRWConcern for all non-sharded replica sets (and/or gating on cr.Spec.DefaultRWConcern != nil), or update the API/docs to reflect the limited scope.
	if replset.Arbiter.Enabled && !cr.Spec.Sharding.Enabled {
		readConcern, writeConcern := defaultRWConcern(cr)
		err := cli.SetDefaultRWConcern(ctx, readConcern, writeConcern)
		// SetDefaultRWConcern introduced in MongoDB 4.4
		if err != nil && !strings.Contains(err.Error(), "CommandNotFound") {
			return api.AppStateError, nil, errors.Wrap(err, "set default RW concern")
		}
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +114 to 118
type DefaultRWConcern struct {
// +kubebuilder:validation:Enum={local,available,majority,linearizable,snapshot}
ReadConcern string `json:"readConcern,omitempty"`
WriteConcern string `json:"writeConcern,omitempty"`
}
Comment thread pkg/apis/psmdb/v1/psmdb_types.go Outdated
Comment on lines +111 to +113
// DefaultRWConcern configures the cluster-wide default read/write concern that
// the operator enforces via setDefaultRWConcern on each reconcile. Fields left
// empty fall back to "majority".
@gkech gkech marked this pull request as ready for review June 15, 2026 11:30
Copilot AI review requested due to automatic review settings June 15, 2026 11:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 11 changed files in this pull request and generated no new comments.

Files not reviewed (1)
  • pkg/apis/psmdb/v1/zz_generated.deepcopy.go: Generated file

Comment on lines +257 to +258
readConcern, writeConcern := defaultRWConcern(cr)
err := cli.SetDefaultRWConcern(ctx, readConcern, writeConcern)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i know it's not introduced in this PR but i don't understand why this command depends on replset.Arbiter.Enabled

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Seems that PSS replsets already get majority as MongoDB's implicit default, making the call a no-op write, while PSA topologies need the explicit push.

With the latest commit, we allow the condition to also fire when the user has set custom values.

@hors hors added this to the v1.23.0 milestone Jun 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.

Files not reviewed (1)
  • pkg/apis/psmdb/v1/zz_generated.deepcopy.go: Generated file

Comment on lines +50 to +55
func shouldSetDefaultRWConcern(cr *api.PerconaServerMongoDB, replset *api.ReplsetSpec) bool {
if cr.Spec.Sharding.Enabled {
return false
}
return replset.Arbiter.Enabled || cr.Spec.DefaultRWConcern != nil
}
Comment on lines +112 to 116
type DefaultRWConcern struct {
// +kubebuilder:validation:Enum={local,available,majority,linearizable,snapshot}
ReadConcern string `json:"readConcern,omitempty"`
WriteConcern string `json:"writeConcern,omitempty"`
}
Comment on lines +51 to +53
if cr.Spec.Sharding.Enabled {
return false
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why we don't set anything in sharded clusters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But we do. This function is used for the non-sharded setup. Please check err = mongosSession.SetDefaultRWConcern(ctx, readConcern, writeConcern)

@gkech gkech requested a review from egegunes June 18, 2026 07:47

@egegunes egegunes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. we can only consider adding this case to one of existing e2e tests

Comment on lines -208 to +231
err = mongosSession.SetDefaultRWConcern(ctx, mongo.DefaultReadConcern, mongo.DefaultWriteConcern)
readConcern, writeConcern := defaultRWConcern(cr)
err = mongosSession.SetDefaultRWConcern(ctx, readConcern, writeConcern)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So, for the writeConcern I can set custom tags that's why the field is defined as just a string with no restrictions on what's inside. However, as I understand those strings should match mongo.RSConfig.Settings.GetLastErrorModes (WriteConcernReplsetTags), so I'm wondering if we should validate the given custom string against this map at least at runtime to avoid misconfiguration.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

WriteConcernReplsetTags

MongoDB itself will reject an unknown custom tag at setDefaultRWConcern time with something like "No write concern mode named " so misconfiguration is already caught, but it surfaces as a generic wrapped error that fails the whole reconcile loop on every pass until the CR is fixed.

@JNKPercona

Copy link
Copy Markdown
Collaborator
Test Name Result Time
arbiter passed 00:00:00
balancer passed 00:00:00
cert-management-policy passed 00:00:00
clustersync passed 00:00:00
cross-site-sharded passed 00:00:00
custom-replset-name passed 00:00:00
custom-tls passed 00:00:00
custom-users-roles passed 00:00:00
custom-users-roles-sharded passed 00:00:00
data-at-rest-encryption passed 00:00:00
data-sharded passed 00:20:39
demand-backup passed 00:00:00
demand-backup-eks-credentials-irsa passed 00:00:00
demand-backup-fs passed 00:00:00
demand-backup-if-unhealthy passed 00:00:00
demand-backup-incremental-aws passed 00:00:00
demand-backup-incremental-azure passed 00:00:00
demand-backup-incremental-gcp-native passed 00:00:00
demand-backup-incremental-gcp-s3 passed 00:00:00
demand-backup-incremental-minio passed 00:00:00
demand-backup-incremental-sharded-aws passed 00:00:00
demand-backup-incremental-sharded-azure passed 00:00:00
demand-backup-incremental-sharded-gcp-native passed 00:00:00
demand-backup-incremental-sharded-gcp-s3 passed 00:00:00
demand-backup-incremental-sharded-minio passed 00:00:00
demand-backup-logical-minio-native-tls passed 00:00:00
demand-backup-physical-parallel passed 00:08:49
demand-backup-physical-aws passed 00:00:00
demand-backup-physical-azure passed 00:00:00
demand-backup-physical-gcp-s3 passed 00:00:00
demand-backup-physical-gcp-native passed 00:00:00
demand-backup-physical-minio passed 00:00:00
demand-backup-physical-minio-native passed 00:00:00
demand-backup-physical-minio-native-tls passed 00:00:00
demand-backup-physical-sharded-parallel passed 00:00:00
demand-backup-physical-sharded-aws passed 00:00:00
demand-backup-physical-sharded-azure passed 00:00:00
demand-backup-physical-sharded-gcp-native passed 00:00:00
demand-backup-physical-sharded-minio passed 00:00:00
demand-backup-physical-sharded-minio-native passed 00:00:00
demand-backup-sharded passed 00:00:00
demand-backup-snapshot passed 00:00:00
demand-backup-snapshot-vault passed 00:00:00
disabled-auth passed 00:00:00
expose-sharded passed 00:00:00
finalizer passed 00:00:00
ignore-labels-annotations passed 00:00:00
init-deploy passed 00:00:00
ldap passed 00:00:00
ldap-tls passed 00:00:00
limits passed 00:00:00
liveness passed 00:00:00
mongod-major-upgrade passed 00:00:00
mongod-major-upgrade-sharded passed 00:00:00
monitoring-2-0 passed 00:00:00
monitoring-pmm3 passed 00:42:54
multi-cluster-service passed 00:00:00
multi-storage passed 00:00:00
non-voting-and-hidden passed 00:00:00
one-pod passed 00:00:00
operator-self-healing-chaos passed 00:00:00
pitr passed 00:00:00
pitr-physical passed 00:00:00
pitr-sharded passed 00:00:00
pitr-to-new-cluster passed 00:00:00
pitr-physical-backup-source passed 00:00:00
preinit-updates passed 00:00:00
pvc-auto-resize passed 00:00:00
pvc-resize passed 00:00:00
recover-no-primary passed 00:00:00
replset-overrides passed 00:00:00
replset-remapping passed 00:00:00
replset-remapping-sharded passed 00:00:00
rs-shard-migration passed 00:00:00
scaling passed 00:00:00
scheduled-backup passed 00:00:00
security-context passed 00:00:00
self-healing-chaos passed 00:00:00
service-per-pod passed 00:00:00
serviceless-external-nodes passed 00:00:00
smart-update passed 00:00:00
split-horizon passed 00:00:00
split-horizon-manual-tls passed 00:00:00
stable-resource-version passed 00:00:00
storage passed 00:00:00
tls-issue-cert-manager passed 00:00:00
unsafe-psa passed 00:00:00
upgrade passed 00:00:00
upgrade-consistency passed 00:00:00
upgrade-consistency-sharded-tls passed 00:00:00
upgrade-sharded passed 00:00:00
upgrade-partial-backup passed 00:00:00
users passed 00:00:00
users-vault passed 00:00:00
vector-search passed 00:00:00
vector-search-sharded passed 00:00:00
version-service passed 00:00:00
Summary Value
Tests Run 97/97
Job Duration 01:10:22
Total Test Time 01:12:23

commit: 1c5b15f
image: perconalab/percona-server-mongodb-operator:PR-2406-1c5b15f05

@hors hors merged commit 58757da into main Jun 22, 2026
20 checks passed
@hors hors deleted the K8SPSMDB-1640 branch June 22, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L 100-499 lines tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants