@@ -7,9 +7,15 @@ package main
7
7
8
8
import (
9
9
"crypto/tls"
10
+ "encoding/json"
10
11
"flag"
12
+ "fmt"
13
+ "net/http"
11
14
"os"
12
15
16
+ uzap "go.uber.org/zap"
17
+ "go.uber.org/zap/zapcore"
18
+
13
19
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
14
20
// to ensure that exec-entrypoint and run can make use of them.
15
21
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -46,6 +52,43 @@ func init() {
46
52
//+kubebuilder:scaffold:scheme
47
53
}
48
54
55
+ func logLevelHandler (atomicLevel uzap.AtomicLevel ) http.HandlerFunc {
56
+ return func (w http.ResponseWriter , r * http.Request ) {
57
+ switch r .Method {
58
+ case http .MethodGet :
59
+ // Return the current log level
60
+ currentLevel := atomicLevel .Level ()
61
+ fmt .Fprintf (w , "current log level: %s\n " , currentLevel .String ())
62
+ case http .MethodPut :
63
+ // Change the log level
64
+ var reqBody struct {
65
+ LogLevel string `json:"log_level"`
66
+ }
67
+ if err := json .NewDecoder (r .Body ).Decode (& reqBody ); err != nil {
68
+ http .Error (w , fmt .Sprintf ("invalid request body: %v\n " , err ), http .StatusBadRequest )
69
+ return
70
+ } else {
71
+ if reqBody .LogLevel == "" {
72
+ http .Error (w , "log_level field is required\n " , http .StatusBadRequest )
73
+ return
74
+ }
75
+ }
76
+ var zapLevel zapcore.Level
77
+ if err := zapLevel .UnmarshalText ([]byte (reqBody .LogLevel )); err != nil {
78
+ http .Error (w , fmt .Sprintf ("invalid log level: %v\n " , err ), http .StatusBadRequest )
79
+ return
80
+ }
81
+ atomicLevel .SetLevel (zapLevel )
82
+ fmt .Fprintf (w , "log level set to %s\n " , zapLevel .String ())
83
+ setupLog .V (1 ).Info ("1 log level set to " , "level" , zapLevel .String ())
84
+ setupLog .V (2 ).Info ("2 log level set to " , "level" , zapLevel .String ())
85
+ setupLog .Info ("log level set to " , "level" , zapLevel .String ())
86
+ default :
87
+ http .Error (w , "method not allowed" , http .StatusMethodNotAllowed )
88
+ }
89
+ }
90
+ }
91
+
49
92
func main () {
50
93
var metricsAddr string
51
94
var enableLeaderElection bool
@@ -61,14 +104,27 @@ func main() {
61
104
"If set the metrics endpoint is served securely" )
62
105
flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
63
106
"If set, HTTP/2 will be enabled for the metrics and webhook servers" )
107
+ atomicLevel := uzap .NewAtomicLevel ()
108
+ atomicLevel .SetLevel (zapcore .InfoLevel ) // Set initial log level
64
109
opts := zap.Options {
65
110
Development : true ,
111
+ Level : atomicLevel ,
66
112
}
67
113
opts .BindFlags (flag .CommandLine )
68
114
flag .Parse ()
69
115
70
116
ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
71
117
118
+ // Set up an HTTP server to change the log level dynamically
119
+ http .HandleFunc ("/loglevel" , logLevelHandler (atomicLevel ))
120
+
121
+ // Start the HTTP server
122
+ go func () {
123
+ if err := http .ListenAndServe (":8008" , nil ); err != nil {
124
+ setupLog .Error (err , "HTTP server failed" )
125
+ }
126
+ }()
127
+
72
128
// if the enable-http2 flag is false (the default), http/2 should be disabled
73
129
// due to its vulnerabilities. More specifically, disabling http/2 will
74
130
// prevent from being vulnerable to the HTTP/2 Stream Cancellation and
0 commit comments