/
main.go
149 lines (122 loc) · 4.33 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
/*
Copyright 2021 Loggie Authors
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 main
import (
"flag"
"fmt"
"github.com/loggie-io/loggie/pkg/ops"
"github.com/pkg/errors"
"go.uber.org/automaxprocs/maxprocs"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/loggie-io/loggie/cmd/subcmd"
"github.com/loggie-io/loggie/pkg/control"
"github.com/loggie-io/loggie/pkg/core/cfg"
"github.com/loggie-io/loggie/pkg/core/global"
"github.com/loggie-io/loggie/pkg/core/log"
"github.com/loggie-io/loggie/pkg/core/reloader"
"github.com/loggie-io/loggie/pkg/core/signals"
"github.com/loggie-io/loggie/pkg/core/sysconfig"
"github.com/loggie-io/loggie/pkg/discovery/kubernetes"
"github.com/loggie-io/loggie/pkg/eventbus"
_ "github.com/loggie-io/loggie/pkg/include"
"github.com/loggie-io/loggie/pkg/ops/helper"
"github.com/loggie-io/loggie/pkg/util/persistence"
"github.com/loggie-io/loggie/pkg/util/yaml"
)
var (
globalConfigFile string
pipelineConfigPath string
configType string
nodeName string
)
func init() {
hostName, _ := os.Hostname()
flag.StringVar(&globalConfigFile, "config.system", "loggie.yml", "global config file")
flag.StringVar(&pipelineConfigPath, "config.pipeline", "pipelines.yml", "reloadable config file")
flag.StringVar(&configType, "config.from", "file", "config from file or env")
flag.StringVar(&nodeName, "meta.nodeName", hostName, "override nodeName")
}
func main() {
flag.Parse()
// init logging configuration
log.InitDefaultLogger()
if err := subcmd.SwitchSubCommand(); err != nil {
return
}
log.Info("version: %s", global.GetVersion())
// set up signals, so we handle the first shutdown signal gracefully
stopCh := signals.SetupSignalHandler()
// Automatically set GOMAXPROCS to match Linux container CPU quota
if _, err := maxprocs.Set(maxprocs.Logger(log.Debug)); err != nil {
log.Fatal("set maxprocs error: %v", err)
}
log.Info("real GOMAXPROCS %d", runtime.GOMAXPROCS(-1))
global.NodeName = nodeName
log.Info("node name: %s", nodeName)
// system config file
syscfg := sysconfig.Config{}
cfg.UnpackTypeDefaultsAndValidate(strings.ToLower(configType), globalConfigFile, &syscfg)
// start eventBus listeners
eventbus.StartAndRun(syscfg.Loggie.MonitorEventBus)
// init log after error func
log.AfterError = eventbus.AfterErrorFunc
log.AfterErrorConfig = syscfg.Loggie.ErrorAlertConfig
log.Info("pipelines config path: %s", pipelineConfigPath)
// pipeline config file
pipecfgs, err := control.ReadPipelineConfig(pipelineConfigPath, configType, func(s os.FileInfo) bool {
return false
})
if pipecfgs != nil {
out, yamlErr := yaml.Marshal(pipecfgs)
if yamlErr != nil {
log.Fatal("marshal initial pipelines failed: %v, config:\n%s", yamlErr, out)
}
log.Info("initial pipelines:\n%s", out)
}
if err != nil && !os.IsNotExist(err) && !errors.Is(err, control.ErrIgnoreAllFile) {
log.Fatal("unpack config.pipeline config file err: %v", err)
}
persistence.SetConfig(syscfg.Loggie.Db)
defer persistence.StopDbHandler()
controller := control.NewController()
controller.Start(pipecfgs)
if syscfg.Loggie.Reload.Enabled {
syscfg.Loggie.Reload.ConfigPath = pipelineConfigPath
reloader.Setup(stopCh, controller, &syscfg.Loggie.Reload)
}
if syscfg.Loggie.Discovery.Enabled {
k8scfg := syscfg.Loggie.Discovery.Kubernetes
k8scfg.NodeName = nodeName
k8scfg.ConfigFilePath = filepath.Dir(pipelineConfigPath)
k8sDiscovery := kubernetes.NewDiscovery(&k8scfg)
go k8sDiscovery.Start(stopCh)
}
// api for debugging
helper.Setup(controller)
// api for get loggie Version
ops.Setup(controller)
if syscfg.Loggie.Http.Enabled {
go func() {
if err = http.ListenAndServe(fmt.Sprintf("%s:%d", syscfg.Loggie.Http.Host, syscfg.Loggie.Http.Port), nil); err != nil {
log.Fatal("http listen and serve err: %v", err)
}
}()
}
log.Info("started Loggie")
<-stopCh
log.Info("shutting down Loggie")
}