From 25f784194d6540b3c3f74dab6f485bfedbe06947 Mon Sep 17 00:00:00 2001 From: Yuichiro Ueno Date: Sat, 11 Dec 2021 11:21:55 +0900 Subject: [PATCH] core: add context parameter to k8sutil configMap This commit adds context parameter to k8sutil configMap functions. By this, we can handle cancellation during API call of configMap resource. Signed-off-by: Yuichiro Ueno --- pkg/operator/k8sutil/cmdreporter/cmdreporter.go | 4 ++-- pkg/operator/k8sutil/configmap.go | 3 +-- pkg/operator/k8sutil/configmap_test.go | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pkg/operator/k8sutil/cmdreporter/cmdreporter.go b/pkg/operator/k8sutil/cmdreporter/cmdreporter.go index 8e38cf3227f5..1ae614583736 100644 --- a/pkg/operator/k8sutil/cmdreporter/cmdreporter.go +++ b/pkg/operator/k8sutil/cmdreporter/cmdreporter.go @@ -155,7 +155,7 @@ func (cr *CmdReporter) Run(ctx context.Context, timeout time.Duration) (stdout, delOpts.Wait = true delOpts.ErrorOnTimeout = true // configmap's name will be the same as the app - err := k8sutil.DeleteConfigMap(cr.clientset, jobName, namespace, delOpts) + err := k8sutil.DeleteConfigMap(ctx, cr.clientset, jobName, namespace, delOpts) if err != nil { return "", "", -1, fmt.Errorf("%s. failed to delete existing results ConfigMap %s. %+v", errMsg, jobName, err) } @@ -180,7 +180,7 @@ func (cr *CmdReporter) Run(ctx context.Context, timeout time.Duration) (stdout, // just to be explicit: delete idempotently, and don't wait for delete to complete delOpts = &k8sutil.DeleteOptions{MustDelete: false, WaitOptions: k8sutil.WaitOptions{Wait: false}} - if err := k8sutil.DeleteConfigMap(cr.clientset, jobName, namespace, delOpts); err != nil { + if err := k8sutil.DeleteConfigMap(ctx, cr.clientset, jobName, namespace, delOpts); err != nil { logger.Errorf("continuing after failing to delete ConfigMap %s for job %s; user may need to delete it manually. %+v", jobName, jobName, err) } diff --git a/pkg/operator/k8sutil/configmap.go b/pkg/operator/k8sutil/configmap.go index 9a10537b40d3..cb6c83306890 100644 --- a/pkg/operator/k8sutil/configmap.go +++ b/pkg/operator/k8sutil/configmap.go @@ -28,8 +28,7 @@ import ( ) // DeleteConfigMap deletes a ConfigMap. -func DeleteConfigMap(clientset kubernetes.Interface, cmName, namespace string, opts *DeleteOptions) error { - ctx := context.TODO() +func DeleteConfigMap(ctx context.Context, clientset kubernetes.Interface, cmName, namespace string, opts *DeleteOptions) error { k8sOpts := BaseKubernetesDeleteOptions() delete := func() error { return clientset.CoreV1().ConfigMaps(namespace).Delete(ctx, cmName, *k8sOpts) } verify := func() error { diff --git a/pkg/operator/k8sutil/configmap_test.go b/pkg/operator/k8sutil/configmap_test.go index eb8f6ea5503b..aca1865003b4 100644 --- a/pkg/operator/k8sutil/configmap_test.go +++ b/pkg/operator/k8sutil/configmap_test.go @@ -51,7 +51,7 @@ func TestDeleteConfigMap(t *testing.T) { opts := &DeleteOptions{} opts.Wait = true opts.ErrorOnTimeout = true - err = DeleteConfigMap(k8s, "test-configmap", "test-namespace", opts) + err = DeleteConfigMap(ctx, k8s, "test-configmap", "test-namespace", opts) assert.NoError(t, err) _, err = k8s.CoreV1().ConfigMaps("test-namespace").Get(ctx, "test-configmap", metav1.GetOptions{})