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

GMC: enhance log #371

Merged
merged 12 commits into from
Sep 11, 2024
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
Loading