Skip to content

Commit

Permalink
core: add context parameter to k8sutil pvc
Browse files Browse the repository at this point in the history
This commit adds context parameter to k8sutil pvc functions. By this, we
can handle cancellation during API call of pvc resource.

Signed-off-by: Yuichiro Ueno <y1r.ueno@gmail.com>
  • Loading branch information
y1r committed Dec 11, 2021
1 parent 3309e9a commit 5352cf5
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/operator/ceph/cluster/mon/mon.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ func (c *Cluster) updateMon(m *monConfig, d *apps.Deployment) error {
if err != nil {
return errors.Wrapf(err, "failed to fetch pvc for mon %q", m.ResourceName)
}
k8sutil.ExpandPVCIfRequired(c.context.Client, desiredPvc, existingPvc)
k8sutil.ExpandPVCIfRequired(c.ClusterInfo.Context, c.context.Client, desiredPvc, existingPvc)
}

logger.Infof("deployment for mon %s already exists. updating if needed",
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/ceph/cluster/osd/deviceSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (c *Cluster) createDeviceSetPVC(existingPVCs map[string]*v1.PersistentVolum
logger.Infof("OSD PVC %q already exists", existingPVC.Name)

// Update the PVC in case the size changed
k8sutil.ExpandPVCIfRequired(c.context.Client, pvc, existingPVC)
k8sutil.ExpandPVCIfRequired(c.clusterInfo.Context, c.context.Client, pvc, existingPVC)
return existingPVC, nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/operator/k8sutil/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// ExpandPVCIfRequired will expand the PVC if requested size is greater than the actual size of existing PVC
func ExpandPVCIfRequired(client client.Client, desiredPVC *v1.PersistentVolumeClaim, currentPVC *v1.PersistentVolumeClaim) {
func ExpandPVCIfRequired(ctx context.Context, client client.Client, desiredPVC *v1.PersistentVolumeClaim, currentPVC *v1.PersistentVolumeClaim) {
desiredSize, desiredOK := desiredPVC.Spec.Resources.Requests[v1.ResourceStorage]
currentSize, currentOK := currentPVC.Spec.Resources.Requests[v1.ResourceStorage]
if !desiredOK || !currentOK {
Expand All @@ -43,7 +43,7 @@ func ExpandPVCIfRequired(client client.Client, desiredPVC *v1.PersistentVolumeCl

// get StorageClass
storageClass := &storagev1.StorageClass{}
err := client.Get(context.TODO(), types.NamespacedName{Name: *(currentPVC.Spec.StorageClassName)}, storageClass)
err := client.Get(ctx, types.NamespacedName{Name: *(currentPVC.Spec.StorageClassName)}, storageClass)
if err != nil {
logger.Errorf("failed to get storageClass %q. %v", *(currentPVC.Spec.StorageClassName), err)
return
Expand All @@ -56,7 +56,7 @@ func ExpandPVCIfRequired(client client.Client, desiredPVC *v1.PersistentVolumeCl

currentPVC.Spec.Resources.Requests[v1.ResourceStorage] = desiredSize
logger.Infof("updating PVC %q size from %s to %s", currentPVC.Name, currentSize.String(), desiredSize.String())
if err = client.Update(context.TODO(), currentPVC); err != nil {
if err = client.Update(ctx, currentPVC); err != nil {
// log the error, but don't fail the reconcile
logger.Errorf("failed to update PVC size. %v", err)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/k8sutil/pvc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestExpandPVCIfRequired(t *testing.T) {

desiredPVC.Spec.Resources.Requests[v1.ResourceStorage] = apiresource.MustParse(tc.desiredPVCSize)

ExpandPVCIfRequired(cl, desiredPVC, existingPVC)
ExpandPVCIfRequired(context.TODO(), cl, desiredPVC, existingPVC)

// get existing PVC
err = cl.Get(context.TODO(), client.ObjectKey{Name: "test", Namespace: "rook-ceph"}, existingPVC)
Expand Down

0 comments on commit 5352cf5

Please sign in to comment.