-
Notifications
You must be signed in to change notification settings - Fork 160
/
config.go
140 lines (111 loc) · 3.23 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mongodb
import (
"errors"
"net"
"strconv"
"time"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)
const (
host = "host"
username = "username"
password = "password"
server = "server"
databaseName = "databaseName"
operationTimeout = "operationTimeout"
params = "params"
adminDatabase = "admin"
defaultTimeout = 5 * time.Second
defaultDBPort = 27017
)
type Config struct {
hosts []string
username string
password string
replSetName string
databaseName string
params string
direct bool
operationTimeout time.Duration
}
var config *Config
func NewConfig(properties map[string]string) (*Config, error) {
config = &Config{
direct: true,
username: "root",
operationTimeout: defaultTimeout,
}
if val, ok := properties[host]; ok && val != "" {
config.hosts = []string{val}
}
if viper.IsSet("KB_SERVICE_PORT") {
config.hosts = []string{"localhost:" + viper.GetString("KB_SERVICE_PORT")}
}
if len(config.hosts) == 0 {
return nil, errors.New("must set 'host' in metadata or KB_SERVICE_PORT environment variable")
}
if val, ok := properties[username]; ok && val != "" {
config.username = val
}
if val, ok := properties[password]; ok && val != "" {
config.password = val
}
if viper.IsSet("KB_SERVICE_USER") {
config.username = viper.GetString("KB_SERVICE_USER")
}
if viper.IsSet("KB_SERVICE_PASSWORD") {
config.password = viper.GetString("KB_SERVICE_PASSWORD")
}
if viper.IsSet("KB_CLUSTER_COMP_NAME") {
config.replSetName = viper.GetString("KB_CLUSTER_COMP_NAME")
}
config.databaseName = adminDatabase
if val, ok := properties[databaseName]; ok && val != "" {
config.databaseName = val
}
if val, ok := properties[params]; ok && val != "" {
config.params = val
}
var err error
if val, ok := properties[operationTimeout]; ok && val != "" {
config.operationTimeout, err = time.ParseDuration(val)
if err != nil {
return nil, errors.New("incorrect operationTimeout field from metadata")
}
}
return config, nil
}
func (config *Config) GetDBPort() int {
_, portStr, err := net.SplitHostPort(config.hosts[0])
if err != nil {
return defaultDBPort
}
port, err := strconv.Atoi(portStr)
if err != nil {
return defaultDBPort
}
return port
}
func (config *Config) DeepCopy() *Config {
newConf := *config
newConf.hosts = make([]string, len(config.hosts))
copy(newConf.hosts, config.hosts)
return &newConf
}
func GetConfig() *Config {
return config
}