diff --git a/hack/.golint_failures b/hack/.golint_failures index bbe9d9a693b7..507ba9a135e4 100644 --- a/hack/.golint_failures +++ b/hack/.golint_failures @@ -344,7 +344,6 @@ staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/oidc staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/tokentest staging/src/k8s.io/apiserver/plugin/pkg/authenticator/token/webhook staging/src/k8s.io/apiserver/plugin/pkg/authorizer/webhook -staging/src/k8s.io/cli-runtime/pkg/genericclioptions staging/src/k8s.io/cli-runtime/pkg/printers staging/src/k8s.io/cli-runtime/pkg/resource staging/src/k8s.io/client-go/discovery diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go index f695fb5f9bd9..acf576a366e7 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags.go @@ -52,6 +52,9 @@ func NewResourceBuilderFlags() *ResourceBuilderFlags { } } +// WithFile sets the FileNameFlags. +// If recurse is set, it will process directory recursively. Useful when you want to manage related manifests +// organized within the same directory. func (o *ResourceBuilderFlags) WithFile(recurse bool, files ...string) *ResourceBuilderFlags { o.FileNameFlags = &FileNameFlags{ Usage: "identifying the resource.", @@ -62,41 +65,49 @@ func (o *ResourceBuilderFlags) WithFile(recurse bool, files ...string) *Resource return o } +// WithLabelSelector sets the LabelSelector flag func (o *ResourceBuilderFlags) WithLabelSelector(selector string) *ResourceBuilderFlags { o.LabelSelector = &selector return o } +// WithFieldSelector sets the FieldSelector flag func (o *ResourceBuilderFlags) WithFieldSelector(selector string) *ResourceBuilderFlags { o.FieldSelector = &selector return o } +// WithAllNamespaces sets the AllNamespaces flag func (o *ResourceBuilderFlags) WithAllNamespaces(defaultVal bool) *ResourceBuilderFlags { o.AllNamespaces = &defaultVal return o } +// WithAll sets the All flag func (o *ResourceBuilderFlags) WithAll(defaultVal bool) *ResourceBuilderFlags { o.All = &defaultVal return o } +// WithLocal sets the Local flag func (o *ResourceBuilderFlags) WithLocal(defaultVal bool) *ResourceBuilderFlags { o.Local = &defaultVal return o } +// WithScheme sets the Scheme flag func (o *ResourceBuilderFlags) WithScheme(scheme *runtime.Scheme) *ResourceBuilderFlags { o.Scheme = scheme return o } +// WithLatest sets the Latest flag func (o *ResourceBuilderFlags) WithLatest() *ResourceBuilderFlags { o.Latest = true return o } +// StopOnError sets the StopOnFirstError flag func (o *ResourceBuilderFlags) StopOnError() *ResourceBuilderFlags { o.StopOnFirstError = true return o diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags_fake.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags_fake.go index 310382919f4c..d43b0c25e0d8 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags_fake.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/builder_flags_fake.go @@ -20,7 +20,7 @@ import ( "k8s.io/cli-runtime/pkg/resource" ) -// NewSimpleResourceFinder builds a super simple ResourceFinder that just iterates over the objects you provided +// NewSimpleFakeResourceFinder builds a super simple ResourceFinder that just iterates over the objects you provided func NewSimpleFakeResourceFinder(infos ...*resource.Info) ResourceFinder { return &fakeResourceFinder{ Infos: infos, diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/client_config.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/client_config.go index f25af27df2d4..0e22d71407d9 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/client_config.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/client_config.go @@ -23,6 +23,7 @@ import ( ) var ( + // ErrEmptyConfig is the error message to be displayed if the configuration info is missing or incomplete ErrEmptyConfig = clientcmd.NewEmptyConfigError(`Missing or incomplete configuration info. Please point to an existing, complete config file: diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags_fake.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags_fake.go index ff986b027f4a..7a96481552d8 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags_fake.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/config_flags_fake.go @@ -27,12 +27,16 @@ import ( clientcmdapi "k8s.io/client-go/tools/clientcmd/api" ) +// TestConfigFlags contains clientConfig struct +// and interfaces that implements RESTClientGetter type TestConfigFlags struct { clientConfig clientcmd.ClientConfig discoveryClient discovery.CachedDiscoveryInterface restMapper meta.RESTMapper } +// ToRawKubeConfigLoader implements RESTClientGetter +// Returns a clientconfig if it's set func (f *TestConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig { if f.clientConfig == nil { panic("attempt to obtain a test RawKubeConfigLoader with no clientConfig specified") @@ -40,14 +44,22 @@ func (f *TestConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig { return f.clientConfig } +// ToRESTConfig implements RESTClientGetter. +// Returns a REST client configuration based on a provided path +// to a .kubeconfig file, loading rules, and config flag overrides. +// Expects the AddFlags method to have been called. func (f *TestConfigFlags) ToRESTConfig() (*rest.Config, error) { return f.ToRawKubeConfigLoader().ClientConfig() } +// ToDiscoveryClient implements RESTClientGetter. +// Returns a CachedDiscoveryInterface func (f *TestConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) { return f.discoveryClient, nil } +// ToRESTMapper implements RESTClientGetter. +// Returns a mapper. func (f *TestConfigFlags) ToRESTMapper() (meta.RESTMapper, error) { if f.restMapper != nil { return f.restMapper, nil @@ -60,21 +72,25 @@ func (f *TestConfigFlags) ToRESTMapper() (meta.RESTMapper, error) { return nil, fmt.Errorf("no restmapper") } +// WithClientConfig sets the clientConfig flag func (f *TestConfigFlags) WithClientConfig(clientConfig clientcmd.ClientConfig) *TestConfigFlags { f.clientConfig = clientConfig return f } +// WithRESTMapper sets the restMapper flag func (f *TestConfigFlags) WithRESTMapper(mapper meta.RESTMapper) *TestConfigFlags { f.restMapper = mapper return f } +// WithDiscoveryClient sets the discoveryClient flag func (f *TestConfigFlags) WithDiscoveryClient(c discovery.CachedDiscoveryInterface) *TestConfigFlags { f.discoveryClient = c return f } +// WithNamespace sets the clientConfig flag by modifying delagate and namespace func (f *TestConfigFlags) WithNamespace(ns string) *TestConfigFlags { if f.clientConfig == nil { panic("attempt to obtain a test RawKubeConfigLoader with no clientConfig specified") @@ -86,6 +102,7 @@ func (f *TestConfigFlags) WithNamespace(ns string) *TestConfigFlags { return f } +// NewTestConfigFlags builds a TestConfigFlags struct to test ConfigFlags func NewTestConfigFlags() *TestConfigFlags { return &TestConfigFlags{} } diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/doc.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/doc.go index 4796a8a449f6..303da330b7d4 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/doc.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/doc.go @@ -14,6 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ -// Package genericclioptions contains flags which can be added to you command, bound, completed, and produce +// Package genericclioptions contains flags which can be added to your command, bound, completed, and produce // useful helper functions. Nothing in this package can depend on kube/kube package genericclioptions // import "k8s.io/cli-runtime/pkg/genericclioptions" diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/filename_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/filename_flags.go index 09e7b5bed181..74259e41700c 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/filename_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/filename_flags.go @@ -25,6 +25,7 @@ import ( "k8s.io/cli-runtime/pkg/resource" ) +// FileNameFlags are flags for processing files. // Usage of this struct by itself is discouraged. // These flags are composed by ResourceBuilderFlags // which should be used instead. @@ -36,6 +37,7 @@ type FileNameFlags struct { Recursive *bool } +// ToOptions creates a new FileNameOptions struct and sets FilenameOptions based on FileNameflags func (o *FileNameFlags) ToOptions() resource.FilenameOptions { options := resource.FilenameOptions{} @@ -56,6 +58,7 @@ func (o *FileNameFlags) ToOptions() resource.FilenameOptions { return options } +// AddFlags binds file name flags to a given flagset func (o *FileNameFlags) AddFlags(flags *pflag.FlagSet) { if o == nil { return diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/json_yaml_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/json_yaml_flags.go index a344eb0b639b..e207fd8742b7 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/json_yaml_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/json_yaml_flags.go @@ -24,6 +24,7 @@ import ( "k8s.io/cli-runtime/pkg/printers" ) +// AllowedFormats returns slice of string of allowed JSONYaml printing format func (f *JSONYamlPrintFlags) AllowedFormats() []string { if f == nil { return []string{} diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/jsonpath_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/jsonpath_flags.go index b5d114de73cc..06bef474a379 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/jsonpath_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/jsonpath_flags.go @@ -46,6 +46,7 @@ type JSONPathPrintFlags struct { TemplateArgument *string } +// AllowedFormats returns slice of string of allowed JSONPath printing format func (f *JSONPathPrintFlags) AllowedFormats() []string { formats := make([]string, 0, len(jsonFormats)) for format := range jsonFormats { @@ -89,7 +90,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.Resource if templateFormat == "jsonpath-file" { data, err := ioutil.ReadFile(templateValue) if err != nil { - return nil, fmt.Errorf("error reading --template %s, %v\n", templateValue, err) + return nil, fmt.Errorf("error reading --template %s, %v", templateValue, err) } templateValue = string(data) @@ -97,7 +98,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.Resource p, err := printers.NewJSONPathPrinter(templateValue) if err != nil { - return nil, fmt.Errorf("error parsing jsonpath %s, %v\n", templateValue, err) + return nil, fmt.Errorf("error parsing jsonpath %s, %v", templateValue, err) } allowMissingKeys := true diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/kube_template_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/kube_template_flags.go index df2b58c1c288..518a20ac6b2d 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/kube_template_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/kube_template_flags.go @@ -33,6 +33,7 @@ type KubeTemplatePrintFlags struct { TemplateArgument *string } +// AllowedFormats returns slice of string of allowed GoTemplete and JSONPathPrint printing formats func (f *KubeTemplatePrintFlags) AllowedFormats() []string { if f == nil { return []string{} @@ -40,6 +41,10 @@ func (f *KubeTemplatePrintFlags) AllowedFormats() []string { return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...) } +// ToPrinter receives an outputFormat and returns a printer capable of +// handling --template printing. +// Returns false if the specified outputFormat does not match a supported format. +// Supported Format types can be found in pkg/printers/printers.go func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) { if f == nil { return nil, NoCompatiblePrinterError{} diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/name_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/name_flags.go index 7aa89ab05aa7..02f400a03ebb 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/name_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/name_flags.go @@ -35,11 +35,13 @@ type NamePrintFlags struct { Operation string } +// Complete sets NamePrintFlags operation flag from sucessTemplate func (f *NamePrintFlags) Complete(successTemplate string) error { f.Operation = fmt.Sprintf(successTemplate, f.Operation) return nil } +// AllowedFormats returns slice of string of allowed Name printing format func (f *NamePrintFlags) AllowedFormats() []string { if f == nil { return []string{} diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags.go index 17b05c8cd69a..1ec93aecc486 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/print_flags.go @@ -27,6 +27,8 @@ import ( "k8s.io/cli-runtime/pkg/printers" ) +// NoCompatiblePrinterError is a struct that contains error information. +// It will be constructed when a invalid printing format is provided type NoCompatiblePrinterError struct { OutputFormat *string AllowedFormats []string @@ -43,6 +45,8 @@ func (e NoCompatiblePrinterError) Error() string { return fmt.Sprintf("unable to match a printer suitable for the output format %q, allowed formats are: %s", output, strings.Join(e.AllowedFormats, ",")) } +// IsNoCompatiblePrinterError returns true if it is a not a compatible printer +// otherwise it will return false func IsNoCompatiblePrinterError(err error) bool { if err == nil { return false @@ -69,10 +73,12 @@ type PrintFlags struct { OutputFlagSpecified func() bool } +// Complete sets NamePrintFlags operation flag from sucessTemplate func (f *PrintFlags) Complete(successTemplate string) error { return f.NamePrintFlags.Complete(successTemplate) } +// AllowedFormats returns slice of string of allowed JSONYaml/Name/Template printing format func (f *PrintFlags) AllowedFormats() []string { ret := []string{} ret = append(ret, f.JSONYamlPrintFlags.AllowedFormats()...) @@ -81,6 +87,10 @@ func (f *PrintFlags) AllowedFormats() []string { return ret } +// ToPrinter returns a printer capable of +// handling --output or --template printing. +// Returns false if the specified outputFormat does not match a supported format. +// Supported format types can be found in pkg/printers/printers.go func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { outputFormat := "" if f.OutputFormat != nil { @@ -118,6 +128,8 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { return nil, NoCompatiblePrinterError{OutputFormat: f.OutputFormat, AllowedFormats: f.AllowedFormats()} } +// AddFlags receives a *cobra.Command reference and binds +// flags related to JSON/Yaml/Name/Template printing to it func (f *PrintFlags) AddFlags(cmd *cobra.Command) { f.JSONYamlPrintFlags.AddFlags(cmd) f.NamePrintFlags.AddFlags(cmd) @@ -145,6 +157,7 @@ func (f *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags { return f } +// NewPrintFlags returns a default *PrintFlags func NewPrintFlags(operation string) *PrintFlags { outputFormat := "" diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/record_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/record_flags.go index faf250d53cbd..095a7b519e31 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/record_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/record_flags.go @@ -21,7 +21,7 @@ import ( "path/filepath" "strings" - "github.com/evanphx/json-patch" + jsonpatch "github.com/evanphx/json-patch" "github.com/spf13/cobra" "github.com/spf13/pflag" @@ -74,6 +74,7 @@ func (f *RecordFlags) Complete(cmd *cobra.Command) error { return nil } +// CompleteWithChangeCause alters changeCause value with a new cause func (f *RecordFlags) CompleteWithChangeCause(cause string) error { if f == nil { return nil diff --git a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/template_flags.go b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/template_flags.go index 342912798a96..4502061ec43e 100644 --- a/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/template_flags.go +++ b/staging/src/k8s.io/cli-runtime/pkg/genericclioptions/template_flags.go @@ -47,6 +47,7 @@ type GoTemplatePrintFlags struct { TemplateArgument *string } +// AllowedFormats returns slice of string of allowed GoTemplatePrint printing format func (f *GoTemplatePrintFlags) AllowedFormats() []string { formats := make([]string, 0, len(templateFormats)) for format := range templateFormats { @@ -90,7 +91,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.Resour if templateFormat == "templatefile" || templateFormat == "go-template-file" { data, err := ioutil.ReadFile(templateValue) if err != nil { - return nil, fmt.Errorf("error reading --template %s, %v\n", templateValue, err) + return nil, fmt.Errorf("error reading --template %s, %v", templateValue, err) } templateValue = string(data) @@ -98,7 +99,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.Resour p, err := printers.NewGoTemplatePrinter([]byte(templateValue)) if err != nil { - return nil, fmt.Errorf("error parsing template %s, %v\n", templateValue, err) + return nil, fmt.Errorf("error parsing template %s, %v", templateValue, err) } allowMissingKeys := true