-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathmain.go
More file actions
174 lines (153 loc) · 4.67 KB
/
main.go
File metadata and controls
174 lines (153 loc) · 4.67 KB
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
//go:build windows
package main
import (
"context"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/Microsoft/go-winio/pkg/etw"
"github.com/Microsoft/go-winio/pkg/etwlogrus"
"github.com/Microsoft/go-winio/pkg/guid"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"go.opencensus.io/trace"
"github.com/Microsoft/hcsshim/internal/log"
"github.com/Microsoft/hcsshim/internal/oc"
"github.com/Microsoft/hcsshim/internal/shimdiag"
hcsversion "github.com/Microsoft/hcsshim/internal/version"
// register common types spec with typeurl
_ "github.com/containerd/containerd/v2/core/runtime"
)
const usage = ``
const ttrpcAddressEnv = "TTRPC_ADDRESS"
// Add a manifest to get proper Windows version detection.
//go:generate pwsh -Command "../../scripts/New-ResourceObjectFile.ps1 -ErrorAction 'Stop' -Destination '.' -Name 'containerd-shim-runhcs-v1' -UseVersionFile -Architecture 'all'"
// `-ldflags '-X ...'` only works if the variable is uninitialized or set to a constant value.
// keep empty and override with data from [internal/version] only if empty to allow
// workflows currently setting these values to work.
var (
// version will be the repo version that the binary was built from
version = ""
// gitCommit will be the hash that the binary was built from
gitCommit = ""
)
var (
namespaceFlag string
addressFlag string
containerdBinaryFlag string
idFlag string
// gracefulShutdownTimeout is how long to wait for clean-up before just exiting
gracefulShutdownTimeout = 3 * time.Second
)
func etwCallback(sourceID guid.GUID, state etw.ProviderState, level etw.Level, matchAnyKeyword uint64, matchAllKeyword uint64, filterData uintptr) {
if state == etw.ProviderStateCaptureState {
resp, err := svc.DiagStacks(context.Background(), &shimdiag.StacksRequest{})
if err != nil {
return
}
log := logrus.WithField("tid", svc.tid)
log.WithField("stack", resp.Stacks).Info("goroutine stack dump")
if resp.GuestStacks != "" {
log.WithField("stack", resp.GuestStacks).Info("guest stack dump")
}
}
}
func main() {
logrus.AddHook(log.NewHook())
// Provider ID: 0b52781f-b24d-5685-ddf6-69830ed40ec3
// Provider and hook aren't closed explicitly, as they will exist until process exit.
provider, err := etw.NewProvider("Microsoft.Virtualization.RunHCS", etwCallback)
if err != nil {
logrus.Error(err)
} else {
if hook, err := etwlogrus.NewHookFromProvider(provider); err == nil {
logrus.AddHook(hook)
} else {
logrus.Error(err)
}
}
// fall back on embedded version info (if any), if variables above were not set
if version == "" {
version = hcsversion.Version
}
if gitCommit == "" {
gitCommit = hcsversion.Commit
}
_ = provider.WriteEvent(
"ShimLaunched",
nil,
etw.WithFields(
etw.StringArray("Args", os.Args),
etw.StringField("version", version),
etw.StringField("commit", gitCommit),
),
)
// Register our OpenCensus logrus exporter
trace.ApplyConfig(trace.Config{DefaultSampler: oc.DefaultSampler})
trace.RegisterExporter(&oc.LogrusExporter{})
app := cli.NewApp()
app.Name = "containerd-shim-runhcs-v1"
app.Usage = usage
var v []string
if version != "" {
v = append(v, version)
}
if gitCommit != "" {
v = append(v, fmt.Sprintf("commit: %s", gitCommit))
}
v = append(v, fmt.Sprintf("spec: %s", specs.Version))
app.Version = strings.Join(v, "\n")
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "namespace",
Usage: "the namespace of the container",
},
cli.StringFlag{
Name: "address",
Usage: "the address of the containerd's main socket",
},
cli.StringFlag{
Name: "publish-binary",
Usage: "the binary path to publish events back to containerd",
},
cli.StringFlag{
Name: "id",
Usage: "the id of the container",
},
cli.StringFlag{
Name: "bundle",
Usage: "the bundle path to delete (delete command only).",
},
cli.BoolFlag{
Name: "debug",
Usage: "run the shim in debug mode",
},
}
app.Commands = []cli.Command{
startCommand,
deleteCommand,
serveCommand,
}
app.Before = func(context *cli.Context) error {
if namespaceFlag = context.GlobalString("namespace"); namespaceFlag == "" {
return errors.New("namespace is required")
}
if addressFlag = context.GlobalString("address"); addressFlag == "" {
return errors.New("address is required")
}
if containerdBinaryFlag = context.GlobalString("publish-binary"); containerdBinaryFlag == "" {
return errors.New("publish-binary is required")
}
if idFlag = context.GlobalString("id"); idFlag == "" {
return errors.New("id is required")
}
return nil
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(cli.ErrWriter, err)
os.Exit(1)
}
}