@@ -25,6 +25,7 @@ import (
2525 corev1 "k8s.io/api/core/v1"
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727 "k8s.io/apimachinery/pkg/runtime"
28+ "k8s.io/apimachinery/pkg/types"
2829 "sigs.k8s.io/controller-runtime/pkg/client"
2930
3031 apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
@@ -95,6 +96,12 @@ func (s *StatusSyncer) Sync(ctx context.Context) (syncer.SyncResult, error) {
9596 // get ready nodes.
9697 var readyNodes []corev1.Pod
9798 for _ , pod := range list .Items {
99+ if pod .ObjectMeta .Labels [utils .LableRebuild ] == "true" {
100+ if err := s .AutoRebuild (ctx , & pod ); err != nil {
101+ log .Error (err , "failed to AutoRebuild" , "pod" , pod .Name , "namespace" , pod .Namespace )
102+ }
103+ continue
104+ }
98105 for _ , cond := range pod .Status .Conditions {
99106 switch cond .Type {
100107 case corev1 .ContainersReady :
@@ -183,6 +190,40 @@ func (s *StatusSyncer) updateClusterStatus() apiv1alpha1.ClusterCondition {
183190 return clusterCondition
184191}
185192
193+ // Rebuild Pod by deleting and creating it.
194+ // Notice: This function just delete Pod and PVC,
195+ // then after k8s recreate pod, it will clone and initial it.
196+ func (s * StatusSyncer ) AutoRebuild (ctx context.Context , pod * corev1.Pod ) error {
197+ ordinal , err := utils .GetOrdinal (pod .Name )
198+ if err != nil {
199+ return err
200+
201+ }
202+ // Set Pod UnHealthy.
203+ pod .Labels ["healthy" ] = "no"
204+ if err := s .cli .Update (ctx , pod ); err != nil {
205+ return err
206+ }
207+ // Delete the Pod.
208+ if err := s .cli .Delete (ctx , pod ); err != nil {
209+ return err
210+ }
211+ // Delete the pvc.
212+ pvcName := fmt .Sprintf ("%s-%s-%d" , utils .DataVolumeName ,
213+ s .GetNameForResource (utils .StatefulSet ), ordinal )
214+ pvc := corev1.PersistentVolumeClaim {}
215+
216+ if err := s .cli .Get (ctx ,
217+ types.NamespacedName {Name : pvcName , Namespace : s .Namespace },
218+ & pvc ); err != nil {
219+ return err
220+ }
221+ if err := s .cli .Delete (ctx , & pvc ); err != nil {
222+ return err
223+ }
224+ return nil
225+ }
226+
186227// updateNodeStatus update the node status.
187228func (s * StatusSyncer ) updateNodeStatus (ctx context.Context , cli client.Client , pods []corev1.Pod ) error {
188229 for _ , pod := range pods {
0 commit comments