Skip to content

Commit

Permalink
Add context argument to BDC client operations
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <[email protected]>
  • Loading branch information
niladrih authored and kmova committed Aug 4, 2021
1 parent 1812875 commit 1653bbd
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 63 deletions.
3 changes: 2 additions & 1 deletion cmd/maya-apiserver/app/command/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package command

import (
"context"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -132,7 +133,7 @@ func performPreflightChecks() error {

// checks existence of NDM related CRDs
func checkForNDMrelatedCRDs() error {
_, err := bdc.NewKubeClient().List(metav1.ListOptions{})
_, err := bdc.NewKubeClient().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return errors.Errorf("precheck for bdc failed: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/maya-apiserver/cstor-operator/spc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func removeSPCFinalizerOnAssociatedBDC(spc *apis.StoragePoolClaim) error {
}

bdcList, err := bdc.NewKubeClient().WithNamespace(namespace).List(
context.TODO(),
metav1.ListOptions{
LabelSelector: string(apis.StoragePoolClaimCPK) + "=" + spc.Name,
})
Expand Down
10 changes: 6 additions & 4 deletions pkg/algorithm/nodeselect/v1alpha1/select_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ limitations under the License.
package v1alpha1

import (
"context"
"strings"

ndmapis "github.com/openebs/maya/pkg/apis/openebs.io/ndm/v1alpha1"
apis "github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
blockdevice "github.com/openebs/maya/pkg/blockdevice/v1alpha1"
Expand All @@ -28,7 +31,6 @@ import (
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
"strings"
)

const (
Expand Down Expand Up @@ -185,7 +187,7 @@ func (ac *Config) selectNode(nodeBlockDeviceMap map[string]*blockDeviceList) (*n
// created filter and use it
bdcList, err = bdc.NewKubeClient().
WithNamespace(ac.Namespace).
List(metav1.ListOptions{LabelSelector: string(apis.StoragePoolClaimCPK) + "=" + ac.Spc.Name})
List(context.TODO(), metav1.ListOptions{LabelSelector: string(apis.StoragePoolClaimCPK) + "=" + ac.Spc.Name})
if err != nil {
return nil, errors.Wrapf(err, "failed to select node and blockdevices")
}
Expand Down Expand Up @@ -344,7 +346,7 @@ func (ac *Config) ClaimBlockDevice(nodeBDs *nodeBlockDevice, spc *apis.StoragePo
nodeClaimedBDs.NodeName = nodeBDs.NodeName
pendingBDCCount := 0

bdcObjList, err := bdcKubeclient.List(metav1.ListOptions{LabelSelector: lselector})
bdcObjList, err := bdcKubeclient.List(context.TODO(), metav1.ListOptions{LabelSelector: lselector})
if err != nil {
return nil, errors.Wrapf(err, "failed to list block device claims for {%s}", spc.Name)
}
Expand Down Expand Up @@ -415,7 +417,7 @@ func (ac *Config) ClaimBlockDevice(nodeBDs *nodeBlockDevice, spc *apis.StoragePo
if err != nil {
return nil, errors.Wrapf(err, "failed to build block device claim for bd {%s}", bdName)
}
_, err = bdcKubeclient.Create(newBDCObj.Object)
_, err = bdcKubeclient.Create(context.TODO(), newBDCObj.Object)
if err != nil {
return nil, errors.Wrapf(err, "failed to create block device claim for bdc {%s}", bdcName)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/blockdevice/v1alpha1/blockdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
"context"
"strings"

ndm "github.com/openebs/maya/pkg/apis/openebs.io/ndm/v1alpha1"
Expand Down Expand Up @@ -231,7 +232,7 @@ func (bdl *BlockDeviceList) GetUsableBlockDevices(spcName, namespace string) (*B
for _, bdObj := range bdl.Items {
if bdObj.Status.ClaimState == ndm.BlockDeviceClaimed {
bdcName := bdObj.Spec.ClaimRef.Name
bdcObj, err := bdcClient.Get(bdcName, metav1.GetOptions{})
bdcObj, err := bdcClient.Get(context.TODO(), bdcName, metav1.GetOptions{})
if err != nil {
return nil, errors.Wrapf(err,
"failed to get blockdeviceclaim %s details of blockdevice %s",
Expand Down
6 changes: 4 additions & 2 deletions pkg/blockdeviceclaim/v1alpha1/blockdeviceclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"context"

apis "github.com/openebs/maya/pkg/apis/openebs.io/ndm/v1alpha1"
ndm "github.com/openebs/maya/pkg/apis/openebs.io/ndm/v1alpha1"
"github.com/openebs/maya/pkg/util"
Expand Down Expand Up @@ -147,7 +149,7 @@ func (bdc *BlockDeviceClaim) AddFinalizer(finalizer string) (*ndm.BlockDeviceCla

bdcAPIObj, err := NewKubeClient(WithKubeConfigPath(bdc.configPath)).
WithNamespace(bdc.Object.Namespace).
Update(bdc.Object)
Update(context.TODO(), bdc.Object)

if err != nil {
return nil, errors.Wrapf(err, "failed to update bdc %s while adding finalizer %s",
Expand Down Expand Up @@ -175,7 +177,7 @@ func (bdc *BlockDeviceClaim) RemoveFinalizer(

newBDC, err := NewKubeClient(WithKubeConfigPath(bdc.configPath)).
WithNamespace(bdc.Object.Namespace).
Update(bdc.Object)
Update(context.TODO(), bdc.Object)
if err != nil {
return nil, errors.Wrap(err, "failed to update object while removing finalizer")
}
Expand Down
70 changes: 35 additions & 35 deletions pkg/blockdeviceclaim/v1alpha1/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,31 @@ type getClientsetForPathFn func(kubeConfigPath string) (clientset *clientset.Cli

// listFn is a typed function that abstracts
// listing of block device
type listFn func(cli *clientset.Clientset, namespace string, opts metav1.ListOptions) (*apis.BlockDeviceClaimList, error)
type listFn func(ctx context.Context, cli *clientset.Clientset, namespace string, opts metav1.ListOptions) (*apis.BlockDeviceClaimList, error)

// getFn is a typed function that
// abstracts fetching of block device
type getFn func(cli *clientset.Clientset, namespace, name string, opts metav1.GetOptions) (*apis.BlockDeviceClaim, error)
type getFn func(ctx context.Context, cli *clientset.Clientset, namespace, name string, opts metav1.GetOptions) (*apis.BlockDeviceClaim, error)

// createFn is a typed function that abstracts
// creation of block device
type createFn func(cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error)
type createFn func(ctx context.Context, cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error)

// deleteFn is a typed function that abstracts
// deletion of bdcs
type deleteFn func(cli *clientset.Clientset, namespace string, name string, deleteOpts *metav1.DeleteOptions) error
type deleteFn func(ctx context.Context, cli *clientset.Clientset, namespace string, name string, deleteOpts *metav1.DeleteOptions) error

// deleteFn is a typed function that abstracts
// deletion of bdc's collection
type deleteCollectionFn func(cli *clientset.Clientset, namespace string, listOpts metav1.ListOptions, deleteOpts *metav1.DeleteOptions) error
type deleteCollectionFn func(ctx context.Context, cli *clientset.Clientset, namespace string, listOpts metav1.ListOptions, deleteOpts *metav1.DeleteOptions) error

// patchFn is a typed function that abstracts
// to patch block device claim
type patchFn func(cli *clientset.Clientset, namespace, name string, pt types.PatchType, data []byte, subresources ...string) (*apis.BlockDeviceClaim, error)
type patchFn func(ctx context.Context, cli *clientset.Clientset, namespace, name string, pt types.PatchType, data []byte, subresources ...string) (*apis.BlockDeviceClaim, error)

// updateFn is a typed function that abstracts to update
// block device claim
type updateFn func(cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error)
type updateFn func(ctx context.Context, cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error)

// make ndm clientset as singleton
var (
Expand Down Expand Up @@ -133,46 +133,46 @@ func (k *Kubeclient) WithDefaults() {
}
}
if k.list == nil {
k.list = func(cli *clientset.Clientset, namespace string, opts metav1.ListOptions) (*apis.BlockDeviceClaimList, error) {
k.list = func(ctx context.Context, cli *clientset.Clientset, namespace string, opts metav1.ListOptions) (*apis.BlockDeviceClaimList, error) {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
List(context.TODO(), opts)
List(ctx, opts)
}
}

if k.get == nil {
k.get = func(cli *clientset.Clientset, namespace, name string, opts metav1.GetOptions) (*apis.BlockDeviceClaim, error) {
k.get = func(ctx context.Context, cli *clientset.Clientset, namespace, name string, opts metav1.GetOptions) (*apis.BlockDeviceClaim, error) {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
Get(context.TODO(), name, opts)
Get(ctx, name, opts)
}
}
if k.create == nil {
k.create = func(cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
k.create = func(ctx context.Context, cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
Create(context.TODO(), bdc, metav1.CreateOptions{})
Create(ctx, bdc, metav1.CreateOptions{})
}
}
if k.del == nil {
k.del = func(cli *clientset.Clientset, namespace string, name string, deleteOpts *metav1.DeleteOptions) error {
k.del = func(ctx context.Context, cli *clientset.Clientset, namespace string, name string, deleteOpts *metav1.DeleteOptions) error {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
Delete(context.TODO(), name, *deleteOpts)
Delete(ctx, name, *deleteOpts)
}
}
if k.delCollection == nil {
k.delCollection = func(cli *clientset.Clientset, namespace string, listOpts metav1.ListOptions, deleteOpts *metav1.DeleteOptions) error {
k.delCollection = func(ctx context.Context, cli *clientset.Clientset, namespace string, listOpts metav1.ListOptions, deleteOpts *metav1.DeleteOptions) error {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
DeleteCollection(context.TODO(), *deleteOpts, listOpts)
DeleteCollection(ctx, *deleteOpts, listOpts)
}
}
if k.patch == nil {
k.patch = func(cli *clientset.Clientset, namespace, name string, pt types.PatchType, data []byte, subresources ...string) (*apis.BlockDeviceClaim, error) {
k.patch = func(ctx context.Context, cli *clientset.Clientset, namespace, name string, pt types.PatchType, data []byte, subresources ...string) (*apis.BlockDeviceClaim, error) {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
Patch(context.TODO(), name, pt, data, metav1.PatchOptions{}, subresources...)
Patch(ctx, name, pt, data, metav1.PatchOptions{}, subresources...)
}
}
if k.update == nil {
k.update = func(cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
k.update = func(ctx context.Context, cli *clientset.Clientset, namespace string, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
return cli.OpenebsV1alpha1().BlockDeviceClaims(namespace).
Update(context.TODO(), bdc, metav1.UpdateOptions{})
Update(ctx, bdc, metav1.UpdateOptions{})
}
}
}
Expand Down Expand Up @@ -234,80 +234,80 @@ func (k *Kubeclient) getClientsetOrCached() (*clientset.Clientset, error) {

// List returns a list of disk
// instances present in kubernetes cluster
func (k *Kubeclient) List(opts metav1.ListOptions) (*apis.BlockDeviceClaimList, error) {
func (k *Kubeclient) List(ctx context.Context, opts metav1.ListOptions) (*apis.BlockDeviceClaimList, error) {
cli, err := k.getClientsetOrCached()
if err != nil {
return nil, errors.Wrapf(err, "failed to list bdc in namespace {%s}", k.namespace)
}
return k.list(cli, k.namespace, opts)
return k.list(ctx, cli, k.namespace, opts)
}

// Get returns a disk object
func (k *Kubeclient) Get(name string, opts metav1.GetOptions) (*apis.BlockDeviceClaim, error) {
func (k *Kubeclient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*apis.BlockDeviceClaim, error) {
if strings.TrimSpace(name) == "" {
return nil, errors.New("failed to get bdc: missing bdc name")
}
cli, err := k.getClientsetOrCached()
if err != nil {
return nil, errors.Wrapf(err, "failed to get bdc {%s} in namespace {%s}", name, k.namespace)
}
return k.get(cli, k.namespace, name, opts)
return k.get(ctx, cli, k.namespace, name, opts)
}

// Create creates a bdc in specified namespace in kubernetes cluster
func (k *Kubeclient) Create(bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
func (k *Kubeclient) Create(ctx context.Context, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
if bdc == nil {
return nil, errors.New("failed to create bdc: nil bdc object")
}
cli, err := k.getClientsetOrCached()
if err != nil {
return nil, errors.Wrapf(err, "failed to create bdc {%s} in namespace {%s}", bdc.Name, bdc.Namespace)
}
return k.create(cli, k.namespace, bdc)
return k.create(ctx, cli, k.namespace, bdc)
}

// DeleteCollection deletes a collection of bdc objects.
func (k *Kubeclient) DeleteCollection(listOpts metav1.ListOptions, deleteOpts *metav1.DeleteOptions) error {
func (k *Kubeclient) DeleteCollection(ctx context.Context, listOpts metav1.ListOptions, deleteOpts *metav1.DeleteOptions) error {
cli, err := k.getClientsetOrCached()
if err != nil {
return errors.Wrapf(err, "failed to delete the collection of bdcs")
}
return k.delCollection(cli, k.namespace, listOpts, deleteOpts)
return k.delCollection(ctx, cli, k.namespace, listOpts, deleteOpts)
}

// Delete deletes a bdc instance from the
// kubecrnetes cluster
func (k *Kubeclient) Delete(name string, deleteOpts *metav1.DeleteOptions) error {
func (k *Kubeclient) Delete(ctx context.Context, name string, deleteOpts *metav1.DeleteOptions) error {
if strings.TrimSpace(name) == "" {
return errors.New("failed to delete bdc: missing bdc name")
}
cli, err := k.getClientsetOrCached()
if err != nil {
return errors.Wrapf(err, "failed to delete bdc {%s} in namespace {%s}", name, k.namespace)
}
return k.del(cli, k.namespace, name, deleteOpts)
return k.del(ctx, cli, k.namespace, name, deleteOpts)
}

// Patch patches the block device claim if present in kubernetes cluster
func (k *Kubeclient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (*apis.BlockDeviceClaim, error) {
func (k *Kubeclient) Patch(ctx context.Context, name string, pt types.PatchType, data []byte, subresources ...string) (*apis.BlockDeviceClaim, error) {
if len(name) == 0 {
return nil, errors.New("failed to patch block device claim: missing bdc name")
}
cli, err := k.getClientsetOrCached()
if err != nil {
return nil, errors.Wrapf(err, "failed to patch bdc: {%s}", name)
}
return k.patch(cli, k.namespace, name, pt, data, subresources...)
return k.patch(ctx, cli, k.namespace, name, pt, data, subresources...)
}

// Update updates the block device claim if present in kubernetes cluster
func (k *Kubeclient) Update(bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
func (k *Kubeclient) Update(ctx context.Context, bdc *apis.BlockDeviceClaim) (*apis.BlockDeviceClaim, error) {
if bdc == nil {
return nil, errors.New("failed to udpate bdc: nil bdc object")
}
cli, err := k.getClientsetOrCached()
if err != nil {
return nil, errors.Wrapf(err, "failed to update bdc {%s} in namespace {%s}", bdc.Name, bdc.Namespace)
}
return k.update(cli, k.namespace, bdc)
return k.update(ctx, cli, k.namespace, bdc)
}
Loading

0 comments on commit 1653bbd

Please sign in to comment.