-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathcommon.go
240 lines (214 loc) · 8.27 KB
/
common.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2017 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/sorintlab/stolon/internal/cluster"
"github.com/sorintlab/stolon/internal/common"
"github.com/sorintlab/stolon/internal/store"
"github.com/sorintlab/stolon/internal/util"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
type CommonConfig struct {
IsStolonCtl bool
StoreBackend string
StoreEndpoints string
StorePrefix string
StoreCertFile string
StoreKeyFile string
StoreCAFile string
StoreSkipTlsVerify bool
ClusterName string
MetricsListenAddress string
LogColor bool
LogLevel string
Debug bool
KubeResourceKind string
KubeConfig string
KubeContext string
KubeNamespace string
StoreTimeout time.Duration
}
func AddCommonFlags(cmd *cobra.Command, cfg *CommonConfig) {
cmd.PersistentFlags().StringVar(&cfg.ClusterName, "cluster-name", "", "cluster name")
cmd.PersistentFlags().StringVar(&cfg.StoreBackend, "store-backend", "", "store backend type (etcdv2/etcd, etcdv3, consul or kubernetes)")
cmd.PersistentFlags().StringVar(&cfg.StoreEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (use https scheme for tls communication) (defaults: http://127.0.0.1:2379 for etcd, http://127.0.0.1:8500 for consul)")
cmd.PersistentFlags().DurationVar(&cfg.StoreTimeout, "store-timeout", cluster.DefaultStoreTimeout, "store request timeout")
cmd.PersistentFlags().StringVar(&cfg.StorePrefix, "store-prefix", common.StorePrefix, "the store base prefix")
cmd.PersistentFlags().StringVar(&cfg.StoreCertFile, "store-cert-file", "", "certificate file for client identification to the store")
cmd.PersistentFlags().StringVar(&cfg.StoreKeyFile, "store-key", "", "private key file for client identification to the store")
cmd.PersistentFlags().BoolVar(&cfg.StoreSkipTlsVerify, "store-skip-tls-verify", false, "skip store certificate verification (insecure!!!)")
cmd.PersistentFlags().StringVar(&cfg.StoreCAFile, "store-ca-file", "", "verify certificates of HTTPS-enabled store servers using this CA bundle")
cmd.PersistentFlags().StringVar(&cfg.MetricsListenAddress, "metrics-listen-address", "", "metrics listen address i.e \"0.0.0.0:8080\" (disabled by default)")
cmd.PersistentFlags().StringVar(&cfg.KubeResourceKind, "kube-resource-kind", "", `the k8s resource kind to be used to store stolon clusterdata and do sentinel leader election (only "configmap" is currently supported)`)
if !cfg.IsStolonCtl {
cmd.PersistentFlags().BoolVar(&cfg.LogColor, "log-color", false, "enable color in log output (default if attached to a terminal)")
cmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", "debug, info (default), warn or error")
}
if cfg.IsStolonCtl {
cmd.PersistentFlags().StringVar(&cfg.LogLevel, "log-level", "info", "debug, info (default), warn or error")
cmd.PersistentFlags().StringVar(&cfg.KubeConfig, "kubeconfig", "", "path to kubeconfig file. Overrides $KUBECONFIG")
cmd.PersistentFlags().StringVar(&cfg.KubeContext, "kube-context", "", "name of the kubeconfig context to use")
cmd.PersistentFlags().StringVar(&cfg.KubeNamespace, "kube-namespace", "", "name of the kubernetes namespace to use")
}
}
var (
// clusterIdentifier provides a Prometheus metric that should uniquely identify the
// cluster that any stolon component is associated with. Users can then join between
// various metric series for the same cluster without making assumptions about service
// discovery labels.
clusterIdentifier = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "stolon_cluster_identifier",
Help: "Set to 1, is labelled with the cluster_name and component",
},
[]string{"cluster_name", "component"},
)
)
func init() {
prometheus.MustRegister(clusterIdentifier)
}
func CheckCommonConfig(cfg *CommonConfig) error {
if cfg.ClusterName == "" {
return fmt.Errorf("cluster name required")
}
if cfg.StoreBackend == "" {
return fmt.Errorf("store backend type required")
}
switch cfg.StoreBackend {
case "consul":
case "etcd":
// etcd is old alias for etcdv2
cfg.StoreBackend = "etcdv2"
case "etcdv2":
case "etcdv3":
case "kubernetes":
if cfg.KubeResourceKind == "" {
return fmt.Errorf("unspecified kubernetes resource kind")
}
if cfg.KubeResourceKind != "configmap" {
return fmt.Errorf("wrong kubernetes resource kind: %q", cfg.KubeResourceKind)
}
default:
return fmt.Errorf("Unknown store backend: %q", cfg.StoreBackend)
}
return nil
}
// SetMetrics should be called by any stolon component that outputs application metrics.
// It sets the clusterIdentifier metric, which is key to joining across all the other
// metric series.
func SetMetrics(cfg *CommonConfig, component string) {
clusterIdentifier.WithLabelValues(cfg.ClusterName, component).Set(1)
}
func IsColorLoggerEnable(cmd *cobra.Command, cfg *CommonConfig) bool {
if cmd.PersistentFlags().Changed("log-color") {
return cfg.LogColor
} else {
return isatty.IsTerminal(os.Stderr.Fd()) || isatty.IsCygwinTerminal(os.Stderr.Fd())
}
}
func NewKVStore(cfg *CommonConfig) (store.KVStore, error) {
return store.NewKVStore(store.Config{
Backend: store.Backend(cfg.StoreBackend),
Endpoints: cfg.StoreEndpoints,
Timeout: cfg.StoreTimeout,
CertFile: cfg.StoreCertFile,
KeyFile: cfg.StoreKeyFile,
CAFile: cfg.StoreCAFile,
SkipTLSVerify: cfg.StoreSkipTlsVerify,
})
}
func NewStore(cfg *CommonConfig) (store.Store, error) {
var s store.Store
switch cfg.StoreBackend {
case "consul":
fallthrough
case "etcdv2":
fallthrough
case "etcdv3":
storePath := filepath.Join(cfg.StorePrefix, cfg.ClusterName)
kvstore, err := NewKVStore(cfg)
if err != nil {
return nil, fmt.Errorf("cannot create kv store: %v", err)
}
s = store.NewKVBackedStore(kvstore, storePath)
case "kubernetes":
kubecli, podName, namespace, err := getKubeValues(cfg)
if err != nil {
return nil, err
}
s, err = store.NewKubeStore(kubecli, podName, namespace, cfg.ClusterName)
if err != nil {
return nil, fmt.Errorf("cannot create store: %v", err)
}
}
return s, nil
}
func NewElection(cfg *CommonConfig, uid string) (store.Election, error) {
var election store.Election
switch cfg.StoreBackend {
case "consul":
fallthrough
case "etcdv2":
fallthrough
case "etcdv3":
storePath := filepath.Join(cfg.StorePrefix, cfg.ClusterName)
kvstore, err := NewKVStore(cfg)
if err != nil {
return nil, fmt.Errorf("cannot create kv store: %v", err)
}
election = store.NewKVBackedElection(kvstore, filepath.Join(storePath, common.SentinelLeaderKey), uid, cfg.StoreTimeout)
case "kubernetes":
kubecli, podName, namespace, err := getKubeValues(cfg)
if err != nil {
return nil, err
}
election, err = store.NewKubeElection(kubecli, podName, namespace, cfg.ClusterName, uid)
if err != nil {
return nil, err
}
}
return election, nil
}
func getKubeValues(cfg *CommonConfig) (*kubernetes.Clientset, string, string, error) {
kubeClientConfig := util.NewKubeClientConfig(cfg.KubeConfig, cfg.KubeContext, cfg.KubeNamespace)
kubecfg, err := kubeClientConfig.ClientConfig()
if err != nil {
return nil, "", "", err
}
kubecfg.Timeout = cfg.StoreTimeout
kubecli, err := kubernetes.NewForConfig(kubecfg)
if err != nil {
return nil, "", "", fmt.Errorf("cannot create kubernetes client: %v", err)
}
var podName string
if !cfg.IsStolonCtl {
podName, err = util.PodName()
if err != nil {
return nil, "", "", err
}
}
namespace, _, err := kubeClientConfig.Namespace()
if err != nil {
return nil, "", "", err
}
return kubecli, podName, namespace, nil
}