This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
main.go
183 lines (160 loc) · 5.46 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
183
package main
import (
"context"
"expvar"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"strings"
"time"
"contrib.go.opencensus.io/exporter/zipkin"
"github.com/Netflix/titus-executor/logger"
"github.com/Netflix/titus-executor/metadataserver/service"
"github.com/Netflix/titus-executor/utils/log"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinHTTP "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
pkgviper "github.com/spf13/viper"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.opencensus.io/zpages"
"golang.org/x/sys/unix"
)
const (
zipkinURLFlagName = "zipkin"
debugAddressFlagName = "debug-address"
)
func setupDebugServer(ctx context.Context, address string) error {
listener, err := net.Listen("tcp", address)
if err != nil {
return errors.Wrap(err, "Cannot setup listener for debug server")
}
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/vars", expvar.Handler())
zpages.Handle(mux, "/trace")
go func() {
<-ctx.Done()
listener.Close()
}()
go func() {
err := http.Serve(listener, mux)
if err != nil && ctx.Err() == nil {
logger.G(ctx).WithError(err).Fatal("Debug server exited problematically")
}
}()
return nil
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logrusLogger := logrus.StandardLogger()
ctx = logger.WithLogger(ctx, logrusLogger)
v := pkgviper.New()
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
rootCmd := &cobra.Command{
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
v.SetEnvPrefix("TITUS_IAM_SERVICE")
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
if err := v.BindPFlags(cmd.Flags()); err != nil {
return err
}
if v.GetBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
logrusLogger.SetLevel(logrus.DebugLevel)
}
if v.GetBool("journald") {
log.MaybeSetupLoggerIfOnJournaldAvailable()
}
view.SetReportingPeriod(time.Second * 1)
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := setupDebugServer(ctx, v.GetString("debug-address")); err != nil {
return err
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, unix.SIGTERM, unix.SIGINT)
sig := <-c
logrus.WithField("signal", sig.String()).Info("Terminating with signal")
cancel()
select {
case <-time.After(time.Second * 30):
case sig = <-c:
logrus.WithField("signal", sig.String()).Info("Received another signal. Immediately terminating")
}
logrus.Fatal("System did not gracefully terminate")
_ = unix.Kill(0, unix.SIGKILL)
}()
listener, err := net.Listen("tcp", v.GetString("address"))
if err != nil {
return errors.Wrap(err, "Could not setup listener")
}
defer listener.Close()
logger.G(ctx).WithField("address", listener.Addr().String()).Info("Listening")
if zipkinURL := v.GetString(zipkinURLFlagName); zipkinURL != "" {
reporter := zipkinHTTP.NewReporter(zipkinURL,
zipkinHTTP.BatchInterval(time.Second*5),
zipkinHTTP.BatchSize(1000),
zipkinHTTP.MaxBacklog(100000),
)
hostname, err := os.Hostname()
if err != nil {
return errors.Wrap(err, "Unable to fetch hostname")
}
endpoint, err := openzipkin.NewEndpoint("titus-iam-service", hostname)
if err != nil {
return errors.Wrap(err, "Failed to create the local zipkinEndpoint")
}
logger.G(ctx).WithField("endpoint", endpoint).WithField("url", zipkinURL).Info("Setting up tracing")
trace.RegisterExporter(zipkin.NewExporter(reporter, endpoint))
}
return service.Run(ctx, service.Config{
Listener: listener,
OpenPolicyAgent: v.GetString("open-policy-agent"),
ClientCA: v.GetString("client-ca"),
Region: v.GetString("region"),
SSLKey: v.GetString("ssl-key"),
SSLCert: v.GetString("ssl-cert"),
})
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// TODO: Add metrics flush
},
}
rootCmd.Flags().String("address", ":7001", "Listening address")
rootCmd.Flags().String("region", "", "Region of STS service to connect to")
rootCmd.Flags().String("ssl-key", "", "The SSL Private Key")
rootCmd.Flags().String("ssl-cert", "", "The SSL Certificate")
rootCmd.Flags().String("client-ca", "", "Client accept CA file")
rootCmd.Flags().String("open-policy-agent", "", "Open policy agent address")
rootCmd.PersistentFlags().String(debugAddressFlagName, ":7003", "Address for zpages, pprof")
rootCmd.PersistentFlags().Bool("debug", false, "Turn on debug logging")
rootCmd.PersistentFlags().Bool("journald", true, "Log exclusively to Journald")
rootCmd.PersistentFlags().String(zipkinURLFlagName, "", "URL To send Zipkin spans to")
if err := v.BindPFlags(rootCmd.PersistentFlags()); err != nil {
logger.G(ctx).WithError(err).Fatal("Unable to configure Viper")
}
if err := v.BindEnv("region", "EC2_REGION"); err != nil {
panic(err)
}
if err := v.BindEnv(zipkinURLFlagName, "ZIPKIN"); err != nil {
panic(err)
}
err := rootCmd.Execute()
if ctx.Err() != nil {
logger.G(ctx).Info("Shutting down gracefully")
} else if err != nil {
logger.G(ctx).WithError(err).Fatal("Failed")
}
}