Skip to content

Commit

Permalink
Change argument from context to logger where logger is sufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
bells17 committed Jan 25, 2024
1 parent 4c7e99a commit 4afa277
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ func (ctrl *ProvisionController) provisionClaimOperation(ctx context.Context, cl

logger.Info("Succeeded")

if err := ctrl.volumeStore.StoreVolume(ctx, claim, volume); err != nil {
if err := ctrl.volumeStore.StoreVolume(logger, claim, volume); err != nil {
return ProvisioningFinished, err
}
if err = ctrl.volumes.Add(volume); err != nil {
Expand Down
25 changes: 12 additions & 13 deletions controller/volume_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type VolumeStore interface {
// is being saved in background.
// In error is returned, no PV was saved and corresponding PVC needs
// to be re-queued (so whole provisioning needs to be done again).
StoreVolume(ctx context.Context, claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) error
StoreVolume(logger klog.Logger, claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) error

// Runs any background goroutines for implementation of the interface.
Run(ctx context.Context, threadiness int)
Expand Down Expand Up @@ -83,11 +83,11 @@ func NewVolumeStoreQueue(
}
}

func (q *queueStore) StoreVolume(ctx context.Context, _ *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) error {
if err := q.doSaveVolume(ctx, volume); err != nil {
func (q *queueStore) StoreVolume(logger klog.Logger, _ *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) error {
if err := q.doSaveVolume(logger, volume); err != nil {
q.volumes.Store(volume.Name, volume)
q.queue.Add(volume.Name)
klog.FromContext(ctx).Error(err, "Failed to save volume", "volume", volume.Name)
logger.Error(err, "Failed to save volume", "volume", volume.Name)
}
// Consume any error, this Store will retry in background.
return nil
Expand Down Expand Up @@ -140,33 +140,33 @@ func (q *queueStore) processNextWorkItem(ctx context.Context) bool {
return true
}

if err := q.doSaveVolume(ctx, volume); err != nil {
logger := klog.FromContext(ctx)
if err := q.doSaveVolume(logger, volume); err != nil {
q.queue.AddRateLimited(volumeName)
utilruntime.HandleError(err)
klog.FromContext(ctx).V(5).Info("Volume enqueued", "volume", volume.Name)
logger.V(5).Info("Volume enqueued", "volume", volume.Name)
return true
}
q.volumes.Delete(volumeName)
q.queue.Forget(volumeName)
return true
}

func (q *queueStore) doSaveVolume(ctx context.Context, volume *v1.PersistentVolume) error {
logger := klog.FromContext(ctx)
func (q *queueStore) doSaveVolume(logger klog.Logger, volume *v1.PersistentVolume) error {
logger.V(5).Info("Saving volume", "volume", volume.Name)
_, err := q.client.CoreV1().PersistentVolumes().Create(context.Background(), volume, metav1.CreateOptions{})
if err == nil || apierrs.IsAlreadyExists(err) {
logger.V(5).Info("Volume saved", "volume", volume.Name)
q.sendSuccessEvent(ctx, volume)
q.sendSuccessEvent(logger, volume)
return nil
}
return fmt.Errorf("error saving volume %s: %s", volume.Name, err)
}

func (q *queueStore) sendSuccessEvent(ctx context.Context, volume *v1.PersistentVolume) {
func (q *queueStore) sendSuccessEvent(logger klog.Logger, volume *v1.PersistentVolume) {
claimObjs, err := q.claimsIndexer.ByIndex(uidIndex, string(volume.Spec.ClaimRef.UID))
if err != nil {
klog.FromContext(ctx).V(2).Info("Error sending event to claim", "claimUID", volume.Spec.ClaimRef.UID, "err", err)
logger.V(2).Info("Error sending event to claim", "claimUID", volume.Spec.ClaimRef.UID, "err", err)
return
}
if len(claimObjs) != 1 {
Expand Down Expand Up @@ -207,9 +207,8 @@ func NewBackoffStore(client kubernetes.Interface,
}
}

func (b *backoffStore) StoreVolume(ctx context.Context, claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) error {
func (b *backoffStore) StoreVolume(logger klog.Logger, claim *v1.PersistentVolumeClaim, volume *v1.PersistentVolume) error {
// Try to create the PV object several times
logger := klog.FromContext(ctx)
var lastSaveError error
err := wait.ExponentialBackoff(*b.backoff, func() (bool, error) {
logger.Info("Trying to save persistentvolume", "persistentvolume", volume.Name)
Expand Down
6 changes: 2 additions & 4 deletions gidallocator/gidallocator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,13 @@ func New(client kubernetes.Interface) Allocator {

// AllocateNext allocates the next available GID for the given ProvisionOptions
// (claim's options for a volume it wants) from the appropriate GID table.
func (a *Allocator) AllocateNext(ctx context.Context, options controller.ProvisionOptions) (int, error) {
func (a *Allocator) AllocateNext(logger klog.Logger, options controller.ProvisionOptions) (int, error) {
class := util.GetPersistentVolumeClaimClass(options.PVC)
gidMin, gidMax, err := parseClassParameters(options.StorageClass.Parameters)
if err != nil {
return 0, err
}

logger := klog.FromContext(ctx)
gidTable, err := a.getGidTable(logger, class, gidMin, gidMax)
if err != nil {
return 0, fmt.Errorf("failed to get gidTable: %v", err)
Expand All @@ -89,14 +88,13 @@ func (a *Allocator) AllocateNext(ctx context.Context, options controller.Provisi

// Release releases the given volume's allocated GID from the appropriate GID
// table.
func (a *Allocator) Release(ctx context.Context, volume *v1.PersistentVolume) error {
func (a *Allocator) Release(logger klog.Logger, volume *v1.PersistentVolume) error {
class, err := a.client.StorageV1().StorageClasses().Get(context.Background(), util.GetPersistentVolumeClass(volume), metav1.GetOptions{})
gidMin, gidMax, err := parseClassParameters(class.Parameters)
if err != nil {
return err
}

logger := klog.FromContext(ctx)
gid, exists, err := getGid(volume)
if err != nil {
logger.Error(err, "Failed to get gid from volume")
Expand Down

0 comments on commit 4afa277

Please sign in to comment.