@@ -118,6 +118,7 @@ type clickhouseReconciler struct {
118118 commander * commander
119119
120120 versionProbe chctrl.VersionProbeResult
121+ readyReplicas int
121122 databasesInSync bool
122123 staleReplicasCleanedUp bool
123124 pvcRevision string
@@ -173,7 +174,7 @@ func (r *clickhouseReconciler) sync(ctx context.Context, log ctrlutil.Logger) (c
173174 unknownConditions = append (unknownConditions , r .NewCondition (cond , metav1 .ConditionUnknown , v1 .ConditionReasonStepFailed , errMsg ))
174175 }
175176
176- meta .SetStatusCondition (& unknownConditions , r .NewCondition (v1 .ConditionTypeReconcileSucceeded , metav1 .ConditionFalse , v1 .ConditionReasonStepFailed , errMsg ))
177+ chctrl .SetStatusCondition (& unknownConditions , r .NewCondition (v1 .ConditionTypeReconcileSucceeded , metav1 .ConditionFalse , v1 .ConditionReasonStepFailed , errMsg ))
177178 r .SetConditions (log , unknownConditions )
178179
179180 if updateErr := r .UpsertStatus (ctx , log ); updateErr != nil {
@@ -272,7 +273,7 @@ func (r *clickhouseReconciler) reconcileCommonResources(ctx context.Context, log
272273 log .Debug ("cluster secret is up to date" )
273274 }
274275
275- r .commander = newCommander (log , r .Cluster , & r .secret )
276+ r .commander = newCommander (log , r .Cluster , & r .secret , r . GetDialer () )
276277
277278 return nil , nil
278279}
@@ -297,14 +298,20 @@ func (r *clickhouseReconciler) reconcileClusterRevisions(ctx context.Context, lo
297298 Namespace : r .Cluster .Namespace ,
298299 Name : r .Cluster .Spec .KeeperClusterRef .Name ,
299300 }, & r .keeper ); err != nil {
301+ if k8serrors .IsNotFound (err ) {
302+ log .Debug ("keeper cluster not found, waiting" )
303+
304+ return & ctrl.Result {RequeueAfter : chctrl .RequeueOnRefreshTimeout }, nil
305+ }
306+
300307 return nil , fmt .Errorf ("get keeper cluster: %w" , err )
301308 }
302309
303310 if cond := meta .FindStatusCondition (r .keeper .Status .Conditions , string (v1 .ConditionTypeReady )); cond == nil || cond .Status != metav1 .ConditionTrue {
304311 if cond == nil {
305- log .Warn ("keeper cluster is not ready" )
312+ log .Info ("keeper cluster is not ready" )
306313 } else {
307- log .Warn ("keeper cluster is not ready" , "reason" , cond .Reason , "message" , cond .Message )
314+ log .Info ("keeper cluster is not ready" , "reason" , cond .Reason , "message" , cond .Message )
308315 }
309316 }
310317
@@ -377,7 +384,7 @@ func (r *clickhouseReconciler) reconcileActiveReplicaStatus(ctx context.Context,
377384 pinged := false
378385 version := ""
379386
380- if ! hasError {
387+ if ! hasError && sts . Status . ReadyReplicas > 0 {
381388 ctx , cancel := context .WithTimeout (ctx , chctrl .LoadReplicaStateTimeout )
382389 defer cancel ()
383390
@@ -419,6 +426,10 @@ func (r *clickhouseReconciler) reconcileActiveReplicaStatus(ctx context.Context,
419426 }
420427
421428 for id , state := range states {
429+ if state .Ready () {
430+ r .readyReplicas ++
431+ }
432+
422433 if exists := r .SetReplica (id , state ); exists {
423434 log .Debug (fmt .Sprintf ("multiple StatefulSets for single replica %v" , id ),
424435 "replica_id" , id , "statefulset" , state .StatefulSet .Name )
@@ -491,7 +502,12 @@ func (r *clickhouseReconciler) reconcileReplicaResources(ctx context.Context, lo
491502
492503func (r * clickhouseReconciler ) reconcileReplicateSchema (ctx context.Context , log ctrlutil.Logger ) (* ctrl.Result , error ) {
493504 if ! r .Cluster .Spec .Settings .EnableDatabaseSync {
494- log .Info ("database sync is disabled, skipping" )
505+ log .Debug ("database sync is disabled, skipping" )
506+ return nil , nil
507+ }
508+
509+ if r .readyReplicas < 2 {
510+ log .Info ("no ready replicas to replicate schema, skipping" )
495511 return nil , nil
496512 }
497513
@@ -502,11 +518,6 @@ func (r *clickhouseReconciler) reconcileReplicateSchema(ctx context.Context, log
502518 }
503519 }
504520
505- if readyReplicas == nil {
506- log .Info ("no ready replicas to replicate schema, skipping" )
507- return nil , nil
508- }
509-
510521 hasNotSynced := false
511522 replicaDatabases := ctrlutil .ExecuteParallel (readyReplicas , func (id v1.ClickHouseReplicaID ) (v1.ClickHouseReplicaID , map [string ]databaseDescriptor , error ) {
512523 if err := r .commander .EnsureDefaultDatabaseEngine (ctx , log , id ); err != nil {
@@ -675,7 +686,7 @@ func (r *clickhouseReconciler) reconcileCleanUp(ctx context.Context, log ctrluti
675686 }
676687 }
677688
678- if r .Cluster .Spec .Settings .EnableDatabaseSync {
689+ if r .Cluster .Spec .Settings .EnableDatabaseSync && r . readyReplicas > 0 {
679690 if err := r .commander .CleanupDatabaseReplicas (ctx , log , runningStaleReplicas ); err != nil {
680691 log .Warn ("failed to cleanup database replicas" , "error" , err )
681692
0 commit comments