func istiodLogCmd()

in istioctl/cmd/istiodconfig.go [365:472]


func istiodLogCmd() *cobra.Command {
	var opts clioptions.ControlPlaneOptions
	outputLogLevel := ""
	stackTraceLevel := ""

	// output format (yaml or short)
	outputFormat := "short"

	logCmd := &cobra.Command{
		Use:   "log [<pod-name>] [--level <scope>:<level>][--stack-trace-level <scope>:<level>]|[-r|--reset]|[--output|-o short|yaml]",
		Short: "Manage istiod logging.",
		Long:  "Retrieve or update logging levels of istiod components.",
		Example: `  # Retrieve information about istiod logging levels.
  istioctl admin log

  # Retrieve information about istiod logging levels on a specific control plane pod.
  istioctl admin l istiod-5c868d8bdd-pmvgg

  # Update levels of the specified loggers.
  istioctl admin log --level ads:debug,authorization:debug

  # Reset levels of all the loggers to default value (info).
  istioctl admin log -r
`,
		Aliases: []string{"l"},
		Args: func(logCmd *cobra.Command, args []string) error {
			if istiodReset && outputLogLevel != "" {
				logCmd.Println(logCmd.UsageString())
				return fmt.Errorf("--level cannot be combined with --reset")
			}
			if istiodReset && stackTraceLevel != "" {
				logCmd.Println(logCmd.UsageString())
				return fmt.Errorf("--stack-trace-level cannot be combined with --reset")
			}
			return nil
		},
		RunE: func(logCmd *cobra.Command, args []string) error {
			client, err := kubeClientWithRevision(kubeconfig, configContext, opts.Revision)
			if err != nil {
				return fmt.Errorf("failed to create k8s client: %v", err)
			}

			var podName, ns string
			if len(args) == 0 {
				pl, err := client.PodsForSelector(context.TODO(), handlers.HandleNamespace(istioNamespace, defaultNamespace), istiodLabelSelector)
				if err != nil {
					return fmt.Errorf("not able to locate pod with selector %s: %v", istiodLabelSelector, err)
				}

				if len(pl.Items) < 1 {
					return errors.New("no pods found")
				}

				if len(pl.Items) > 1 {
					log.Warnf("more than 1 pods fits selector: %s; will use pod: %s", istiodLabelSelector, pl.Items[0].Name)
				}

				// only use the first pod in the list
				podName = pl.Items[0].Name
				ns = pl.Items[0].Namespace
			} else if len(args) == 1 {
				podName, ns = args[0], istioNamespace
			}

			portForwarder, err := client.NewPortForwarder(podName, ns, bindAddress, 0, controlZport)
			if err != nil {
				return fmt.Errorf("could not build port forwarder for ControlZ %s: %v", podName, err)
			}
			defer portForwarder.Close()
			err = portForwarder.Start()
			if err != nil {
				return fmt.Errorf("could not start port forwarder for ControlZ %s: %v", podName, err)
			}

			ctrlzClient := &ControlzClient{
				baseURL: &url.URL{
					Scheme: "http",
					Host:   portForwarder.Address(),
					Path:   "scopej",
				},
				httpClient: &http.Client{},
			}
			istiodConfigCmd := chooseClientFlag(ctrlzClient, istiodReset, outputLogLevel, stackTraceLevel, outputFormat)
			output, err := istiodConfigCmd.execute()
			if output != "" {
				_, err := logCmd.OutOrStdout().Write([]byte(output))
				if err != nil {
					return err
				}
			}
			if err != nil {
				return err
			}
			return nil
		},
	}
	logCmd.PersistentFlags().BoolVarP(&istiodReset, "reset", "r", istiodReset, "Reset levels to default value. (info)")
	logCmd.PersistentFlags().IntVar(&controlZport, "ctrlz_port", 9876, "ControlZ port")
	logCmd.PersistentFlags().StringVar(&outputLogLevel, "level", outputLogLevel,
		"Comma-separated list of output logging level for scopes in format <scope>:<level>[,<scope>:<level>,...]"+
			"Possible values for <level>: none, error, warn, info, debug")
	logCmd.PersistentFlags().StringVar(&stackTraceLevel, "stack-trace-level", stackTraceLevel,
		"Comma-separated list of stack trace level  for scopes in format <scope>:<stack-trace-level>[,<scope>:<stack-trace-level>,...] "+
			"Possible values for <stack-trace-level>: none, error, warn, info, debug")
	logCmd.PersistentFlags().StringVarP(&outputFormat, "output", "o",
		outputFormat, "Output format: one of json|short")
	return logCmd
}