@@ -78,12 +78,7 @@ func (s *StatusSyncer) GetOwner() runtime.Object { return s.MysqlCluster }
7878
7979// Sync persists data into the external store.
8080func (s * StatusSyncer ) Sync (ctx context.Context ) (syncer.SyncResult , error ) {
81- clusterCondition := apiv1alpha1.ClusterCondition {
82- Type : apiv1alpha1 .ClusterInit ,
83- Status : corev1 .ConditionTrue ,
84- LastTransitionTime : metav1 .NewTime (time .Now ()),
85- }
86- s .Status .State = apiv1alpha1 .ClusterInit
81+ clusterCondition := s .updateClusterStatus ()
8782
8883 list := corev1.PodList {}
8984 err := s .cli .List (
@@ -109,23 +104,24 @@ func (s *StatusSyncer) Sync(ctx context.Context) (syncer.SyncResult, error) {
109104 }
110105 case corev1 .PodScheduled :
111106 if cond .Reason == corev1 .PodReasonUnschedulable {
107+ // When an error occurs, it is first recorded in the condition,
108+ // but the cluster status is not updated immediately.
112109 clusterCondition = apiv1alpha1.ClusterCondition {
113- Type : apiv1alpha1 .ClusterError ,
110+ Type : apiv1alpha1 .ConditionError ,
114111 Status : corev1 .ConditionTrue ,
115112 LastTransitionTime : metav1 .NewTime (time .Now ()),
116113 Reason : corev1 .PodReasonUnschedulable ,
117114 Message : cond .Message ,
118115 }
119- s .Status .State = apiv1alpha1 .ClusterError
120116 }
121117 }
122118 }
123119 }
124120
125121 s .Status .ReadyNodes = len (readyNodes )
126- if s .Status .ReadyNodes == int (* s .Spec .Replicas ) {
127- s .Status .State = apiv1alpha1 .ClusterReady
128- clusterCondition .Type = apiv1alpha1 .ClusterReady
122+ if s .Status .ReadyNodes == int (* s .Spec .Replicas ) && int ( * s . Spec . Replicas ) != 0 {
123+ s .Status .State = apiv1alpha1 .ClusterReadyState
124+ clusterCondition .Type = apiv1alpha1 .ConditionReady
129125 }
130126
131127 if len (s .Status .Conditions ) == 0 {
@@ -140,10 +136,45 @@ func (s *StatusSyncer) Sync(ctx context.Context) (syncer.SyncResult, error) {
140136 s .Status .Conditions = s .Status .Conditions [len (s .Status .Conditions )- maxStatusesQuantity :]
141137 }
142138
143- // update ready nodes' status.
139+ // Update ready nodes' status.
144140 return syncer.SyncResult {}, s .updateNodeStatus (ctx , s .cli , readyNodes )
145141}
146142
143+ // updateClusterStatus update the cluster status and returns condition.
144+ func (s * StatusSyncer ) updateClusterStatus () apiv1alpha1.ClusterCondition {
145+ clusterCondition := apiv1alpha1.ClusterCondition {
146+ Type : apiv1alpha1 .ConditionInit ,
147+ Status : corev1 .ConditionTrue ,
148+ LastTransitionTime : metav1 .NewTime (time .Now ()),
149+ }
150+
151+ oldState := s .Status .State
152+ // If the state does not exist, the cluster is being initialized.
153+ if oldState == "" {
154+ s .Status .State = apiv1alpha1 .ClusterInitState
155+ return clusterCondition
156+ }
157+ // If the expected number of replicas and the actual number
158+ // of replicas are both 0, the cluster has been closed.
159+ if int (* s .Spec .Replicas ) == 0 && s .Status .ReadyNodes == 0 {
160+ clusterCondition .Type = apiv1alpha1 .ConditionClose
161+ s .Status .State = apiv1alpha1 .ClusterCloseState
162+ return clusterCondition
163+ }
164+ // When the cluster is ready or closed, the number of replicas changes,
165+ // indicating that the cluster is updating nodes.
166+ if oldState == apiv1alpha1 .ClusterReadyState || oldState == apiv1alpha1 .ClusterCloseState {
167+ if int (* s .Spec .Replicas ) != s .Status .ReadyNodes {
168+ clusterCondition .Type = apiv1alpha1 .ConditionUpdate
169+ s .Status .State = apiv1alpha1 .ClusterUpdateState
170+ return clusterCondition
171+ }
172+ }
173+
174+ clusterCondition .Type = apiv1alpha1 .ClusterConditionType (oldState )
175+ return clusterCondition
176+ }
177+
147178// updateNodeStatus update the node status.
148179func (s * StatusSyncer ) updateNodeStatus (ctx context.Context , cli client.Client , pods []corev1.Pod ) error {
149180 sctName := s .GetNameForResource (utils .Secret )
0 commit comments