Skip to content

Commit

Permalink
Merge pull request #7760 from kargakis/move-generators-in-factory
Browse files Browse the repository at this point in the history
Setup generators in factory
  • Loading branch information
wojtek-t committed May 5, 2015
2 parents 02bce10 + c9e79a3 commit 1e55bc6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/expose.go
Expand Up @@ -88,7 +88,7 @@ func RunExpose(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str

generatorName := cmdutil.GetFlagString(cmd, "generator")

generator, found := kubectl.Generators[generatorName]
generator, found := f.Generator(generatorName)
if !found {
return cmdutil.UsageError(cmd, fmt.Sprintf("generator %q not found.", generator))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubectl/cmd/run.go
Expand Up @@ -82,7 +82,7 @@ func RunRunContainer(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args
}

generatorName := cmdutil.GetFlagString(cmd, "generator")
generator, found := kubectl.Generators[generatorName]
generator, found := f.Generator(generatorName)
if !found {
return cmdutil.UsageError(cmd, fmt.Sprintf("Generator: %s not found.", generator))
}
Expand Down
21 changes: 17 additions & 4 deletions pkg/kubectl/cmd/util/factory.go
Expand Up @@ -48,8 +48,9 @@ const (
// TODO: pass the various interfaces on the factory directly into the command constructors (so the
// commands are decoupled from the factory).
type Factory struct {
clients *clientCache
flags *pflag.FlagSet
clients *clientCache
flags *pflag.FlagSet
generators map[string]kubectl.Generator

// Returns interfaces for dealing with arbitrary runtime.Objects.
Object func() (meta.RESTMapper, runtime.ObjectTyper)
Expand Down Expand Up @@ -77,6 +78,8 @@ type Factory struct {
Validator func() (validation.Schema, error)
// Returns the default namespace to use in cases where no other namespace is specified
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
Expand All @@ -88,6 +91,11 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
flags := pflag.NewFlagSet("", pflag.ContinueOnError)
flags.SetNormalizeFunc(util.WordSepNormalizeFunc)

generators := map[string]kubectl.Generator{
"run-container/v1": kubectl.BasicReplicationController{},
"service/v1": kubectl.ServiceGenerator{},
}

clientConfig := optionalClientConfig
if optionalClientConfig == nil {
clientConfig = DefaultClientConfig(flags)
Expand All @@ -99,8 +107,9 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
}

return &Factory{
clients: clients,
flags: flags,
clients: clients,
flags: flags,
generators: generators,

Object: func() (meta.RESTMapper, runtime.ObjectTyper) {
cfg, err := clientConfig.ClientConfig()
Expand Down Expand Up @@ -221,6 +230,10 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
DefaultNamespace: func() (string, error) {
return clientConfig.Namespace()
},
Generator: func(name string) (kubectl.Generator, bool) {
generator, ok := generators[name]
return generator, ok
},
}
}

Expand Down
7 changes: 0 additions & 7 deletions pkg/kubectl/generate.go
Expand Up @@ -39,13 +39,6 @@ type Generator interface {
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
func ValidateParams(paramSpec []GeneratorParam, params map[string]string) error {
for ix := range paramSpec {
Expand Down

0 comments on commit 1e55bc6

Please sign in to comment.