Skip to content

Commit 6995236

Browse files
authored
Merge branch 'main' into K8SPSMDB-1211
2 parents 756aefe + 2d30b4f commit 6995236

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

pkg/controller/perconaservermongodbbackup/perconaservermongodbbackup_controller.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ import (
3737
"github.com/percona/percona-server-mongodb-operator/pkg/version"
3838
)
3939

40-
/**
41-
* USER ACTION REQUIRED: This is a scaffold file intended for the user to modify with their own Controller
42-
* business logic. Delete these comments after modifying this file.*
43-
*/
44-
4540
// Add creates a new PerconaServerMongoDBBackup Controller and adds it to the Manager. The Manager will set fields on the Controller
4641
// and Start it when the Manager is Started.
4742
func Add(mgr manager.Manager) error {
@@ -61,6 +56,7 @@ func newReconciler(mgr manager.Manager) (reconcile.Reconciler, error) {
6156

6257
return &ReconcilePerconaServerMongoDBBackup{
6358
client: mgr.GetClient(),
59+
apiReader: mgr.GetAPIReader(),
6460
scheme: mgr.GetScheme(),
6561
clientcmd: cli,
6662
newPBMFunc: backup.NewPBM,
@@ -91,6 +87,7 @@ type ReconcilePerconaServerMongoDBBackup struct {
9187
// This client, initialized using mgr.Client() above, is a split client
9288
// that reads objects from the cache and writes to the apiserver
9389
client client.Client
90+
apiReader client.Reader
9491
scheme *runtime.Scheme
9592
clientcmd *clientcmd.Client
9693

@@ -113,7 +110,14 @@ func (r *ReconcilePerconaServerMongoDBBackup) Reconcile(ctx context.Context, req
113110
}
114111
// Fetch the PerconaServerMongoDBBackup instance
115112
cr := &psmdbv1.PerconaServerMongoDBBackup{}
116-
err := r.client.Get(ctx, request.NamespacedName, cr)
113+
114+
// Here we use k8s APIReader to read the k8s object by making the
115+
// direct call to k8s apiserver instead of using k8sClient.
116+
// The reason is that k8sClient uses a cache and sometimes k8sClient can
117+
// return stale copy of object.
118+
// It is okay to make direct call to k8s apiserver because we are only
119+
// making single read call for complete reconciler loop.
120+
err := r.apiReader.Get(ctx, request.NamespacedName, cr)
117121
if err != nil {
118122
if k8serrors.IsNotFound(err) {
119123
// Request object not found, could have been deleted after reconcile request.
@@ -125,6 +129,8 @@ func (r *ReconcilePerconaServerMongoDBBackup) Reconcile(ctx context.Context, req
125129
return rr, err
126130
}
127131

132+
log.V(1).Info("Got object from API server", "state", cr.Status.State)
133+
128134
if (cr.Status.State == psmdbv1.BackupStateReady || cr.Status.State == psmdbv1.BackupStateError) &&
129135
cr.ObjectMeta.DeletionTimestamp == nil {
130136
return reconcile.Result{}, nil

0 commit comments

Comments
 (0)