Repository: kubernetes/kubernetes
File: staging/src/k8s.io/kubectl/pkg/cmd/run/run.go (Go)

Code before:
	if err := opts.Run(); err != nil {
		fmt.Fprintf(opts.ErrOut, "warning: couldn't attach to pod/%s, falling back to streaming logs: %v\n", name, err)
		return logOpts(context.Background(), f, pod, opts, nil)
	}
	return nil
}

// logOpts logs output from opts to the pods log.
func logOpts(ctx context.Context, restClientGetter genericclioptions.RESTClientGetter, pod *corev1.Pod, opts *attach.AttachOptions, podOpts *corev1.PodLogOptions) error {
	if podOpts == nil {
		ctrName, err := opts.GetContainerName(pod)
		if err != nil {
			return err
		}

		podOpts = &corev1.PodLogOptions{}
		podOpts.Container = ctrName
	}

	requests, err := polymorphichelpers.LogsForObjectFn(restClientGetter, pod, podOpts, opts.GetPodTimeout, false)
	if err != nil {
		return err
	}
	for _, request := range requests {
		if err := logs.DefaultConsumeRequest(ctx, request, opts.Out); err != nil {
			if podOpts != nil && podOpts.Follow && (err == context.Canceled || err == context.DeadlineExceeded) {
				return nil
			}
			return err
		}
	}

	return nil
}

func getRestartPolicy(cmd *cobra.Command, interactive bool) (corev1.RestartPolicy, error) {
	restart := cmdutil.GetFlagString(cmd, "restart")
	if len(restart) == 0 {
		if interactive {
			return corev1.RestartPolicyOnFailure, nil
		}
		return corev1.RestartPolicyAlways, nil
	}
	switch corev1.RestartPolicy(restart) {
	case corev1.RestartPolicyAlways:
		return corev1.RestartPolicyAlways, nil
	case corev1.RestartPolicyOnFailure:
		return corev1.RestartPolicyOnFailure, nil
	case corev1.RestartPolicyNever:
		return corev1.RestartPolicyNever, nil
	}

Review comment (bug):
Can you update the code per [the linter](https://prow.k8s.io/view/gs/kubernetes-ci-logs/pr-logs/pull/136010/pull-kubernetes-linter-hints/2013221696336564224) suggestion: 
```
staging/src/k8s.io/kubectl/pkg/cmd/run/run.go:528:44: comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
			if podOpts != nil && podOpts.Follow && (err == context.Canceled || err == context.DeadlineExceeded) {
```
the other one (about error check can be safely ignored with `//nolint:errcheck`). 

Code after:
	}

	if err := opts.Run(); err != nil {
		fmt.Fprintf(opts.ErrOut, "warning: couldn't attach to pod/%s, falling back to streaming logs: %v\n", name, err)
		return logOpts(context.Background(), f, pod, opts, nil)
	}
	return nil
}

// logOpts logs output from opts to the pods log.
func logOpts(ctx context.Context, restClientGetter genericclioptions.RESTClientGetter, pod *corev1.Pod, opts *attach.AttachOptions, podOpts *corev1.PodLogOptions) error {
	if podOpts == nil {
		ctrName, err := opts.GetContainerName(pod)
		if err != nil {
			return err
		}

		podOpts = &corev1.PodLogOptions{}
		podOpts.Container = ctrName
	}

	requests, err := polymorphichelpers.LogsForObjectFn(restClientGetter, pod, podOpts, opts.GetPodTimeout, false)
	if err != nil {
		return err
	}
	for _, request := range requests {
		if err := logs.DefaultConsumeRequest(ctx, request, opts.Out); err != nil {
			if podOpts != nil && podOpts.Follow && (errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)) {
				return nil
			}
			return err
		}
	}

	return nil
}

func getRestartPolicy(cmd *cobra.Command, interactive bool) (corev1.RestartPolicy, error) {
	restart := cmdutil.GetFlagString(cmd, "restart")
	if len(restart) == 0 {
		if interactive {
			return corev1.RestartPolicyOnFailure, nil
		}
		return corev1.RestartPolicyAlways, nil
	}
	switch corev1.RestartPolicy(restart) {
	case corev1.RestartPolicyAlways:
		return corev1.RestartPolicyAlways, nil
	case corev1.RestartPolicyOnFailure:
		return corev1.RestartPolicyOnFailure, nil
	case corev1.RestartPolicyNever: