Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26737,6 +26737,8 @@ spec:
value: percona-server-mongodb-operator
- name: RESYNC_PERIOD
value: 5s
- name: RECONCILE_INTERVAL
value: 5s
- name: DISABLE_TELEMETRY
value: "false"
- name: MAX_CONCURRENT_RECONCILES
Expand Down
2 changes: 2 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26763,6 +26763,8 @@ spec:
value: percona-server-mongodb-operator
- name: RESYNC_PERIOD
value: 5s
- name: RECONCILE_INTERVAL
value: 5s
- name: DISABLE_TELEMETRY
value: "false"
- name: MAX_CONCURRENT_RECONCILES
Expand Down
2 changes: 2 additions & 0 deletions deploy/cw-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ spec:
value: percona-server-mongodb-operator
- name: RESYNC_PERIOD
value: 5s
- name: RECONCILE_INTERVAL
value: 5s
- name: DISABLE_TELEMETRY
value: "false"
- name: MAX_CONCURRENT_RECONCILES
Expand Down
2 changes: 2 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ spec:
value: percona-server-mongodb-operator
- name: RESYNC_PERIOD
value: 5s
- name: RECONCILE_INTERVAL
value: 5s
- name: DISABLE_TELEMETRY
value: "false"
- name: MAX_CONCURRENT_RECONCILES
Expand Down
28 changes: 27 additions & 1 deletion pkg/controller/perconaservermongodb/psmdb_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) {
client: client,
scheme: mgr.GetScheme(),
serverVersion: sv,
reconcileIn: time.Second * 5,
reconcileIn: getReconcileInterval(),
crons: NewCronRegistry(),
lockers: newLockStore(),
newPBM: backup.NewPBM,
Expand Down Expand Up @@ -140,6 +140,32 @@ func getOperatorPodImage(ctx context.Context) (string, error) {
return pod.Spec.Containers[0].Image, nil
}

// getReconcileInterval returns the reconciliation interval from the RECONCILE_INTERVAL
// environment variable, or the default of 5 seconds if not set or invalid.
func getReconcileInterval() time.Duration {
defaultInterval := 5 * time.Second

interval := os.Getenv("RECONCILE_INTERVAL")
if interval == "" {
return defaultInterval
}

d, err := time.ParseDuration(interval)
if err != nil {
log := logf.Log.WithName("psmdb-controller")
log.Info("Invalid RECONCILE_INTERVAL value, using default (5s)", "value", interval, "error", err, "default", defaultInterval)
return defaultInterval
}

if d < defaultInterval {
log := logf.Log.WithName("psmdb-controller")
log.Info("RECONCILE_INTERVAL must be at least 5s, using 5s", "value", interval, "default", defaultInterval)
return defaultInterval
}

return d
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if it makes sense to have a value 1second for example.

Copy link
Contributor

Choose a reason for hiding this comment

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

good point, i agree. maybe it's better to not allow values less than 5 seconds and fallback to 5 seconds if value is smaller

}

// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
return builder.ControllerManagedBy(mgr).
Expand Down
66 changes: 66 additions & 0 deletions pkg/controller/perconaservermongodb/psmdb_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ package perconaservermongodb

import (
"context"
"os"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -13,6 +18,67 @@ import (
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
)

func TestGetReconcileInterval(t *testing.T) {
tests := []struct {
name string
envValue string
setEnv bool
want time.Duration
}{
{
name: "unset",
setEnv: false,
want: 5 * time.Second,
},
{
name: "valid duration",
envValue: "30s",
setEnv: true,
want: 30 * time.Second,
},
{
name: "invalid duration falls back to default",
envValue: "invalid",
setEnv: true,
want: 5 * time.Second,
},
{
name: "zero duration falls back to default",
envValue: "0s",
setEnv: true,
want: 5 * time.Second,
},
{
name: "negative duration falls back to default",
envValue: "-5s",
setEnv: true,
want: 5 * time.Second,
},
{
name: "duration less than 5s falls back to default",
envValue: "1s",
setEnv: true,
want: 5 * time.Second,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer func() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think all this logic is not needed, we can simply unsert the env after the end of each test.

We can do something like that:

			defer func() {
				err := os.Unsetenv("RECONCILE_INTERVAL")
				require.NoError(t, err)
			}()
			if tt.setEnv {
				err := os.Setenv("RECONCILE_INTERVAL", tt.envValue)
				require.NoError(t, err)
			}

Which is also handling the errors of setting and unsetting, wdyt?

err := os.Unsetenv("RECONCILE_INTERVAL")
require.NoError(t, err)
}()
if tt.setEnv {
err := os.Setenv("RECONCILE_INTERVAL", tt.envValue)
require.NoError(t, err)
Comment on lines +67 to +73
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

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

This test permanently unsets the RECONCILE_INTERVAL environment variable and does not restore any previously configured value, which can cause hidden dependencies or flakiness if other tests or tooling rely on that variable. Capture the original value at the start of the test case and restore it in the deferred cleanup instead of always calling Unsetenv.

Copilot uses AI. Check for mistakes.
}

got := getReconcileInterval()
assert.Equal(t, tt.want, got)
})
}
}

var _ = Describe("PerconaServerMongoDB", Ordered, func() {
ctx := context.Background()
const ns = "psmdb"
Expand Down
Loading