github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/operations/replica/getrole.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  
    25  	"github.com/go-logr/logr"
    26  	"github.com/pkg/errors"
    27  	ctrl "sigs.k8s.io/controller-runtime"
    28  
    29  	"github.com/1aal/kubeblocks/pkg/lorry/dcs"
    30  	"github.com/1aal/kubeblocks/pkg/lorry/engines"
    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 GetRole struct {
    37  	operations.Base
    38  	dcsStore  dcs.DCS
    39  	dbManager engines.DBManager
    40  	logger    logr.Logger
    41  }
    42  
    43  var getrole operations.Operation = &GetRole{}
    44  
    45  func init() {
    46  	err := operations.Register("getrole", getrole)
    47  	if err != nil {
    48  		panic(err.Error())
    49  	}
    50  }
    51  
    52  func (s *GetRole) 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  	dbManager, err := register.GetDBManager()
    59  	if err != nil {
    60  		return errors.Wrap(err, "get manager failed")
    61  	}
    62  
    63  	s.dbManager = dbManager
    64  	s.logger = ctrl.Log.WithName("getrole")
    65  	return nil
    66  }
    67  
    68  func (s *GetRole) IsReadonly(ctx context.Context) bool {
    69  	return true
    70  }
    71  
    72  func (s *GetRole) Do(ctx context.Context, req *operations.OpsRequest) (*operations.OpsResponse, error) {
    73  	resp := &operations.OpsResponse{
    74  		Data: map[string]any{},
    75  	}
    76  	resp.Data["operation"] = util.GetRoleOperation
    77  
    78  	cluster := s.dcsStore.GetClusterFromCache()
    79  	role, err := s.dbManager.GetReplicaRole(ctx, cluster)
    80  	if err != nil {
    81  		s.logger.Info("executing getrole error", "error", err)
    82  		return resp, err
    83  	}
    84  
    85  	resp.Data["role"] = role
    86  	return resp, err
    87  }