Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package containerd |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 21 | "fmt" |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 22 | "io" |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 23 | "os" |
| 24 | "os/exec" |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 25 | "time" |
| 26 | |
| 27 | "git.monogon.dev/source/nexantic.git/core/internal/common/supervisor" |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 28 | |
| 29 | "git.monogon.dev/source/nexantic.git/core/pkg/logbuffer" |
| 30 | |
| 31 | "golang.org/x/sys/unix" |
| 32 | ) |
| 33 | |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 34 | const runscLogsFIFOPath = "/containerd/run/runsc-logs.fifo" |
| 35 | |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 36 | type Service struct { |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 37 | Log *logbuffer.LogBuffer |
| 38 | RunscLog *logbuffer.LogBuffer |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 39 | } |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 40 | |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 41 | func New() (*Service, error) { |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 42 | return &Service{Log: logbuffer.New(5000, 16384), RunscLog: logbuffer.New(5000, 16384)}, nil |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 43 | } |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 44 | |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 45 | func (s *Service) Run() supervisor.Runnable { |
| 46 | return func(ctx context.Context) error { |
| 47 | cmd := exec.CommandContext(ctx, "/containerd/bin/containerd", "--config", "/containerd/conf/config.toml") |
| 48 | cmd.Stdout = s.Log |
| 49 | cmd.Stderr = s.Log |
| 50 | cmd.Env = []string{"PATH=/containerd/bin", "TMPDIR=/containerd/run/tmp"} |
| 51 | |
| 52 | if err := unix.Mount("tmpfs", "/containerd/run", "tmpfs", 0, ""); err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | if err := os.MkdirAll("/containerd/run/tmp", 0755); err != nil { |
| 56 | panic(err) |
| 57 | } |
| 58 | |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 59 | runscFifo, err := os.OpenFile(runscLogsFIFOPath, os.O_CREATE|os.O_RDONLY, os.ModeNamedPipe|0777) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | go func() { |
| 64 | for { |
| 65 | n, err := io.Copy(s.RunscLog, runscFifo) |
| 66 | if n == 0 && err == nil { |
| 67 | // Hack because pipes/FIFOs can return zero reads when nobody is writing. To avoid busy-looping, |
| 68 | // sleep a bit before retrying. This does not loose data since the FIFO internal buffer will |
| 69 | // stall writes when it becomes full. 10ms maximum stall in a non-latency critical process (reading |
| 70 | // debug logs) is not an issue for us. |
| 71 | time.Sleep(10 * time.Millisecond) |
| 72 | } else if err != nil { |
| 73 | // TODO: Use supervisor.Logger() and Error() before exiting. Should never happen. |
| 74 | fmt.Println(err) |
| 75 | return // It's likely that this will busy-loop printing errors if it encounters one, so bail |
| 76 | } |
| 77 | } |
| 78 | }() |
| 79 | |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 80 | // TODO(lorenz): Healthcheck against CRI RuntimeService.Status() and SignalHealthy |
| 81 | |
Lorenz Brun | 6acfc32 | 2020-05-13 17:01:26 +0200 | [diff] [blame] | 82 | err = cmd.Run() |
Lorenz Brun | 878f5f9 | 2020-05-12 16:15:39 +0200 | [diff] [blame] | 83 | fmt.Fprintf(s.Log, "containerd stopped: %v\n", err) |
| 84 | return err |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 85 | } |
Lorenz Brun | c88c82d | 2020-05-08 14:35:04 +0200 | [diff] [blame] | 86 | } |