Skip to content

Commit

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

Signed-off-by: Yuichiro Ueno <y1r.ueno@gmail.com>
  • Loading branch information
y1r committed Nov 13, 2021
1 parent c3b5d3b commit e278812
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
6 changes: 3 additions & 3 deletions pkg/operator/ceph/csi/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ func (r *ReconcileCSI) startDrivers(ver *version.Info, ownerInfo *k8sutil.OwnerI
if multusApplied {
rbdPlugin.Spec.Template.Spec.HostNetwork = false
}
err = k8sutil.CreateDaemonSet(csiRBDPlugin, r.opConfig.OperatorNamespace, r.context.Clientset, rbdPlugin)
err = k8sutil.CreateDaemonSet(r.opManagerContext, csiRBDPlugin, r.opConfig.OperatorNamespace, r.context.Clientset, rbdPlugin)
if err != nil {
return errors.Wrapf(err, "failed to start rbdplugin daemonset %q", rbdPlugin.Name)
}
Expand Down Expand Up @@ -478,7 +478,7 @@ func (r *ReconcileCSI) startDrivers(ver *version.Info, ownerInfo *k8sutil.OwnerI
if multusApplied {
cephfsPlugin.Spec.Template.Spec.HostNetwork = false
}
err = k8sutil.CreateDaemonSet(csiCephFSPlugin, r.opConfig.OperatorNamespace, r.context.Clientset, cephfsPlugin)
err = k8sutil.CreateDaemonSet(r.opManagerContext, csiCephFSPlugin, r.opConfig.OperatorNamespace, r.context.Clientset, cephfsPlugin)
if err != nil {
return errors.Wrapf(err, "failed to start cephfs plugin daemonset %q", cephfsPlugin.Name)
}
Expand Down Expand Up @@ -570,7 +570,7 @@ func (r *ReconcileCSI) deleteCSIDriverResources(ver *version.Info, daemonset, de
if ver.Major > KubeMinMajor || ver.Major == KubeMinMajor && ver.Minor >= kubeMinVerForV1csiDriver {
csiDriverobj = v1CsiDriver{}
}
err := k8sutil.DeleteDaemonset(r.context.Clientset, r.opConfig.OperatorNamespace, daemonset)
err := k8sutil.DeleteDaemonset(r.opManagerContext, r.context.Clientset, r.opConfig.OperatorNamespace, daemonset)
if err != nil {
logger.Errorf("failed to delete the %q. %v", daemonset, err)
succeeded = false
Expand Down
9 changes: 3 additions & 6 deletions pkg/operator/k8sutil/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (
)

// CreateDaemonSet creates
func CreateDaemonSet(name, namespace string, clientset kubernetes.Interface, ds *appsv1.DaemonSet) error {
ctx := context.TODO()
func CreateDaemonSet(ctx context.Context, name, namespace string, clientset kubernetes.Interface, ds *appsv1.DaemonSet) error {
_, err := clientset.AppsV1().DaemonSets(namespace).Create(ctx, ds, metav1.CreateOptions{})
if err != nil {
if k8serrors.IsAlreadyExists(err) {
Expand All @@ -42,8 +41,7 @@ func CreateDaemonSet(name, namespace string, clientset kubernetes.Interface, ds
}

// DeleteDaemonset makes a best effort at deleting a daemonset and its pods, then waits for them to be deleted
func DeleteDaemonset(clientset kubernetes.Interface, namespace, name string) error {
ctx := context.TODO()
func DeleteDaemonset(ctx context.Context, clientset kubernetes.Interface, namespace, name string) error {
deleteAction := func(options *metav1.DeleteOptions) error {
return clientset.AppsV1().DaemonSets(namespace).Delete(ctx, name, *options)
}
Expand All @@ -69,9 +67,8 @@ func AddRookVersionLabelToDaemonSet(d *appsv1.DaemonSet) {
// GetDaemonsets returns a list of daemonsets names labels matching a given selector
// example of a label selector might be "app=rook-ceph-mon, mon!=b"
// more: https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
func GetDaemonsets(clientset kubernetes.Interface, namespace, labelSelector string) (*appsv1.DaemonSetList, error) {
func GetDaemonsets(ctx context.Context, clientset kubernetes.Interface, namespace, labelSelector string) (*appsv1.DaemonSetList, error) {
listOptions := metav1.ListOptions{LabelSelector: labelSelector}
ctx := context.TODO()
daemonsets, err := clientset.AppsV1().DaemonSets(namespace).List(ctx, listOptions)
if err != nil {
return nil, fmt.Errorf("failed to list deployments with labelSelector %s: %v", labelSelector, err)
Expand Down

0 comments on commit e278812

Please sign in to comment.