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

add support to configure logging format #98

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ spec:

* All glog / klog arguments are supported, such as `-v <log level>` or `-alsologtostderr`.

* `--logging-format`: Set the log format. Default is `text`.

## Community, discussion, contribution, and support

Learn how to engage with the Kubernetes community on the [community page](http://kubernetes.io/community/).
Expand Down
10 changes: 5 additions & 5 deletions cmd/livenessprobe/livenessprobe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"flag"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -29,6 +28,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/kubernetes-csi/csi-lib-utils/metrics"
"github.com/kubernetes-csi/csi-test/v4/driver"
"github.com/spf13/pflag"
)

const (
Expand Down Expand Up @@ -75,8 +75,8 @@ func TestProbe(t *testing.T) {
_, driver, idServer, _, _, cleanUpFunc := createMockServer(t)
defer cleanUpFunc()

flag.Set("csi-address", driver.Address())
flag.Parse()
pflag.Set("csi-address", driver.Address())
pflag.Parse()

var injectedErr error

Expand Down Expand Up @@ -117,8 +117,8 @@ func TestProbe_issue68(t *testing.T) {
_, driver, idServer, _, _, cleanUpFunc := createMockServer(t)
defer cleanUpFunc()

flag.Set("csi-address", driver.Address())
flag.Parse()
pflag.Set("csi-address", driver.Address())
pflag.Parse()

var injectedErr error

Expand Down
31 changes: 21 additions & 10 deletions cmd/livenessprobe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ package main

import (
"context"
"flag"
goflag "flag"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"

"github.com/spf13/pflag"
"google.golang.org/grpc"
"k8s.io/component-base/logs"
"k8s.io/klog/v2"

connlib "github.com/kubernetes-csi/csi-lib-utils/connection"
Expand All @@ -40,12 +42,12 @@ const (

// Command line flags
var (
probeTimeout = flag.Duration("probe-timeout", time.Second, "Probe timeout in seconds.")
csiAddress = flag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
healthzPort = flag.String("health-port", defaultHealthzPort, fmt.Sprintf("(deprecated) TCP ports for listening healthz requests. The default is `%s`. If set, `--http-endpoint` cannot be set.", defaultHealthzPort))
metricsAddress = flag.String("metrics-address", "", "(deprecated) The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled. If set, `--http-endpoint` cannot be set, and the address cannot resolve to localhost + the port from `--health-port`.")
httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including CSI driver health check and metrics. The default is empty string, which means the server is disabled. If set, `--health-port` and `--metrics-address` cannot be explicitly set.")
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
probeTimeout = pflag.Duration("probe-timeout", time.Second, "Probe timeout in seconds.")
csiAddress = pflag.String("csi-address", "/run/csi/socket", "Address of the CSI driver socket.")
healthzPort = pflag.String("health-port", defaultHealthzPort, fmt.Sprintf("(deprecated) TCP ports for listening healthz requests. The default is `%s`. If set, `--http-endpoint` cannot be set.", defaultHealthzPort))
metricsAddress = pflag.String("metrics-address", "", "(deprecated) The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled. If set, `--http-endpoint` cannot be set, and the address cannot resolve to localhost + the port from `--health-port`.")
httpEndpoint = pflag.String("http-endpoint", "", "The TCP network address where the HTTP server for diagnostics, including CSI driver health check and metrics. The default is empty string, which means the server is disabled. If set, `--health-port` and `--metrics-address` cannot be explicitly set.")
metricsPath = pflag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
)

type healthProbe struct {
Expand Down Expand Up @@ -119,9 +121,18 @@ func acquireConnection(ctx context.Context, metricsManager metrics.CSIMetricsMan
}

func main() {
klog.InitFlags(nil)
flag.Set("logtostderr", "true")
flag.Parse()
logs.InitLogs()
defer logs.FlushLogs()

logOptions := logs.NewOptions()
logOptions.AddFlags(pflag.CommandLine)

pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
pflag.Set("logtostderr", "true")
pflag.Parse()

// set log formatter type
logOptions.Apply()

if *healthzPort != defaultHealthzPort && *httpEndpoint != "" {
klog.Error("only one of `--health-port` and `--http-endpoint` can be explicitly set.")
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/golang/mock v1.4.3
github.com/kubernetes-csi/csi-lib-utils v0.9.0
github.com/kubernetes-csi/csi-test/v4 v4.0.0-20200806214950-555d70a11a8b
github.com/spf13/pflag v1.0.5
google.golang.org/grpc v1.29.0
k8s.io/klog/v2 v2.3.0
k8s.io/component-base v0.20.0
k8s.io/klog/v2 v2.4.0
)
Loading