Skip to content

Commit

Permalink
core: add context parameter to k8sutil configMap
Browse files Browse the repository at this point in the history
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 <y1r.ueno@gmail.com>
  • Loading branch information
y1r committed Dec 11, 2021
1 parent 3309e9a commit 25f7841
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/operator/k8sutil/cmdreporter/cmdreporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/operator/k8sutil/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/k8sutil/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
Expand Down

0 comments on commit 25f7841

Please sign in to comment.