This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
/
main.go
203 lines (179 loc) · 5.38 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"contrib.go.opencensus.io/exporter/zipkin"
"github.com/Netflix/metrics-client-go/metrics"
"github.com/Netflix/titus-executor/cmd/common"
"github.com/Netflix/titus-executor/config"
"github.com/Netflix/titus-executor/executor/runner"
"github.com/Netflix/titus-executor/executor/runtime/docker"
"github.com/Netflix/titus-executor/logger"
"github.com/Netflix/titus-executor/tag"
log2 "github.com/Netflix/titus-executor/utils/log"
"github.com/Netflix/titus-executor/vk/backend"
openzipkin "github.com/openzipkin/zipkin-go"
zipkinHTTP "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.opencensus.io/trace"
v1 "k8s.io/api/core/v1"
)
type commandConfig struct {
// runtime dir will have pod.json
// and be populated with state.json
// All other file names are meant to be ignored.
runtimeDir string
journald bool
zipkin string
debug bool
}
func main() {
go common.HandleQuitSignal()
mainCfg := commandConfig{}
var flags = []cli.Flag{
cli.StringFlag{
Name: "runtime-dir",
Destination: &mainCfg.runtimeDir,
Usage: "The location of the pod spec file (json-ish)",
Value: "/run/titus-executor/default__",
},
cli.BoolTFlag{
Name: "journald",
Usage: "Enable logging to journald",
Destination: &mainCfg.journald,
},
cli.StringFlag{
Name: "zipkin",
Destination: &mainCfg.zipkin,
EnvVar: "ZIPKIN",
},
cli.BoolFlag{
Name: "debug",
Destination: &mainCfg.debug,
EnvVar: "DEBUG",
},
}
app := cli.NewApp()
app.Name = "titus-virtual-kubelet-backend"
// avoid os.Exit as much as possible to let deferred functions run
defer time.Sleep(1 * time.Second)
dockerCfg, dockerCfgFlags := docker.NewConfig()
app.Flags = append(flags, dockerCfgFlags...)
cfg, cfgFlags := config.NewConfig()
app.Flags = append(app.Flags, cfgFlags...)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logrusLogger := logrus.StandardLogger()
ctx = logger.WithLogger(ctx, logrusLogger)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
m := metrics.New(ctx, logrus.StandardLogger(), tag.Defaults)
m = runner.NewReporter(m)
defer m.Flush()
app.Action = func(c *cli.Context) error {
// We can only inspect the config here, because looking at it any earlier doesn't work since the flags
// are not parsed yet.
if mainCfg.journald {
log2.MaybeSetupLoggerIfOnJournaldAvailable()
} else {
logrusLogger.Warning("Not enabling journald")
}
if mainCfg.debug {
logrus.SetLevel(logrus.DebugLevel)
logrusLogger.Debug("enabled debug logging")
}
cfg.RuntimeDir = mainCfg.runtimeDir
if err := mainWithError(ctx, dockerCfg, cfg, &mainCfg, m); err != nil {
return cli.NewExitError(err, 1)
}
return nil
}
if err := app.Run(os.Args); err != nil {
panic(err)
}
}
func mainWithError(ctx context.Context, dockerCfg *docker.Config, cfg *config.Config, mainCfg *commandConfig, m metrics.Reporter) error {
var ew *exporterWrapper
if mainCfg.zipkin != "" {
reporter := zipkinHTTP.NewReporter(mainCfg.zipkin,
zipkinHTTP.BatchInterval(time.Second*5),
zipkinHTTP.BatchSize(10),
zipkinHTTP.MaxBacklog(100000),
)
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("Unable to fetch hostname: %w", err)
}
endpoint, err := openzipkin.NewEndpoint("titus-executor", hostname)
if err != nil {
return fmt.Errorf("Failed to create the local zipkinEndpoint: %w", err)
}
logger.G(ctx).WithField("endpoint", endpoint).WithField("url", mainCfg.zipkin).Info("Setting up tracing")
exporter := zipkin.NewExporter(reporter, endpoint)
ew = &exporterWrapper{exporter: exporter}
trace.RegisterExporter(ew)
defer func() {
reporterErr := reporter.Close()
if reporterErr != nil {
logger.G(ctx).WithError(err).Error("Unable to close reporter")
}
}()
}
logger.G(ctx).WithField("cfg", logger.ShouldJSON(ctx, *cfg)).WithField("dockerCfg", logger.ShouldJSON(ctx, *dockerCfg)).Debug("Loaded config and docker config")
var pod v1.Pod
podFileName := filepath.Join(mainCfg.runtimeDir, "pod.json")
data, err := ioutil.ReadFile(podFileName)
if err != nil {
return fmt.Errorf("Unable to read pod file %q: %w", podFileName, err)
}
err = json.Unmarshal(data, &pod)
if err != nil {
return fmt.Errorf("Could not deserialize pod file: %w", err)
}
if ew != nil {
ew.Lock()
ew.taskID = pod.Name
ew.Unlock()
}
logger.G(ctx).WithField("pod", pod).Debug("Got pod")
rp, err := docker.NewDockerRuntime(ctx, m, *dockerCfg, *cfg)
if err != nil {
return fmt.Errorf("Cannot create Titus executor: %w", err)
}
b, err := backend.NewBackend(ctx, rp, &pod, cfg, m)
if err != nil {
return err
}
go func() {
err := b.Ready(ctx)
if err == nil {
logger.G(ctx).Info("Notified systemd we are ready")
notifySystemd(ctx)
} else {
logger.G(ctx).WithError(err).Error("Could not notify systemd we are ready")
}
}()
return b.RunWithOutputDir(ctx, mainCfg.runtimeDir)
}
type exporterWrapper struct {
sync.RWMutex
exporter *zipkin.Exporter
taskID string
}
func (e *exporterWrapper) ExportSpan(s *trace.SpanData) {
e.RLock()
if e.taskID != "" {
if s.Attributes == nil {
s.Attributes = map[string]interface{}{}
}
s.Attributes["taskid"] = e.taskID
}
e.RUnlock()
e.exporter.ExportSpan(s)
}