(kubectl debug): Support debugging via files

Currently `kubectl debug` only supports passing names in command line.
However, users might want to pass resources in files by passing `-f` flag like
in all other kubectl commands.

This PR adds this ability.

Kubernetes-commit: e0fedec69d494cf02ac99a83733d7d92f6cc0c51
This commit is contained in:
Arda Güçlü 2023-02-10 10:21:15 +03:00 committed by Kubernetes Publisher
parent c230e1b221
commit 2d1cd8a4fc
1 changed files with 20 additions and 14 deletions

View File

@ -77,6 +77,10 @@ var (
# Create an interactive debugging session in pod mypod and immediately attach to it. # Create an interactive debugging session in pod mypod and immediately attach to it.
kubectl debug mypod -it --image=busybox kubectl debug mypod -it --image=busybox
# Create an interactive debugging session for the pod in the file pod.yaml and immediately attach to it.
# (requires the EphemeralContainers feature to be enabled in the cluster)
kubectl debug -f pod.yaml -it --image=busybox
# Create a debug container named debugger using a custom automated debugging image. # Create a debug container named debugger using a custom automated debugging image.
kubectl debug --image=myproj/debug-tools -c debugger mypod kubectl debug --image=myproj/debug-tools -c debugger mypod
@ -123,6 +127,7 @@ type DebugOptions struct {
Profile string Profile string
Applier ProfileApplier Applier ProfileApplier
explicitNamespace bool
attachChanged bool attachChanged bool
shareProcessedChanged bool shareProcessedChanged bool
@ -130,6 +135,8 @@ type DebugOptions struct {
genericclioptions.IOStreams genericclioptions.IOStreams
WarningPrinter *printers.WarningPrinter WarningPrinter *printers.WarningPrinter
resource.FilenameOptions
} }
// NewDebugOptions returns a DebugOptions initialized with default values. // NewDebugOptions returns a DebugOptions initialized with default values.
@ -160,6 +167,7 @@ func NewCmdDebug(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.
} }
addDebugFlags(cmd, o) addDebugFlags(cmd, o)
cmdutil.AddJsonFilenameFlag(cmd.Flags(), &o.FilenameOptions.Filenames, "identifying the resource to debug")
return cmd return cmd
} }
@ -212,7 +220,7 @@ func (o *DebugOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
} }
// Namespace // Namespace
o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace() o.Namespace, o.explicitNamespace, err = f.ToRawKubeConfigLoader().Namespace()
if err != nil { if err != nil {
return err return err
} }
@ -272,8 +280,8 @@ func (o *DebugOptions) Validate() error {
} }
// Name // Name
if len(o.TargetNames) == 0 { if len(o.TargetNames) == 0 && len(o.FilenameOptions.Filenames) == 0 {
return fmt.Errorf("NAME is required for debug") return fmt.Errorf("NAME or filename is required for debug")
} }
// Pull Policy // Pull Policy
@ -327,6 +335,7 @@ func (o *DebugOptions) Run(f cmdutil.Factory, cmd *cobra.Command) error {
r := f.NewBuilder(). r := f.NewBuilder().
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...). WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
FilenameParam(o.explicitNamespace, &o.FilenameOptions).
NamespaceParam(o.Namespace).DefaultNamespace().ResourceNames("pods", o.TargetNames...). NamespaceParam(o.Namespace).DefaultNamespace().ResourceNames("pods", o.TargetNames...).
Do() Do()
if err := r.Err(); err != nil { if err := r.Err(); err != nil {
@ -691,18 +700,15 @@ func (o *DebugOptions) computeDebugContainerName(pod *corev1.Pod) string {
if len(o.Container) > 0 { if len(o.Container) > 0 {
return o.Container return o.Container
} }
name := o.Container
if len(name) == 0 { cn, containerByName := "", containerNameToRef(pod)
cn, containerByName := "", containerNameToRef(pod) for len(cn) == 0 || (containerByName[cn] != nil) {
for len(cn) == 0 || (containerByName[cn] != nil) { cn = fmt.Sprintf("debugger-%s", nameSuffixFunc(5))
cn = fmt.Sprintf("debugger-%s", nameSuffixFunc(5))
}
if !o.Quiet {
fmt.Fprintf(o.Out, "Defaulting debug container name to %s.\n", cn)
}
name = cn
} }
return name if !o.Quiet {
fmt.Fprintf(o.Out, "Defaulting debug container name to %s.\n", cn)
}
return cn
} }
func containerNameToRef(pod *corev1.Pod) map[string]*corev1.Container { func containerNameToRef(pod *corev1.Pod) map[string]*corev1.Container {