Skip to content

Commit

Permalink
Log: Remove ObjectRef method
Browse files Browse the repository at this point in the history
This method was only used in tests.
Make this local fuction.

Signed-off-by: L. Pivarc <[email protected]>
  • Loading branch information
xpivarc committed Feb 22, 2023
1 parent 4b3b684 commit 53a3fce
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 49 deletions.
1 change: 0 additions & 1 deletion staging/src/kubevirt.io/client-go/log/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ go_library(
"//staging/src/github.com/golang/glog:go_default_library",
"//vendor/github.com/go-kit/kit/log:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
],
Expand Down
19 changes: 0 additions & 19 deletions staging/src/kubevirt.io/client-go/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/go-kit/kit/log"
"github.com/golang/glog"
flag "github.com/spf13/pflag"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
)
Expand Down Expand Up @@ -228,24 +227,6 @@ func (l FilteredLogger) Object(obj LoggableObject) *FilteredLogger {
return &l
}

func (l FilteredLogger) ObjectRef(obj *v1.ObjectReference) *FilteredLogger {

if obj == nil {
return &l
}

logParams := make([]interface{}, 0)
if obj.Namespace != "" {
logParams = append(logParams, "namespace", obj.Namespace)
}
logParams = append(logParams, "name", obj.Name)
logParams = append(logParams, "kind", obj.Kind)
logParams = append(logParams, "uid", obj.UID)

l.with(logParams...)
return &l
}

func (l FilteredLogger) With(obj ...interface{}) *FilteredLogger {
l.logger = log.With(l.logger, obj...)
return &l
Expand Down
26 changes: 0 additions & 26 deletions staging/src/kubevirt.io/client-go/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"strings"
"testing"

k8sv1 "k8s.io/api/core/v1"
v12 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "kubevirt.io/api/core/v1"
Expand Down Expand Up @@ -308,31 +307,6 @@ func TestObject(t *testing.T) {
tearDown()
}

func TestObjectRef(t *testing.T) {
setUp()
log := MakeLogger(MockLogger{})
log.SetLogLevel(INFO)
vmRef := &k8sv1.ObjectReference{
Kind: "test",
Name: "test",
Namespace: "test",
UID: "test",
}
log.ObjectRef(vmRef).Log("test", "message")
logEntry := logParams[0].([]interface{})
assert(t, logEntry[0].(string) == "level", "Logged line did not have level entry")
assert(t, logEntry[1].(string) == LogLevelNames[INFO], "Logged line was not of level infoLevel")
assert(t, logEntry[2].(string) == "timestamp", "Logged line is not expected format")
assert(t, logEntry[4].(string) == "pos", "Logged line was not pos")
assert(t, logEntry[6].(string) == "component", "Logged line is not expected format")
assert(t, logEntry[7].(string) == "test", "Component was not logged")
assert(t, logEntry[8].(string) == "namespace", "Logged line did not contain object namespace")
assert(t, logEntry[10].(string) == "name", "Logged line did not contain object name")
assert(t, logEntry[12].(string) == "kind", "Logged line did not contain object kind")
assert(t, logEntry[14].(string) == "uid", "Logged line did not contain UUID")
tearDown()
}

func TestError(t *testing.T) {
setUp()
log := MakeLogger(MockLogger{})
Expand Down
22 changes: 19 additions & 3 deletions tests/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,38 @@ func (w *ObjectEventWatcher) Watch(ctx context.Context, processFunc ProcessFunc,

f := processFunc

objectRefOption := func(obj *v1.ObjectReference) []interface{} {
logParams := make([]interface{}, 0)
if obj == nil {
return logParams
}

if obj.Namespace != "" {
logParams = append(logParams, "namespace", obj.Namespace)
}
logParams = append(logParams, "name", obj.Name)
logParams = append(logParams, "kind", obj.Kind)
logParams = append(logParams, "uid", obj.UID)

return logParams
}

if w.warningPolicy.FailOnWarnings {
f = func(event *v1.Event) bool {
msg := fmt.Sprintf("Event(%#v): type: '%v' reason: '%v' %v", event.InvolvedObject, event.Type, event.Reason, event.Message)
if w.warningPolicy.shouldIgnoreWarning(event) == false {
ExpectWithOffset(1, event.Type).NotTo(Equal(string(WarningEvent)), "Unexpected Warning event received: %s,%s: %s", event.InvolvedObject.Name, event.InvolvedObject.UID, event.Message)
}
log.Log.ObjectRef(&event.InvolvedObject).Info(msg)
log.Log.With(objectRefOption(&event.InvolvedObject)).Info(msg)

return processFunc(event)
}
} else {
f = func(event *v1.Event) bool {
if event.Type == string(WarningEvent) {
log.Log.ObjectRef(&event.InvolvedObject).Reason(fmt.Errorf("warning event received")).Error(event.Message)
log.Log.With(objectRefOption(&event.InvolvedObject)).Reason(fmt.Errorf("warning event received")).Error(event.Message)
} else {
log.Log.ObjectRef(&event.InvolvedObject).Infof(event.Message)
log.Log.With(objectRefOption(&event.InvolvedObject)).Infof(event.Message)
}
return processFunc(event)
}
Expand Down

0 comments on commit 53a3fce

Please sign in to comment.