github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/mongodb/config.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  	"errors"
    24  	"net"
    25  	"strconv"
    26  	"time"
    27  
    28  	"github.com/spf13/viper"
    29  )
    30  
    31  const (
    32  	host             = "host"
    33  	username         = "username"
    34  	password         = "password"
    35  	server           = "server"
    36  	databaseName     = "databaseName"
    37  	operationTimeout = "operationTimeout"
    38  	params           = "params"
    39  	adminDatabase    = "admin"
    40  
    41  	defaultTimeout = 5 * time.Second
    42  	defaultDBPort  = 27017
    43  )
    44  
    45  type Config struct {
    46  	hosts            []string
    47  	username         string
    48  	password         string
    49  	replSetName      string
    50  	databaseName     string
    51  	params           string
    52  	direct           bool
    53  	operationTimeout time.Duration
    54  }
    55  
    56  var config *Config
    57  
    58  func NewConfig(properties map[string]string) (*Config, error) {
    59  	config = &Config{
    60  		direct:           true,
    61  		username:         "root",
    62  		operationTimeout: defaultTimeout,
    63  	}
    64  
    65  	if val, ok := properties[host]; ok && val != "" {
    66  		config.hosts = []string{val}
    67  	}
    68  
    69  	if viper.IsSet("KB_SERVICE_PORT") {
    70  		config.hosts = []string{"localhost:" + viper.GetString("KB_SERVICE_PORT")}
    71  	}
    72  
    73  	if len(config.hosts) == 0 {
    74  		return nil, errors.New("must set 'host' in metadata or KB_SERVICE_PORT environment variable")
    75  	}
    76  
    77  	if val, ok := properties[username]; ok && val != "" {
    78  		config.username = val
    79  	}
    80  
    81  	if val, ok := properties[password]; ok && val != "" {
    82  		config.password = val
    83  	}
    84  
    85  	if viper.IsSet("KB_SERVICE_USER") {
    86  		config.username = viper.GetString("KB_SERVICE_USER")
    87  	}
    88  
    89  	if viper.IsSet("KB_SERVICE_PASSWORD") {
    90  		config.password = viper.GetString("KB_SERVICE_PASSWORD")
    91  	}
    92  
    93  	if viper.IsSet("KB_CLUSTER_COMP_NAME") {
    94  		config.replSetName = viper.GetString("KB_CLUSTER_COMP_NAME")
    95  	}
    96  
    97  	config.databaseName = adminDatabase
    98  	if val, ok := properties[databaseName]; ok && val != "" {
    99  		config.databaseName = val
   100  	}
   101  
   102  	if val, ok := properties[params]; ok && val != "" {
   103  		config.params = val
   104  	}
   105  
   106  	var err error
   107  	if val, ok := properties[operationTimeout]; ok && val != "" {
   108  		config.operationTimeout, err = time.ParseDuration(val)
   109  		if err != nil {
   110  			return nil, errors.New("incorrect operationTimeout field from metadata")
   111  		}
   112  	}
   113  
   114  	return config, nil
   115  }
   116  
   117  func (config *Config) GetDBPort() int {
   118  	_, portStr, err := net.SplitHostPort(config.hosts[0])
   119  	if err != nil {
   120  		return defaultDBPort
   121  	}
   122  
   123  	port, err := strconv.Atoi(portStr)
   124  	if err != nil {
   125  		return defaultDBPort
   126  	}
   127  
   128  	return port
   129  }
   130  
   131  func (config *Config) DeepCopy() *Config {
   132  	newConf := *config
   133  	newConf.hosts = make([]string, len(config.hosts))
   134  	copy(newConf.hosts, config.hosts)
   135  	return &newConf
   136  }
   137  
   138  func GetConfig() *Config {
   139  	return config
   140  }