From c24c115d895b7daddf1a2a30105c38324a8b28b1 Mon Sep 17 00:00:00 2001 From: Humble Chirammal Date: Thu, 7 Feb 2019 22:30:26 +0530 Subject: [PATCH] Migrate to k8s.io/klog from glog Signed-off-by: Humble Chirammal --- Gopkg.lock | 54 +++++++++--- Gopkg.toml | 4 - cmd/main.go | 23 ++--- pkg/connection/connection.go | 18 ++-- .../golang/glog => k8s.io/klog}/LICENSE | 0 .../glog/glog.go => k8s.io/klog/klog.go} | 87 ++++++++++++++++--- .../glog_file.go => k8s.io/klog/klog_file.go} | 18 ++-- 7 files changed, 146 insertions(+), 58 deletions(-) rename vendor/{github.com/golang/glog => k8s.io/klog}/LICENSE (100%) rename vendor/{github.com/golang/glog/glog.go => k8s.io/klog/klog.go} (93%) rename vendor/{github.com/golang/glog/glog_file.go => k8s.io/klog/klog_file.go} (90%) diff --git a/Gopkg.lock b/Gopkg.lock index 3bf6dff9..96774bd6 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,24 +2,23 @@ [[projects]] + digest = "1:94ffc0947c337d618b6ff5ed9abaddc1217b090c1b3a1ae4739b35b7b25851d5" name = "github.com/container-storage-interface/spec" packages = ["lib/go/csi"] + pruneopts = "NUT" revision = "ed0bb0e1557548aa028307f48728767cfe8f6345" version = "v1.0.0" [[projects]] - branch = "master" - name = "github.com/golang/glog" - packages = ["."] - revision = "23def4e6c14b4da8ac2ed8007337bc5eb5007998" - -[[projects]] + digest = "1:bc38c7c481812e178d85160472e231c5e1c9a7f5845d67e23ee4e706933c10d8" name = "github.com/golang/mock" packages = ["gomock"] + pruneopts = "NUT" revision = "c34cdb4725f4c3844d095133c6e40e448b86589b" version = "v1.1.1" [[projects]] + digest = "1:3b583d3cd8cec5a31ca9f928ddedd17770987daafa8f326b678c54e8e9798430" name = "github.com/golang/protobuf" packages = [ "descriptor", @@ -29,28 +28,34 @@ "ptypes/any", "ptypes/duration", "ptypes/timestamp", - "ptypes/wrappers" + "ptypes/wrappers", ] + pruneopts = "NUT" revision = "b4deda0973fb4c70b50d226b1af49f3da59f5265" version = "v1.1.0" [[projects]] + digest = "1:610f50d23ee3e0fa37d77fc23bf2c3b6897d2dc1c934739f13cdf551e9fc57d9" name = "github.com/kubernetes-csi/csi-lib-utils" packages = ["protosanitizer"] + pruneopts = "NUT" revision = "1628ab5351eafa4fc89a96862a08a891e601e03a" version = "v0.1.0" [[projects]] branch = "master" + digest = "1:cab5d1fe86e273b35887f707dbec779d77d87613d9f2f14ea23002912197ce81" name = "github.com/kubernetes-csi/csi-test" packages = [ "driver", - "utils" + "utils", ] + pruneopts = "NUT" revision = "619da6853e10bef67ddcc8f1c2b68b73154bf11d" [[projects]] branch = "master" + digest = "1:98f099782e33665fa7aa22c534581cd4a18f9a06b9af22cf57721019511d74b4" name = "golang.org/x/net" packages = [ "context", @@ -59,17 +64,21 @@ "http2/hpack", "idna", "internal/timeseries", - "trace" + "trace", ] + pruneopts = "NUT" revision = "c39426892332e1bb5ec0a434a079bf82f5d30c54" [[projects]] branch = "master" + digest = "1:e73252ec48f8c6e92a565b3e1381298bb88d2fe261fc2de619dd5e24a9a663be" name = "golang.org/x/sys" packages = ["unix"] + pruneopts = "NUT" revision = "4e1fef5609515ec7a2cee7b5de30ba6d9b438cbf" [[projects]] + digest = "1:e7071ed636b5422cc51c0e3a6cebc229d6c9fffc528814b519a980641422d619" name = "golang.org/x/text" packages = [ "collate", @@ -85,18 +94,22 @@ "unicode/bidi", "unicode/cldr", "unicode/norm", - "unicode/rangetable" + "unicode/rangetable", ] + pruneopts = "NUT" revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0" version = "v0.3.0" [[projects]] branch = "master" + digest = "1:077c1c599507b3b3e9156d17d36e1e61928ee9b53a5b420f10f28ebd4a0b275c" name = "google.golang.org/genproto" packages = ["googleapis/rpc/status"] + pruneopts = "NUT" revision = "383e8b2c3b9e36c4076b235b32537292176bae20" [[projects]] + digest = "1:9b0340b5befd6c99a69affff1ac35003690e3c5d5886e531c09285f6d9445aa9" name = "google.golang.org/grpc" packages = [ ".", @@ -126,14 +139,31 @@ "resolver/passthrough", "stats", "status", - "tap" + "tap", ] + pruneopts = "NUT" revision = "32fb0ac620c32ba40a4626ddf94d90d12cce3455" version = "v1.14.0" +[[projects]] + digest = "1:9cc257b3c9ff6a0158c9c661ab6eebda1fe8a4a4453cd5c4044dc9a2ebfb992b" + name = "k8s.io/klog" + packages = ["."] + pruneopts = "NUT" + revision = "a5bc97fbc634d635061f3146511332c7e313a55a" + version = "v0.1.0" + [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "9c8b69924630543c8fd15df1bf5295e34c12b2d6f7d0b77986685bfa6dab43f1" + input-imports = [ + "github.com/container-storage-interface/spec/lib/go/csi", + "github.com/golang/mock/gomock", + "github.com/kubernetes-csi/csi-lib-utils/protosanitizer", + "github.com/kubernetes-csi/csi-test/driver", + "google.golang.org/grpc", + "google.golang.org/grpc/connectivity", + "k8s.io/klog", + ] solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index b24d37db..c41db658 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -25,10 +25,6 @@ name = "github.com/container-storage-interface/spec" version = "1.0.0-rc2" -[[constraint]] - branch = "master" - name = "github.com/golang/glog" - [[constraint]] branch = "master" name = "github.com/kubernetes-csi/csi-test" diff --git a/cmd/main.go b/cmd/main.go index a97a7317..5f5a0958 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -23,7 +23,7 @@ import ( "net/http" "time" - "github.com/golang/glog" + "k8s.io/klog" "github.com/kubernetes-csi/livenessprobe/pkg/connection" ) @@ -43,32 +43,32 @@ var ( func runProbe(ctx context.Context, csiConn connection.CSIConnection) error { // Get CSI driver name. - glog.Infof("Calling CSI driver to discover driver name.") + klog.Infof("Calling CSI driver to discover driver name.") csiDriverName, err := csiConn.GetDriverName(ctx) if err != nil { return err } - glog.Infof("CSI driver name: %q", csiDriverName) + klog.Infof("CSI driver name: %q", csiDriverName) // Sending Probe request - glog.Infof("Sending probe request to CSI driver.") + klog.Infof("Sending probe request to CSI driver.") err = csiConn.LivenessProbe(ctx) return err } func getCSIConnection() (connection.CSIConnection, error) { // Connect to CSI. - glog.Infof("Attempting to open a gRPC connection with: %s", *csiAddress) + klog.Infof("Attempting to open a gRPC connection with: %s", *csiAddress) csiConn, err := connection.NewConnection(*csiAddress, *connectionTimeout) return csiConn, err } func checkHealth(w http.ResponseWriter, req *http.Request) { - glog.Infof("Request: %s from: %s\n", req.URL.Path, req.RemoteAddr) + klog.Infof("Request: %s from: %s\n", req.URL.Path, req.RemoteAddr) csiConn, err := getCSIConnection() if err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) - glog.Errorf("Failed to get connection to CSI with error: %v.", err) + klog.Errorf("Failed to get connection to CSI with error: %v.", err) return } defer csiConn.Close() @@ -77,23 +77,24 @@ func checkHealth(w http.ResponseWriter, req *http.Request) { if err := runProbe(ctx, csiConn); err != nil { w.WriteHeader(http.StatusInternalServerError) w.Write([]byte(err.Error())) - glog.Errorf("Health check failed with: %v.", err) + klog.Errorf("Health check failed with: %v.", err) } else { w.WriteHeader(http.StatusOK) w.Write([]byte(`ok`)) - glog.Infof("Health check succeeded.") + klog.Infof("Health check succeeded.") } } func main() { + klog.InitFlags(nil) flag.Set("logtostderr", "true") flag.Parse() addr := net.JoinHostPort("0.0.0.0", *healthzPort) http.HandleFunc("/healthz", checkHealth) - glog.Infof("Serving requests to /healthz on: %s", addr) + klog.Infof("Serving requests to /healthz on: %s", addr) err := http.ListenAndServe(addr, nil) if err != nil { - glog.Fatalf("failed to start http server with error: %v", err) + klog.Fatalf("failed to start http server with error: %v", err) } } diff --git a/pkg/connection/connection.go b/pkg/connection/connection.go index 50e8a27e..bcd9a3c5 100644 --- a/pkg/connection/connection.go +++ b/pkg/connection/connection.go @@ -24,10 +24,10 @@ import ( "time" "github.com/container-storage-interface/spec/lib/go/csi" - "github.com/golang/glog" "github.com/kubernetes-csi/csi-lib-utils/protosanitizer" "google.golang.org/grpc" "google.golang.org/grpc/connectivity" + "k8s.io/klog" ) // CSIConnection is gRPC connection to a remote CSI driver and abstracts all @@ -65,7 +65,7 @@ func NewConnection( } func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) { - glog.Infof("Connecting to %s", address) + klog.Infof("Connecting to %s", address) dialOptions := []grpc.DialOption{ grpc.WithInsecure(), grpc.WithBackoffMaxDelay(time.Second), @@ -85,14 +85,14 @@ func connect(address string, timeout time.Duration) (*grpc.ClientConn, error) { defer cancel() for { if !conn.WaitForStateChange(ctx, conn.GetState()) { - glog.Infof("Connection timed out") + klog.Infof("Connection timed out") return conn, nil // return nil, subsequent GetPluginInfo will show the real connection error } if conn.GetState() == connectivity.Ready { - glog.Infof("Connected") + klog.Infof("Connected") return conn, nil } - glog.Infof("Still trying, connection is %s", conn.GetState()) + klog.Infof("Still trying, connection is %s", conn.GetState()) } } @@ -126,10 +126,10 @@ func (c *csiConnection) Close() error { } func logGRPC(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { - glog.Infof("GRPC call: %s", method) - glog.Infof("GRPC request: %s", protosanitizer.StripSecrets(req)) + klog.Infof("GRPC call: %s", method) + klog.Infof("GRPC request: %s", protosanitizer.StripSecrets(req)) err := invoker(ctx, method, req, reply, cc, opts...) - glog.Infof("GRPC response: %s", protosanitizer.StripSecrets(reply)) - glog.Infof("GRPC error: %v", err) + klog.Infof("GRPC response: %s", protosanitizer.StripSecrets(reply)) + klog.Infof("GRPC error: %v", err) return err } diff --git a/vendor/github.com/golang/glog/LICENSE b/vendor/k8s.io/klog/LICENSE similarity index 100% rename from vendor/github.com/golang/glog/LICENSE rename to vendor/k8s.io/klog/LICENSE diff --git a/vendor/github.com/golang/glog/glog.go b/vendor/k8s.io/klog/klog.go similarity index 93% rename from vendor/github.com/golang/glog/glog.go rename to vendor/k8s.io/klog/klog.go index 54bd7afd..13bcc81a 100644 --- a/vendor/github.com/golang/glog/glog.go +++ b/vendor/k8s.io/klog/klog.go @@ -14,7 +14,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package glog implements logging analogous to the Google-internal C++ INFO/ERROR/V setup. +// Package klog implements logging analogous to the Google-internal C++ INFO/ERROR/V setup. // It provides functions Info, Warning, Error, Fatal, plus formatting variants such as // Infof. It also provides V-style logging controlled by the -v and -vmodule=file=2 flags. // @@ -68,7 +68,7 @@ // -vmodule=gopher*=3 // sets the V level to 3 in all Go files whose names begin "gopher". // -package glog +package klog import ( "bufio" @@ -396,13 +396,6 @@ type flushSyncWriter interface { } func init() { - flag.BoolVar(&logging.toStderr, "logtostderr", false, "log to standard error instead of files") - flag.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files") - flag.Var(&logging.verbosity, "v", "log level for V logs") - flag.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr") - flag.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging") - flag.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace") - // Default stderrThreshold is ERROR. logging.stderrThreshold = errorLog @@ -410,6 +403,22 @@ func init() { go logging.flushDaemon() } +// InitFlags is for explicitly initializing the flags +func InitFlags(flagset *flag.FlagSet) { + if flagset == nil { + flagset = flag.CommandLine + } + flagset.StringVar(&logging.logDir, "log_dir", "", "If non-empty, write log files in this directory") + flagset.StringVar(&logging.logFile, "log_file", "", "If non-empty, use this log file") + flagset.BoolVar(&logging.toStderr, "logtostderr", false, "log to standard error instead of files") + flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", false, "log to standard error as well as files") + flagset.Var(&logging.verbosity, "v", "log level for V logs") + flagset.BoolVar(&logging.skipHeaders, "skip_headers", false, "If true, avoid header prefixes in the log messages") + flagset.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr") + flagset.Var(&logging.vmodule, "vmodule", "comma-separated list of pattern=N settings for file-filtered logging") + flagset.Var(&logging.traceLocation, "log_backtrace_at", "when logging hits line file:N, emit a stack trace") +} + // Flush flushes all pending log I/O. func Flush() { logging.lockAndFlushAll() @@ -453,6 +462,17 @@ type loggingT struct { // safely using atomic.LoadInt32. vmodule moduleSpec // The state of the -vmodule flag. verbosity Level // V logging level, the value of the -v flag/ + + // If non-empty, overrides the choice of directory in which to write logs. + // See createLogDirs for the full list of possible destinations. + logDir string + + // If non-empty, specifies the path of the file to write logs. mutually exclusive + // with the log-dir option. + logFile string + + // If true, do not add the prefix headers, useful when used with SetOutput + skipHeaders bool } // buffer holds a byte Buffer for reuse. The zero value is ready for use. @@ -556,6 +576,9 @@ func (l *loggingT) formatHeader(s severity, file string, line int) *buffer { s = infoLog // for safety. } buf := l.getBuffer() + if l.skipHeaders { + return buf + } // Avoid Fprintf, for speed. The format is so simple that we can do it quickly by hand. // It's worth about 3X. Fprintf is hard. @@ -667,6 +690,45 @@ func (l *loggingT) printWithFileLine(s severity, file string, line int, alsoToSt l.output(s, buf, file, line, alsoToStderr) } +// redirectBuffer is used to set an alternate destination for the logs +type redirectBuffer struct { + w io.Writer +} + +func (rb *redirectBuffer) Sync() error { + return nil +} + +func (rb *redirectBuffer) Flush() error { + return nil +} + +func (rb *redirectBuffer) Write(bytes []byte) (n int, err error) { + return rb.w.Write(bytes) +} + +// SetOutput sets the output destination for all severities +func SetOutput(w io.Writer) { + for s := fatalLog; s >= infoLog; s-- { + rb := &redirectBuffer{ + w: w, + } + logging.file[s] = rb + } +} + +// SetOutputBySeverity sets the output destination for specific severity +func SetOutputBySeverity(name string, w io.Writer) { + sev, ok := severityByName(name) + if !ok { + panic(fmt.Sprintf("SetOutputBySeverity(%q): unrecognized severity name", name)) + } + rb := &redirectBuffer{ + w: w, + } + logging.file[sev] = rb +} + // output writes the data to the log files and releases the buffer. func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoToStderr bool) { l.mu.Lock() @@ -676,10 +738,7 @@ func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoTo } } data := buf.Bytes() - if !flag.Parsed() { - os.Stderr.Write([]byte("ERROR: logging before flag.Parse: ")) - os.Stderr.Write(data) - } else if l.toStderr { + if l.toStderr { os.Stderr.Write(data) } else { if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() { @@ -879,7 +938,7 @@ const flushInterval = 30 * time.Second // flushDaemon periodically flushes the log file buffers. func (l *loggingT) flushDaemon() { - for _ = range time.NewTicker(flushInterval).C { + for range time.NewTicker(flushInterval).C { l.lockAndFlushAll() } } diff --git a/vendor/github.com/golang/glog/glog_file.go b/vendor/k8s.io/klog/klog_file.go similarity index 90% rename from vendor/github.com/golang/glog/glog_file.go rename to vendor/k8s.io/klog/klog_file.go index 65075d28..b76a4e10 100644 --- a/vendor/github.com/golang/glog/glog_file.go +++ b/vendor/k8s.io/klog/klog_file.go @@ -16,11 +16,10 @@ // File I/O for logs. -package glog +package klog import ( "errors" - "flag" "fmt" "os" "os/user" @@ -36,13 +35,9 @@ var MaxSize uint64 = 1024 * 1024 * 1800 // logDirs lists the candidate directories for new log files. var logDirs []string -// If non-empty, overrides the choice of directory in which to write logs. -// See createLogDirs for the full list of possible destinations. -var logDir = flag.String("log_dir", "", "If non-empty, write log files in this directory") - func createLogDirs() { - if *logDir != "" { - logDirs = append(logDirs, *logDir) + if logging.logDir != "" { + logDirs = append(logDirs, logging.logDir) } logDirs = append(logDirs, os.TempDir()) } @@ -103,6 +98,13 @@ var onceLogDirs sync.Once // successfully, create also attempts to update the symlink for that tag, ignoring // errors. func create(tag string, t time.Time) (f *os.File, filename string, err error) { + if logging.logFile != "" { + f, err := os.Create(logging.logFile) + if err == nil { + return f, logging.logFile, nil + } + return nil, "", fmt.Errorf("log: unable to create log: %v", err) + } onceLogDirs.Do(createLogDirs) if len(logDirs) == 0 { return nil, "", errors.New("log: no log dirs")