mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-28 21:04:03 +00:00
Merge pull request #24554 from ingvagabund/dhodovska-events-flag-for-describers
Automatic merge from submit-queue Introduce events flag for describers Printing events for a given object is not always needed. Thus, introducing --show-events=false to ``kubectl describe`` to skip events printing. Fixes: #24239
This commit is contained in:
@@ -151,12 +151,14 @@ func (t *testPrinter) HandledResources() []string {
|
||||
|
||||
type testDescriber struct {
|
||||
Name, Namespace string
|
||||
Settings kubectl.DescriberSettings
|
||||
Output string
|
||||
Err error
|
||||
}
|
||||
|
||||
func (t *testDescriber) Describe(namespace, name string) (output string, err error) {
|
||||
func (t *testDescriber) Describe(namespace, name string, describerSettings kubectl.DescriberSettings) (output string, err error) {
|
||||
t.Namespace, t.Name = namespace, name
|
||||
t.Settings = describerSettings
|
||||
return t.Output, t.Err
|
||||
}
|
||||
|
||||
|
||||
@@ -73,6 +73,7 @@ kubectl describe pods frontend`
|
||||
|
||||
func NewCmdDescribe(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
options := &DescribeOptions{}
|
||||
describerSettings := &kubectl.DescriberSettings{}
|
||||
|
||||
validArgs := kubectl.DescribableResources()
|
||||
argAliases := kubectl.ResourceAliases(validArgs)
|
||||
@@ -83,7 +84,7 @@ func NewCmdDescribe(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
Long: describe_long,
|
||||
Example: describe_example,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := RunDescribe(f, out, cmd, args, options)
|
||||
err := RunDescribe(f, out, cmd, args, options, describerSettings)
|
||||
cmdutil.CheckErr(err)
|
||||
},
|
||||
ValidArgs: validArgs,
|
||||
@@ -93,11 +94,12 @@ func NewCmdDescribe(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
|
||||
cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
|
||||
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
|
||||
cmd.Flags().BoolVar(&describerSettings.ShowEvents, "show-events", true, "If true, display events related to the described object.")
|
||||
cmdutil.AddInclude3rdPartyFlags(cmd)
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunDescribe(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *DescribeOptions) error {
|
||||
func RunDescribe(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *DescribeOptions, describerSettings *kubectl.DescriberSettings) error {
|
||||
selector := cmdutil.GetFlagString(cmd, "selector")
|
||||
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
|
||||
if err != nil {
|
||||
@@ -126,7 +128,7 @@ func RunDescribe(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
infos, err := r.Infos()
|
||||
if err != nil {
|
||||
if apierrors.IsNotFound(err) && len(args) == 2 {
|
||||
return DescribeMatchingResources(mapper, typer, f, cmdNamespace, args[0], args[1], out, err)
|
||||
return DescribeMatchingResources(mapper, typer, f, cmdNamespace, args[0], args[1], describerSettings, out, err)
|
||||
}
|
||||
allErrs = append(allErrs, err)
|
||||
}
|
||||
@@ -138,7 +140,7 @@ func RunDescribe(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
allErrs = append(allErrs, err)
|
||||
continue
|
||||
}
|
||||
s, err := describer.Describe(info.Namespace, info.Name)
|
||||
s, err := describer.Describe(info.Namespace, info.Name, *describerSettings)
|
||||
if err != nil {
|
||||
allErrs = append(allErrs, err)
|
||||
continue
|
||||
@@ -149,7 +151,7 @@ func RunDescribe(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []s
|
||||
return utilerrors.NewAggregate(allErrs)
|
||||
}
|
||||
|
||||
func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper, f *cmdutil.Factory, namespace, rsrc, prefix string, out io.Writer, originalError error) error {
|
||||
func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper, f *cmdutil.Factory, namespace, rsrc, prefix string, describerSettings *kubectl.DescriberSettings, out io.Writer, originalError error) error {
|
||||
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
|
||||
NamespaceParam(namespace).DefaultNamespace().
|
||||
ResourceTypeOrNameArgs(true, rsrc).
|
||||
@@ -173,7 +175,7 @@ func DescribeMatchingResources(mapper meta.RESTMapper, typer runtime.ObjectTyper
|
||||
info := infos[ix]
|
||||
if strings.HasPrefix(info.Name, prefix) {
|
||||
isFound = true
|
||||
s, err := describer.Describe(info.Namespace, info.Name)
|
||||
s, err := describer.Describe(info.Namespace, info.Name, *describerSettings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -100,3 +100,43 @@ func TestDescribeListObjects(t *testing.T) {
|
||||
t.Errorf("unexpected output: %s", buf.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescribeObjectShowEvents(t *testing.T) {
|
||||
pods, _, _ := testData()
|
||||
f, tf, codec := NewAPIFactory()
|
||||
d := &testDescriber{Output: "test output"}
|
||||
tf.Describer = d
|
||||
tf.Client = &fake.RESTClient{
|
||||
Codec: codec,
|
||||
Resp: &http.Response{StatusCode: 200, Body: objBody(codec, pods)},
|
||||
}
|
||||
|
||||
tf.Namespace = "test"
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
cmd := NewCmdDescribe(f, buf)
|
||||
cmd.Flags().Set("show-events", "true")
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
if d.Settings.ShowEvents != true {
|
||||
t.Errorf("ShowEvents = true expected, got ShowEvents = %v", d.Settings.ShowEvents)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDescribeObjectSkipEvents(t *testing.T) {
|
||||
pods, _, _ := testData()
|
||||
f, tf, codec := NewAPIFactory()
|
||||
d := &testDescriber{Output: "test output"}
|
||||
tf.Describer = d
|
||||
tf.Client = &fake.RESTClient{
|
||||
Codec: codec,
|
||||
Resp: &http.Response{StatusCode: 200, Body: objBody(codec, pods)},
|
||||
}
|
||||
|
||||
tf.Namespace = "test"
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
cmd := NewCmdDescribe(f, buf)
|
||||
cmd.Flags().Set("show-events", "false")
|
||||
cmd.Run(cmd, []string{"pods"})
|
||||
if d.Settings.ShowEvents != false {
|
||||
t.Errorf("ShowEvents = false expected, got ShowEvents = %v", d.Settings.ShowEvents)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user