Skip to content
Merged
Changes from 4 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
31 changes: 30 additions & 1 deletion pkg/controller/perconaservermongodb/mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
logf "sigs.k8s.io/controller-runtime/pkg/log"

api "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
Expand Down Expand Up @@ -719,8 +721,35 @@ func (r *ReconcilePerconaServerMongoDB) handleReplsetInit(ctx context.Context, c
return nil, nil, fmt.Errorf("exec rs.initiate: %v / %s / %s", err, outb.String(), errb.String())
}

backoff := wait.Backoff{
Steps: 5,
Duration: 50 * time.Millisecond,
Factor: 5.0,
Jitter: 0.1,
}
err = retry.OnError(backoff, func(err error) bool { return true }, func() error {
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.

Are we going to also retry permanent errors, like auth, for example?

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.

i think yes. at this point there shouldn't be any auth errors any way. and in the worst case scenario we'll retry for 6 seconds.

var stderr, stdout bytes.Buffer

hello := []string{"sh", "-c",
mongoCmd + " --quiet --eval 'db.runCommand({ hello: 1 }).isWritablePrimary'"}
err = r.clientcmd.Exec(ctx, &pod, "mongod", hello, nil, &stdout, &stderr, false)
if err != nil {
return errors.Wrapf(err, "run hello stdout: %s, stderr: %s", stdout.String(), stderr.String())
}

out := strings.TrimSpace(stdout.String())
if out != "true" {
return errors.Errorf("%s is not the writable primary", pod.Name)
}

log.Info(pod.Name+" is the writable primary", "replset", replsetName)

return nil
})
Comment on lines +716 to +740
Copy link

Copilot AI Jan 5, 2026

Choose a reason for hiding this comment

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

The new backoff retry logic for waiting until a primary is elected lacks test coverage. Consider adding unit tests to verify the retry behavior, including successful primary election, timeout scenarios, and error handling. Other functions in mgo_test.go demonstrate the testing pattern used in this package.

Copilot uses AI. Check for mistakes.
if err != nil {
return nil, nil, errors.Wrap(err, "wait for replset initialization")
}
log.Info("replset initialized", "replset", replsetName, "pod", pod.Name)
time.Sleep(time.Second * 5)

log.Info("creating user admin", "replset", replsetName, "pod", pod.Name, "user", api.RoleUserAdmin)
userAdmin, err := getInternalCredentials(ctx, r.client, cr, api.RoleUserAdmin)
Expand Down
Loading