Skip to content

Commit bf37b32

Browse files
committed
Use Cobra command context instead of placeholder .TODO()
1 parent 9d496c2 commit bf37b32

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

cmd/kubectl-datadog/agent/check/check.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ func (o *options) validate(cmd *cobra.Command) error {
115115

116116
// run runs the check command
117117
func (o *options) run(cmd *cobra.Command) error {
118+
ctx := cmd.Context()
118119
var goRoutinesCount int
119120
var pods []corev1.Pod
120121

@@ -180,7 +181,7 @@ func (o *options) run(cmd *cobra.Command) error {
180181
if isCLCRunner(pod) {
181182
container = "cluster-checks-runner"
182183
}
183-
stdOut, stdErr, err := o.execInPod(&pod, statusCmd, container)
184+
stdOut, stdErr, err := o.execInPod(ctx, &pod, statusCmd, container)
184185
if err != nil {
185186
cmd.Println(fmt.Sprintf("Ignoring pod: %s, error: %v", pod.Name, err))
186187
continue
@@ -220,7 +221,7 @@ func (o *options) run(cmd *cobra.Command) error {
220221
}
221222

222223
// execInPod exec a command in an Agent pod
223-
func (o *options) execInPod(pod *corev1.Pod, cmd []string, container string) (string, string, error) {
224+
func (o *options) execInPod(ctx context.Context, pod *corev1.Pod, command []string, container string) (string, string, error) {
224225
req := o.Clientset.CoreV1().RESTClient().Post().
225226
Resource("pods").
226227
Name(pod.Name).
@@ -234,7 +235,7 @@ func (o *options) execInPod(pod *corev1.Pod, cmd []string, container string) (st
234235

235236
parameterCodec := runtime.NewParameterCodec(scheme)
236237
req.VersionedParams(&corev1.PodExecOptions{
237-
Command: cmd,
238+
Command: command,
238239
Container: container,
239240
Stdin: false,
240241
Stdout: true,
@@ -248,7 +249,7 @@ func (o *options) execInPod(pod *corev1.Pod, cmd []string, container string) (st
248249
}
249250

250251
var stdout, stderr bytes.Buffer
251-
err = exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{
252+
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
252253
Stdin: nil,
253254
Stdout: &stdout,
254255
Stderr: &stderr,

cmd/kubectl-datadog/flare/flare.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ func (o *options) validate() error {
168168

169169
// run runs the flare command
170170
func (o *options) run(cmd *cobra.Command) error {
171+
ctx := cmd.Context()
171172
// Prepare base directory
172173
baseDir := filepath.Join(os.TempDir(), "datadog-operator")
173174
if err := os.MkdirAll(baseDir, os.ModePerm); err != nil {
@@ -206,7 +207,7 @@ func (o *options) run(cmd *cobra.Command) error {
206207
}
207208

208209
// Collect operator version
209-
if err = o.createVersionFile(leaderPod, baseDir, cmd); err != nil {
210+
if err = o.createVersionFile(ctx, leaderPod, baseDir, cmd); err != nil {
210211
cmd.Println(fmt.Sprintf("Couldn't collect operator version: %v", err))
211212
}
212213

@@ -217,7 +218,7 @@ func (o *options) run(cmd *cobra.Command) error {
217218
}
218219

219220
// Get the operator version
220-
version, err := o.getVersion(leaderPod)
221+
version, err := o.getVersion(ctx, leaderPod)
221222
if err != nil {
222223
cmd.Println(fmt.Sprintf("Couldn't get operator version: %v", err))
223224

@@ -358,14 +359,14 @@ func (o *options) createStatusFile(pod *corev1.Pod, dir string, cmd *cobra.Comma
358359
}
359360

360361
// createVersionFile gets the version from the operator pod and stores it in a file
361-
func (o *options) createVersionFile(pod *corev1.Pod, dir string, cmd *cobra.Command) error {
362+
func (o *options) createVersionFile(ctx context.Context, pod *corev1.Pod, dir string, cmd *cobra.Command) error {
362363
if pod == nil {
363364
return errors.New("nil leader pod")
364365
}
365366

366367
// Prepare command and execute it
367368
versionCmd := []string{"bash", "-c", "/usr/local/bin/datadog-operator --version --version-format text"}
368-
version, err := o.execInPod(versionCmd, pod)
369+
version, err := o.execInPod(ctx, versionCmd, pod)
369370
if err != nil {
370371
return err
371372
}
@@ -374,14 +375,14 @@ func (o *options) createVersionFile(pod *corev1.Pod, dir string, cmd *cobra.Comm
374375
}
375376

376377
// getOperatorVersion gets the version from the operator pod
377-
func (o *options) getVersion(pod *corev1.Pod) (string, error) {
378+
func (o *options) getVersion(ctx context.Context, pod *corev1.Pod) (string, error) {
378379
if pod == nil {
379380
return "", errors.New("nil leader pod")
380381
}
381382

382383
// Prepare command and execute it
383384
versionCmd := []string{"bash", "-c", "/usr/local/bin/datadog-operator --version --version-format json"}
384-
versionJSON, err := o.execInPod(versionCmd, pod)
385+
versionJSON, err := o.execInPod(ctx, versionCmd, pod)
385386
if err != nil {
386387
return "", err
387388
}
@@ -425,7 +426,7 @@ func (o *options) getLeader() (*corev1.Pod, error) {
425426
}
426427

427428
// execInPod execs a given command in a given pod
428-
func (o *options) execInPod(command []string, pod *corev1.Pod) ([]byte, error) {
429+
func (o *options) execInPod(ctx context.Context, command []string, pod *corev1.Pod) ([]byte, error) {
429430
req := o.Clientset.CoreV1().RESTClient().Post().
430431
Resource("pods").
431432
Name(pod.Name).
@@ -458,7 +459,7 @@ func (o *options) execInPod(command []string, pod *corev1.Pod) ([]byte, error) {
458459
}
459460

460461
var stdout bytes.Buffer
461-
if err := exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{Stdout: &stdout}); err != nil {
462+
if err := exec.StreamWithContext(ctx, remotecommand.StreamOptions{Stdout: &stdout}); err != nil {
462463
return []byte{}, err
463464
}
464465

0 commit comments

Comments
 (0)