-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
140 lines (126 loc) · 3.41 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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/http/pprof"
"os"
"soul-mirror/controller"
"soul-mirror/model"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/zerolog/diode"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var (
loglevel = flag.String("loglevel", "info", "info, debug, trace")
enableElection = flag.Bool("enable-election", false, "用于开启选举")
)
func main() {
flag.Parse()
wr := diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) {
fmt.Printf("Logger Dropped %d messages", missed)
})
logrus.SetOutput(wr)
go app()
sync(getConfig())
}
func app() {
router := http.NewServeMux()
level, err := logrus.ParseLevel(*loglevel)
if err != nil {
logrus.WithError(err).Fatalf("failed to parse loglevel")
}
logrus.SetLevel(level)
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
router.HandleFunc("/logging", func(writer http.ResponseWriter, request *http.Request) {
level := request.URL.Query().Get("level")
if len(level) == 0 {
_, _ = writer.Write([]byte(*loglevel))
return
}
l, err := logrus.ParseLevel(level)
if err != nil {
_, _ = writer.Write([]byte(err.Error()))
return
}
*loglevel = l.String()
logrus.SetLevel(l)
})
router.HandleFunc("/health", func(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write([]byte("ok"))
})
ph := promhttp.Handler()
router.Handle("/metrics", ph)
err = http.ListenAndServe(":9527", router)
if err != nil {
logrus.WithError(err).Fatalf("failed to listen 9527")
}
}
func sync(appCfg *model.Config) {
// 选举
if *enableElection {
localCfg := config.GetConfigOrDie()
mgr, err := manager.New(localCfg, manager.Options{
LeaderElection: true,
LeaderElectionID: "soul-mirror-service-controller-election",
})
if err != nil {
logrus.WithError(err).Fatal("unable to set up overall controller manager")
}
go func() {
err = mgr.Start(context.TODO())
if err != nil {
logrus.WithError(err).Fatal("unable to start controller manager")
}
}()
ech := mgr.Elected()
<-ech
}
for _, c := range appCfg.Clusters {
err := filter.UpdateCluster(&c)
if err != nil {
logrus.WithError(err).Fatal("unable to initialize cluster")
}
}
for _, m := range appCfg.Mirrors {
filter.UpdateMirror(m)
logrus.Infof("filter %v running", m.Name)
}
s := make(chan struct{})
filter.Start(s)
<-s
}
func getConfig() *model.Config {
viper.SetConfigName("cluster.yaml")
viper.SetConfigType("yaml")
viper.AddConfigPath("/config/")
viper.AddConfigPath("./config/")
err := viper.ReadInConfig()
if err != nil {
logrus.WithError(err).Fatalf("failed to read clsuter getConfig")
}
clusters := &model.Config{}
err = viper.Unmarshal(clusters)
if err != nil {
logrus.WithError(err).Fatalf("failed to read clsuter getConfig")
}
viper.SetConfigName("mirror.yaml")
err = viper.ReadInConfig()
if err != nil {
logrus.WithError(err).Fatalf("failed to read clsuter getConfig")
}
err = viper.Unmarshal(clusters)
if err != nil {
logrus.WithError(err).Fatalf("failed to read clsuter getConfig")
}
return clusters
}