|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +// containerd-shim-lcow-v2 is a containerd shim implementation for Linux Containers on Windows (LCOW). |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "os" |
| 12 | + |
| 13 | + "github.com/Microsoft/hcsshim/cmd/containerd-shim-lcow-v2/service" |
| 14 | + _ "github.com/Microsoft/hcsshim/cmd/containerd-shim-lcow-v2/service/plugin" |
| 15 | + runhcsopts "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/options" |
| 16 | + "github.com/Microsoft/hcsshim/internal/log" |
| 17 | + "github.com/Microsoft/hcsshim/internal/oc" |
| 18 | + "github.com/Microsoft/hcsshim/internal/shim" |
| 19 | + |
| 20 | + "github.com/containerd/errdefs" |
| 21 | + "github.com/sirupsen/logrus" |
| 22 | + "go.opencensus.io/trace" |
| 23 | +) |
| 24 | + |
| 25 | +// Add a manifest to get proper Windows version detection. |
| 26 | +//go:generate go tool github.com/josephspurrier/goversioninfo/cmd/goversioninfo -platform-specific |
| 27 | + |
| 28 | +func main() { |
| 29 | + logrus.AddHook(log.NewHook()) |
| 30 | + |
| 31 | + // Register our OpenCensus logrus exporter so that trace spans are emitted via logrus. |
| 32 | + trace.ApplyConfig(trace.Config{DefaultSampler: oc.DefaultSampler}) |
| 33 | + trace.RegisterExporter(&oc.LogrusExporter{}) |
| 34 | + |
| 35 | + logrus.SetFormatter(log.NopFormatter{}) |
| 36 | + logrus.SetOutput(io.Discard) |
| 37 | + |
| 38 | + // Set the log configuration. |
| 39 | + // If we encounter an error, we exit with non-zero code. |
| 40 | + if err := setLogConfiguration(); err != nil { |
| 41 | + _, _ = fmt.Fprintf(os.Stderr, "%s: %s", service.ShimName, err) |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + // Start the shim manager event loop. The manager is responsible for |
| 46 | + // handling containerd start/stop lifecycle calls for the shim process. |
| 47 | + shim.Run(context.Background(), newShimManager(service.ShimName), func(c *shim.Config) { |
| 48 | + // We don't want the shim package to set up logging options. |
| 49 | + c.NoSetupLogger = true |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +// setLogConfiguration reads the runtime options from stdin and sets the log configuration. |
| 54 | +// We only set up the log configuration for serve action. |
| 55 | +func setLogConfiguration() error { |
| 56 | + // We set up the log configuration in the serve action only. |
| 57 | + // This is because we want to avoid reading the stdin in start action, |
| 58 | + // so that we can pass it along to the invocation for serve action. |
| 59 | + if len(os.Args) > 1 && os.Args[len(os.Args)-1] == "serve" { |
| 60 | + // The serve process is started with stderr pointing to panic.log file. |
| 61 | + // We want to keep that file only for pure Go panics. Any explicit writes |
| 62 | + // to os.Stderr should go to stdout instead, which is connected to the parent's |
| 63 | + // stderr for regular logging. |
| 64 | + // We can safely redirect os.Stderr to os.Stdout because in case of panics, |
| 65 | + // the Go runtime will write the panic stack trace directly to the file descriptor, |
| 66 | + // bypassing os.Stderr, so it will still go to panic.log. |
| 67 | + os.Stderr = os.Stdout |
| 68 | + |
| 69 | + opts, err := shim.ReadRuntimeOptions[*runhcsopts.Options](os.Stdin) |
| 70 | + if err != nil { |
| 71 | + if !errors.Is(err, errdefs.ErrNotFound) { |
| 72 | + return fmt.Errorf("failed to read runtime options from stdin: %w", err) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if opts != nil { |
| 77 | + if opts.LogLevel != "" { |
| 78 | + // If log level is specified, set the corresponding logrus logging level. |
| 79 | + lvl, err := logrus.ParseLevel(opts.LogLevel) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("failed to parse shim log level %q: %w", opts.LogLevel, err) |
| 82 | + } |
| 83 | + logrus.SetLevel(lvl) |
| 84 | + } |
| 85 | + |
| 86 | + if opts.ScrubLogs { |
| 87 | + log.SetScrubbing(true) |
| 88 | + } |
| 89 | + } |
| 90 | + _ = os.Stdin.Close() |
| 91 | + } |
| 92 | + return nil |
| 93 | +} |
0 commit comments