Add auto sync PXC users with ProxySQL
@@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"strings"
+ "sync/atomic"
"time"
"github.com/pkg/errors"
@@ -95,6 +96,7 @@ type ReconcilePerconaXtraDBCluster struct {
scheme *runtime.Scheme
crons CronRegistry
clientcmd *clientcmd.Client
+ syncUsersState int32
serverVersion *api.ServerVersion
}
@@ -109,6 +111,11 @@ type Shedule struct {
CronShedule string
}
+const (
+ stateFree = 0
+ stateLocked = 1
+)
+
func NewCronRegistry() CronRegistry {
c := CronRegistry{
crons: cron.New(),
@@ -306,6 +313,8 @@ func (r *ReconcilePerconaXtraDBCluster) Reconcile(request reconcile.Request) (re
}
}
+ r.resyncPXCUsersWithProxySQL(o)
+
return rr, nil
}
@@ -679,3 +688,17 @@ func OwnerRef(ro runtime.Object, scheme *runtime.Scheme) (metav1.OwnerReference,
Controller: &trueVar,
}, nil
}
+
+// resyncPXCUsersWithProxySQL calls the method of synchronizing users and makes sure that only one Goroutine works at a time
+func (r *ReconcilePerconaXtraDBCluster) resyncPXCUsersWithProxySQL(cr *api.PerconaXtraDBCluster) {
+ if cr.Status.Status != api.AppStateReady || !atomic.CompareAndSwapInt32(&r.syncUsersState, stateFree, stateLocked) {
+ return
+ }
+ go func() {
+ err := r.syncPXCUsersWithProxySQL(cr)
+ if err != nil {
+ log.Error(err, "sync users")
+ }
+ atomic.StoreInt32(&r.syncUsersState, stateFree)
+ }()
+}
