Skip to content

Commit

Permalink
Use loglevel to dynamically change logrus' log level during runtime (a…
Browse files Browse the repository at this point in the history
…corn-io#752) (acorn-io#1861)

Signed-off-by: Grant Linville <[email protected]>
  • Loading branch information
g-linville authored and cloudnautique committed Sep 28, 2023
1 parent e74ce67 commit 67e7f9e
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ RUN apk -U add curl
RUN curl -sfL https://github.com/loft-sh/devspace/archive/refs/tags/v6.3.2.tar.gz | tar xzf - --strip-components=1
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

FROM ghcr.io/acorn-io/images-mirror/golang:1.20-alpine AS loglevel
WORKDIR /usr/src
RUN apk -U add curl
RUN curl -sfL https://github.com/acorn-io/loglevel/archive/refs/tags/v0.1.6.tar.gz | tar xzf - --strip-components=1
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"

FROM ghcr.io/acorn-io/images-mirror/golang:1.20 AS build
COPY / /src
WORKDIR /src
Expand All @@ -31,6 +37,7 @@ COPY --from=klipper-lb /usr/bin/entry /usr/local/bin/klipper-lb
COPY ./scripts/ds-containerd-config-path-entry /usr/local/bin
COPY ./scripts/setup-binfmt /usr/local/bin
COPY --from=helper /usr/local/bin/acorn-helper /usr/local/bin/
COPY --from=loglevel /usr/local/bin/loglevel /usr/local/bin/
VOLUME /var/lib/buildkit

COPY /scripts/acorn-helper-init /usr/local/bin
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
cuelang.org/go v0.5.0
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/acorn-io/aml v0.0.0-20230619192500-1f56a8955db2
github.com/acorn-io/baaah v0.0.0-20230617011755-3291c17915f5
github.com/acorn-io/baaah v0.0.0-20230628211933-3f682344a78d
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78
github.com/adrg/xdg v0.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpH
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/acorn-io/aml v0.0.0-20230619192500-1f56a8955db2 h1:tWz51cHGC6GURkKqA4PdG3ObHz2YBrVzCfxXacNUqVY=
github.com/acorn-io/aml v0.0.0-20230619192500-1f56a8955db2/go.mod h1:UEx5RRLFjryCEHN2pM59+d8A0mPJ3VAxggJOTzPymwg=
github.com/acorn-io/baaah v0.0.0-20230617011755-3291c17915f5 h1:NdDW2tFRDHElXpcFeqTDKg6wB0uRLzJ5Kq0YDgYl/Dg=
github.com/acorn-io/baaah v0.0.0-20230617011755-3291c17915f5/go.mod h1:LtwaWrYK/VuGptWxeD5Sgl0sgJV1ksicpTzyLilow1U=
github.com/acorn-io/baaah v0.0.0-20230628211933-3f682344a78d h1:a+geMqB3U52a4+iMmLZ9R/EXkgxrp0QCetoqLAUnTD8=
github.com/acorn-io/baaah v0.0.0-20230628211933-3f682344a78d/go.mod h1:LtwaWrYK/VuGptWxeD5Sgl0sgJV1ksicpTzyLilow1U=
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500 h1:tiM36bM+iMWuW9HM+YlM1GfNDXC7f565z8Be5epO0qM=
github.com/acorn-io/mink v0.0.0-20230523184405-ceaaa366d500/go.mod h1:y6aYj2dF/SlU205bDfA43Y5c9sa/aYr4X5GDqYJzJTU=
github.com/acorn-io/namegenerator v0.0.0-20220915160418-9e3d5a0ffe78 h1:5zs9L/CXNkuTdJSbhFWczAorbmx67nqlqswx5CQi7XI=
Expand Down
3 changes: 3 additions & 0 deletions pkg/cli/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
minkserver "github.com/acorn-io/mink/pkg/server"
cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/logserver"
"github.com/acorn-io/runtime/pkg/server"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,6 +41,8 @@ func (a *APIServer) Run(cmd *cobra.Command, args []string) error {
return err
}

logserver.StartServerWithDefaults()

<-cmd.Context().Done()
return cmd.Context().Err()
}
2 changes: 2 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/acorn-io/runtime/pkg/event"
"github.com/acorn-io/runtime/pkg/imagesystem"
"github.com/acorn-io/runtime/pkg/k8sclient"
"github.com/acorn-io/runtime/pkg/logserver"
"github.com/acorn-io/runtime/pkg/scheme"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -103,6 +104,7 @@ func (c *Controller) Start(ctx context.Context) error {
go wait.UntilWithContext(ctx, dnsInit.RenewAndSync, dnsRenewPeriodHours)

autoupgrade.StartSync(ctx, c.Router.Backend())
logserver.StartServerWithDefaults()
}()

return c.Router.Start(ctx)
Expand Down
79 changes: 79 additions & 0 deletions pkg/logserver/logserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Adapted from https://github.com/rancher/log/blob/52031d45f5fdb71cecb9a314865624b42514dbed/server/server.go
// The only real differences here are that we are using logrus directly instead of a wrapper around it,
// and that we are using an abstract namespace (non-file) socket.
// This is only compatible with our fork of the client: https://github.com/acorn-io/loglevel

package logserver

import (
"fmt"
"net"
"net/http"
"os"

"github.com/sirupsen/logrus"
)

var (
DefaultSocketLocation = "\x00logserver"
)

// Server structure is used to the store backend information
type Server struct {
SocketLocation string
Debug bool
}

// StartServerWithDefaults starts the server with default values
func StartServerWithDefaults() {
logrus.SetLevel(logrus.InfoLevel)
s := Server{
SocketLocation: DefaultSocketLocation,
}
s.Start()
}

// Start the server
func (s *Server) Start() {
_ = os.Remove(s.SocketLocation)
go func() {
_ = s.ListenAndServe()
}()
}

// ListenAndServe is used to setup handlers and
// start listening on the specified location
func (s *Server) ListenAndServe() error {
logrus.Infof("Listening on %s", s.SocketLocation)
server := http.Server{}
http.HandleFunc("/v1/loglevel", s.loglevel)
socketListener, err := net.Listen("unix", s.SocketLocation)
if err != nil {
return err
}
return server.Serve(socketListener)
}

func (s *Server) loglevel(rw http.ResponseWriter, req *http.Request) {
// curl -X POST -d "level=debug" localhost:12345/v1/loglevel
logrus.Debugf("Received loglevel request")
if req.Method == http.MethodGet {
level := logrus.GetLevel().String()
_, _ = rw.Write([]byte(fmt.Sprintf("%s\n", level)))
}

if req.Method == http.MethodPost {
if err := req.ParseForm(); err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("Failed to parse form: %v\n", err)))
}
level, err := logrus.ParseLevel(req.Form.Get("level"))
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
_, _ = rw.Write([]byte(fmt.Sprintf("Failed to parse loglevel: %v\n", err)))
} else {
logrus.SetLevel(level)
_, _ = rw.Write([]byte("OK\n"))
}
}
}

0 comments on commit 67e7f9e

Please sign in to comment.