mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 21:53:52 +00:00
Allow logs to be used with a preloaded pod.
This commit is contained in:
@@ -61,6 +61,7 @@ type LogsOptions struct {
|
|||||||
ClientMapper resource.ClientMapper
|
ClientMapper resource.ClientMapper
|
||||||
Decoder runtime.Decoder
|
Decoder runtime.Decoder
|
||||||
|
|
||||||
|
Object runtime.Object
|
||||||
LogsForObject func(object, options runtime.Object) (*restclient.Request, error)
|
LogsForObject func(object, options runtime.Object) (*restclient.Request, error)
|
||||||
|
|
||||||
Out io.Writer
|
Out io.Writer
|
||||||
@@ -150,14 +151,27 @@ func (o *LogsOptions) Complete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Com
|
|||||||
logOptions.SinceSeconds = &sec
|
logOptions.SinceSeconds = &sec
|
||||||
}
|
}
|
||||||
o.Options = logOptions
|
o.Options = logOptions
|
||||||
|
|
||||||
o.Mapper, o.Typer = f.Object()
|
|
||||||
o.Decoder = f.Decoder(true)
|
|
||||||
o.ClientMapper = resource.ClientMapperFunc(f.ClientForMapping)
|
|
||||||
o.LogsForObject = f.LogsForObject
|
o.LogsForObject = f.LogsForObject
|
||||||
|
o.ClientMapper = resource.ClientMapperFunc(f.ClientForMapping)
|
||||||
o.Out = out
|
o.Out = out
|
||||||
|
|
||||||
|
mapper, typer := f.Object()
|
||||||
|
decoder := f.Decoder(true)
|
||||||
|
if o.Object == nil {
|
||||||
|
infos, err := resource.NewBuilder(mapper, typer, o.ClientMapper, decoder).
|
||||||
|
NamespaceParam(o.Namespace).DefaultNamespace().
|
||||||
|
ResourceNames("pods", o.ResourceArg).
|
||||||
|
SingleResourceType().
|
||||||
|
Do().Infos()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(infos) != 1 {
|
||||||
|
return errors.New("expected a resource")
|
||||||
|
}
|
||||||
|
o.Object = infos[0].Object
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -178,20 +192,7 @@ func (o LogsOptions) Validate() error {
|
|||||||
|
|
||||||
// RunLogs retrieves a pod log
|
// RunLogs retrieves a pod log
|
||||||
func (o LogsOptions) RunLogs() (int64, error) {
|
func (o LogsOptions) RunLogs() (int64, error) {
|
||||||
infos, err := resource.NewBuilder(o.Mapper, o.Typer, o.ClientMapper, o.Decoder).
|
req, err := o.LogsForObject(o.Object, o.Options)
|
||||||
NamespaceParam(o.Namespace).DefaultNamespace().
|
|
||||||
ResourceNames("pods", o.ResourceArg).
|
|
||||||
SingleResourceType().
|
|
||||||
Do().Infos()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
if len(infos) != 1 {
|
|
||||||
return 0, errors.New("expected a resource")
|
|
||||||
}
|
|
||||||
info := infos[0]
|
|
||||||
|
|
||||||
req, err := o.LogsForObject(info.Object, o.Options)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
@@ -130,7 +130,6 @@ func TestValidateLogFlags(t *testing.T) {
|
|||||||
cmd.Run = func(cmd *cobra.Command, args []string) {
|
cmd.Run = func(cmd *cobra.Command, args []string) {
|
||||||
o.Complete(f, os.Stdout, cmd, args)
|
o.Complete(f, os.Stdout, cmd, args)
|
||||||
out = o.Validate().Error()
|
out = o.Validate().Error()
|
||||||
o.RunLogs()
|
|
||||||
}
|
}
|
||||||
cmd.Run(cmd, []string{"foo"})
|
cmd.Run(cmd, []string{"foo"})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user