mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-06 10:43:56 +00:00
remove single use, non-polymorphic helpers from factory
This commit is contained in:
parent
f2ea83bef8
commit
4b36c15872
@ -44,7 +44,7 @@ var (
|
|||||||
// NewCmdPlugin creates the command that is the top-level for plugin commands.
|
// NewCmdPlugin creates the command that is the top-level for plugin commands.
|
||||||
func NewCmdPlugin(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
|
func NewCmdPlugin(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command {
|
||||||
// Loads plugins and create commands for each plugin identified
|
// Loads plugins and create commands for each plugin identified
|
||||||
loadedPlugins, loadErr := f.PluginLoader().Load()
|
loadedPlugins, loadErr := pluginLoader().Load()
|
||||||
if loadErr != nil {
|
if loadErr != nil {
|
||||||
glog.V(1).Infof("Unable to load plugins: %v", loadErr)
|
glog.V(1).Infof("Unable to load plugins: %v", loadErr)
|
||||||
}
|
}
|
||||||
@ -63,7 +63,7 @@ func NewCmdPlugin(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(loadedPlugins) > 0 {
|
if len(loadedPlugins) > 0 {
|
||||||
pluginRunner := f.PluginRunner()
|
pluginRunner := pluginRunner()
|
||||||
for _, p := range loadedPlugins {
|
for _, p := range loadedPlugins {
|
||||||
cmd.AddCommand(NewCmdForPlugin(f, p, pluginRunner, streams))
|
cmd.AddCommand(NewCmdForPlugin(f, p, pluginRunner, streams))
|
||||||
}
|
}
|
||||||
@ -166,3 +166,22 @@ func (p *factoryAttrsPluginEnvProvider) Env() (plugins.EnvList, error) {
|
|||||||
plugins.Env{N: "KUBECTL_PLUGINS_CURRENT_NAMESPACE", V: cmdNamespace},
|
plugins.Env{N: "KUBECTL_PLUGINS_CURRENT_NAMESPACE", V: cmdNamespace},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pluginLoader loads plugins from a path set by the KUBECTL_PLUGINS_PATH env var.
|
||||||
|
// If this env var is not set, it defaults to
|
||||||
|
// "~/.kube/plugins", plus
|
||||||
|
// "./kubectl/plugins" directory under the "data dir" directory specified by the XDG
|
||||||
|
// system directory structure spec for the given platform.
|
||||||
|
func pluginLoader() plugins.PluginLoader {
|
||||||
|
if len(os.Getenv("KUBECTL_PLUGINS_PATH")) > 0 {
|
||||||
|
return plugins.KubectlPluginsPathPluginLoader()
|
||||||
|
}
|
||||||
|
return plugins.TolerantMultiPluginLoader{
|
||||||
|
plugins.XDGDataDirsPluginLoader(),
|
||||||
|
plugins.UserDirPluginLoader(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pluginRunner() plugins.PluginRunner {
|
||||||
|
return &plugins.ExecPluginRunner{}
|
||||||
|
}
|
||||||
|
@ -168,7 +168,7 @@ func (o *ScaleOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
o.scaler, err = f.Scaler()
|
o.scaler, err = scaler(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -288,3 +288,12 @@ func ScaleJob(info *resource.Info, jobsClient batchclient.JobsGetter, count uint
|
|||||||
|
|
||||||
return scaler.Scale(info.Namespace, info.Name, count, jobPreconditions, jobRetry, jobWaitForReplicas)
|
return scaler.Scale(info.Namespace, info.Name, count, jobPreconditions, jobRetry, jobWaitForReplicas)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func scaler(f cmdutil.Factory) (kubectl.Scaler, error) {
|
||||||
|
scalesGetter, err := f.ScaleClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return kubectl.NewScaler(scalesGetter), nil
|
||||||
|
}
|
||||||
|
@ -286,10 +286,6 @@ func (f *TestFactory) Cleanup() {
|
|||||||
os.Remove(f.tempConfigFile.Name())
|
os.Remove(f.tempConfigFile.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *TestFactory) CategoryExpander() (restmapper.CategoryExpander, error) {
|
|
||||||
return resource.FakeCategoryExpander, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *TestFactory) ClientConfig() (*restclient.Config, error) {
|
func (f *TestFactory) ClientConfig() (*restclient.Config, error) {
|
||||||
return f.ClientConfigVal, nil
|
return f.ClientConfigVal, nil
|
||||||
}
|
}
|
||||||
@ -334,7 +330,6 @@ func (f *TestFactory) Command(*cobra.Command, bool) string {
|
|||||||
|
|
||||||
func (f *TestFactory) NewBuilder() *resource.Builder {
|
func (f *TestFactory) NewBuilder() *resource.Builder {
|
||||||
mapper, err := f.RESTMapper()
|
mapper, err := f.RESTMapper()
|
||||||
categoryExpander, err2 := f.CategoryExpander()
|
|
||||||
|
|
||||||
return resource.NewFakeBuilder(
|
return resource.NewFakeBuilder(
|
||||||
func(version schema.GroupVersion) (resource.RESTClient, error) {
|
func(version schema.GroupVersion) (resource.RESTClient, error) {
|
||||||
@ -347,8 +342,8 @@ func (f *TestFactory) NewBuilder() *resource.Builder {
|
|||||||
return f.Client, nil
|
return f.Client, nil
|
||||||
},
|
},
|
||||||
mapper,
|
mapper,
|
||||||
categoryExpander,
|
resource.FakeCategoryExpander,
|
||||||
).AddError(err).AddError(err2)
|
).AddError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *TestFactory) KubernetesClientSet() (*kubernetes.Clientset, error) {
|
func (f *TestFactory) KubernetesClientSet() (*kubernetes.Clientset, error) {
|
||||||
|
@ -30,7 +30,6 @@ go_library(
|
|||||||
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
"//pkg/kubectl/cmd/util/openapi:go_default_library",
|
||||||
"//pkg/kubectl/cmd/util/openapi/validation:go_default_library",
|
"//pkg/kubectl/cmd/util/openapi/validation:go_default_library",
|
||||||
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
"//pkg/kubectl/genericclioptions/resource:go_default_library",
|
||||||
"//pkg/kubectl/plugins:go_default_library",
|
|
||||||
"//pkg/kubectl/validation:go_default_library",
|
"//pkg/kubectl/validation:go_default_library",
|
||||||
"//pkg/printers:go_default_library",
|
"//pkg/printers:go_default_library",
|
||||||
"//pkg/printers/internalversion:go_default_library",
|
"//pkg/printers/internalversion:go_default_library",
|
||||||
|
@ -185,7 +185,7 @@ func (o *EditOptions) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *EditOptions) Run() error {
|
func (o *EditOptions) Run() error {
|
||||||
edit := NewDefaultEditor(o.f.EditorEnvs())
|
edit := NewDefaultEditor(editorEnvs())
|
||||||
// editFn is invoked for each edit session (once with a list for normal edit, once for each individual resource in a edit-on-create invocation)
|
// editFn is invoked for each edit session (once with a list for normal edit, once for each individual resource in a edit-on-create invocation)
|
||||||
editFn := func(infos []*resource.Info) error {
|
editFn := func(infos []*resource.Info) error {
|
||||||
var (
|
var (
|
||||||
@ -811,3 +811,11 @@ func hashOnLineBreak(s string) string {
|
|||||||
}
|
}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// editorEnvs returns an ordered list of env vars to check for editor preferences.
|
||||||
|
func editorEnvs() []string {
|
||||||
|
return []string{
|
||||||
|
"KUBE_EDITOR",
|
||||||
|
"EDITOR",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
@ -36,7 +35,6 @@ import (
|
|||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/restmapper"
|
|
||||||
scaleclient "k8s.io/client-go/scale"
|
scaleclient "k8s.io/client-go/scale"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
apiv1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
apiv1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
||||||
@ -45,7 +43,6 @@ import (
|
|||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/plugins"
|
|
||||||
"k8s.io/kubernetes/pkg/kubectl/validation"
|
"k8s.io/kubernetes/pkg/kubectl/validation"
|
||||||
"k8s.io/kubernetes/pkg/printers"
|
"k8s.io/kubernetes/pkg/printers"
|
||||||
)
|
)
|
||||||
@ -66,14 +63,6 @@ type Factory interface {
|
|||||||
BuilderFactory
|
BuilderFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
type DiscoveryClientFactory interface {
|
|
||||||
// Returns a discovery client
|
|
||||||
DiscoveryClient() (discovery.CachedDiscoveryInterface, error)
|
|
||||||
|
|
||||||
// BindFlags adds any discovery flags that are common to all kubectl sub commands.
|
|
||||||
BindFlags(flags *pflag.FlagSet)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClientAccessFactory holds the first level of factory methods.
|
// ClientAccessFactory holds the first level of factory methods.
|
||||||
// Generally provides discovery, negotiation, and no-dep calls.
|
// Generally provides discovery, negotiation, and no-dep calls.
|
||||||
// TODO The polymorphic calls probably deserve their own interface.
|
// TODO The polymorphic calls probably deserve their own interface.
|
||||||
@ -151,18 +140,11 @@ type ClientAccessFactory interface {
|
|||||||
CanBeExposed(kind schema.GroupKind) error
|
CanBeExposed(kind schema.GroupKind) error
|
||||||
// Check whether the kind of resources could be autoscaled
|
// Check whether the kind of resources could be autoscaled
|
||||||
CanBeAutoscaled(kind schema.GroupKind) error
|
CanBeAutoscaled(kind schema.GroupKind) error
|
||||||
|
|
||||||
// EditorEnvs returns a group of environment variables that the edit command
|
|
||||||
// can range over in order to determine if the user has specified an editor
|
|
||||||
// of their choice.
|
|
||||||
EditorEnvs() []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ObjectMappingFactory holds the second level of factory methods. These functions depend upon ClientAccessFactory methods.
|
// ObjectMappingFactory holds the second level of factory methods. These functions depend upon ClientAccessFactory methods.
|
||||||
// Generally they provide object typing and functions that build requests based on the negotiated clients.
|
// Generally they provide object typing and functions that build requests based on the negotiated clients.
|
||||||
type ObjectMappingFactory interface {
|
type ObjectMappingFactory interface {
|
||||||
// Returns interface for expanding categories like `all`.
|
|
||||||
CategoryExpander() (restmapper.CategoryExpander, error)
|
|
||||||
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
// Returns a RESTClient for working with the specified RESTMapping or an error. This is intended
|
||||||
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
|
// for working with arbitrary resources and is not guaranteed to point to a Kubernetes APIServer.
|
||||||
ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error)
|
||||||
@ -197,12 +179,6 @@ type ObjectMappingFactory interface {
|
|||||||
// BuilderFactory holds the third level of factory methods. These functions depend upon ObjectMappingFactory and ClientAccessFactory methods.
|
// BuilderFactory holds the third level of factory methods. These functions depend upon ObjectMappingFactory and ClientAccessFactory methods.
|
||||||
// Generally they depend upon client mapper functions
|
// Generally they depend upon client mapper functions
|
||||||
type BuilderFactory interface {
|
type BuilderFactory interface {
|
||||||
// PluginLoader provides the implementation to be used to load cli plugins.
|
|
||||||
PluginLoader() plugins.PluginLoader
|
|
||||||
// PluginRunner provides the implementation to be used to run cli plugins.
|
|
||||||
PluginRunner() plugins.PluginRunner
|
|
||||||
// Returns a Scaler for changing the size of the specified RESTMapping type or an error
|
|
||||||
Scaler() (kubectl.Scaler, error)
|
|
||||||
// ScaleClient gives you back scale getter
|
// ScaleClient gives you back scale getter
|
||||||
ScaleClient() (scaleclient.ScalesGetter, error)
|
ScaleClient() (scaleclient.ScalesGetter, error)
|
||||||
// Returns a Reaper for gracefully shutting down resources.
|
// Returns a Reaper for gracefully shutting down resources.
|
||||||
|
@ -19,13 +19,10 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
scaleclient "k8s.io/client-go/scale"
|
scaleclient "k8s.io/client-go/scale"
|
||||||
"k8s.io/kubernetes/pkg/kubectl"
|
"k8s.io/kubernetes/pkg/kubectl"
|
||||||
"k8s.io/kubernetes/pkg/kubectl/plugins"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ring2Factory struct {
|
type ring2Factory struct {
|
||||||
@ -42,25 +39,6 @@ func NewBuilderFactory(clientAccessFactory ClientAccessFactory, objectMappingFac
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
// PluginLoader loads plugins from a path set by the KUBECTL_PLUGINS_PATH env var.
|
|
||||||
// If this env var is not set, it defaults to
|
|
||||||
// "~/.kube/plugins", plus
|
|
||||||
// "./kubectl/plugins" directory under the "data dir" directory specified by the XDG
|
|
||||||
// system directory structure spec for the given platform.
|
|
||||||
func (f *ring2Factory) PluginLoader() plugins.PluginLoader {
|
|
||||||
if len(os.Getenv("KUBECTL_PLUGINS_PATH")) > 0 {
|
|
||||||
return plugins.KubectlPluginsPathPluginLoader()
|
|
||||||
}
|
|
||||||
return plugins.TolerantMultiPluginLoader{
|
|
||||||
plugins.XDGDataDirsPluginLoader(),
|
|
||||||
plugins.UserDirPluginLoader(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *ring2Factory) PluginRunner() plugins.PluginRunner {
|
|
||||||
return &plugins.ExecPluginRunner{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *ring2Factory) ScaleClient() (scaleclient.ScalesGetter, error) {
|
func (f *ring2Factory) ScaleClient() (scaleclient.ScalesGetter, error) {
|
||||||
discoClient, err := f.clientAccessFactory.DiscoveryClient()
|
discoClient, err := f.clientAccessFactory.DiscoveryClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -79,15 +57,6 @@ func (f *ring2Factory) ScaleClient() (scaleclient.ScalesGetter, error) {
|
|||||||
return scaleclient.New(restClient, mapper, dynamic.LegacyAPIPathResolverFunc, resolver), nil
|
return scaleclient.New(restClient, mapper, dynamic.LegacyAPIPathResolverFunc, resolver), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ring2Factory) Scaler() (kubectl.Scaler, error) {
|
|
||||||
scalesGetter, err := f.ScaleClient()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return kubectl.NewScaler(scalesGetter), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *ring2Factory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) {
|
func (f *ring2Factory) Reaper(mapping *meta.RESTMapping) (kubectl.Reaper, error) {
|
||||||
clientset, clientsetErr := f.clientAccessFactory.ClientSet()
|
clientset, clientsetErr := f.clientAccessFactory.ClientSet()
|
||||||
if clientsetErr != nil {
|
if clientsetErr != nil {
|
||||||
|
@ -578,10 +578,6 @@ func (f *ring0Factory) CanBeAutoscaled(kind schema.GroupKind) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ring0Factory) EditorEnvs() []string {
|
|
||||||
return []string{"KUBE_EDITOR", "EDITOR"}
|
|
||||||
}
|
|
||||||
|
|
||||||
// overlyCautiousIllegalFileCharacters matches characters that *might* not be supported. Windows is really restrictive, so this is really restrictive
|
// overlyCautiousIllegalFileCharacters matches characters that *might* not be supported. Windows is really restrictive, so this is really restrictive
|
||||||
var overlyCautiousIllegalFileCharacters = regexp.MustCompile(`[^(\w/\.)]`)
|
var overlyCautiousIllegalFileCharacters = regexp.MustCompile(`[^(\w/\.)]`)
|
||||||
|
|
||||||
|
@ -40,7 +40,6 @@ import (
|
|||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/client-go/dynamic"
|
"k8s.io/client-go/dynamic"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/restmapper"
|
|
||||||
"k8s.io/kubernetes/pkg/apis/apps"
|
"k8s.io/kubernetes/pkg/apis/apps"
|
||||||
"k8s.io/kubernetes/pkg/apis/batch"
|
"k8s.io/kubernetes/pkg/apis/batch"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
@ -75,15 +74,6 @@ func NewObjectMappingFactory(clientAccessFactory ClientAccessFactory) ObjectMapp
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *ring1Factory) CategoryExpander() (restmapper.CategoryExpander, error) {
|
|
||||||
discoveryClient, err := f.clientAccessFactory.DiscoveryClient()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return restmapper.NewDiscoveryCategoryExpander(discoveryClient), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *ring1Factory) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
func (f *ring1Factory) ClientForMapping(mapping *meta.RESTMapping) (resource.RESTClient, error) {
|
||||||
cfg, err := f.clientAccessFactory.ClientConfig()
|
cfg, err := f.clientAccessFactory.ClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user