github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/replica/leave.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package replica
    21  
    22  import (
    23  	"context"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/go-logr/logr"
    28  	"github.com/pkg/errors"
    29  
    30  	"github.com/1aal/kubeblocks/pkg/lorry/dcs"
    31  	"github.com/1aal/kubeblocks/pkg/lorry/engines/register"
    32  	"github.com/1aal/kubeblocks/pkg/lorry/operations"
    33  	"github.com/1aal/kubeblocks/pkg/lorry/util"
    34  )
    35  
    36  type Leave struct {
    37  	operations.Base
    38  	dcsStore dcs.DCS
    39  	logger   logr.Logger
    40  	Timeout  time.Duration
    41  }
    42  
    43  var leave operations.Operation = &Leave{}
    44  
    45  func init() {
    46  	err := operations.Register(strings.ToLower(string(util.LeaveMemberOperation)), leave)
    47  	if err != nil {
    48  		panic(err.Error())
    49  	}
    50  }
    51  
    52  func (s *Leave) Init(ctx context.Context) error {
    53  	s.dcsStore = dcs.GetStore()
    54  	if s.dcsStore == nil {
    55  		return errors.New("dcs store init failed")
    56  	}
    57  
    58  	return nil
    59  }
    60  
    61  func (s *Leave) Do(ctx context.Context, req *operations.OpsRequest) (*operations.OpsResponse, error) {
    62  	manager, err := register.GetDBManager()
    63  	if err != nil {
    64  		return nil, errors.Wrap(err, "get manager failed")
    65  	}
    66  
    67  	cluster, err := s.dcsStore.GetCluster()
    68  	if err != nil {
    69  		s.logger.Error(err, "get cluster failed")
    70  		return nil, err
    71  	}
    72  
    73  	currentMember := cluster.GetMemberWithName(manager.GetCurrentMemberName())
    74  	if !cluster.HaConfig.IsDeleting(currentMember) {
    75  		cluster.HaConfig.AddMemberToDelete(currentMember)
    76  		_ = s.dcsStore.UpdateHaConfig()
    77  	}
    78  
    79  	// remove current member from db cluster
    80  	err = manager.LeaveMemberFromCluster(ctx, cluster, manager.GetCurrentMemberName())
    81  	if err != nil {
    82  		s.logger.Error(err, "Leave member from cluster failed")
    83  		return nil, err
    84  	}
    85  
    86  	return nil, nil
    87  }