mirror of
				https://github.com/k3s-io/kubernetes.git
				synced 2025-11-03 23:40:03 +00:00 
			
		
		
		
	Setup generators in factory
This commit is contained in:
		@@ -88,7 +88,7 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	generatorName := cmdutil.GetFlagString(cmd, "generator")
 | 
						generatorName := cmdutil.GetFlagString(cmd, "generator")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	generator, found := kubectl.Generators[generatorName]
 | 
						generator, found := f.Generator(generatorName)
 | 
				
			||||||
	if !found {
 | 
						if !found {
 | 
				
			||||||
		return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generator))
 | 
							return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generator))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -82,7 +82,7 @@ func RunRunContainer(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	generatorName := cmdutil.GetFlagString(cmd, "generator")
 | 
						generatorName := cmdutil.GetFlagString(cmd, "generator")
 | 
				
			||||||
	generator, found := kubectl.Generators[generatorName]
 | 
						generator, found := f.Generator(generatorName)
 | 
				
			||||||
	if !found {
 | 
						if !found {
 | 
				
			||||||
		return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not found.", generator))
 | 
							return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not found.", generator))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,8 +48,9 @@ const (
 | 
				
			|||||||
// TODO: pass the various interfaces on the factory directly into the command constructors (so the
 | 
					// TODO: pass the various interfaces on the factory directly into the command constructors (so the
 | 
				
			||||||
// commands are decoupled from the factory).
 | 
					// commands are decoupled from the factory).
 | 
				
			||||||
type Factory struct {
 | 
					type Factory struct {
 | 
				
			||||||
	clients *clientCache
 | 
						clients    *clientCache
 | 
				
			||||||
	flags   *pflag.FlagSet
 | 
						flags      *pflag.FlagSet
 | 
				
			||||||
 | 
						generators map[string]kubectl.Generator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Returns interfaces for dealing with arbitrary runtime.Objects.
 | 
						// Returns interfaces for dealing with arbitrary runtime.Objects.
 | 
				
			||||||
	Object func() (meta.RESTMapper, runtime.ObjectTyper)
 | 
						Object func() (meta.RESTMapper, runtime.ObjectTyper)
 | 
				
			||||||
@@ -77,6 +78,8 @@ type Factory struct {
 | 
				
			|||||||
	Validator func() (validation.Schema, error)
 | 
						Validator func() (validation.Schema, error)
 | 
				
			||||||
	// Returns the default namespace to use in cases where no other namespace is specified
 | 
						// Returns the default namespace to use in cases where no other namespace is specified
 | 
				
			||||||
	DefaultNamespace func() (string, error)
 | 
						DefaultNamespace func() (string, error)
 | 
				
			||||||
 | 
						// Returns the generator for the provided generator name
 | 
				
			||||||
 | 
						Generator func(name string) (kubectl.Generator, bool)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewFactory creates a factory with the default Kubernetes resources defined
 | 
					// NewFactory creates a factory with the default Kubernetes resources defined
 | 
				
			||||||
@@ -88,6 +91,11 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
 | 
				
			|||||||
	flags := pflag.NewFlagSet("", pflag.ContinueOnError)
 | 
						flags := pflag.NewFlagSet("", pflag.ContinueOnError)
 | 
				
			||||||
	flags.SetNormalizeFunc(util.WordSepNormalizeFunc)
 | 
						flags.SetNormalizeFunc(util.WordSepNormalizeFunc)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						generators := map[string]kubectl.Generator{
 | 
				
			||||||
 | 
							"run-container/v1": kubectl.BasicReplicationController{},
 | 
				
			||||||
 | 
							"service/v1":       kubectl.ServiceGenerator{},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	clientConfig := optionalClientConfig
 | 
						clientConfig := optionalClientConfig
 | 
				
			||||||
	if optionalClientConfig == nil {
 | 
						if optionalClientConfig == nil {
 | 
				
			||||||
		clientConfig = DefaultClientConfig(flags)
 | 
							clientConfig = DefaultClientConfig(flags)
 | 
				
			||||||
@@ -99,8 +107,9 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &Factory{
 | 
						return &Factory{
 | 
				
			||||||
		clients: clients,
 | 
							clients:    clients,
 | 
				
			||||||
		flags:   flags,
 | 
							flags:      flags,
 | 
				
			||||||
 | 
							generators: generators,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
 | 
							Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
 | 
				
			||||||
			cfg, err := clientConfig.ClientConfig()
 | 
								cfg, err := clientConfig.ClientConfig()
 | 
				
			||||||
@@ -221,6 +230,10 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
 | 
				
			|||||||
		DefaultNamespace: func() (string, error) {
 | 
							DefaultNamespace: func() (string, error) {
 | 
				
			||||||
			return clientConfig.Namespace()
 | 
								return clientConfig.Namespace()
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
 | 
							Generator: func(name string) (kubectl.Generator, bool) {
 | 
				
			||||||
 | 
								generator, ok := generators[name]
 | 
				
			||||||
 | 
								return generator, ok
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -39,13 +39,6 @@ type Generator interface {
 | 
				
			|||||||
	ParamNames() []GeneratorParam
 | 
						ParamNames() []GeneratorParam
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Generators is a global list of known generators.
 | 
					 | 
				
			||||||
// TODO: Dynamically create this from a list of template files?
 | 
					 | 
				
			||||||
var Generators map[string]Generator = map[string]Generator{
 | 
					 | 
				
			||||||
	"run-container/v1": BasicReplicationController{},
 | 
					 | 
				
			||||||
	"service/v1":       ServiceGenerator{},
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// ValidateParams ensures that all required params are present in the params map
 | 
					// ValidateParams ensures that all required params are present in the params map
 | 
				
			||||||
func ValidateParams(paramSpec []GeneratorParam, params map[string]string) error {
 | 
					func ValidateParams(paramSpec []GeneratorParam, params map[string]string) error {
 | 
				
			||||||
	for ix := range paramSpec {
 | 
						for ix := range paramSpec {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user