Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send container logs to syslog server #117

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions hook.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ onboot:
services:
- name: getty
image: linuxkit/getty:v0.8
binds.add:
- /etc/profile.d/local.sh:/etc/profile.d/local.sh
Comment on lines +26 to +27
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find, this had been annoying me so much!

env:
- INSECURE=true
- name: rngd
Expand Down Expand Up @@ -51,7 +53,6 @@ services:
binds:
- /etc/resolv.conf:/etc/resolv.conf
- /lib/modules:/lib/modules
- /etc/docker/daemon.json:/etc/docker/daemon.json
- /var/run/docker:/var/run
- /var/run/worker:/worker
- /dev/console:/dev/console
Expand All @@ -76,11 +77,10 @@ services:
mkdir:
- /var/run/docker
files:
- path: etc/docker/daemon.json
contents: '{"debug": true}'
- path: etc/profile.d/local.sh
contents: |
alias docker='ctr -n services.linuxkit tasks exec --tty --exec-id shell docker docker'
alias docker='ctr -n services.linuxkit tasks exec --tty --exec-id cmd docker docker'
alias docker-shell='ctr -n services.linuxkit tasks exec --tty --exec-id shell docker sh'
mode: "0644"
trust:
org:
Expand Down
4 changes: 2 additions & 2 deletions tink-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# syntax=docker/dockerfile:experimental

FROM golang:1.15-alpine as dev
FROM golang:1.18-alpine as dev
COPY . /src/
WORKDIR /src
ENV GO111MODULE=on
RUN --mount=type=cache,sharing=locked,id=gomod,target=/go/pkg/mod/cache \
--mount=type=cache,sharing=locked,id=goroot,target=/root/.cache/go-build \
CGO_ENABLED=0 go build -a -ldflags '-w -extldflags "-static"' -o /tink-docker

FROM docker:19.03.8-dind
FROM docker:20.10.14-dind
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
RUN apk update; apk add kexec-tools
COPY --from=dev /tink-docker .
Expand Down
2 changes: 1 addition & 1 deletion tink-docker/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/tinkerbell/hook/tink-docker

go 1.15
go 1.18
34 changes: 34 additions & 0 deletions tink-docker/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -15,10 +16,17 @@ type tinkConfig struct {
registry string
baseURL string
tinkerbell string
syslogHost string

// TODO add others
}

type dockerConfig struct {
Debug bool `json:"debug"`
LogDriver string `json:"log-driver,omitempty"`
LogOpts map[string]string `json:"log-opts,omitempty"`
}

func main() {
fmt.Println("Starting Tink-Docker")
go rebootWatch()
Expand All @@ -45,6 +53,17 @@ func main() {
}
fmt.Println("Downloaded the repository certificates, starting the Docker Engine")

d := dockerConfig{
Debug: true,
LogDriver: "syslog",
LogOpts: map[string]string{
"syslog-address": fmt.Sprintf("udp://%v:514", cfg.syslogHost),
},
}
if err := d.writeToDisk("/etc/docker/daemon.json"); err != nil {
fmt.Println("Failed to write docker config:", err)
}

// Build the command, and execute
cmd := exec.Command("/usr/local/bin/docker-init", "/usr/local/bin/dockerd")
cmd.Stdout = os.Stdout
Expand All @@ -55,6 +74,19 @@ func main() {
}
}

// writeToDisk writes the dockerConfig to loc.
func (d dockerConfig) writeToDisk(loc string) error {
b, err := json.Marshal(d)
if err != nil {
return fmt.Errorf("unable to marshal docker config: %w", err)
}
if err := ioutil.WriteFile(loc, b, 0600); err != nil {
return fmt.Errorf("Error writing daemon.json: %w", err)
}

return nil
}

// parseCmdLine will parse the command line.
func parseCmdLine(cmdLines []string) (cfg tinkConfig) {
for i := range cmdLines {
Expand All @@ -71,6 +103,8 @@ func parseCmdLine(cmdLines []string) (cfg tinkConfig) {
cfg.baseURL = cmdLine[1]
case "tinkerbell":
cfg.tinkerbell = cmdLine[1]
case "syslog_host":
cfg.syslogHost = cmdLine[1]
}
}
return cfg
Expand Down
47 changes: 47 additions & 0 deletions tink-docker/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"bytes"
"errors"
"io/ioutil"
"os"
"testing"
)

func TestWriteToDisk(t *testing.T) {
tests := map[string]struct {
cfg dockerConfig
want []byte
wantErr error
}{
"success": {cfg: dockerConfig{Debug: false, LogDriver: "json-file"}, want: []byte(`{"debug":false,"log-driver":"json-file"}`)},
"success - empty struct": {cfg: dockerConfig{}, want: []byte(`{"debug":false}`)},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
// Create a temporary directory
dir, err := ioutil.TempDir("", "tink-docker")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
loc := dir + "daemon.json"

err = tt.cfg.writeToDisk(loc)
if !errors.Is(err, tt.wantErr) {
t.Fatalf("got err %v, want %v", err, tt.wantErr)
}

if tt.wantErr == nil {
got, err := ioutil.ReadFile(loc)
if err != nil {
t.Fatal(err)
}

if !bytes.Equal(got, tt.want) {
t.Fatalf("\ngot:\n %s\nwant:\n %s", got, tt.want)
}
}
})
}
}