genericclioptions

package
v0.21.5-rc.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2021 License: Apache-2.0 Imports: 28 Imported by: 5,940

Documentation

Overview

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

Index

Constants

View Source
const ChangeCauseAnnotation = "kubernetes.io/change-cause"

ChangeCauseAnnotation is the annotation indicating a guess at "why" something was changed

Variables

View Source
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:


  1. Via the command-line flag --kubeconfig
  2. Via the KUBECONFIG environment variable
  3. In your home directory as ~/.kube/config

To view or setup config directly use the 'config' command.`)
)

Functions

func IsNoCompatiblePrinterError

func IsNoCompatiblePrinterError(err error) bool

IsNoCompatiblePrinterError returns true if it is a not a compatible printer otherwise it will return false

Types

type ChangeCauseRecorder

type ChangeCauseRecorder struct {
	// contains filtered or unexported fields
}

ChangeCauseRecorder annotates a "change-cause" to an input runtime object

func (*ChangeCauseRecorder) MakeRecordMergePatch

func (r *ChangeCauseRecorder) MakeRecordMergePatch(obj runtime.Object) ([]byte, error)

MakeRecordMergePatch produces a merge patch for updating the recording annotation.

func (*ChangeCauseRecorder) Record

func (r *ChangeCauseRecorder) Record(obj runtime.Object) error

Record annotates a "change-cause" to a given info if either "shouldRecord" is true, or the resource info previously contained a "change-cause" annotation.

type CommandHeaderRoundTripper added in v0.21.0

type CommandHeaderRoundTripper struct {
	Delegate http.RoundTripper
	Headers  map[string]string
}

CommandHeaderRoundTripper adds a layer around the standard round tripper to add Request headers before delegation. Implements the go standard library "http.RoundTripper" interface.

func (*CommandHeaderRoundTripper) ParseCommandHeaders added in v0.21.0

func (c *CommandHeaderRoundTripper) ParseCommandHeaders(cmd *cobra.Command, args []string)

ParseCommandHeaders fills in a map of X-Headers into the CommandHeaderRoundTripper. These headers are then filled into each request. For details on X-Headers see:

https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/859-kubectl-headers

Each call overwrites the previously parsed command headers (not additive). TODO(seans3): Parse/add flags removing PII from flag values.

func (*CommandHeaderRoundTripper) RoundTrip added in v0.21.0

func (c *CommandHeaderRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

CommandHeaderRoundTripper adds Request headers before delegating to standard round tripper. These headers are kubectl command headers which detail the kubectl command. See SIG CLI KEP 859:

https://github.com/kubernetes/enhancements/tree/master/keps/sig-cli/859-kubectl-headers

type ConfigFlags

type ConfigFlags struct {
	CacheDir   *string
	KubeConfig *string

	// config flags
	ClusterName      *string
	AuthInfoName     *string
	Context          *string
	Namespace        *string
	APIServer        *string
	TLSServerName    *string
	Insecure         *bool
	CertFile         *string
	KeyFile          *string
	CAFile           *string
	BearerToken      *string
	Impersonate      *string
	ImpersonateGroup *[]string
	Username         *string
	Password         *string
	Timeout          *string
	// If non-nil, wrap config function can transform the Config
	// before it is returned in ToRESTConfig function.
	WrapConfigFn func(*rest.Config) *rest.Config
	// contains filtered or unexported fields
}

ConfigFlags composes the set of values necessary for obtaining a REST client config

func NewConfigFlags

func NewConfigFlags(usePersistentConfig bool) *ConfigFlags

NewConfigFlags returns ConfigFlags with default values set

func (*ConfigFlags) AddFlags

func (f *ConfigFlags) AddFlags(flags *pflag.FlagSet)

AddFlags binds client configuration flags to a given flagset

func (*ConfigFlags) ToDiscoveryClient

func (f *ConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error)

ToDiscoveryClient implements RESTClientGetter. Expects the AddFlags method to have been called. Returns a CachedDiscoveryInterface using a computed RESTConfig.

func (*ConfigFlags) ToRESTConfig

func (f *ConfigFlags) ToRESTConfig() (*rest.Config, error)

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. If WrapConfigFn is non-nil this function can transform config before return.

func (*ConfigFlags) ToRESTMapper

func (f *ConfigFlags) ToRESTMapper() (meta.RESTMapper, error)

ToRESTMapper returns a mapper.

func (*ConfigFlags) ToRawKubeConfigLoader

func (f *ConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig

ToRawKubeConfigLoader binds config flag values to config overrides Returns an interactive clientConfig if the password flag is enabled, or a non-interactive clientConfig otherwise.

func (*ConfigFlags) WithDeprecatedPasswordFlag

func (f *ConfigFlags) WithDeprecatedPasswordFlag() *ConfigFlags

WithDeprecatedPasswordFlag enables the username and password config flags

func (*ConfigFlags) WithDiscoveryBurst added in v0.21.0

func (f *ConfigFlags) WithDiscoveryBurst(discoveryBurst int) *ConfigFlags

WithDiscoveryBurst sets the RESTClient burst for discovery.

type FileNameFlags

type FileNameFlags struct {
	Usage string

	Filenames *[]string
	Kustomize *string
	Recursive *bool
}

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.

func (*FileNameFlags) AddFlags

func (o *FileNameFlags) AddFlags(flags *pflag.FlagSet)

AddFlags binds file name flags to a given flagset

func (*FileNameFlags) ToOptions

func (o *FileNameFlags) ToOptions() resource.FilenameOptions

ToOptions creates a new FileNameOptions struct and sets FilenameOptions based on FileNameflags

type GoTemplatePrintFlags

type GoTemplatePrintFlags struct {
	// indicates if it is OK to ignore missing keys for rendering
	// an output template.
	AllowMissingKeys *bool
	TemplateArgument *string
}

GoTemplatePrintFlags provides default flags necessary for template printing. Given the following flag values, a printer can be requested that knows how to handle printing based on these values.

func NewGoTemplatePrintFlags

func NewGoTemplatePrintFlags() *GoTemplatePrintFlags

NewGoTemplatePrintFlags returns flags associated with --template printing, with default values set.

func (*GoTemplatePrintFlags) AddFlags

func (f *GoTemplatePrintFlags) AddFlags(c *cobra.Command)

AddFlags receives a *cobra.Command reference and binds flags related to template printing to it

func (*GoTemplatePrintFlags) AllowedFormats

func (f *GoTemplatePrintFlags) AllowedFormats() []string

AllowedFormats returns slice of string of allowed GoTemplatePrint printing format

func (*GoTemplatePrintFlags) ToPrinter

func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error)

ToPrinter receives an templateFormat and returns a printer capable of handling --template format printing. Returns false if the specified templateFormat does not match a template format.

type IOStreams

type IOStreams struct {
	// In think, os.Stdin
	In io.Reader
	// Out think, os.Stdout
	Out io.Writer
	// ErrOut think, os.Stderr
	ErrOut io.Writer
}

IOStreams provides the standard names for iostreams. This is useful for embedding and for unit testing. Inconsistent and different names make it hard to read and review code

func NewTestIOStreams

func NewTestIOStreams() (IOStreams, *bytes.Buffer, *bytes.Buffer, *bytes.Buffer)

NewTestIOStreams returns a valid IOStreams and in, out, errout buffers for unit tests

func NewTestIOStreamsDiscard

func NewTestIOStreamsDiscard() IOStreams

NewTestIOStreamsDiscard returns a valid IOStreams that just discards

type JSONPathPrintFlags

type JSONPathPrintFlags struct {
	// indicates if it is OK to ignore missing keys for rendering
	// an output template.
	AllowMissingKeys *bool
	TemplateArgument *string
}

JSONPathPrintFlags provides default flags necessary for template printing. Given the following flag values, a printer can be requested that knows how to handle printing based on these values.

func NewJSONPathPrintFlags

func NewJSONPathPrintFlags(templateValue string, allowMissingKeys bool) *JSONPathPrintFlags

NewJSONPathPrintFlags returns flags associated with --template printing, with default values set.

func (*JSONPathPrintFlags) AddFlags

func (f *JSONPathPrintFlags) AddFlags(c *cobra.Command)

AddFlags receives a *cobra.Command reference and binds flags related to template printing to it

func (*JSONPathPrintFlags) AllowedFormats

func (f *JSONPathPrintFlags) AllowedFormats() []string

AllowedFormats returns slice of string of allowed JSONPath printing format

func (*JSONPathPrintFlags) ToPrinter

func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error)

ToPrinter receives an templateFormat and returns a printer capable of handling --template format printing. Returns false if the specified templateFormat does not match a template format.

type JSONYamlPrintFlags

type JSONYamlPrintFlags struct {
	// contains filtered or unexported fields
}

JSONYamlPrintFlags provides default flags necessary for json/yaml printing. Given the following flag values, a printer can be requested that knows how to handle printing based on these values.

func NewJSONYamlPrintFlags

func NewJSONYamlPrintFlags() *JSONYamlPrintFlags

NewJSONYamlPrintFlags returns flags associated with yaml or json printing, with default values set.

func (*JSONYamlPrintFlags) AddFlags

func (f *JSONYamlPrintFlags) AddFlags(c *cobra.Command)

AddFlags receives a *cobra.Command reference and binds flags related to JSON or Yaml printing to it

func (*JSONYamlPrintFlags) AllowedFormats

func (f *JSONYamlPrintFlags) AllowedFormats() []string

AllowedFormats returns slice of string of allowed JSONYaml printing format

func (*JSONYamlPrintFlags) ToPrinter

func (f *JSONYamlPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error)

ToPrinter receives an outputFormat and returns a printer capable of handling --output=(yaml|json) printing. Returns false if the specified outputFormat does not match a supported format. Supported Format types can be found in pkg/printers/printers.go

type KubeTemplatePrintFlags

type KubeTemplatePrintFlags struct {
	GoTemplatePrintFlags *GoTemplatePrintFlags
	JSONPathPrintFlags   *JSONPathPrintFlags

	AllowMissingKeys *bool
	TemplateArgument *string
}

KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer. This is necessary if dealing with cases that require support both both printers, since both sets of flags require overlapping flags.

func NewKubeTemplatePrintFlags

func NewKubeTemplatePrintFlags() *KubeTemplatePrintFlags

NewKubeTemplatePrintFlags returns flags associated with --template printing, with default values set.

func (*KubeTemplatePrintFlags) AddFlags

func (f *KubeTemplatePrintFlags) AddFlags(c *cobra.Command)

AddFlags receives a *cobra.Command reference and binds flags related to template printing to it

func (*KubeTemplatePrintFlags) AllowedFormats

func (f *KubeTemplatePrintFlags) AllowedFormats() []string

AllowedFormats returns slice of string of allowed GoTemplete and JSONPathPrint printing formats

func (*KubeTemplatePrintFlags) ToPrinter

func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error)

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

type NamePrintFlags

type NamePrintFlags struct {
	// Operation describes the name of the action that
	// took place on an object, to be included in the
	// finalized "successful" message.
	Operation string
}

NamePrintFlags provides default flags necessary for printing a resource's fully-qualified Kind.group/name, or a successful message about that resource if an Operation is provided.

func NewNamePrintFlags

func NewNamePrintFlags(operation string) *NamePrintFlags

NewNamePrintFlags returns flags associated with --name printing, with default values set.

func (*NamePrintFlags) AddFlags

func (f *NamePrintFlags) AddFlags(c *cobra.Command)

AddFlags receives a *cobra.Command reference and binds flags related to name printing to it

func (*NamePrintFlags) AllowedFormats

func (f *NamePrintFlags) AllowedFormats() []string

AllowedFormats returns slice of string of allowed Name printing format

func (*NamePrintFlags) Complete

func (f *NamePrintFlags) Complete(successTemplate string) error

Complete sets NamePrintFlags operation flag from sucessTemplate

func (*NamePrintFlags) ToPrinter

func (f *NamePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error)

ToPrinter receives an outputFormat and returns a printer capable of handling --output=name printing. Returns false if the specified outputFormat does not match a supported format. Supported format types can be found in pkg/printers/printers.go

type NoCompatiblePrinterError

type NoCompatiblePrinterError struct {
	OutputFormat   *string
	AllowedFormats []string
	Options        interface{}
}

NoCompatiblePrinterError is a struct that contains error information. It will be constructed when a invalid printing format is provided

func (NoCompatiblePrinterError) Error

func (e NoCompatiblePrinterError) Error() string

type NoopRecorder

type NoopRecorder struct{}

NoopRecorder does nothing. It is a "do nothing" that can be returned so code doesn't switch on it.

func (NoopRecorder) MakeRecordMergePatch

func (r NoopRecorder) MakeRecordMergePatch(obj runtime.Object) ([]byte, error)

MakeRecordMergePatch implements Recorder

func (NoopRecorder) Record

func (r NoopRecorder) Record(obj runtime.Object) error

Record implements Recorder

type PrintFlags

type PrintFlags struct {
	JSONYamlPrintFlags   *JSONYamlPrintFlags
	NamePrintFlags       *NamePrintFlags
	TemplatePrinterFlags *KubeTemplatePrintFlags

	TypeSetterPrinter *printers.TypeSetterPrinter

	OutputFormat *string

	// OutputFlagSpecified indicates whether the user specifically requested a certain kind of output.
	// Using this function allows a sophisticated caller to change the flag binding logic if they so desire.
	OutputFlagSpecified func() bool
}

PrintFlags composes common printer flag structs used across all commands, and provides a method of retrieving a known printer based on flag values provided.

func NewPrintFlags

func NewPrintFlags(operation string) *PrintFlags

NewPrintFlags returns a default *PrintFlags

func (*PrintFlags) AddFlags

func (f *PrintFlags) AddFlags(cmd *cobra.Command)

AddFlags receives a *cobra.Command reference and binds flags related to JSON/Yaml/Name/Template printing to it

func (*PrintFlags) AllowedFormats

func (f *PrintFlags) AllowedFormats() []string

AllowedFormats returns slice of string of allowed JSONYaml/Name/Template printing format

func (*PrintFlags) Complete

func (f *PrintFlags) Complete(successTemplate string) error

Complete sets NamePrintFlags operation flag from sucessTemplate

func (*PrintFlags) ToPrinter

func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error)

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 (*PrintFlags) WithDefaultOutput

func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags

WithDefaultOutput sets a default output format if one is not provided through a flag value

func (*PrintFlags) WithTypeSetter

func (f *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags

WithTypeSetter sets a wrapper than will surround the returned printer with a printer to type resources

type RESTClientGetter

type RESTClientGetter interface {
	// ToRESTConfig returns restconfig
	ToRESTConfig() (*rest.Config, error)
	// ToDiscoveryClient returns discovery client
	ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error)
	// ToRESTMapper returns a restmapper
	ToRESTMapper() (meta.RESTMapper, error)
	// ToRawKubeConfigLoader return kubeconfig loader as-is
	ToRawKubeConfigLoader() clientcmd.ClientConfig
}

RESTClientGetter is an interface that the ConfigFlags describe to provide an easier way to mock for commands and eliminate the direct coupling to a struct type. Users may wish to duplicate this type in their own packages as per the golang type overlapping.

type RecordFlags

type RecordFlags struct {
	// Record indicates the state of the recording flag.  It is a pointer so a caller can opt out or rebind
	Record *bool
	// contains filtered or unexported fields
}

RecordFlags contains all flags associated with the "--record" operation

func NewRecordFlags

func NewRecordFlags() *RecordFlags

NewRecordFlags provides a RecordFlags with reasonable default values set for use

func (*RecordFlags) AddFlags

func (f *RecordFlags) AddFlags(cmd *cobra.Command)

AddFlags binds the requested flags to the provided flagset TODO have this only take a flagset

func (*RecordFlags) Complete

func (f *RecordFlags) Complete(cmd *cobra.Command) error

Complete is called before the command is run, but after it is invoked to finish the state of the struct before use.

func (*RecordFlags) CompleteWithChangeCause

func (f *RecordFlags) CompleteWithChangeCause(cause string) error

CompleteWithChangeCause alters changeCause value with a new cause

func (*RecordFlags) ToRecorder

func (f *RecordFlags) ToRecorder() (Recorder, error)

ToRecorder returns a ChangeCause recorder if --record=false was not explicitly given by the user

type Recorder

type Recorder interface {
	// Record records why a runtime.Object was changed in an annotation.
	Record(runtime.Object) error
	MakeRecordMergePatch(runtime.Object) ([]byte, error)
}

Recorder is used to record why a runtime.Object was changed in an annotation.

type ResourceBuilderFlags

type ResourceBuilderFlags struct {
	FileNameFlags *FileNameFlags

	LabelSelector *string
	FieldSelector *string
	AllNamespaces *bool
	All           *bool
	Local         *bool

	Scheme           *runtime.Scheme
	Latest           bool
	StopOnFirstError bool
}

ResourceBuilderFlags are flags for finding resources TODO(juanvallejo): wire --local flag from commands through

func NewResourceBuilderFlags

func NewResourceBuilderFlags() *ResourceBuilderFlags

NewResourceBuilderFlags returns a default ResourceBuilderFlags

func (*ResourceBuilderFlags) AddFlags

func (o *ResourceBuilderFlags) AddFlags(flagset *pflag.FlagSet)

AddFlags registers flags for finding resources

func (*ResourceBuilderFlags) StopOnError

func (o *ResourceBuilderFlags) StopOnError() *ResourceBuilderFlags

StopOnError sets the StopOnFirstError flag

func (*ResourceBuilderFlags) ToBuilder

func (o *ResourceBuilderFlags) ToBuilder(restClientGetter RESTClientGetter, resources []string) ResourceFinder

ToBuilder gives you back a resource finder to visit resources that are located

func (*ResourceBuilderFlags) WithAll

func (o *ResourceBuilderFlags) WithAll(defaultVal bool) *ResourceBuilderFlags

WithAll sets the All flag

func (*ResourceBuilderFlags) WithAllNamespaces

func (o *ResourceBuilderFlags) WithAllNamespaces(defaultVal bool) *ResourceBuilderFlags

WithAllNamespaces sets the AllNamespaces flag

func (*ResourceBuilderFlags) WithFieldSelector

func (o *ResourceBuilderFlags) WithFieldSelector(selector string) *ResourceBuilderFlags

WithFieldSelector sets the FieldSelector flag

func (*ResourceBuilderFlags) WithFile

func (o *ResourceBuilderFlags) WithFile(recurse bool, files ...string) *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 (*ResourceBuilderFlags) WithLabelSelector

func (o *ResourceBuilderFlags) WithLabelSelector(selector string) *ResourceBuilderFlags

WithLabelSelector sets the LabelSelector flag

func (*ResourceBuilderFlags) WithLatest

func (o *ResourceBuilderFlags) WithLatest() *ResourceBuilderFlags

WithLatest sets the Latest flag

func (*ResourceBuilderFlags) WithLocal

func (o *ResourceBuilderFlags) WithLocal(defaultVal bool) *ResourceBuilderFlags

WithLocal sets the Local flag

func (*ResourceBuilderFlags) WithScheme

func (o *ResourceBuilderFlags) WithScheme(scheme *runtime.Scheme) *ResourceBuilderFlags

WithScheme sets the Scheme flag

type ResourceFindBuilderWrapper

type ResourceFindBuilderWrapper struct {
	// contains filtered or unexported fields
}

ResourceFindBuilderWrapper wraps a builder in an interface

func (*ResourceFindBuilderWrapper) Do

Do finds you resources to check

type ResourceFinder

type ResourceFinder interface {
	Do() resource.Visitor
}

ResourceFinder allows mocking the resource builder TODO resource builders needs to become more interfacey

func NewSimpleFakeResourceFinder

func NewSimpleFakeResourceFinder(infos ...*resource.Info) ResourceFinder

NewSimpleFakeResourceFinder builds a super simple ResourceFinder that just iterates over the objects you provided

func ResourceFinderForResult

func ResourceFinderForResult(result resource.Visitor) ResourceFinder

ResourceFinderForResult skins a visitor for re-use as a ResourceFinder

type ResourceFinderFunc

type ResourceFinderFunc func() resource.Visitor

ResourceFinderFunc is a handy way to make a ResourceFinder

func (ResourceFinderFunc) Do

Do implements ResourceFinder

type TestConfigFlags

type TestConfigFlags struct {
	// contains filtered or unexported fields
}

TestConfigFlags contains clientConfig struct and interfaces that implements RESTClientGetter

func NewTestConfigFlags

func NewTestConfigFlags() *TestConfigFlags

NewTestConfigFlags builds a TestConfigFlags struct to test ConfigFlags

func (*TestConfigFlags) ToDiscoveryClient

func (f *TestConfigFlags) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error)

ToDiscoveryClient implements RESTClientGetter. Returns a CachedDiscoveryInterface

func (*TestConfigFlags) ToRESTConfig

func (f *TestConfigFlags) ToRESTConfig() (*rest.Config, error)

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 (*TestConfigFlags) ToRESTMapper

func (f *TestConfigFlags) ToRESTMapper() (meta.RESTMapper, error)

ToRESTMapper implements RESTClientGetter. Returns a mapper.

func (*TestConfigFlags) ToRawKubeConfigLoader

func (f *TestConfigFlags) ToRawKubeConfigLoader() clientcmd.ClientConfig

ToRawKubeConfigLoader implements RESTClientGetter Returns a clientconfig if it's set

func (*TestConfigFlags) WithClientConfig

func (f *TestConfigFlags) WithClientConfig(clientConfig clientcmd.ClientConfig) *TestConfigFlags

WithClientConfig sets the clientConfig flag

func (*TestConfigFlags) WithDiscoveryClient

WithDiscoveryClient sets the discoveryClient flag

func (*TestConfigFlags) WithNamespace

func (f *TestConfigFlags) WithNamespace(ns string) *TestConfigFlags

WithNamespace sets the clientConfig flag by modifying delagate and namespace

func (*TestConfigFlags) WithRESTMapper

func (f *TestConfigFlags) WithRESTMapper(mapper meta.RESTMapper) *TestConfigFlags

WithRESTMapper sets the restMapper flag

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL