/
render.go
322 lines (272 loc) · 9.02 KB
/
render.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package cmd
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"gopkg.in/op/go-logging.v1"
"sigs.k8s.io/yaml"
"github.com/gitpod-io/observability/installer/pkg/common"
"github.com/gitpod-io/observability/installer/pkg/components"
"github.com/gitpod-io/observability/installer/pkg/config"
"github.com/gitpod-io/observability/installer/pkg/importer"
"github.com/gitpod-io/observability/installer/pkg/postprocess"
)
type AppType string
const (
MonitoringSatelliteApp AppType = "monitoring-satellite"
MonitoringCentralApp AppType = "monitoring-central"
)
// String is used both by fmt.Print and by Cobra in help text
func (a *AppType) String() string {
return string(*a)
}
// Set must have pointer receiver so it doesn't change the value of a copy
func (a *AppType) Set(app string) error {
switch app {
case "monitoring-satellite", "monitoring-central":
*a = AppType(app)
return nil
default:
return errors.New("must be on of ['monitoring-satellite', 'monitoring-central']")
}
}
// Type is only used in help text
func (a *AppType) Type() string {
return "AppType"
}
func AppTypeCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{
"monitoring-satellite\tInstall the observability stack for single clusters, responsible for the ingestion of Metrics and Traces",
"monitoring-central\tInstall the observability stack responsible for storing Metrics and Traces for long-term period, also provides the tooling necessary to analyze and use the stored data",
}, cobra.ShellCompDirectiveDefault
}
var renderOpts struct {
ConfigFN string
ValidateConfigDisabled bool
FilesDir string
App AppType
}
// renderCmd represents the render command
var renderCmd = &cobra.Command{
Use: "render",
Short: "Renders the Kubernetes manifests required to install Gitpod's observability stack",
Long: `Renders the Kubernetes manifests required to install Gitpod's observability stack
A config file is required which can be generated with the init command.`,
Example: ` # Default install.
installer render --config config.yaml | kubectl apply -f -
# Install Gitpod's observability stack into a non-default namespace.
installer render --config config.yaml --app monitoring-satellite | kubectl apply -f -`,
RunE: func(cmd *cobra.Command, args []string) error {
yaml, err := renderFn(renderOpts.App)
if err != nil {
return err
}
if renderOpts.FilesDir != "" {
err := saveYamlToFiles(renderOpts.FilesDir, yaml)
if err != nil {
return err
}
return nil
}
for _, item := range yaml {
fmt.Println(item)
}
return nil
},
}
func renderFn(app AppType) ([]string, error) {
_, cfg, err := loadConfig(renderOpts.ConfigFN, app)
if err != nil {
return nil, err
}
return renderKubernetesObjects(cfg)
}
func saveYamlToFiles(dir string, yaml []string) error {
for i, mf := range yaml {
objs, err := common.YamlToRuntimeObject([]string{mf})
if err != nil {
return err
}
obj := objs[0]
fn := filepath.Join(dir, fmt.Sprintf("%03d_%s_%s.yaml", i, obj.Kind, obj.Metadata.Name))
err = os.WriteFile(fn, []byte(mf), 0644)
if err != nil {
return err
}
}
return nil
}
func loadConfig(cfgFN string, app AppType) (rawCfg interface{}, cfg *config.Config, err error) {
var overrideConfig string
// Update overrideConfig if cfgFN is not empty
switch cfgFN {
case "-":
b, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, nil, err
}
overrideConfig = string(b)
case "":
return nil, nil, fmt.Errorf("missing config file")
default:
cfgBytes, err := ioutil.ReadFile(cfgFN)
if err != nil {
panic(fmt.Sprintf("couldn't read file %s, %s", cfgFN, err))
}
overrideConfig = string(cfgBytes)
}
switch app {
case MonitoringSatelliteApp:
rawCfg, err = config.LoadSatellite(overrideConfig, rootOpts.StrictConfigParse)
if err != nil {
err = fmt.Errorf("error loading config: %w", err)
return
}
case MonitoringCentralApp:
return nil, nil, errors.New("monitoring-central isn't implemented yet, aborting")
default:
return nil, nil, errors.New("app not set or invalid, aborting")
}
cfg = rawCfg.(*config.Config)
cfg = replaceDeprecatedFields(cfg)
return rawCfg, cfg, err
}
func renderKubernetesObjects(cfg *config.Config) ([]string, error) {
ctx, err := common.NewRenderContext(*cfg, renderOpts.App.String())
if err != nil {
return nil, err
}
objs, err := common.CompositeRenderFunc(components.MonitoringSatelliteObjects(ctx))(ctx)
if err != nil {
return nil, err
}
k8s := make([]string, 0)
for _, o := range objs {
fc, err := yaml.Marshal(o)
if err != nil {
return nil, err
}
k8s = append(k8s, fmt.Sprintf("---\n%s\n", string(fc)))
}
// convert everything to individual objects
runtimeObjs, err := common.YamlToRuntimeObject(k8s)
if err != nil {
return nil, err
}
// sort the objects and return the plain YAML
sortedObjs, err := common.DependencySortingRenderFunc(runtimeObjs)
if err != nil {
return nil, err
}
postProcessed, err := postprocess.Run(sortedObjs)
if err != nil {
return nil, err
}
// output the YAML to stdout
output := make([]string, 0)
for _, c := range postProcessed {
output = append(output, fmt.Sprintf("---\n# %s/%s %s\n%s", c.TypeMeta.APIVersion, c.TypeMeta.Kind, c.Metadata.Name, c.Content))
}
imports, err := runImports(ctx.Config.Imports.Kustomize, ctx.Config.Imports.YAML)
if err != nil {
return nil, err
}
output = append(output, imports...)
if ctx.Config.Grafana.Install {
grafanaImporter := importer.NewYAMLImporter("https://github.com/gitpod-io/observability", "monitoring-satellite/manifests/grafana")
imports, err := grafanaImporter.Import()
if err != nil {
return nil, fmt.Errorf("failed to import grafana manifests: %v", err)
}
output = append(output, imports...)
output = append(output, "---")
}
return output, nil
}
func replaceDeprecatedFields(cfg *config.Config) *config.Config {
// No deprecated config is set
if !(cfg.Prober.Install) {
return cfg
}
// Set up logging to stderr, so it is not mixed with the rendered output.
var format = logging.MustStringFormatter(
`%{color}%{time:15:04:05} [%{level:.4s}]%{color:reset} %{message}`,
)
var backend = logging.AddModuleLevel(
logging.NewBackendFormatter(logging.NewLogBackend(os.Stderr, "", 0), format))
backend.SetLevel(logging.INFO, "")
logging.SetBackend(backend)
logger, _ := logging.GetLogger("INFO")
if cfg.Imports == nil {
cfg.Imports = &config.Imports{
YAML: []importer.YAMLImporter{},
Kustomize: []importer.KustomizeImporter{},
}
}
if cfg.Prober.Install {
logger.Info("prober.install is deprecated, please use the importer interface instead.")
cfg.Imports.YAML = append(cfg.Imports.YAML, importer.YAMLImporter{
Importer: &importer.Importer{
GitURL: "https://github.com/gitpod-io/observability",
Path: "monitoring-satellite/manifests/probers",
},
})
}
return cfg
}
func init() {
rootCmd.AddCommand(renderCmd)
renderCmd.PersistentFlags().StringVarP(&renderOpts.ConfigFN, "config", "c", "", "path to the config file, use - for stdin")
renderCmd.Flags().BoolVar(&renderOpts.ValidateConfigDisabled, "no-validation", false, "if set, the config will not be validated before running")
renderCmd.Flags().StringVar(&renderOpts.FilesDir, "output-split-files", "", "path to output individual Kubernetes manifests to")
renderCmd.PersistentFlags().Var(&renderOpts.App, "app", "Which observability app will be installed. Valid options are ['monitoring-satellite', 'monitoring-central'].")
err := renderCmd.RegisterFlagCompletionFunc("app", AppTypeCompletionFunc)
if err != nil {
fmt.Printf("There was an error while compiling the CLI, please reach out to the platform team")
os.Exit(1)
}
}
// runImports will import all manifests declared in the import interface using parallelism
func runImports(kImports []importer.KustomizeImporter, yImports []importer.YAMLImporter) ([]string, error) {
var imports []string
g := new(errgroup.Group)
importKustomize := func(i importer.KustomizeImporter) error {
kImporter := importer.NewKustomizeImporter(i.GitURL, i.Path)
imps, err := kImporter.Import()
if err != nil {
return fmt.Errorf("failed to import Kustomize. gitURL: %s path: %s: %v", i.GitURL, i.Path, err)
}
imports = append(imports, imps...)
return nil
}
importYAML := func(i importer.YAMLImporter) error {
yImporter := importer.NewYAMLImporter(i.GitURL, i.Path)
imps, err := yImporter.Import()
if err != nil {
return fmt.Errorf("failed to import YAML. gitURL: %s path: %s: %v", i.GitURL, i.Path, err)
}
imports = append(imports, imps...)
return nil
}
for _, imp := range kImports {
imp := imp // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return importKustomize(imp)
})
}
for _, imp := range yImports {
imp := imp // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return importYAML(imp)
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return imports, nil
}