github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mongodb/replset.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 mongodb
    21  
    22  import (
    23  	"context"
    24  
    25  	"github.com/pkg/errors"
    26  	"go.mongodb.org/mongo-driver/bson"
    27  	"go.mongodb.org/mongo-driver/mongo"
    28  )
    29  
    30  func GetReplSetStatus(ctx context.Context, client *mongo.Client) (*ReplSetStatus, error) {
    31  	status := &ReplSetStatus{}
    32  
    33  	resp := client.Database("admin").RunCommand(ctx, bson.D{{Key: "replSetGetStatus", Value: 1}})
    34  	if resp.Err() != nil {
    35  		err := errors.Wrap(resp.Err(), "replSetGetStatus")
    36  		return nil, err
    37  	}
    38  
    39  	if err := resp.Decode(status); err != nil {
    40  		err := errors.Wrap(err, "failed to decode rs status")
    41  		return nil, err
    42  	}
    43  
    44  	if status.OK != 1 {
    45  		err := errors.Errorf("mongo says: %s", status.Errmsg)
    46  		return nil, err
    47  	}
    48  
    49  	return status, nil
    50  }
    51  
    52  func SetReplSetConfig(ctx context.Context, rsClient *mongo.Client, cfg *RSConfig) error {
    53  	resp := OKResponse{}
    54  
    55  	res := rsClient.Database("admin").RunCommand(ctx, bson.D{{Key: "replSetReconfig", Value: cfg}})
    56  	if res.Err() != nil {
    57  		err := errors.Wrap(res.Err(), "replSetReconfig")
    58  		return err
    59  	}
    60  
    61  	if err := res.Decode(&resp); err != nil {
    62  		err = errors.Wrap(err, "failed to decode to replSetReconfigResponse")
    63  		return err
    64  	}
    65  
    66  	if resp.OK != 1 {
    67  		err := errors.Errorf("mongo says: %s", resp.Errmsg)
    68  		return err
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func GetReplSetConfig(ctx context.Context, client *mongo.Client) (*RSConfig, error) {
    75  	resp := ReplSetGetConfig{}
    76  	res := client.Database("admin").RunCommand(ctx, bson.D{{Key: "replSetGetConfig", Value: 1}})
    77  	if res.Err() != nil {
    78  		err := errors.Wrap(res.Err(), "replSetGetConfig")
    79  		return nil, err
    80  	}
    81  	if err := res.Decode(&resp); err != nil {
    82  		err := errors.Wrap(err, "failed to decode to replSetGetConfig")
    83  		return nil, err
    84  	}
    85  
    86  	if resp.Config == nil {
    87  		err := errors.Errorf("mongo says: %s", resp.Errmsg)
    88  		return nil, err
    89  	}
    90  
    91  	return resp.Config, nil
    92  }