forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
182 lines (154 loc) · 5.89 KB
/
main.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/fatih/structtag"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/pkg/errors"
"github.com/thanos-io/objstore/client"
"github.com/thanos-io/objstore/providers/azure"
"github.com/thanos-io/objstore/providers/bos"
"github.com/thanos-io/objstore/providers/cos"
"github.com/thanos-io/objstore/providers/filesystem"
"github.com/thanos-io/objstore/providers/gcs"
"github.com/thanos-io/objstore/providers/oss"
"github.com/thanos-io/objstore/providers/s3"
"github.com/thanos-io/objstore/providers/swift"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v2"
"github.com/thanos-io/thanos/pkg/httpconfig"
"github.com/thanos-io/thanos/pkg/alert"
"github.com/thanos-io/thanos/pkg/cacheutil"
"github.com/thanos-io/thanos/pkg/logging"
"github.com/thanos-io/thanos/pkg/queryfrontend"
storecache "github.com/thanos-io/thanos/pkg/store/cache"
trclient "github.com/thanos-io/thanos/pkg/tracing/client"
"github.com/thanos-io/thanos/pkg/tracing/elasticapm"
"github.com/thanos-io/thanos/pkg/tracing/google_cloud"
"github.com/thanos-io/thanos/pkg/tracing/jaeger"
"github.com/thanos-io/thanos/pkg/tracing/lightstep"
"github.com/thanos-io/thanos/pkg/tracing/otlp"
)
var (
configs map[string]interface{}
possibleValues []string
bucketConfigs = map[client.ObjProvider]interface{}{
client.AZURE: azure.Config{},
client.GCS: gcs.Config{},
client.S3: s3.DefaultConfig,
client.SWIFT: swift.DefaultConfig,
client.COS: cos.DefaultConfig,
client.ALIYUNOSS: oss.Config{},
client.FILESYSTEM: filesystem.Config{},
client.BOS: bos.Config{},
}
tracingConfigs = map[trclient.TracingProvider]interface{}{
trclient.OpenTelemetryProtocol: otlp.Config{},
trclient.Jaeger: jaeger.Config{},
trclient.GoogleCloud: google_cloud.Config{},
trclient.ElasticAPM: elasticapm.Config{},
trclient.Lightstep: lightstep.Config{},
}
indexCacheConfigs = map[storecache.IndexCacheProvider]interface{}{
storecache.INMEMORY: storecache.InMemoryIndexCacheConfig{},
storecache.MEMCACHED: cacheutil.MemcachedClientConfig{},
storecache.REDIS: cacheutil.DefaultRedisClientConfig,
}
queryfrontendCacheConfigs = map[queryfrontend.ResponseCacheProvider]interface{}{
queryfrontend.INMEMORY: queryfrontend.InMemoryResponseCacheConfig{},
queryfrontend.MEMCACHED: queryfrontend.MemcachedResponseCacheConfig{},
queryfrontend.REDIS: queryfrontend.DefaultRedisConfig,
}
)
func init() {
configs = map[string]interface{}{}
configs[name(logging.RequestConfig{})] = logging.RequestConfig{}
alertmgrCfg := alert.DefaultAlertmanagerConfig()
alertmgrCfg.EndpointsConfig.FileSDConfigs = []httpconfig.FileSDConfig{{}}
configs[name(alert.AlertingConfig{})] = alert.AlertingConfig{Alertmanagers: []alert.AlertmanagerConfig{alertmgrCfg}}
queryCfg := httpconfig.DefaultConfig()
queryCfg.EndpointsConfig.FileSDConfigs = []httpconfig.FileSDConfig{{}}
configs[name(httpconfig.Config{})] = []httpconfig.Config{queryCfg}
for typ, config := range bucketConfigs {
configs[name(config)] = client.BucketConfig{Type: typ, Config: config}
}
for typ, config := range tracingConfigs {
configs[name(config)] = trclient.TracingConfig{Type: typ, Config: config}
}
for typ, config := range indexCacheConfigs {
configs[name(config)] = storecache.IndexCacheConfig{Type: typ, Config: config}
}
for typ, config := range queryfrontendCacheConfigs {
configs[name(config)] = queryfrontend.CacheProviderConfig{Type: typ, Config: config}
}
for k := range configs {
possibleValues = append(possibleValues, k)
}
}
func name(typ interface{}) string {
return fmt.Sprintf("%T", typ)
}
func main() {
app := kingpin.New(filepath.Base(os.Args[0]), "Thanos config examples generator.")
app.HelpFlag.Short('h')
structName := app.Flag("name", fmt.Sprintf("Name of the struct to generated example for. Possible values: %v", strings.Join(possibleValues, ","))).Required().String()
errLogger := level.Error(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)))
if _, err := app.Parse(os.Args[1:]); err != nil {
errLogger.Log("err", err)
os.Exit(1)
}
if c, ok := configs[*structName]; ok {
if err := generate(c, os.Stdout); err != nil {
errLogger.Log("err", err)
os.Exit(1)
}
return
}
errLogger.Log("err", errors.Errorf("%v struct not found. Possible values %v", *structName, strings.Join(possibleValues, ",")))
os.Exit(1)
}
func generate(obj interface{}, w io.Writer) error {
// We forbid omitempty option. This is for simplification for doc generation.
if err := checkForOmitEmptyTagOption(obj); err != nil {
return errors.Wrap(err, "invalid type")
}
return yaml.NewEncoder(w).Encode(obj)
}
func checkForOmitEmptyTagOption(obj interface{}) error {
return checkForOmitEmptyTagOptionRec(reflect.ValueOf(obj))
}
func checkForOmitEmptyTagOptionRec(v reflect.Value) error {
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
tags, err := structtag.Parse(string(v.Type().Field(i).Tag))
if err != nil {
return errors.Wrapf(err, "%s: failed to parse tag %q", v.Type().Field(i).Name, v.Type().Field(i).Tag)
}
tag, err := tags.Get("yaml")
if err != nil {
return errors.Wrapf(err, "%s: failed to get tag %q", v.Type().Field(i).Name, v.Type().Field(i).Tag)
}
for _, opts := range tag.Options {
if opts == "omitempty" {
return errors.Errorf("omitempty is forbidden for config, but spotted on field '%s'", v.Type().Field(i).Name)
}
}
if err := checkForOmitEmptyTagOptionRec(v.Field(i)); err != nil {
return errors.Wrapf(err, "%s", v.Type().Field(i).Name)
}
}
case reflect.Ptr:
return errors.New("nil pointers are not allowed in configuration")
case reflect.Interface:
return checkForOmitEmptyTagOptionRec(v.Elem())
}
return nil
}