github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/redis/manager.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 redis
    21  
    22  import (
    23  	"context"
    24  	"strings"
    25  	"time"
    26  
    27  	"github.com/redis/go-redis/v9"
    28  	ctrl "sigs.k8s.io/controller-runtime"
    29  
    30  	"github.com/1aal/kubeblocks/pkg/lorry/engines"
    31  	viper "github.com/1aal/kubeblocks/pkg/viperx"
    32  )
    33  
    34  var (
    35  	redisUser   = "default"
    36  	redisPasswd = ""
    37  )
    38  
    39  type Manager struct {
    40  	engines.DBManagerBase
    41  	client         redis.UniversalClient
    42  	clientSettings *Settings
    43  
    44  	ctx     context.Context
    45  	cancel  context.CancelFunc
    46  	startAt time.Time
    47  }
    48  
    49  var _ engines.DBManager = &Manager{}
    50  
    51  func NewManager(properties engines.Properties) (engines.DBManager, error) {
    52  	logger := ctrl.Log.WithName("Redis")
    53  
    54  	if viper.IsSet("KB_SERVICE_USER") {
    55  		redisUser = viper.GetString("KB_SERVICE_USER")
    56  	}
    57  
    58  	if viper.IsSet("KB_SERVICE_PASSWORD") {
    59  		redisPasswd = viper.GetString("KB_SERVICE_PASSWORD")
    60  	}
    61  
    62  	managerBase, err := engines.NewDBManagerBase(logger)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	mgr := &Manager{
    67  		DBManagerBase: *managerBase,
    68  	}
    69  
    70  	mgr.startAt = time.Now()
    71  
    72  	defaultSettings := &Settings{
    73  		Password: redisPasswd,
    74  		Username: redisUser,
    75  	}
    76  	mgr.client, mgr.clientSettings, err = ParseClientFromProperties(map[string]string(properties), defaultSettings)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	mgr.ctx, mgr.cancel = context.WithCancel(context.Background())
    82  	return mgr, nil
    83  }
    84  
    85  func (mgr *Manager) IsDBStartupReady() bool {
    86  	if mgr.DBStartupReady {
    87  		return true
    88  	}
    89  	ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
    90  	defer cancel()
    91  
    92  	if _, err := mgr.client.Ping(ctx).Result(); err != nil {
    93  		mgr.Logger.Info("connecting to redis failed", "host", mgr.clientSettings.Host, "error", err)
    94  		return false
    95  	}
    96  
    97  	mgr.DBStartupReady = true
    98  	mgr.Logger.Info("DB startup ready")
    99  	return true
   100  }
   101  
   102  func tokenizeCmd2Args(cmd string) []interface{} {
   103  	args := strings.Split(cmd, " ")
   104  	redisArgs := make([]interface{}, 0, len(args))
   105  	for _, arg := range args {
   106  		redisArgs = append(redisArgs, arg)
   107  	}
   108  	return redisArgs
   109  }