From b97fdaf627fc1a4611956f7e9b2e17583c0782d6 Mon Sep 17 00:00:00 2001 From: amecea Date: Tue, 12 Feb 2019 16:29:19 +0200 Subject: [PATCH] Move to controller-util log util functions --- Gopkg.lock | 7 +- Gopkg.toml | 2 +- cmd/mysql-operator-sidecar/main.go | 6 +- cmd/mysql-operator/main.go | 13 +++- hack/dev-values.yaml | 1 - hack/docker/sidecar-entrypoint.sh | 2 +- pkg/controller/internal/testutil/testutil.go | 18 +---- pkg/util/log/log.go | 74 ------------------- test/e2e-values.yaml | 1 - .../presslabs/controller-util/Gopkg.toml | 4 - 10 files changed, 24 insertions(+), 104 deletions(-) delete mode 100644 pkg/util/log/log.go diff --git a/Gopkg.lock b/Gopkg.lock index cf2c070e5..6c55d886c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -382,7 +382,7 @@ version = "v0.8.0" [[projects]] - digest = "1:c2e43fd96b4d164d4d573afd73cb8610a27c2ed735a4d028329f162c48d8782e" + digest = "1:0018931fa4284edacbbfc104fbd34c7a560edee63a1375188c8627e3c000c6c6" name = "github.com/presslabs/controller-util" packages = [ "mergo/transformers", @@ -390,8 +390,8 @@ "syncer", ] pruneopts = "T" - revision = "2411d6b0c73e38cce863544a0f9945f6fc848c22" - version = "v0.1.9" + revision = "3df98a3c1e2b9cfe8cd1b1960dc5067845035d91" + version = "v0.1.10" [[projects]] digest = "1:3b5729e3fc486abc6fc16ce026331c3d196e788c3b973081ecf5d28ae3e1050d" @@ -1038,6 +1038,7 @@ "k8s.io/client-go/transport/spdy", "k8s.io/client-go/util/workqueue", "k8s.io/code-generator/cmd/deepcopy-gen", + "k8s.io/klog", "sigs.k8s.io/controller-runtime/pkg/client", "sigs.k8s.io/controller-runtime/pkg/client/config", "sigs.k8s.io/controller-runtime/pkg/controller", diff --git a/Gopkg.toml b/Gopkg.toml index 9862626bb..bd94e05f0 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -28,7 +28,7 @@ required = [ [[constraint]] name="github.com/presslabs/controller-util" - version = "v0.1.9" + version = "v0.1.10" # use wgliang version of cron because provides cron removal [[override]] diff --git a/cmd/mysql-operator-sidecar/main.go b/cmd/mysql-operator-sidecar/main.go index e86a17960..389b6b0ce 100644 --- a/cmd/mysql-operator-sidecar/main.go +++ b/cmd/mysql-operator-sidecar/main.go @@ -21,6 +21,7 @@ import ( "fmt" "os" + utilLog "github.com/presslabs/controller-util/log" "github.com/spf13/cobra" "sigs.k8s.io/controller-runtime/pkg/runtime/signals" @@ -28,7 +29,6 @@ import ( "github.com/presslabs/mysql-operator/pkg/sidecar/appconf" "github.com/presslabs/mysql-operator/pkg/sidecar/apphelper" "github.com/presslabs/mysql-operator/pkg/sidecar/apptakebackup" - customLog "github.com/presslabs/mysql-operator/pkg/util/log" logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" ) @@ -49,6 +49,8 @@ func main() { } // add flags and parse them + debug := false + flag.BoolVar(&debug, "debug", false, "Set logger in debug mode") cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine) if err := cmd.ParseFlags(os.Args[1:]); err != nil { fmt.Fprintf(os.Stderr, "failed to parse global flags, see helps, err: %s", err) @@ -56,7 +58,7 @@ func main() { } // setup logging - logf.SetLogger(customLog.ZapLogger()) + logf.SetLogger(utilLog.ZapLogger(debug)) confCmd := &cobra.Command{ Use: "init-configs", diff --git a/cmd/mysql-operator/main.go b/cmd/mysql-operator/main.go index fef5c6f9f..4baf77991 100644 --- a/cmd/mysql-operator/main.go +++ b/cmd/mysql-operator/main.go @@ -21,9 +21,10 @@ import ( "fmt" "os" - customLog "github.com/presslabs/mysql-operator/pkg/util/log" + utilLog "github.com/presslabs/controller-util/log" "github.com/spf13/pflag" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + "k8s.io/klog" "sigs.k8s.io/controller-runtime/pkg/client/config" "sigs.k8s.io/controller-runtime/pkg/manager" logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" @@ -40,6 +41,9 @@ func main() { fs := pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) fs.AddGoFlagSet(flag.CommandLine) + debug := false + fs.BoolVar(&debug, "debug", false, "Set logger in debug mode") + opt := options.GetOptions() opt.AddFlags(fs) if err := fs.Parse(os.Args); err != nil { @@ -48,7 +52,12 @@ func main() { } // set logging - logf.SetLogger(customLog.ZapLogger()) + klogFlags := flag.NewFlagSet("klog", flag.ExitOnError) + klog.InitFlags(klogFlags) + klogFlags.Set("logtostderr", "true") // nolint: errcheck + klogFlags.Set("alsologtostderr", "false") // nolint: errcheck + + logf.SetLogger(utilLog.ZapLogger(debug)) if err := opt.Validate(); err != nil { log.Error(err, "failed to validate command line args, see help.") diff --git a/hack/dev-values.yaml b/hack/dev-values.yaml index 97afe18d6..11422f1f8 100644 --- a/hack/dev-values.yaml +++ b/hack/dev-values.yaml @@ -1,5 +1,4 @@ extraArgs: - - -v=1 - --debug installCRDs: false diff --git a/hack/docker/sidecar-entrypoint.sh b/hack/docker/sidecar-entrypoint.sh index 5f39fb427..2ab9de3c4 100755 --- a/hack/docker/sidecar-entrypoint.sh +++ b/hack/docker/sidecar-entrypoint.sh @@ -36,7 +36,7 @@ ${GCS_SERVICE_ACCOUNT_JSON_KEY} EOF SIDECAR_BIN=mysql-operator-sidecar -VERBOSE="-v 1 --debug" +VERBOSE="--debug" # exec command case "$1" in diff --git a/pkg/controller/internal/testutil/testutil.go b/pkg/controller/internal/testutil/testutil.go index eb0d8dabd..fe4cb61da 100644 --- a/pkg/controller/internal/testutil/testutil.go +++ b/pkg/controller/internal/testutil/testutil.go @@ -22,10 +22,8 @@ import ( // loggging "github.com/go-logr/logr" - "github.com/go-logr/zapr" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" + utilLog "github.com/presslabs/controller-util/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" ) @@ -46,16 +44,6 @@ func DrainChan(requests <-chan reconcile.Request) { } // NewTestLogger returns a logger good for tests -func NewTestLogger(w io.Writer, options ...zap.Option) logr.Logger { - encoderCfg := zapcore.EncoderConfig{ - MessageKey: "msg", - LevelKey: "level", - NameKey: "logger", - EncodeLevel: zapcore.LowercaseLevelEncoder, - EncodeTime: zapcore.ISO8601TimeEncoder, - EncodeDuration: zapcore.StringDurationEncoder, - } - sink := zapcore.AddSync(w) - core := zapcore.NewCore(zapcore.NewConsoleEncoder(encoderCfg), sink, zap.DebugLevel) - return zapr.NewLogger(zap.New(core).WithOptions(options...)) +func NewTestLogger(w io.Writer) logr.Logger { + return utilLog.ZapLoggerTo(w, true) } diff --git a/pkg/util/log/log.go b/pkg/util/log/log.go deleted file mode 100644 index d51d045b4..000000000 --- a/pkg/util/log/log.go +++ /dev/null @@ -1,74 +0,0 @@ -/* -Copyright 2018 Pressinfra SRL - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package log - -import ( - "flag" - "log" - - "github.com/go-logr/logr" - "github.com/go-logr/zapr" - "go.uber.org/zap" - "go.uber.org/zap/zapcore" - "k8s.io/klog" -) - -var debug = false -var logLevel = 0 - -func init() { - flag.BoolVar(&debug, "debug", false, "Set logger in debug mode") - flag.IntVar(&logLevel, "v", 0, "Set verbosity level") - - klogFlags := flag.NewFlagSet("klog", flag.ExitOnError) - klog.InitFlags(klogFlags) - klogFlags.Set("logtostderr", "true") // nolint: errcheck - klogFlags.Set("alsologtostderr", "false") // nolint: errcheck -} - -// ZapLogger returns a configured logged based on command line flags -v and --debug -func ZapLogger() logr.Logger { - cfg := zap.NewProductionConfig() - maxLevel := 1 - if debug { - cfg = zap.NewDevelopmentConfig() - maxLevel = 100 - } - - //cfg.DisableStacktrace = true - - zapLog, err := cfg.Build(zap.AddCallerSkip(1)) - if err != nil { - log.Fatalf("logger building error: %v ", err) // who watches the watchmen? - } - - // get the v value from flags and then set the logger value - level := zapcore.WarnLevel - if logLevel > 0 { - if logLevel > maxLevel { - logLevel = maxLevel - } - level = zapcore.Level(-1 * logLevel) - } - - // set debugger level - cfg.Level.SetLevel(level) - - // the AtomicLevel can be used to dynamically adjust the logging level - // there's a helper function (zap.LevelFlag) to add a flag for adjusting it too - return zapr.NewLogger(zapLog) -} diff --git a/test/e2e-values.yaml b/test/e2e-values.yaml index 80bb8405f..b026022f3 100644 --- a/test/e2e-values.yaml +++ b/test/e2e-values.yaml @@ -1,5 +1,4 @@ extraArgs: - - -v=1 - --debug orchestrator: diff --git a/vendor/github.com/presslabs/controller-util/Gopkg.toml b/vendor/github.com/presslabs/controller-util/Gopkg.toml index 5715fa19b..8de731268 100644 --- a/vendor/github.com/presslabs/controller-util/Gopkg.toml +++ b/vendor/github.com/presslabs/controller-util/Gopkg.toml @@ -19,10 +19,6 @@ required = [ [prune] go-tests = true -[[constraint]] - name="code.cloudfoundry.org/lager" - version = "v2.0.0" - [[constraint]] name="github.com/imdario/mergo" version = "v0.3.6"