Skip to content

Commit

Permalink
testutil: use contextual logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Octopusjust committed May 30, 2023
1 parent bdf34b3 commit f9ae3e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
1 change: 0 additions & 1 deletion hack/logcheck.conf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ contextual k8s.io/kubernetes/test/e2e/dra/.*
-contextual k8s.io/kubernetes/pkg/controller/nodeipam/.*
-contextual k8s.io/kubernetes/pkg/controller/podgc/.*
-contextual k8s.io/kubernetes/pkg/controller/replicaset/.*
-contextual k8s.io/kubernetes/pkg/controller/testutil/.*
-contextual k8s.io/kubernetes/pkg/controller/util/.*
-contextual k8s.io/kubernetes/pkg/controller/volume/attachdetach/attach_detach_controller.go
-contextual k8s.io/kubernetes/pkg/controller/volume/attachdetach/testing/testvolumespec.go
Expand Down
21 changes: 11 additions & 10 deletions pkg/controller/testutil/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (m *FakeNodeHandler) Watch(_ context.Context, opts metav1.ListOptions) (wat
}

// Patch patches a Node in the fake store.
func (m *FakeNodeHandler) Patch(_ context.Context, name string, pt types.PatchType, data []byte, _ metav1.PatchOptions, subresources ...string) (*v1.Node, error) {
func (m *FakeNodeHandler) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, _ metav1.PatchOptions, subresources ...string) (*v1.Node, error) {
m.lock.Lock()
defer func() {
m.RequestCount++
Expand All @@ -312,12 +312,12 @@ func (m *FakeNodeHandler) Patch(_ context.Context, name string, pt types.PatchTy

originalObjJS, err := json.Marshal(nodeCopy)
if err != nil {
klog.Errorf("Failed to marshal %v", nodeCopy)
klog.FromContext(ctx).Error(nil, "Failed to marshal", "node", klog.KObj(&nodeCopy))
return nil, nil
}
var originalNode v1.Node
if err = json.Unmarshal(originalObjJS, &originalNode); err != nil {
klog.Errorf("Failed to unmarshal original object: %v", err)
klog.FromContext(ctx).Error(err, "Failed to unmarshal original object")
return nil, nil
}

Expand All @@ -326,31 +326,31 @@ func (m *FakeNodeHandler) Patch(_ context.Context, name string, pt types.PatchTy
case types.JSONPatchType:
patchObj, err := jsonpatch.DecodePatch(data)
if err != nil {
klog.Error(err.Error())
klog.FromContext(ctx).Error(err, "")
return nil, nil
}
if patchedObjJS, err = patchObj.Apply(originalObjJS); err != nil {
klog.Error(err.Error())
klog.FromContext(ctx).Error(err, "")
return nil, nil
}
case types.MergePatchType:
if patchedObjJS, err = jsonpatch.MergePatch(originalObjJS, data); err != nil {
klog.Error(err.Error())
klog.FromContext(ctx).Error(err, "")
return nil, nil
}
case types.StrategicMergePatchType:
if patchedObjJS, err = strategicpatch.StrategicMergePatch(originalObjJS, data, originalNode); err != nil {
klog.Error(err.Error())
klog.FromContext(ctx).Error(err, "")
return nil, nil
}
default:
klog.Errorf("unknown Content-Type header for patch: %v", pt)
klog.FromContext(ctx).Error(nil, "Unknown Content-Type header", "patch", pt)
return nil, nil
}

var updatedNode v1.Node
if err = json.Unmarshal(patchedObjJS, &updatedNode); err != nil {
klog.Errorf("Failed to unmarshal patched object: %v", err)
klog.FromContext(ctx).Error(err, "Failed to unmarshal patched object")
return nil, nil
}

Expand Down Expand Up @@ -422,9 +422,10 @@ func (f *FakeRecorder) AnnotatedEventf(obj runtime.Object, annotations map[strin
func (f *FakeRecorder) generateEvent(obj runtime.Object, timestamp metav1.Time, eventtype, reason, message string) {
f.Lock()
defer f.Unlock()
ctx := context.TODO()
ref, err := ref.GetReference(legacyscheme.Scheme, obj)
if err != nil {
klog.Errorf("Encountered error while getting reference: %v", err)
klog.FromContext(ctx).Error(err, "Encountered error while getting reference")
return
}
event := f.makeEvent(ref, eventtype, reason, message)
Expand Down

0 comments on commit f9ae3e7

Please sign in to comment.