mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-02 11:18:16 +00:00
volume: use contextual logging
This commit is contained in:
@@ -17,8 +17,10 @@ limitations under the License.
|
||||
package testing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"k8s.io/klog/v2"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"sync"
|
||||
@@ -32,7 +34,6 @@ import (
|
||||
"k8s.io/apimachinery/pkg/watch"
|
||||
"k8s.io/client-go/kubernetes/fake"
|
||||
core "k8s.io/client-go/testing"
|
||||
"k8s.io/klog/v2"
|
||||
)
|
||||
|
||||
// ErrVersionConflict is the error returned when resource version of requested
|
||||
@@ -87,14 +88,14 @@ type ReactorError struct {
|
||||
// to evaluate test results.
|
||||
// All updated objects are also inserted into changedObjects queue and
|
||||
// optionally sent back to the controller via its watchers.
|
||||
func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
func (r *VolumeReactor) React(ctx context.Context, action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
klog.V(4).Infof("reactor got operation %q on %q", action.GetVerb(), action.GetResource())
|
||||
logger := klog.FromContext(ctx)
|
||||
logger.V(4).Info("Reactor got operation", "resource", action.GetResource(), "verb", action.GetVerb())
|
||||
|
||||
// Inject error when requested
|
||||
err = r.injectReactError(action)
|
||||
err = r.injectReactError(ctx, action)
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
@@ -124,7 +125,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
}
|
||||
r.changedObjects = append(r.changedObjects, volume)
|
||||
r.changedSinceLastSync++
|
||||
klog.V(4).Infof("created volume %s", volume.Name)
|
||||
logger.V(4).Info("Created volume", "volumeName", volume.Name)
|
||||
return true, volume, nil
|
||||
|
||||
case action.Matches("create", "persistentvolumeclaims"):
|
||||
@@ -144,7 +145,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
}
|
||||
r.changedObjects = append(r.changedObjects, claim)
|
||||
r.changedSinceLastSync++
|
||||
klog.V(4).Infof("created claim %s", claim.Name)
|
||||
logger.V(4).Info("Created claim", "PVC", klog.KObj(claim))
|
||||
return true, claim, nil
|
||||
|
||||
case action.Matches("update", "persistentvolumes"):
|
||||
@@ -160,7 +161,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
return true, obj, ErrVersionConflict
|
||||
}
|
||||
if reflect.DeepEqual(storedVolume, volume) {
|
||||
klog.V(4).Infof("nothing updated volume %s", volume.Name)
|
||||
logger.V(4).Info("Nothing updated volume", "volumeName", volume.Name)
|
||||
return true, volume, nil
|
||||
}
|
||||
// Don't modify the existing object
|
||||
@@ -177,7 +178,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
r.volumes[volume.Name] = volume
|
||||
r.changedObjects = append(r.changedObjects, volume)
|
||||
r.changedSinceLastSync++
|
||||
klog.V(4).Infof("saved updated volume %s", volume.Name)
|
||||
logger.V(4).Info("Saved updated volume", "volumeName", volume.Name)
|
||||
return true, volume, nil
|
||||
|
||||
case action.Matches("update", "persistentvolumeclaims"):
|
||||
@@ -193,7 +194,7 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
return true, obj, ErrVersionConflict
|
||||
}
|
||||
if reflect.DeepEqual(storedClaim, claim) {
|
||||
klog.V(4).Infof("nothing updated claim %s", claim.Name)
|
||||
logger.V(4).Info("Nothing updated claim", "PVC", klog.KObj(claim))
|
||||
return true, claim, nil
|
||||
}
|
||||
// Don't modify the existing object
|
||||
@@ -210,32 +211,33 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
r.claims[claim.Name] = claim
|
||||
r.changedObjects = append(r.changedObjects, claim)
|
||||
r.changedSinceLastSync++
|
||||
klog.V(4).Infof("saved updated claim %s", claim.Name)
|
||||
logger.V(4).Info("Saved updated claim", "PVC", klog.KObj(claim))
|
||||
return true, claim, nil
|
||||
|
||||
case action.Matches("get", "persistentvolumes"):
|
||||
name := action.(core.GetAction).GetName()
|
||||
volume, found := r.volumes[name]
|
||||
if found {
|
||||
klog.V(4).Infof("GetVolume: found %s", volume.Name)
|
||||
logger.V(4).Info("GetVolume: found volume", "volumeName", volume.Name)
|
||||
return true, volume.DeepCopy(), nil
|
||||
}
|
||||
klog.V(4).Infof("GetVolume: volume %s not found", name)
|
||||
logger.V(4).Info("GetVolume: volume not found", "volumeName", name)
|
||||
return true, nil, apierrors.NewNotFound(action.GetResource().GroupResource(), name)
|
||||
|
||||
case action.Matches("get", "persistentvolumeclaims"):
|
||||
name := action.(core.GetAction).GetName()
|
||||
nameSpace := action.(core.GetAction).GetNamespace()
|
||||
claim, found := r.claims[name]
|
||||
if found {
|
||||
klog.V(4).Infof("GetClaim: found %s", claim.Name)
|
||||
logger.V(4).Info("GetClaim: found claim", "PVC", klog.KObj(claim))
|
||||
return true, claim.DeepCopy(), nil
|
||||
}
|
||||
klog.V(4).Infof("GetClaim: claim %s not found", name)
|
||||
logger.V(4).Info("GetClaim: claim not found", "PVC", klog.KRef(nameSpace, name))
|
||||
return true, nil, apierrors.NewNotFound(action.GetResource().GroupResource(), name)
|
||||
|
||||
case action.Matches("delete", "persistentvolumes"):
|
||||
name := action.(core.DeleteAction).GetName()
|
||||
klog.V(4).Infof("deleted volume %s", name)
|
||||
logger.V(4).Info("Deleted volume", "volumeName", name)
|
||||
obj, found := r.volumes[name]
|
||||
if found {
|
||||
delete(r.volumes, name)
|
||||
@@ -249,7 +251,8 @@ func (r *VolumeReactor) React(action core.Action) (handled bool, ret runtime.Obj
|
||||
|
||||
case action.Matches("delete", "persistentvolumeclaims"):
|
||||
name := action.(core.DeleteAction).GetName()
|
||||
klog.V(4).Infof("deleted claim %s", name)
|
||||
nameSpace := action.(core.DeleteAction).GetNamespace()
|
||||
logger.V(4).Info("Deleted claim", "PVC", klog.KRef(nameSpace, name))
|
||||
obj, found := r.claims[name]
|
||||
if found {
|
||||
delete(r.claims, name)
|
||||
@@ -297,18 +300,18 @@ func (r *VolumeReactor) getWatches(gvr schema.GroupVersionResource, ns string) [
|
||||
|
||||
// injectReactError returns an error when the test requested given action to
|
||||
// fail. nil is returned otherwise.
|
||||
func (r *VolumeReactor) injectReactError(action core.Action) error {
|
||||
func (r *VolumeReactor) injectReactError(ctx context.Context, action core.Action) error {
|
||||
if len(r.errors) == 0 {
|
||||
// No more errors to inject, everything should succeed.
|
||||
return nil
|
||||
}
|
||||
|
||||
logger := klog.FromContext(ctx)
|
||||
for i, expected := range r.errors {
|
||||
klog.V(4).Infof("trying to match %q %q with %q %q", expected.Verb, expected.Resource, action.GetVerb(), action.GetResource())
|
||||
logger.V(4).Info("Trying to match resource verb", "resource", action.GetResource(), "verb", action.GetVerb(), "expectedResource", expected.Resource, "expectedVerb", expected.Verb)
|
||||
if action.Matches(expected.Verb, expected.Resource) {
|
||||
// That's the action we're waiting for, remove it from injectedErrors
|
||||
r.errors = append(r.errors[:i], r.errors[i+1:]...)
|
||||
klog.V(4).Infof("reactor found matching error at index %d: %q %q, returning %v", i, expected.Verb, expected.Resource, expected.Error)
|
||||
logger.V(4).Info("Reactor found matching error", "index", i, "expectedResource", expected.Resource, "expectedVerb", expected.Verb, "err", expected.Error)
|
||||
return expected.Error
|
||||
}
|
||||
}
|
||||
@@ -382,7 +385,7 @@ func (r *VolumeReactor) CheckClaims(expectedClaims []*v1.PersistentVolumeClaim)
|
||||
|
||||
// PopChange returns one recorded updated object, either *v1.PersistentVolume
|
||||
// or *v1.PersistentVolumeClaim. Returns nil when there are no changes.
|
||||
func (r *VolumeReactor) PopChange() interface{} {
|
||||
func (r *VolumeReactor) PopChange(ctx context.Context) interface{} {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
|
||||
@@ -391,14 +394,15 @@ func (r *VolumeReactor) PopChange() interface{} {
|
||||
}
|
||||
|
||||
// For debugging purposes, print the queue
|
||||
logger := klog.FromContext(ctx)
|
||||
for _, obj := range r.changedObjects {
|
||||
switch obj.(type) {
|
||||
case *v1.PersistentVolume:
|
||||
vol, _ := obj.(*v1.PersistentVolume)
|
||||
klog.V(4).Infof("reactor queue: %s", vol.Name)
|
||||
logger.V(4).Info("Reactor queue", "volumeName", vol.Name)
|
||||
case *v1.PersistentVolumeClaim:
|
||||
claim, _ := obj.(*v1.PersistentVolumeClaim)
|
||||
klog.V(4).Infof("reactor queue: %s", claim.Name)
|
||||
logger.V(4).Info("Reactor queue", "PVC", klog.KObj(claim))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -539,7 +543,7 @@ func (r *VolumeReactor) MarkVolumeAvailable(name string) {
|
||||
}
|
||||
|
||||
// NewVolumeReactor creates a volume reactor.
|
||||
func NewVolumeReactor(client *fake.Clientset, fakeVolumeWatch, fakeClaimWatch *watch.FakeWatcher, errors []ReactorError) *VolumeReactor {
|
||||
func NewVolumeReactor(ctx context.Context, client *fake.Clientset, fakeVolumeWatch, fakeClaimWatch *watch.FakeWatcher, errors []ReactorError) *VolumeReactor {
|
||||
reactor := &VolumeReactor{
|
||||
volumes: make(map[string]*v1.PersistentVolume),
|
||||
claims: make(map[string]*v1.PersistentVolumeClaim),
|
||||
@@ -548,13 +552,30 @@ func NewVolumeReactor(client *fake.Clientset, fakeVolumeWatch, fakeClaimWatch *w
|
||||
errors: errors,
|
||||
watchers: make(map[schema.GroupVersionResource]map[string][]*watch.RaceFreeFakeWatcher),
|
||||
}
|
||||
client.AddReactor("create", "persistentvolumes", reactor.React)
|
||||
client.AddReactor("create", "persistentvolumeclaims", reactor.React)
|
||||
client.AddReactor("update", "persistentvolumes", reactor.React)
|
||||
client.AddReactor("update", "persistentvolumeclaims", reactor.React)
|
||||
client.AddReactor("get", "persistentvolumes", reactor.React)
|
||||
client.AddReactor("get", "persistentvolumeclaims", reactor.React)
|
||||
client.AddReactor("delete", "persistentvolumes", reactor.React)
|
||||
client.AddReactor("delete", "persistentvolumeclaims", reactor.React)
|
||||
client.AddReactor("create", "persistentvolumes", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
|
||||
client.AddReactor("create", "persistentvolumeclaims", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
client.AddReactor("update", "persistentvolumes", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
client.AddReactor("update", "persistentvolumeclaims", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
client.AddReactor("get", "persistentvolumes", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
client.AddReactor("get", "persistentvolumeclaims", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
client.AddReactor("delete", "persistentvolumes", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
client.AddReactor("delete", "persistentvolumeclaims", func(action core.Action) (handled bool, ret runtime.Object, err error) {
|
||||
return reactor.React(ctx, action)
|
||||
})
|
||||
return reactor
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user