Merge pull request #1468 from RainbowMango/pr_grouping_log_flags

grouping flags for karmada-controller-manager
This commit is contained in:
karmada-bot 2022-03-14 17:31:36 +08:00 committed by GitHub
commit 42c8c3103b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 149 additions and 8 deletions

View File

@ -13,8 +13,9 @@ import (
"k8s.io/client-go/dynamic"
kubeclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/component-base/term"
"k8s.io/klog/v2"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/config/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/event"
@ -41,6 +42,8 @@ import (
"github.com/karmada-io/karmada/pkg/features"
"github.com/karmada-io/karmada/pkg/karmadactl"
"github.com/karmada-io/karmada/pkg/resourceinterpreter"
"github.com/karmada-io/karmada/pkg/sharedcli"
"github.com/karmada-io/karmada/pkg/sharedcli/klogflag"
"github.com/karmada-io/karmada/pkg/util"
"github.com/karmada-io/karmada/pkg/util/gclient"
"github.com/karmada-io/karmada/pkg/util/helper"
@ -69,13 +72,26 @@ func NewControllerManagerCommand(ctx context.Context) *cobra.Command {
},
}
// Init log flags
// TODO(@RainbowMango): Group the flags to "logs" flag set.
klog.InitFlags(flag.CommandLine)
fss := cliflag.NamedFlagSets{}
genericFlagSet := fss.FlagSet("generic")
// Add the flag(--kubeconfig) that is added by controller-runtime
// (https://github.com/kubernetes-sigs/controller-runtime/blob/v0.11.1/pkg/client/config/config.go#L39),
// and update the flag usage.
genericFlagSet.AddGoFlagSet(flag.CommandLine)
genericFlagSet.Lookup("kubeconfig").Usage = "Path to karmada control plane kubeconfig file."
opts.AddFlags(genericFlagSet, controllers.ControllerNames())
// Set klog flags
logsFlagSet := fss.FlagSet("logs")
klogflag.Add(logsFlagSet)
cmd.Flags().AddGoFlagSet(flag.CommandLine)
cmd.AddCommand(sharedcommand.NewCmdVersion(os.Stdout, "karmada-controller-manager"))
opts.AddFlags(cmd.Flags(), controllers.ControllerNames())
cmd.Flags().AddFlagSet(genericFlagSet)
cmd.Flags().AddFlagSet(logsFlagSet)
cols, _, _ := term.TerminalSize(cmd.OutOrStdout())
sharedcli.SetUsageAndHelpFunc(cmd, fss, cols)
return cmd
}

View File

@ -121,8 +121,6 @@ func NewOptions() *Options {
// AddFlags adds flags to the specified FlagSet.
func (o *Options) AddFlags(flags *pflag.FlagSet, allControllers []string) {
flags.Lookup("kubeconfig").Usage = "Path to karmada control plane kubeconfig file."
flags.StringSliceVar(&o.Controllers, "controllers", []string{"*"}, fmt.Sprintf(
"A list of controllers to enable. '*' enables all on-by-default controllers, 'foo' enables the controller named 'foo', '-foo' disables the controller named 'foo'. All controllers: %s.",
strings.Join(allControllers, ", "),

View File

@ -0,0 +1,55 @@
package sharedcli
import (
"fmt"
"strings"
"github.com/spf13/cobra"
cliflag "k8s.io/component-base/cli/flag"
)
const (
usageFmt = "Usage:\n %s\n"
)
// generatesAvailableSubCommands generates command's subcommand information which
// is usually part of a help message. E.g.:
//
// Available Commands:
// karmada-controller-manager completion generate the autocompletion script for the specified shell
// karmada-controller-manager help Help about any command
// karmada-controller-manager version Print the version information.
//
func generatesAvailableSubCommands(cmd *cobra.Command) []string {
if !cmd.HasAvailableSubCommands() {
return nil
}
info := []string{"\nAvailable Commands:"}
for _, sub := range cmd.Commands() {
if !sub.Hidden {
info = append(info, fmt.Sprintf(" %s %-30s %s", cmd.CommandPath(), sub.Name(), sub.Short))
}
}
return info
}
// SetUsageAndHelpFunc set both usage and help function.
func SetUsageAndHelpFunc(cmd *cobra.Command, fss cliflag.NamedFlagSets, cols int) {
cmd.SetUsageFunc(func(cmd *cobra.Command) error {
fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine())
if cmd.HasAvailableSubCommands() {
fmt.Fprintf(cmd.OutOrStderr(), "%s\n", strings.Join(generatesAvailableSubCommands(cmd), "\n"))
}
cliflag.PrintSections(cmd.OutOrStderr(), fss, cols)
return nil
})
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine())
if cmd.HasAvailableSubCommands() {
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", strings.Join(generatesAvailableSubCommands(cmd), "\n"))
}
cliflag.PrintSections(cmd.OutOrStdout(), fss, cols)
})
}

2
pkg/sharedcli/docs.go Normal file
View File

@ -0,0 +1,2 @@
// Package sharedcli is the destination for common commands, flags or utils.
package sharedcli

View File

@ -0,0 +1,18 @@
package klogflag
import (
"flag"
"os"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
)
// Add used to add klog flags to specified flag set.
func Add(fs *pflag.FlagSet) {
// Since klog only accepts golang flag set, so introduce a shim here.
flagSetShim := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
klog.InitFlags(flagSetShim)
fs.AddGoFlagSet(flagSetShim)
}

12
vendor/k8s.io/component-base/term/OWNERS generated vendored Normal file
View File

@ -0,0 +1,12 @@
# See the OWNERS docs at https://go.k8s.io/owners
# Currently assigned this directory to sig-cli since the main use of
# term seems to be for getting terminal size when printing help text.
approvers:
- sig-cli-maintainers
reviewers:
- sig-cli
labels:
- sig/cli

39
vendor/k8s.io/component-base/term/term.go generated vendored Normal file
View File

@ -0,0 +1,39 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package term
import (
"fmt"
"io"
"github.com/moby/term"
)
// TerminalSize returns the current width and height of the user's terminal. If it isn't a terminal,
// nil is returned. On error, zero values are returned for width and height.
// Usually w must be the stdout of the process. Stderr won't work.
func TerminalSize(w io.Writer) (int, int, error) {
outFd, isTerminal := term.GetFdInfo(w)
if !isTerminal {
return 0, 0, fmt.Errorf("given writer is no terminal")
}
winsize, err := term.GetWinsize(outFd)
if err != nil {
return 0, 0, err
}
return int(winsize.Width), int(winsize.Height), nil
}

1
vendor/modules.txt vendored
View File

@ -1328,6 +1328,7 @@ k8s.io/component-base/metrics
k8s.io/component-base/metrics/legacyregistry
k8s.io/component-base/metrics/prometheus/workqueue
k8s.io/component-base/metrics/testutil
k8s.io/component-base/term
k8s.io/component-base/traces
k8s.io/component-base/version
# k8s.io/component-helpers v0.23.4