From 42d533e40c112f62635a8aa0e3c3def298b26cc9 Mon Sep 17 00:00:00 2001 From: "Dr. Stefan Schimanski" Date: Wed, 30 May 2018 14:45:37 +0200 Subject: [PATCH] apiserver: output flags in logical sections --- cmd/kube-apiserver/app/BUILD | 1 + cmd/kube-apiserver/app/options/BUILD | 2 +- cmd/kube-apiserver/app/options/options.go | 37 ++++---- .../app/options/options_test.go | 8 +- cmd/kube-apiserver/app/server.go | 20 +++- cmd/kube-apiserver/app/testing/testserver.go | 4 +- .../Godeps/Godeps.json | 20 ++++ .../src/k8s.io/apiserver/Godeps/Godeps.json | 20 ++++ .../src/k8s.io/apiserver/pkg/util/flag/BUILD | 2 + .../apiserver/pkg/util/flag/sectioned.go | 92 +++++++++++++++++++ .../k8s.io/kube-aggregator/Godeps/Godeps.json | 20 ++++ .../sample-apiserver/Godeps/Godeps.json | 20 ++++ 12 files changed, 222 insertions(+), 24 deletions(-) create mode 100644 staging/src/k8s.io/apiserver/pkg/util/flag/sectioned.go diff --git a/cmd/kube-apiserver/app/BUILD b/cmd/kube-apiserver/app/BUILD index cdfccec6a6d..73fafa50e0e 100644 --- a/cmd/kube-apiserver/app/BUILD +++ b/cmd/kube-apiserver/app/BUILD @@ -76,6 +76,7 @@ go_library( "//staging/src/k8s.io/apiserver/pkg/server/storage:go_default_library", "//staging/src/k8s.io/apiserver/pkg/storage/etcd3/preflight:go_default_library", "//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library", + "//staging/src/k8s.io/apiserver/pkg/util/flag:go_default_library", "//staging/src/k8s.io/client-go/discovery/cached:go_default_library", "//staging/src/k8s.io/client-go/informers:go_default_library", "//staging/src/k8s.io/client-go/kubernetes:go_default_library", diff --git a/cmd/kube-apiserver/app/options/BUILD b/cmd/kube-apiserver/app/options/BUILD index aa1a0a3c7e7..857701f7903 100644 --- a/cmd/kube-apiserver/app/options/BUILD +++ b/cmd/kube-apiserver/app/options/BUILD @@ -25,8 +25,8 @@ go_library( "//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library", "//staging/src/k8s.io/apiserver/pkg/server/options:go_default_library", "//staging/src/k8s.io/apiserver/pkg/storage/storagebackend:go_default_library", + "//staging/src/k8s.io/apiserver/pkg/util/flag:go_default_library", "//staging/src/k8s.io/kube-aggregator/pkg/apiserver/scheme:go_default_library", - "//vendor/github.com/spf13/pflag:go_default_library", ], ) diff --git a/cmd/kube-apiserver/app/options/options.go b/cmd/kube-apiserver/app/options/options.go index bb8d8dc7e2c..bf12ee805f6 100644 --- a/cmd/kube-apiserver/app/options/options.go +++ b/cmd/kube-apiserver/app/options/options.go @@ -25,6 +25,7 @@ import ( utilnet "k8s.io/apimachinery/pkg/util/net" genericoptions "k8s.io/apiserver/pkg/server/options" "k8s.io/apiserver/pkg/storage/storagebackend" + apiserverflag "k8s.io/apiserver/pkg/util/flag" api "k8s.io/kubernetes/pkg/apis/core" kubeoptions "k8s.io/kubernetes/pkg/kubeapiserver/options" kubeletclient "k8s.io/kubernetes/pkg/kubelet/client" @@ -33,8 +34,6 @@ import ( // add the kubernetes feature gates _ "k8s.io/kubernetes/pkg/features" - - "github.com/spf13/pflag" ) // ServerRunOptions runs a kubernetes api server. @@ -122,26 +121,26 @@ func NewServerRunOptions() *ServerRunOptions { return &s } -// AddFlags adds flags for a specific APIServer to the specified FlagSet -func (s *ServerRunOptions) AddFlags(fs *pflag.FlagSet) { +// Flags returns flags for a specific APIServer by section name +func (s *ServerRunOptions) Flags() (fss apiserverflag.NamedFlagSets) { // Add the generic flags. - s.GenericServerRunOptions.AddUniversalFlags(fs) - s.Etcd.AddFlags(fs) - s.SecureServing.AddFlags(fs) - s.InsecureServing.AddFlags(fs) - s.InsecureServing.AddDeprecatedFlags(fs) - s.Audit.AddFlags(fs) - s.Features.AddFlags(fs) - s.Authentication.AddFlags(fs) - s.Authorization.AddFlags(fs) - s.CloudProvider.AddFlags(fs) - s.StorageSerialization.AddFlags(fs) - s.APIEnablement.AddFlags(fs) - s.Admission.AddFlags(fs) + s.GenericServerRunOptions.AddUniversalFlags(fss.FlagSet("generic")) + s.Etcd.AddFlags(fss.FlagSet("etcd")) + s.SecureServing.AddFlags(fss.FlagSet("secure serving")) + s.InsecureServing.AddFlags(fss.FlagSet("insecure serving")) + s.InsecureServing.AddDeprecatedFlags(fss.FlagSet("insecure serving")) + s.Audit.AddFlags(fss.FlagSet("auditing")) + s.Features.AddFlags(fss.FlagSet("features")) + s.Authentication.AddFlags(fss.FlagSet("authentication")) + s.Authorization.AddFlags(fss.FlagSet("authorization")) + s.CloudProvider.AddFlags(fss.FlagSet("cloud provider")) + s.StorageSerialization.AddFlags(fss.FlagSet("storage")) + s.APIEnablement.AddFlags(fss.FlagSet("api enablement")) + s.Admission.AddFlags(fss.FlagSet("admission")) // Note: the weird ""+ in below lines seems to be the only way to get gofmt to // arrange these text blocks sensibly. Grrr. - + fs := fss.FlagSet("misc") fs.DurationVar(&s.EventTTL, "event-ttl", s.EventTTL, "Amount of time to retain events.") @@ -235,4 +234,6 @@ func (s *ServerRunOptions) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&s.ServiceAccountSigningKeyFile, "service-account-signing-key-file", s.ServiceAccountSigningKeyFile, ""+ "Path to the file that contains the current private key of the service account token issuer. The issuer will sign issued ID tokens with this private key. (Requires the 'TokenRequest' feature gate.)") + + return fss } diff --git a/cmd/kube-apiserver/app/options/options_test.go b/cmd/kube-apiserver/app/options/options_test.go index 4e0ca207b1c..eb0898e9882 100644 --- a/cmd/kube-apiserver/app/options/options_test.go +++ b/cmd/kube-apiserver/app/options/options_test.go @@ -40,9 +40,11 @@ import ( ) func TestAddFlags(t *testing.T) { - f := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) + fs := pflag.NewFlagSet("addflagstest", pflag.ContinueOnError) s := NewServerRunOptions() - s.AddFlags(f) + for _, f := range s.Flags().FlagSets { + fs.AddFlagSet(f) + } args := []string{ "--enable-admission-plugins=AlwaysDeny", @@ -113,7 +115,7 @@ func TestAddFlags(t *testing.T) { "--request-timeout=2m", "--storage-backend=etcd2", } - f.Parse(args) + fs.Parse(args) // This is a snapshot of expected options parsed by args. expected := &ServerRunOptions{ diff --git a/cmd/kube-apiserver/app/server.go b/cmd/kube-apiserver/app/server.go index 033dda2d29f..c28a311fc94 100644 --- a/cmd/kube-apiserver/app/server.go +++ b/cmd/kube-apiserver/app/server.go @@ -55,6 +55,7 @@ import ( serverstorage "k8s.io/apiserver/pkg/server/storage" "k8s.io/apiserver/pkg/storage/etcd3/preflight" utilfeature "k8s.io/apiserver/pkg/util/feature" + apiserverflag "k8s.io/apiserver/pkg/util/flag" cacheddiscovery "k8s.io/client-go/discovery/cached" clientgoinformers "k8s.io/client-go/informers" clientgoclientset "k8s.io/client-go/kubernetes" @@ -134,7 +135,24 @@ cluster's shared state through which all other components interact.`, return Run(completedOptions, stopCh) }, } - s.AddFlags(cmd.Flags()) + + fs := cmd.Flags() + namedFlagSets := s.Flags() + for _, f := range namedFlagSets.FlagSets { + fs.AddFlagSet(f) + } + + usageFmt := "Usage:\n %s\n" + cols, _, _ := apiserverflag.TerminalSize(cmd.OutOrStdout()) + cmd.SetUsageFunc(func(cmd *cobra.Command) error { + fmt.Fprintf(cmd.OutOrStderr(), usageFmt, cmd.UseLine()) + apiserverflag.PrintSections(cmd.OutOrStderr(), namedFlagSets, cols) + return nil + }) + cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) { + fmt.Fprintf(cmd.OutOrStdout(), "%s\n\n"+usageFmt, cmd.Long, cmd.UseLine()) + apiserverflag.PrintSections(cmd.OutOrStdout(), namedFlagSets, cols) + }) return cmd } diff --git a/cmd/kube-apiserver/app/testing/testserver.go b/cmd/kube-apiserver/app/testing/testserver.go index 628ccdefd30..21e13576fc2 100644 --- a/cmd/kube-apiserver/app/testing/testserver.go +++ b/cmd/kube-apiserver/app/testing/testserver.go @@ -109,7 +109,9 @@ func StartTestServer(t Logger, instanceOptions *TestServerInstanceOptions, custo fs := pflag.NewFlagSet("test", pflag.PanicOnError) s := options.NewServerRunOptions() - s.AddFlags(fs) + for _, f := range s.Flags().FlagSets { + fs.AddFlagSet(f) + } s.InsecureServing.BindPort = 0 diff --git a/staging/src/k8s.io/apiextensions-apiserver/Godeps/Godeps.json b/staging/src/k8s.io/apiextensions-apiserver/Godeps/Godeps.json index f4edc57f08a..9d233eb07c8 100644 --- a/staging/src/k8s.io/apiextensions-apiserver/Godeps/Godeps.json +++ b/staging/src/k8s.io/apiextensions-apiserver/Godeps/Godeps.json @@ -10,6 +10,14 @@ "ImportPath": "bitbucket.org/ww/goautoneg", "Rev": "75cd24fc2f2c2a2088577d12123ddee5f54e0675" }, + { + "ImportPath": "github.com/Azure/go-ansiterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, + { + "ImportPath": "github.com/Azure/go-ansiterm/winterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, { "ImportPath": "github.com/NYTimes/gziphandler", "Rev": "56545f4a5d46df9a6648819d1664c3a03a13ffdb" @@ -354,6 +362,14 @@ "ImportPath": "github.com/dgrijalva/jwt-go", "Rev": "01aeca54ebda6e0fbfafd0a524d234159c05ec20" }, + { + "ImportPath": "github.com/docker/docker/pkg/term", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, + { + "ImportPath": "github.com/docker/docker/pkg/term/windows", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, { "ImportPath": "github.com/elazarl/go-bindata-assetfs", "Rev": "3dcc96556217539f50599357fb481ac0dc7439b9" @@ -598,6 +614,10 @@ "ImportPath": "github.com/prometheus/procfs/xfs", "Rev": "65c1f6f8f0fc1e2185eb9863a3bc751496404259" }, + { + "ImportPath": "github.com/sirupsen/logrus", + "Rev": "89742aefa4b206dcf400792f3bd35b542998eb3b" + }, { "ImportPath": "github.com/spf13/cobra", "Rev": "c439c4fa093711d42e1b01acb1235b52004753c1" diff --git a/staging/src/k8s.io/apiserver/Godeps/Godeps.json b/staging/src/k8s.io/apiserver/Godeps/Godeps.json index a943a431760..adb1e97a2d0 100644 --- a/staging/src/k8s.io/apiserver/Godeps/Godeps.json +++ b/staging/src/k8s.io/apiserver/Godeps/Godeps.json @@ -10,6 +10,14 @@ "ImportPath": "bitbucket.org/ww/goautoneg", "Rev": "75cd24fc2f2c2a2088577d12123ddee5f54e0675" }, + { + "ImportPath": "github.com/Azure/go-ansiterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, + { + "ImportPath": "github.com/Azure/go-ansiterm/winterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, { "ImportPath": "github.com/NYTimes/gziphandler", "Rev": "56545f4a5d46df9a6648819d1664c3a03a13ffdb" @@ -354,6 +362,14 @@ "ImportPath": "github.com/dgrijalva/jwt-go", "Rev": "01aeca54ebda6e0fbfafd0a524d234159c05ec20" }, + { + "ImportPath": "github.com/docker/docker/pkg/term", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, + { + "ImportPath": "github.com/docker/docker/pkg/term/windows", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, { "ImportPath": "github.com/elazarl/go-bindata-assetfs", "Rev": "3dcc96556217539f50599357fb481ac0dc7439b9" @@ -578,6 +594,10 @@ "ImportPath": "github.com/prometheus/procfs/xfs", "Rev": "65c1f6f8f0fc1e2185eb9863a3bc751496404259" }, + { + "ImportPath": "github.com/sirupsen/logrus", + "Rev": "89742aefa4b206dcf400792f3bd35b542998eb3b" + }, { "ImportPath": "github.com/spf13/pflag", "Rev": "583c0c0531f06d5278b7d917446061adc344b5cd" diff --git a/staging/src/k8s.io/apiserver/pkg/util/flag/BUILD b/staging/src/k8s.io/apiserver/pkg/util/flag/BUILD index 31898df513b..6370f398c22 100644 --- a/staging/src/k8s.io/apiserver/pkg/util/flag/BUILD +++ b/staging/src/k8s.io/apiserver/pkg/util/flag/BUILD @@ -33,6 +33,7 @@ go_library( "namedcertkey_flag.go", "noop.go", "omitempty.go", + "sectioned.go", "string_flag.go", "tristate.go", ], @@ -40,6 +41,7 @@ go_library( importpath = "k8s.io/apiserver/pkg/util/flag", deps = [ "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", + "//vendor/github.com/docker/docker/pkg/term:go_default_library", "//vendor/github.com/golang/glog:go_default_library", "//vendor/github.com/spf13/pflag:go_default_library", ], diff --git a/staging/src/k8s.io/apiserver/pkg/util/flag/sectioned.go b/staging/src/k8s.io/apiserver/pkg/util/flag/sectioned.go new file mode 100644 index 00000000000..b2b9cea2b7e --- /dev/null +++ b/staging/src/k8s.io/apiserver/pkg/util/flag/sectioned.go @@ -0,0 +1,92 @@ +/* +Copyright 2018 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 flag + +import ( + "bytes" + "fmt" + "io" + "strings" + + "github.com/docker/docker/pkg/term" + "github.com/spf13/pflag" +) + +// NamedFlagSets stores named flag sets in the order of calling FlagSet. +type NamedFlagSets struct { + // Order is an ordered list of flag set names. + Order []string + // FlagSets stores the flag sets by name. + FlagSets map[string]*pflag.FlagSet +} + +// FlagSet returns the flag set with the given name and adds it to the +// ordered name list if it is not in there yet. +func (nfs *NamedFlagSets) FlagSet(name string) *pflag.FlagSet { + if nfs.FlagSets == nil { + nfs.FlagSets = map[string]*pflag.FlagSet{} + } + if _, ok := nfs.FlagSets[name]; !ok { + nfs.FlagSets[name] = pflag.NewFlagSet(name, pflag.ExitOnError) + nfs.Order = append(nfs.Order, name) + } + return nfs.FlagSets[name] +} + +// PrintSections prints the given names flag sets in sections, with the maximal given column number. +// If cols is zero, lines are not wrapped. +func PrintSections(w io.Writer, fss NamedFlagSets, cols int) { + for _, name := range fss.Order { + fs := fss.FlagSets[name] + + wideFS := pflag.NewFlagSet("", pflag.ExitOnError) + wideFS.AddFlagSet(fs) + + var zzz string + if cols > 24 { + zzz = strings.Repeat("z", cols-24) + wideFS.Int(zzz, 0, strings.Repeat("z", cols-24)) + } + + var buf bytes.Buffer + fmt.Fprintf(&buf, "\n%s flags:\n\n%s", strings.ToUpper(name[:1])+name[1:], wideFS.FlagUsagesWrapped(cols)) + + if cols > 24 { + i := strings.Index(buf.String(), zzz) + lines := strings.Split(buf.String()[:i], "\n") + fmt.Fprint(w, strings.Join(lines[:len(lines)-1], "\n")) + fmt.Fprintln(w) + } else { + fmt.Fprint(w, buf.String()) + } + } +} + +// 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 +} diff --git a/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json b/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json index a2c573111d3..4cd17627757 100644 --- a/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json +++ b/staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json @@ -10,6 +10,14 @@ "ImportPath": "bitbucket.org/ww/goautoneg", "Rev": "75cd24fc2f2c2a2088577d12123ddee5f54e0675" }, + { + "ImportPath": "github.com/Azure/go-ansiterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, + { + "ImportPath": "github.com/Azure/go-ansiterm/winterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, { "ImportPath": "github.com/NYTimes/gziphandler", "Rev": "56545f4a5d46df9a6648819d1664c3a03a13ffdb" @@ -86,6 +94,14 @@ "ImportPath": "github.com/davecgh/go-spew/spew", "Rev": "782f4967f2dc4564575ca782fe2d04090b5faca8" }, + { + "ImportPath": "github.com/docker/docker/pkg/term", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, + { + "ImportPath": "github.com/docker/docker/pkg/term/windows", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, { "ImportPath": "github.com/docker/spdystream", "Rev": "449fdfce4d962303d702fec724ef0ad181c92528" @@ -290,6 +306,10 @@ "ImportPath": "github.com/prometheus/procfs/xfs", "Rev": "65c1f6f8f0fc1e2185eb9863a3bc751496404259" }, + { + "ImportPath": "github.com/sirupsen/logrus", + "Rev": "89742aefa4b206dcf400792f3bd35b542998eb3b" + }, { "ImportPath": "github.com/spf13/cobra", "Rev": "c439c4fa093711d42e1b01acb1235b52004753c1" diff --git a/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json b/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json index 168056af1e0..426304a6da7 100644 --- a/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json +++ b/staging/src/k8s.io/sample-apiserver/Godeps/Godeps.json @@ -10,6 +10,14 @@ "ImportPath": "bitbucket.org/ww/goautoneg", "Rev": "75cd24fc2f2c2a2088577d12123ddee5f54e0675" }, + { + "ImportPath": "github.com/Azure/go-ansiterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, + { + "ImportPath": "github.com/Azure/go-ansiterm/winterm", + "Rev": "19f72df4d05d31cbe1c56bfc8045c96babff6c7e" + }, { "ImportPath": "github.com/NYTimes/gziphandler", "Rev": "56545f4a5d46df9a6648819d1664c3a03a13ffdb" @@ -86,6 +94,14 @@ "ImportPath": "github.com/davecgh/go-spew/spew", "Rev": "782f4967f2dc4564575ca782fe2d04090b5faca8" }, + { + "ImportPath": "github.com/docker/docker/pkg/term", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, + { + "ImportPath": "github.com/docker/docker/pkg/term/windows", + "Rev": "4f3616fb1c112e206b88cb7a9922bf49067a7756" + }, { "ImportPath": "github.com/elazarl/go-bindata-assetfs", "Rev": "3dcc96556217539f50599357fb481ac0dc7439b9" @@ -274,6 +290,10 @@ "ImportPath": "github.com/prometheus/procfs/xfs", "Rev": "65c1f6f8f0fc1e2185eb9863a3bc751496404259" }, + { + "ImportPath": "github.com/sirupsen/logrus", + "Rev": "89742aefa4b206dcf400792f3bd35b542998eb3b" + }, { "ImportPath": "github.com/spf13/cobra", "Rev": "c439c4fa093711d42e1b01acb1235b52004753c1"