Skip to content

Commit

Permalink
GMC: enhance log (#371)
Browse files Browse the repository at this point in the history
* enhance log

Signed-off-by: KfreeZ <[email protected]>

* enhance log

Signed-off-by: KfreeZ <[email protected]>

* add debug level classifier

Signed-off-by: KfreeZ <[email protected]>

* add debug level classifier

Signed-off-by: KfreeZ <[email protected]>

* add dynamic change log level

Signed-off-by: KfreeZ <[email protected]>

* re-trigger e2e

Signed-off-by: KfreeZ <[email protected]>

* add validatation of http requests

Signed-off-by: KfreeZ <[email protected]>

* update doc for log control

Signed-off-by: KfreeZ <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: KfreeZ <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
KfreeZ and pre-commit-ci[bot] authored Sep 11, 2024
1 parent 994250c commit a18404e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 55 deletions.
56 changes: 56 additions & 0 deletions microservices-connector/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ package main

import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"net/http"
"os"

uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
Expand Down Expand Up @@ -46,6 +52,43 @@ func init() {
//+kubebuilder:scaffold:scheme
}

func logLevelHandler(atomicLevel uzap.AtomicLevel) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// Return the current log level
currentLevel := atomicLevel.Level()
fmt.Fprintf(w, "current log level: %s\n", currentLevel.String())
case http.MethodPut:
// Change the log level
var reqBody struct {
LogLevel string `json:"log_level"`
}
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
http.Error(w, fmt.Sprintf("invalid request body: %v\n", err), http.StatusBadRequest)
return
} else {
if reqBody.LogLevel == "" {
http.Error(w, "log_level field is required\n", http.StatusBadRequest)
return
}
}
var zapLevel zapcore.Level
if err := zapLevel.UnmarshalText([]byte(reqBody.LogLevel)); err != nil {
http.Error(w, fmt.Sprintf("invalid log level: %v\n", err), http.StatusBadRequest)
return
}
atomicLevel.SetLevel(zapLevel)
fmt.Fprintf(w, "log level set to %s\n", zapLevel.String())
setupLog.V(1).Info("1 log level set to ", "level", zapLevel.String())
setupLog.V(2).Info("2 log level set to ", "level", zapLevel.String())
setupLog.Info("log level set to ", "level", zapLevel.String())
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}
}

func main() {
var metricsAddr string
var enableLeaderElection bool
Expand All @@ -61,14 +104,27 @@ func main() {
"If set the metrics endpoint is served securely")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
atomicLevel := uzap.NewAtomicLevel()
atomicLevel.SetLevel(zapcore.InfoLevel) // Set initial log level
opts := zap.Options{
Development: true,
Level: atomicLevel,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

// Set up an HTTP server to change the log level dynamically
http.HandleFunc("/loglevel", logLevelHandler(atomicLevel))

// Start the HTTP server
go func() {
if err := http.ListenAndServe(":8008", nil); err != nil {
setupLog.Error(err, "HTTP server failed")
}
}()

// if the enable-http2 flag is false (the default), http/2 should be disabled
// due to its vulnerabilities. More specifically, disabling http/2 will
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
Expand Down
Loading

0 comments on commit a18404e

Please sign in to comment.