github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/replica/join.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 Join struct {
    37  	operations.Base
    38  	dcsStore dcs.DCS
    39  	logger   logr.Logger
    40  	Timeout  time.Duration
    41  }
    42  
    43  var join operations.Operation = &Join{}
    44  
    45  func init() {
    46  	err := operations.Register(strings.ToLower(string(util.JoinMemberOperation)), join)
    47  	if err != nil {
    48  		panic(err.Error())
    49  	}
    50  }
    51  
    52  func (s *Join) 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 *Join) 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  	// join current member to db cluster
    74  	err = manager.JoinCurrentMemberToCluster(ctx, cluster)
    75  	if err != nil {
    76  		s.logger.Error(err, "join member to cluster failed")
    77  		return nil, err
    78  	}
    79  
    80  	return nil, nil
    81  }