This is an automated email from the ASF dual-hosted git repository.
zhongxjian pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo-kubernetes.git
The following commit(s) were added to refs/heads/master by this push:
new e38c9667 [dubboctl] fix log lib (#698)
e38c9667 is described below
commit e38c966723255309e4d028b578ec175f3b0ba7a5
Author: Jian Zhong <[email protected]>
AuthorDate: Fri May 2 11:08:07 2025 +0800
[dubboctl] fix log lib (#698)
---
dubboctl/pkg/cli/context.go | 6 +-
dubboctl/pkg/cli/options.go | 6 +-
operator/cmd/cluster/install.go | 5 +-
operator/cmd/cluster/manifest.go | 2 +-
operator/cmd/cluster/uninstall.go | 2 +-
operator/cmd/cluster/upgrade.go | 2 +-
.../pkg/uninstall/{uninstaller.go => prune.go} | 11 +-
operator/pkg/util/clog/clog.go | 18 +--
operator/pkg/util/clog/log/default.go | 24 ----
operator/pkg/util/clog/log/option.go | 39 ------
operator/pkg/util/clog/log/scope.go | 139 ---------------------
operator/pkg/util/clog/log/zap.go | 42 -------
.../pkg/util/{pointer/pointer.go => ptr/ptr.go} | 2 +-
operator/pkg/values/map.go | 8 +-
14 files changed, 22 insertions(+), 284 deletions(-)
diff --git a/dubboctl/pkg/cli/context.go b/dubboctl/pkg/cli/context.go
index 06a11ce5..feee8e7b 100644
--- a/dubboctl/pkg/cli/context.go
+++ b/dubboctl/pkg/cli/context.go
@@ -18,7 +18,7 @@
package cli
import (
- "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
+ "github.com/apache/dubbo-kubernetes/operator/pkg/util/ptr"
"github.com/apache/dubbo-kubernetes/pkg/kube"
"k8s.io/client-go/rest"
)
@@ -37,8 +37,8 @@ type Context interface {
func NewCLIContext(rootFlags *RootFlags) Context {
if rootFlags == nil {
rootFlags = &RootFlags{
- kubeconfig: pointer.Of[string](""),
- Context: pointer.Of[string](""),
+ kubeconfig: ptr.Of[string](""),
+ Context: ptr.Of[string](""),
}
}
return &instance{
diff --git a/dubboctl/pkg/cli/options.go b/dubboctl/pkg/cli/options.go
index c4a94876..a111cfc2 100644
--- a/dubboctl/pkg/cli/options.go
+++ b/dubboctl/pkg/cli/options.go
@@ -18,7 +18,7 @@
package cli
import (
- "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
+ "github.com/apache/dubbo-kubernetes/operator/pkg/util/ptr"
"github.com/spf13/pflag"
)
@@ -34,8 +34,8 @@ type RootFlags struct {
func AddRootFlags(flags *pflag.FlagSet) *RootFlags {
rootFlags := &RootFlags{
- kubeconfig: pointer.Of[string](""),
- Context: pointer.Of[string](""),
+ kubeconfig: ptr.Of[string](""),
+ Context: ptr.Of[string](""),
}
flags.StringVarP(rootFlags.kubeconfig, KubeConfigFlag, "c", "",
"Kubernetes configuration file")
flags.StringVar(rootFlags.Context, ContextFlag, "", "Kubernetes
configuration context")
diff --git a/operator/cmd/cluster/install.go b/operator/cmd/cluster/install.go
index 92fcacfe..56521d61 100644
--- a/operator/cmd/cluster/install.go
+++ b/operator/cmd/cluster/install.go
@@ -23,7 +23,6 @@ import (
"github.com/apache/dubbo-kubernetes/operator/pkg/install"
"github.com/apache/dubbo-kubernetes/operator/pkg/render"
"github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
- "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog/log"
"github.com/apache/dubbo-kubernetes/operator/pkg/util/progress"
"github.com/apache/dubbo-kubernetes/pkg/art"
"github.com/apache/dubbo-kubernetes/pkg/kube"
@@ -35,8 +34,6 @@ import (
"time"
)
-var InstallerScope = log.RegisterScope("installer")
-
type installArgs struct {
// filenames is an array of paths to input DubboOperator CR files.
// TODO
@@ -86,7 +83,7 @@ func InstallCmdWithArgs(ctx cli.Context, rootArgs *RootArgs,
iArgs *installArgs)
return err
}
p := NewPrinterForWriter(cmd.OutOrStderr())
- cl := clog.NewConsoleLogger(cmd.OutOrStdout(),
cmd.ErrOrStderr(), InstallerScope)
+ cl := clog.NewConsoleLogger(cmd.OutOrStdout(),
cmd.ErrOrStderr())
p.Printf("%v\n", art.DubboColoredArt())
return Install(kubeClient, rootArgs, iArgs, cl,
cmd.OutOrStdout(), p)
},
diff --git a/operator/cmd/cluster/manifest.go b/operator/cmd/cluster/manifest.go
index aa3e977b..4bd44f00 100644
--- a/operator/cmd/cluster/manifest.go
+++ b/operator/cmd/cluster/manifest.go
@@ -90,7 +90,7 @@ func manifestGenerateCmd(ctx cli.Context, _ *RootArgs, mgArgs
*manifestGenerateA
}
kubeClient = kc
- cl := clog.NewConsoleLogger(cmd.OutOrStdout(),
cmd.ErrOrStderr(), InstallerScope)
+ cl := clog.NewConsoleLogger(cmd.OutOrStdout(),
cmd.ErrOrStderr())
return manifestGenerate(kubeClient, mgArgs, cl)
},
}
diff --git a/operator/cmd/cluster/uninstall.go
b/operator/cmd/cluster/uninstall.go
index dd241769..56b17eff 100644
--- a/operator/cmd/cluster/uninstall.go
+++ b/operator/cmd/cluster/uninstall.go
@@ -81,7 +81,7 @@ func UninstallCmd(ctx cli.Context) *cobra.Command {
// Uninstall uninstalls by deleting specified manifests.
func Uninstall(cmd *cobra.Command, ctx cli.Context, rootArgs *RootArgs, uiArgs
*uninstallArgs) error {
- cl := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr(),
InstallerScope)
+ cl := clog.NewConsoleLogger(cmd.OutOrStdout(), cmd.ErrOrStderr())
var kubeClient kube.CLIClient
var err error
kubeClient, err = ctx.CLIClientWithRevision("")
diff --git a/operator/cmd/cluster/upgrade.go b/operator/cmd/cluster/upgrade.go
index 789e7aa2..7f5ace70 100644
--- a/operator/cmd/cluster/upgrade.go
+++ b/operator/cmd/cluster/upgrade.go
@@ -47,7 +47,7 @@ func UpgradeCmd(ctx cli.Context) *cobra.Command {
`,
RunE: func(cmd *cobra.Command, args []string) error {
- cl := clog.NewConsoleLogger(cmd.OutOrStdout(),
cmd.ErrOrStderr(), InstallerScope)
+ cl := clog.NewConsoleLogger(cmd.OutOrStdout(),
cmd.ErrOrStderr())
p := NewPrinterForWriter(cmd.OutOrStderr())
client, err := ctx.CLIClient()
if err != nil {
diff --git a/operator/pkg/uninstall/uninstaller.go
b/operator/pkg/uninstall/prune.go
similarity index 92%
rename from operator/pkg/uninstall/uninstaller.go
rename to operator/pkg/uninstall/prune.go
index 92321a72..edb5b93c 100644
--- a/operator/pkg/uninstall/uninstaller.go
+++ b/operator/pkg/uninstall/prune.go
@@ -23,7 +23,7 @@ import (
"github.com/apache/dubbo-kubernetes/operator/pkg/manifest"
"github.com/apache/dubbo-kubernetes/operator/pkg/util"
"github.com/apache/dubbo-kubernetes/operator/pkg/util/clog"
- "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
+ "github.com/apache/dubbo-kubernetes/operator/pkg/util/ptr"
"github.com/apache/dubbo-kubernetes/pkg/config/schema/gvk"
"github.com/apache/dubbo-kubernetes/pkg/kube"
kerrors "k8s.io/apimachinery/pkg/api/errors"
@@ -127,7 +127,7 @@ func DeleteObjectsList(c kube.CLIClient, dryRun bool, log
clog.Logger, objectsLi
return errs.ToError()
}
-func DeleteResource(kc kube.CLIClient, dryRun bool, _ clog.Logger, obj
*unstructured.Unstructured) error {
+func DeleteResource(kc kube.CLIClient, dryRun bool, log clog.Logger, obj
*unstructured.Unstructured) error {
name := fmt.Sprintf("%v/%s.%s", obj.GroupVersionKind(), obj.GetName(),
obj.GetNamespace())
if dryRun {
fmt.Printf("Not remove object %s because of dry run.", name)
@@ -139,16 +139,17 @@ func DeleteResource(kc kube.CLIClient, dryRun bool, _
clog.Logger, obj *unstruct
return err
}
- if err := c.Delete(context.TODO(), obj.GetName(),
metav1.DeleteOptions{PropagationPolicy:
pointer.Of(metav1.DeletePropagationForeground)}); err != nil {
+ if err := c.Delete(context.TODO(), obj.GetName(),
metav1.DeleteOptions{PropagationPolicy:
ptr.Of(metav1.DeletePropagationForeground)}); err != nil {
if !kerrors.IsNotFound(err) {
return err
}
- fmt.Printf("object: %s is not being deleted because it no
longer exists", name)
+ // do not return error if resources are not found
+ log.LogAndPrintf("object: %s is not being deleted because it no
longer exists", name)
return nil
}
- fmt.Printf("✔︎ Removed %s.\n", name)
+ log.LogAndPrintf("✔︎ Removed %s.\n", name)
return nil
}
diff --git a/operator/pkg/util/clog/clog.go b/operator/pkg/util/clog/clog.go
index 826934df..2d7fb4d2 100644
--- a/operator/pkg/util/clog/clog.go
+++ b/operator/pkg/util/clog/clog.go
@@ -19,7 +19,6 @@ package clog
import (
"fmt"
- "github.com/apache/dubbo-kubernetes/operator/pkg/util/clog/log"
"io"
"os"
)
@@ -39,35 +38,23 @@ type Logger interface {
type ConsoleLogger struct {
stdOut io.Writer
stdErr io.Writer
- scope *log.Scope
}
// NewConsoleLogger creates a new logger and returns a pointer to it.
// stdOut and stdErr can be used to capture output for testing.
-func NewConsoleLogger(stdOut, stdErr io.Writer, scope *log.Scope)
*ConsoleLogger {
- s := scope
- if s == nil {
- s = log.RegisterScope(log.DefaultScopeName)
- }
+func NewConsoleLogger(stdOut, stdErr io.Writer) *ConsoleLogger {
return &ConsoleLogger{
stdOut: stdOut,
stdErr: stdErr,
- scope: s,
}
}
-// NewDefaultLogger creates a new logger that outputs to stdout/stderr at
default scope.
-func NewDefaultLogger() *ConsoleLogger {
- return NewConsoleLogger(os.Stdout, os.Stderr, nil)
-}
-
func (l *ConsoleLogger) LogAndPrint(v ...any) {
if len(v) == 0 {
return
}
s := fmt.Sprint(v...)
l.Print(s + "\n")
- l.scope.Infof(s)
}
func (l *ConsoleLogger) LogAndError(v ...any) {
if len(v) == 0 {
@@ -75,7 +62,6 @@ func (l *ConsoleLogger) LogAndError(v ...any) {
}
s := fmt.Sprint(v...)
l.PrintErr(s + "\n")
- l.scope.Infof(s)
}
func (l *ConsoleLogger) LogAndFatal(a ...any) {
l.LogAndError(a...)
@@ -84,12 +70,10 @@ func (l *ConsoleLogger) LogAndFatal(a ...any) {
func (l *ConsoleLogger) LogAndPrintf(format string, a ...any) {
s := fmt.Sprintf(format, a...)
l.Print(s + "\n")
- l.scope.Infof(s)
}
func (l *ConsoleLogger) LogAndErrorf(format string, a ...any) {
s := fmt.Sprintf(format, a...)
l.PrintErr(s + "\n")
- l.scope.Infof(s)
}
func (l *ConsoleLogger) LogAndFatalf(format string, a ...any) {
diff --git a/operator/pkg/util/clog/log/default.go
b/operator/pkg/util/clog/log/default.go
deleted file mode 100644
index f10c36fc..00000000
--- a/operator/pkg/util/clog/log/default.go
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
-
-func registerDefaultScopes() (defaults *Scope) {
- return registerScope(DefaultScopeName, 1)
-}
-
-var defaultScope = registerDefaultScopes()
diff --git a/operator/pkg/util/clog/log/option.go
b/operator/pkg/util/clog/log/option.go
deleted file mode 100644
index 8bc43a80..00000000
--- a/operator/pkg/util/clog/log/option.go
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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
-
-const (
- DefaultScopeName = "default"
- defaultStackTraceLevel = NoneLevel
- defaultOutputPath = "stdout"
- defaultErrorOutputPath = "stderr"
-)
-
-type Level int
-
-const (
- // NoneLevel disables logging
- NoneLevel Level = iota
- FatalLevel
- ErrorLevel
- WarnLevel
- InfoLevel
- DebugLevel
-)
-
-type Options struct{}
diff --git a/operator/pkg/util/clog/log/scope.go
b/operator/pkg/util/clog/log/scope.go
deleted file mode 100644
index 949ac7b7..00000000
--- a/operator/pkg/util/clog/log/scope.go
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 (
- "fmt"
- "go.uber.org/atomic"
- "go.uber.org/zap"
- "go.uber.org/zap/zapcore"
- "runtime"
- "strings"
- "sync"
- "time"
-)
-
-type Scope struct {
- name string
- nameToEmit string
- outputLevel *atomic.Value
- stackTraceLevel *atomic.Value
- logCallers *atomic.Value
- labels map[string]any
- labelKeys []string
- callerSkip int
-}
-
-var (
- scopes = make(map[string]*Scope)
- lock sync.RWMutex
-)
-
-func RegisterScope(name string) *Scope {
- return registerScope(name, 0)
-}
-
-func registerScope(name string, callerSkip int) *Scope {
- if strings.ContainsAny(name, ":,.") {
- panic(fmt.Sprintf("scope name %s is invalid, it cannot contain
colons, commas, or periods", name))
- }
- lock.Lock()
- defer lock.Unlock()
- s, ok := scopes[name]
- if !ok {
- s = &Scope{
- name: name,
- callerSkip: callerSkip,
- outputLevel: &atomic.Value{},
- stackTraceLevel: &atomic.Value{},
- logCallers: &atomic.Value{},
- }
- s.SetOutputLevel(InfoLevel)
- s.SetStackTraceLevel(NoneLevel)
- s.SetLogCallers(false)
- if name != DefaultScopeName {
- s.nameToEmit = name
- }
- scopes[name] = s
- }
- s.labels = make(map[string]any)
- return s
-}
-
-func (s *Scope) SetOutputLevel(l Level) {
- s.outputLevel.Store(l)
-}
-func (s *Scope) SetStackTraceLevel(l Level) {
- s.stackTraceLevel.Store(l)
-}
-func (s *Scope) SetLogCallers(logCallers bool) {
- s.logCallers.Store(logCallers)
-}
-
-func (s *Scope) GetOutputLevel() Level {
- return s.outputLevel.Load().(Level)
-}
-
-func (s *Scope) GetStackTraceLevel() Level {
- return s.stackTraceLevel.Load().(Level)
-}
-
-func (s *Scope) Infof(format string, args ...any) {
- if s.GetOutputLevel() >= InfoLevel {
- msg := maybeSprintf(format, args)
- s.emit(zapcore.InfoLevel, msg)
- }
-}
-
-func maybeSprintf(format string, args []any) string {
- msg := format
- if len(args) > 0 {
- msg = fmt.Sprintf(format, args...)
- }
- return msg
-}
-
-func (s *Scope) emit(level zapcore.Level, msg string) {
- s.emitWithTime(level, msg, time.Now())
-}
-
-func (s *Scope) emitWithTime(level zapcore.Level, msg string, t time.Time) {
- if t.IsZero() {
- t = time.Now()
- }
-
- e := zapcore.Entry{
- Message: msg,
- Level: level,
- Time: t,
- LoggerName: s.nameToEmit,
- }
-
- if s.GetLogCallers() {
- e.Caller = zapcore.NewEntryCaller(runtime.Caller(s.callerSkip +
callerSkipOffset))
- }
-
- if dumpStack(level, s) {
- e.Stack = zap.Stack("").String
- }
- // TODO
-}
-
-func (s *Scope) GetLogCallers() bool {
- return s.logCallers.Load().(bool)
-}
diff --git a/operator/pkg/util/clog/log/zap.go
b/operator/pkg/util/clog/log/zap.go
deleted file mode 100644
index bbe2943b..00000000
--- a/operator/pkg/util/clog/log/zap.go
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 "go.uber.org/zap/zapcore"
-
-var toLevel = map[zapcore.Level]Level{
- zapcore.FatalLevel: FatalLevel,
- zapcore.ErrorLevel: ErrorLevel,
- zapcore.WarnLevel: WarnLevel,
- zapcore.InfoLevel: InfoLevel,
- zapcore.DebugLevel: DebugLevel,
-}
-
-const callerSkipOffset = 3
-
-func dumpStack(level zapcore.Level, scope *Scope) bool {
- thresh := toLevel[level]
- if scope != defaultScope {
- thresh = ErrorLevel
- switch level {
- case zapcore.FatalLevel:
- thresh = FatalLevel
- }
- }
- return scope.GetStackTraceLevel() >= thresh
-}
diff --git a/operator/pkg/util/pointer/pointer.go b/operator/pkg/util/ptr/ptr.go
similarity index 98%
rename from operator/pkg/util/pointer/pointer.go
rename to operator/pkg/util/ptr/ptr.go
index 13e3e2f7..684aa0b4 100644
--- a/operator/pkg/util/pointer/pointer.go
+++ b/operator/pkg/util/ptr/ptr.go
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package pointer
+package ptr
// Of returns a pointer to the input. In most cases, callers should just do
&t. However, in some cases
// Go cannot take a pointer. For example, `pointer.Of(f())`.
diff --git a/operator/pkg/values/map.go b/operator/pkg/values/map.go
index 33de0b67..22976cca 100644
--- a/operator/pkg/values/map.go
+++ b/operator/pkg/values/map.go
@@ -20,7 +20,7 @@ package values
import (
"encoding/json"
"fmt"
- "github.com/apache/dubbo-kubernetes/operator/pkg/util/pointer"
+ "github.com/apache/dubbo-kubernetes/operator/pkg/util/ptr"
"path/filepath"
"reflect"
"sigs.k8s.io/yaml"
@@ -145,7 +145,7 @@ func splitPath(path string) []string {
func GetPathAs[T any](m Map, name string) T {
v, ok := m.GetPath(name)
if !ok {
- return pointer.Empty[T]()
+ return ptr.Empty[T]()
}
t, _ := v.(T)
return t
@@ -159,7 +159,7 @@ func (m Map) GetPathString(s string) string {
// GetPathStringOr is a helper around TryGetPathAs[string] to allow usage as a
method (otherwise impossible with generics),
// with an allowance for a default value if it is not found/not set.
func (m Map) GetPathStringOr(s string, def string) string {
- return pointer.NonEmptyOrDefault(m.GetPathString(s), def)
+ return ptr.NonEmptyOrDefault(m.GetPathString(s), def)
}
func (m Map) GetPath(name string) (any, bool) {
@@ -242,7 +242,7 @@ func fromJSON[T any](overlay []byte) (T, error) {
v := new(T)
err := json.Unmarshal(overlay, &v)
if err != nil {
- return pointer.Empty[T](), err
+ return ptr.Empty[T](), err
}
return *v, nil
}