Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 36e45cb

Browse files
authored
Use loglevel to dynamically change logrus' log level during runtime (#752) (#1861)
Signed-off-by: Grant Linville <[email protected]>
1 parent 2423dcc commit 36e45cb

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ RUN apk -U add curl
1111
RUN curl -sfL https://github.com/loft-sh/devspace/archive/refs/tags/v6.3.2.tar.gz | tar xzf - --strip-components=1
1212
RUN --mount=type=cache,target=/go/pkg --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -o /usr/local/bin/acorn-helper -ldflags "-s -w" ./helper
1313

14+
FROM ghcr.io/acorn-io/images-mirror/golang:1.20-alpine AS loglevel
15+
WORKDIR /usr/src
16+
RUN apk -U add curl
17+
RUN curl -sfL https://github.com/acorn-io/loglevel/archive/refs/tags/v0.1.6.tar.gz | tar xzf - --strip-components=1
18+
RUN --mount=type=cache,target=/go/pkg --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -o /usr/local/bin/loglevel -ldflags "-s -w"
19+
1420
FROM ghcr.io/acorn-io/images-mirror/golang:1.20 AS build
1521
COPY / /src
1622
WORKDIR /src
@@ -31,6 +37,7 @@ COPY --from=klipper-lb /usr/bin/entry /usr/local/bin/klipper-lb
3137
COPY ./scripts/ds-containerd-config-path-entry /usr/local/bin
3238
COPY ./scripts/setup-binfmt /usr/local/bin
3339
COPY --from=helper /usr/local/bin/acorn-helper /usr/local/bin/
40+
COPY --from=loglevel /usr/local/bin/loglevel /usr/local/bin/
3441
VOLUME /var/lib/buildkit
3542

3643
COPY /scripts/acorn-helper-init /usr/local/bin

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
cuelang.org/go v0.5.0
1515
github.com/AlecAivazis/survey/v2 v2.3.6
1616
github.com/acorn-io/aml v0.0.0-20230619192500-1f56a8955db2
17-
github.com/acorn-io/baaah v0.0.0-20230617011755-3291c17915f5
17+
github.com/acorn-io/baaah v0.0.0-20230628211933-3f682344a78d
1818
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500
1919
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78
2020
github.com/adrg/xdg v0.4.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpH
9494
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
9595
github.com/acorn-io/aml v0.0.0-20230619192500-1f56a8955db2 h1:tWz51cHGC6GURkKqA4PdG3ObHz2YBrVzCfxXacNUqVY=
9696
github.com/acorn-io/aml v0.0.0-20230619192500-1f56a8955db2/go.mod h1:UEx5RRLFjryCEHN2pM59+d8A0mPJ3VAxggJOTzPymwg=
97-
github.com/acorn-io/baaah v0.0.0-20230617011755-3291c17915f5 h1:NdDW2tFRDHElXpcFeqTDKg6wB0uRLzJ5Kq0YDgYl/Dg=
98-
github.com/acorn-io/baaah v0.0.0-20230617011755-3291c17915f5/go.mod h1:LtwaWrYK/VuGptWxeD5Sgl0sgJV1ksicpTzyLilow1U=
97+
github.com/acorn-io/baaah v0.0.0-20230628211933-3f682344a78d h1:a+geMqB3U52a4+iMmLZ9R/EXkgxrp0QCetoqLAUnTD8=
98+
github.com/acorn-io/baaah v0.0.0-20230628211933-3f682344a78d/go.mod h1:LtwaWrYK/VuGptWxeD5Sgl0sgJV1ksicpTzyLilow1U=
9999
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500 h1:tiM36bM+iMWuW9HM+YlM1GfNDXC7f565z8Be5epO0qM=
100100
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500/go.mod h1:y6aYj2dF/SlU205bDfA43Y5c9sa/aYr4X5GDqYJzJTU=
101101
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78 h1:5zs9L/CXNkuTdJSbhFWczAorbmx67nqlqswx5CQi7XI=

pkg/cli/apiserver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
minkserver "github.com/acorn-io/mink/pkg/server"
55
cli "github.com/acorn-io/runtime/pkg/cli/builder"
6+
"github.com/acorn-io/runtime/pkg/logserver"
67
"github.com/acorn-io/runtime/pkg/server"
78
"github.com/spf13/cobra"
89
)
@@ -40,6 +41,8 @@ func (a *APIServer) Run(cmd *cobra.Command, args []string) error {
4041
return err
4142
}
4243

