mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-16 23:29:21 +00:00
kubectl debug: Introduce customizable AttachFunc instead static one
Currently, kubectl debug statically relies on handleAttachPod function in order to attach to the pod. However, external tools would want to set their own customized attach function and this commit introduces generic `AttachFunc` function interface which can also override by external tools. From the point of kubectl debug, there is no functionality change.
This commit is contained in:
parent
d25c0a1bdb
commit
baed624046
@ -111,6 +111,7 @@ type DebugOptions struct {
|
|||||||
Args []string
|
Args []string
|
||||||
ArgsOnly bool
|
ArgsOnly bool
|
||||||
Attach bool
|
Attach bool
|
||||||
|
AttachFunc func(ctx context.Context, restClientGetter genericclioptions.RESTClientGetter, cmdPath string, ns, podName, containerName string) error
|
||||||
Container string
|
Container string
|
||||||
CopyTo string
|
CopyTo string
|
||||||
Replace bool
|
Replace bool
|
||||||
@ -212,6 +213,9 @@ func (o *DebugOptions) Complete(restClientGetter genericclioptions.RESTClientGet
|
|||||||
attachFlag := cmd.Flags().Lookup("attach")
|
attachFlag := cmd.Flags().Lookup("attach")
|
||||||
if !attachFlag.Changed && o.Interactive {
|
if !attachFlag.Changed && o.Interactive {
|
||||||
o.Attach = true
|
o.Attach = true
|
||||||
|
if o.AttachFunc == nil {
|
||||||
|
o.AttachFunc = o.handleAttachPod
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Environment
|
// Environment
|
||||||
@ -377,26 +381,8 @@ func (o *DebugOptions) Run(restClientGetter genericclioptions.RESTClientGetter,
|
|||||||
return visitErr
|
return visitErr
|
||||||
}
|
}
|
||||||
|
|
||||||
if o.Attach && len(containerName) > 0 {
|
if o.Attach && len(containerName) > 0 && o.AttachFunc != nil {
|
||||||
opts := &attach.AttachOptions{
|
if err := o.AttachFunc(ctx, restClientGetter, cmd.Parent().CommandPath(), debugPod.Namespace, debugPod.Name, containerName); err != nil {
|
||||||
StreamOptions: exec.StreamOptions{
|
|
||||||
IOStreams: o.IOStreams,
|
|
||||||
Stdin: o.Interactive,
|
|
||||||
TTY: o.TTY,
|
|
||||||
Quiet: o.Quiet,
|
|
||||||
},
|
|
||||||
CommandName: cmd.Parent().CommandPath() + " attach",
|
|
||||||
|
|
||||||
Attach: &attach.DefaultRemoteAttach{},
|
|
||||||
}
|
|
||||||
config, err := restClientGetter.ToRESTConfig()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
opts.Config = config
|
|
||||||
opts.AttachFunc = attach.DefaultAttachFunc
|
|
||||||
|
|
||||||
if err := o.handleAttachPod(ctx, restClientGetter, debugPod.Namespace, debugPod.Name, containerName, opts); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -795,7 +781,25 @@ func (o *DebugOptions) waitForContainer(ctx context.Context, ns, podName, contai
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *DebugOptions) handleAttachPod(ctx context.Context, restClientGetter genericclioptions.RESTClientGetter, ns, podName, containerName string, opts *attach.AttachOptions) error {
|
func (o *DebugOptions) handleAttachPod(ctx context.Context, restClientGetter genericclioptions.RESTClientGetter, cmdPath string, ns, podName, containerName string) error {
|
||||||
|
opts := &attach.AttachOptions{
|
||||||
|
StreamOptions: exec.StreamOptions{
|
||||||
|
IOStreams: o.IOStreams,
|
||||||
|
Stdin: o.Interactive,
|
||||||
|
TTY: o.TTY,
|
||||||
|
Quiet: o.Quiet,
|
||||||
|
},
|
||||||
|
CommandName: cmdPath + " attach",
|
||||||
|
|
||||||
|
Attach: &attach.DefaultRemoteAttach{},
|
||||||
|
}
|
||||||
|
config, err := restClientGetter.ToRESTConfig()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
opts.Config = config
|
||||||
|
opts.AttachFunc = attach.DefaultAttachFunc
|
||||||
|
|
||||||
pod, err := o.waitForContainer(ctx, ns, podName, containerName)
|
pod, err := o.waitForContainer(ctx, ns, podName, containerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -2104,7 +2104,7 @@ func TestCompleteAndValidate(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreFields(DebugOptions{},
|
if diff := cmp.Diff(tc.wantOpts, opts, cmpFilter, cmpopts.IgnoreFields(DebugOptions{},
|
||||||
"attachChanged", "shareProcessedChanged", "podClient", "WarningPrinter", "Applier", "explicitNamespace", "Builder")); diff != "" {
|
"attachChanged", "shareProcessedChanged", "podClient", "WarningPrinter", "Applier", "explicitNamespace", "Builder", "AttachFunc")); diff != "" {
|
||||||
t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff)
|
t.Error("CompleteAndValidate unexpected diff in generated object: (-want +got):\n", diff)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user