44+
logserver.StartServerWithDefaults()
45+
4346
<-cmd.Context().Done()
4447
return cmd.Context().Err()
4548
}

pkg/controller/controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/acorn-io/runtime/pkg/event"
1818
"github.com/acorn-io/runtime/pkg/imagesystem"
1919
"github.com/acorn-io/runtime/pkg/k8sclient"
20+
"github.com/acorn-io/runtime/pkg/logserver"
2021
"github.com/acorn-io/runtime/pkg/scheme"
2122
"k8s.io/apimachinery/pkg/runtime"
2223
"k8s.io/apimachinery/pkg/util/wait"
@@ -103,6 +104,7 @@ func (c *Controller) Start(ctx context.Context) error {
103104
go wait.UntilWithContext(ctx, dnsInit.RenewAndSync, dnsRenewPeriodHours)
104105

105106
autoupgrade.StartSync(ctx, c.Router.Backend())
107+
logserver.StartServerWithDefaults()
106108
}()
107109

108110
return c.Router.Start(ctx)

pkg/logserver/logserver.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Adapted from https://github.com/rancher/log/blob/52031d45f5fdb71cecb9a314865624b42514dbed/server/server.go
2+
// The only real differences here are that we are using logrus directly instead of a wrapper around it,
3+
// and that we are using an abstract namespace (non-file) socket.
4+
// This is only compatible with our fork of the client: https://github.com/acorn-io/loglevel
5+
6+
package logserver
7+
8+
import (
9+
"fmt"
10+
"net"
11+
"net/http"
12+
"os"
13+
14+
"github.com/sirupsen/logrus"
15+
)
16+
17+
var (
18+
DefaultSocketLocation = "\x00logserver"
19+
)
20+
21+
// Server structure is used to the store backend information
22+
type Server struct {
23+
SocketLocation string
24+
Debug bool
25+
}
26+
27+
// StartServerWithDefaults starts the server with default values
28+
func StartServerWithDefaults() {
29+
logrus.SetLevel(logrus.InfoLevel)
30+
s := Server{
31+
SocketLocation: DefaultSocketLocation,
32+
}
33+
s.Start()
34+
}
35+
36+
// Start the server
37+
func (s *Server) Start() {
38+
_ = os.Remove(s.SocketLocation)
39+
go func() {
40+
_ = s.ListenAndServe()
41+
}()
42+
}
43+
44+
// ListenAndServe is used to setup handlers and
45+
// start listening on the specified location
46+
func (s *Server) ListenAndServe() error {
47+
logrus.Infof("Listening on %s", s.SocketLocation)
48+
server := http.Server{}
49+
http.HandleFunc("/v1/loglevel", s.loglevel)
50+
socketListener, err := net.Listen("unix", s.SocketLocation)
51+
if err != nil {
52+
return err
53+
}
54+
return server.Serve(socketListener)
55+
}
56+
57+
func (s *Server) loglevel(rw http.ResponseWriter, req *http.Request) {
58+
// curl -X POST -d "level=debug" localhost:12345/v1/loglevel
59+
logrus.Debugf("Received loglevel request")
60+
if req.Method == http.MethodGet {
61+
level := logrus.GetLevel().String()
62+
_, _ = rw.Write([]byte(fmt.Sprintf("%s\n", level)))
63+
}
64+
65+
if req.Method == http.MethodPost {
66+
if err := req.ParseForm(); err != nil {
67+
rw.WriteHeader(http.StatusInternalServerError)
68+
_, _ = rw.Write([]byte(fmt.Sprintf("Failed to parse form: %v\n", err)))
69+
}
70+
level, err := logrus.ParseLevel(req.Form.Get("level"))
71+
if err != nil {
72+
rw.WriteHeader(http.StatusInternalServerError)
73+
_, _ = rw.Write([]byte(fmt.Sprintf("Failed to parse loglevel: %v\n", err)))
74+
} else {
75+
logrus.SetLevel(level)
76+
_, _ = rw.Write([]byte("OK\n"))
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)